Skip to content

Commit

Permalink
Merge pull request #17481 from calixteman/editor_default_init
Browse files Browse the repository at this point in the history
[Editor] Init the default highlight color before creating the first editor instance
  • Loading branch information
calixteman authored Jan 5, 2024
2 parents 130a0fe + 17e1519 commit 6c5e237
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/display/editor/annotation_editor_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class AnnotationEditorLayer {
if (!AnnotationEditorLayer._initialized) {
AnnotationEditorLayer._initialized = true;
for (const editorType of editorTypes) {
editorType.initialize(l10n);
editorType.initialize(l10n, uiManager);
}
}
uiManager.registerEditorTypes(editorTypes);
Expand Down
2 changes: 1 addition & 1 deletion src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class AnnotationEditor {
* Initialize the l10n stuff for this type of editor.
* @param {Object} l10n
*/
static initialize(l10n, options = null) {
static initialize(l10n, _uiManager, options) {
AnnotationEditor._l10nPromise ||= new Map(
[
"pdfjs-editor-alt-text-button-label",
Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/freetext.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class FreeTextEditor extends AnnotationEditor {
}

/** @inheritdoc */
static initialize(l10n) {
AnnotationEditor.initialize(l10n, {
static initialize(l10n, uiManager) {
AnnotationEditor.initialize(l10n, uiManager, {
strings: ["pdfjs-free-text-default-content"],
});
const style = getComputedStyle(document.documentElement);
Expand Down
8 changes: 4 additions & 4 deletions src/display/editor/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ class HighlightEditor extends AnnotationEditor {

constructor(params) {
super({ ...params, name: "highlightEditor" });
HighlightEditor._defaultColor ||=
this._uiManager.highlightColors?.values().next().value || "#fff066";
this.color = params.color || HighlightEditor._defaultColor;
this.#opacity = params.opacity || HighlightEditor._defaultOpacity;
this.#boxes = params.boxes || null;
Expand Down Expand Up @@ -97,8 +95,10 @@ class HighlightEditor extends AnnotationEditor {
];
}

static initialize(l10n) {
AnnotationEditor.initialize(l10n);
static initialize(l10n, uiManager) {
AnnotationEditor.initialize(l10n, uiManager);
HighlightEditor._defaultColor ||=
uiManager.highlightColors?.values().next().value || "#fff066";
}

static updateDefaultParams(type, value) {
Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/ink.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class InkEditor extends AnnotationEditor {
}

/** @inheritdoc */
static initialize(l10n) {
AnnotationEditor.initialize(l10n);
static initialize(l10n, uiManager) {
AnnotationEditor.initialize(l10n, uiManager);
}

/** @inheritdoc */
Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/stamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class StampEditor extends AnnotationEditor {
}

/** @inheritdoc */
static initialize(l10n) {
AnnotationEditor.initialize(l10n);
static initialize(l10n, uiManager) {
AnnotationEditor.initialize(l10n, uiManager);
}

static get supportedTypes() {
Expand Down
57 changes: 57 additions & 0 deletions test/integration/highlight_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
})
);
});
});
});
11 changes: 9 additions & 2 deletions test/integration/test_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os from "os";
const isMac = os.platform() === "darwin";

function loadAndWait(filename, selector, zoom, pageSetup) {
function loadAndWait(filename, selector, zoom, pageSetup, options) {
return Promise.all(
global.integrationSessions.map(async session => {
const page = await session.browser.newPage();
Expand All @@ -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 = `${
global.integrationBaseUrl
}?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}`;
}?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}${app_options}`;

await page.goto(url);
if (pageSetup) {
Expand Down
10 changes: 10 additions & 0 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,16 @@ const PDFViewerApplication = {
) {
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")
);
}
}
},

/**
Expand Down
2 changes: 1 addition & 1 deletion web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ const defaultOptions = {
},
pdfBugEnabled: {
/** @type {boolean} */
value: typeof PDFJSDev === "undefined",
value: typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING"),
kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
},
printResolution: {
Expand Down

0 comments on commit 6c5e237

Please sign in to comment.