[Editor] Init the default highlight color before creating the first editor instance

We want to be able to draw an highlight with the default color but without having an
instance of the HighlightEditor.
This commit is contained in:
Calixte Denizet 2024-01-05 10:10:01 +01:00
parent 130a0fef3d
commit 17e1519410
10 changed files with 89 additions and 15 deletions

View File

@ -110,7 +110,7 @@ class AnnotationEditorLayer {
if (!AnnotationEditorLayer._initialized) { if (!AnnotationEditorLayer._initialized) {
AnnotationEditorLayer._initialized = true; AnnotationEditorLayer._initialized = true;
for (const editorType of editorTypes) { for (const editorType of editorTypes) {
editorType.initialize(l10n); editorType.initialize(l10n, uiManager);
} }
} }
uiManager.registerEditorTypes(editorTypes); uiManager.registerEditorTypes(editorTypes);

View File

@ -185,7 +185,7 @@ class AnnotationEditor {
* Initialize the l10n stuff for this type of editor. * Initialize the l10n stuff for this type of editor.
* @param {Object} l10n * @param {Object} l10n
*/ */
static initialize(l10n, options = null) { static initialize(l10n, _uiManager, options) {
AnnotationEditor._l10nPromise ||= new Map( AnnotationEditor._l10nPromise ||= new Map(
[ [
"pdfjs-editor-alt-text-button-label", "pdfjs-editor-alt-text-button-label",

View File

@ -144,8 +144,8 @@ class FreeTextEditor extends AnnotationEditor {
} }
/** @inheritdoc */ /** @inheritdoc */
static initialize(l10n) { static initialize(l10n, uiManager) {
AnnotationEditor.initialize(l10n, { AnnotationEditor.initialize(l10n, uiManager, {
strings: ["pdfjs-free-text-default-content"], strings: ["pdfjs-free-text-default-content"],
}); });
const style = getComputedStyle(document.documentElement); const style = getComputedStyle(document.documentElement);

View File

@ -59,8 +59,6 @@ class HighlightEditor extends AnnotationEditor {
constructor(params) { constructor(params) {
super({ ...params, name: "highlightEditor" }); super({ ...params, name: "highlightEditor" });
HighlightEditor._defaultColor ||=
this._uiManager.highlightColors?.values().next().value || "#fff066";
this.color = params.color || HighlightEditor._defaultColor; this.color = params.color || HighlightEditor._defaultColor;
this.#opacity = params.opacity || HighlightEditor._defaultOpacity; this.#opacity = params.opacity || HighlightEditor._defaultOpacity;
this.#boxes = params.boxes || null; this.#boxes = params.boxes || null;
@ -97,8 +95,10 @@ class HighlightEditor extends AnnotationEditor {
]; ];
} }
static initialize(l10n) { static initialize(l10n, uiManager) {
AnnotationEditor.initialize(l10n); AnnotationEditor.initialize(l10n, uiManager);
HighlightEditor._defaultColor ||=
uiManager.highlightColors?.values().next().value || "#fff066";
} }
static updateDefaultParams(type, value) { static updateDefaultParams(type, value) {

View File

@ -84,8 +84,8 @@ class InkEditor extends AnnotationEditor {
} }
/** @inheritdoc */ /** @inheritdoc */
static initialize(l10n) { static initialize(l10n, uiManager) {
AnnotationEditor.initialize(l10n); AnnotationEditor.initialize(l10n, uiManager);
} }
/** @inheritdoc */ /** @inheritdoc */

View File

@ -55,8 +55,8 @@ class StampEditor extends AnnotationEditor {
} }
/** @inheritdoc */ /** @inheritdoc */
static initialize(l10n) { static initialize(l10n, uiManager) {
AnnotationEditor.initialize(l10n); AnnotationEditor.initialize(l10n, uiManager);
} }
static get supportedTypes() { static get supportedTypes() {

View File

@ -132,4 +132,61 @@ describe("Highlight Editor", () => {
); );
}); });
}); });
describe("The default color must have the correct value", () => {
let pages;
beforeAll(async () => {
pages = await loadAndWait(
"tracemonkey.pdf",
".annotationEditorLayer",
null,
null,
{ highlightEditorColors: "red=#AB0000" }
);
});
afterAll(async () => {
await closePages(pages);
});
it("must highlight with red color", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.click("#editorHighlight");
await page.waitForSelector(".annotationEditorLayer.highlightEditing");
const rect = await page.evaluate(() => {
for (const el of document.querySelectorAll(
`.page[data-page-number="1"] > .textLayer > span`
)) {
if (el.textContent === "Abstract") {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
}
}
return null;
});
const x = rect.x + rect.width / 2;
const y = rect.y + rect.height / 2;
await page.mouse.click(x, y, { count: 2 });
await page.waitForSelector(`${getEditorSelector(0)}`);
await page.waitForSelector(
`.page[data-page-number = "1"] svg.highlightOutline.selected`
);
const usedColor = await page.evaluate(() => {
const highlight = document.querySelector(
`.page[data-page-number = "1"] .canvasWrapper > svg.highlight`
);
return highlight.getAttribute("fill");
});
expect(usedColor).withContext(`In ${browserName}`).toEqual("#AB0000");
})
);
});
});
}); });

View File

@ -16,7 +16,7 @@
import os from "os"; import os from "os";
const isMac = os.platform() === "darwin"; const isMac = os.platform() === "darwin";
function loadAndWait(filename, selector, zoom, pageSetup) { function loadAndWait(filename, selector, zoom, pageSetup, options) {
return Promise.all( return Promise.all(
global.integrationSessions.map(async session => { global.integrationSessions.map(async session => {
const page = await session.browser.newPage(); const page = await session.browser.newPage();
@ -36,9 +36,16 @@ function loadAndWait(filename, selector, zoom, pageSetup) {
}); });
}); });
let app_options = "";
if (options) {
// Options must be handled in app.js::_parseHashParams.
for (const [key, value] of Object.entries(options)) {
app_options += `&${key}=${encodeURIComponent(value)}`;
}
}
const url = `${ const url = `${
global.integrationBaseUrl global.integrationBaseUrl
}?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}`; }?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}${app_options}`;
await page.goto(url); await page.goto(url);
if (pageSetup) { if (pageSetup) {

View File

@ -353,6 +353,16 @@ const PDFViewerApplication = {
) { ) {
AppOptions.set("locale", params.get("locale")); AppOptions.set("locale", params.get("locale"));
} }
// Set some specific preferences for tests.
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) {
if (params.has("highlighteditorcolors")) {
AppOptions.set(
"highlightEditorColors",
params.get("highlighteditorcolors")
);
}
}
}, },
/** /**

View File

@ -203,7 +203,7 @@ const defaultOptions = {
}, },
pdfBugEnabled: { pdfBugEnabled: {
/** @type {boolean} */ /** @type {boolean} */
value: typeof PDFJSDev === "undefined", value: typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING"),
kind: OptionKind.VIEWER + OptionKind.PREFERENCE, kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
}, },
printResolution: { printResolution: {