Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions l10n/en-US/viewer.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ pdfjs-highlight-floating-button1 =
.title = Highlight
.aria-label = Highlight
pdfjs-highlight-floating-button-label = Highlight
pdfjs-comment-floating-button =
.title = Comment
.aria-label = Comment
pdfjs-comment-floating-button-label = Comment
pdfjs-editor-signature-button =
.title = Add signature
pdfjs-editor-signature-button-label = Add signature
Expand Down
42 changes: 27 additions & 15 deletions src/display/editor/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class EditorToolbar {
const { editorType, _uiManager } = this.#editor;

const button = document.createElement("button");
button.className = "delete";
Comment thread
calixteman marked this conversation as resolved.
button.classList.add("basic", "deleteButton");
button.tabIndex = 0;
button.setAttribute("data-l10n-id", EditorToolbar.#l10nRemove[editorType]);
this.#addListenersToElement(button);
Expand Down Expand Up @@ -214,7 +214,7 @@ class EditorToolbar {
}
}

class HighlightToolbar {
class FloatingToolbar {
#buttons = null;

#toolbar = null;
Expand All @@ -237,7 +237,25 @@ class HighlightToolbar {
buttons.className = "buttons";
editToolbar.append(buttons);

this.#addHighlightButton();
if (this.#uiManager.hasCommentManager()) {
this.#makeButton(
"commentButton",
`pdfjs-comment-floating-button`,
"pdfjs-comment-floating-button-label",
() => {
this.#uiManager.commentSelection("floating_button");
}
);
}

this.#makeButton(
"highlightButton",
`pdfjs-highlight-floating-button1`,
"pdfjs-highlight-floating-button-label",
() => {
this.#uiManager.highlightSelection("floating_button");
}
);

return editToolbar;
}
Expand Down Expand Up @@ -279,26 +297,20 @@ class HighlightToolbar {
this.#toolbar.remove();
}

#addHighlightButton() {
#makeButton(buttonClass, l10nId, labelL10nId, clickHandler) {
const button = document.createElement("button");
button.className = "highlightButton";
button.classList.add("basic", buttonClass);
button.tabIndex = 0;
button.setAttribute("data-l10n-id", `pdfjs-highlight-floating-button1`);
button.setAttribute("data-l10n-id", l10nId);
const span = document.createElement("span");
button.append(span);
span.className = "visuallyHidden";
span.setAttribute("data-l10n-id", "pdfjs-highlight-floating-button-label");
span.setAttribute("data-l10n-id", labelL10nId);
const signal = this.#uiManager._signal;
button.addEventListener("contextmenu", noContextMenu, { signal });
button.addEventListener(
"click",
() => {
this.#uiManager.highlightSelection("floating_button");
},
{ signal }
);
button.addEventListener("click", clickHandler, { signal });
this.#buttons.append(button);
}
}

export { EditorToolbar, HighlightToolbar };
export { EditorToolbar, FloatingToolbar };
36 changes: 23 additions & 13 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
PixelsPerInch,
stopEvent,
} from "../display_utils.js";
import { HighlightToolbar } from "./toolbar.js";
import { FloatingToolbar } from "./toolbar.js";

function bindEvents(obj, element, names) {
for (const name of names) {
Expand Down Expand Up @@ -631,7 +631,7 @@ class AnnotationEditorUIManager {

#highlightWhenShiftUp = false;

#highlightToolbar = null;
#floatingToolbar = null;

#idManager = new IdManager();

Expand Down Expand Up @@ -908,8 +908,8 @@ class AnnotationEditorUIManager {
this.#altTextManager?.destroy();
this.#commentManager?.destroy();
this.#signatureManager?.destroy();
this.#highlightToolbar?.hide();
this.#highlightToolbar = null;
this.#floatingToolbar?.hide();
this.#floatingToolbar = null;
this.#mainHighlightColorPicker?.destroy();
this.#mainHighlightColorPicker = null;
if (this.#focusMainContainerTimeoutId) {
Expand Down Expand Up @@ -1157,7 +1157,7 @@ class AnnotationEditorUIManager {
return null;
}

highlightSelection(methodOfCreation = "") {
highlightSelection(methodOfCreation = "", comment = false) {
const selection = document.getSelection();
if (!selection || selection.isCollapsed) {
return;
Expand All @@ -1175,7 +1175,7 @@ class AnnotationEditorUIManager {
const layer = this.#getLayerForTextLayer(textLayer);
const isNoneMode = this.#mode === AnnotationEditorType.NONE;
const callback = () => {
layer?.createAndAddNewEditor({ x: 0, y: 0 }, false, {
const editor = layer?.createAndAddNewEditor({ x: 0, y: 0 }, false, {
methodOfCreation,
boxes,
anchorNode,
Expand All @@ -1187,6 +1187,9 @@ class AnnotationEditorUIManager {
if (isNoneMode) {
this.showAllEditors("highlight", true, /* updateButton = */ true);
}
if (comment) {
editor?.editComment();
}
};
if (isNoneMode) {
this.switchToMode(AnnotationEditorType.HIGHLIGHT, callback);
Expand All @@ -1195,7 +1198,11 @@ class AnnotationEditorUIManager {
callback();
}

#displayHighlightToolbar() {
commentSelection(methodOfCreation = "") {
this.highlightSelection(methodOfCreation, /* comment */ true);
}

#displayFloatingToolbar() {
const selection = document.getSelection();
if (!selection || selection.isCollapsed) {
return;
Expand All @@ -1206,8 +1213,8 @@ class AnnotationEditorUIManager {
if (!boxes) {
return;
}
this.#highlightToolbar ||= new HighlightToolbar(this);
this.#highlightToolbar.show(textLayer, boxes, this.direction === "ltr");
this.#floatingToolbar ||= new FloatingToolbar(this);
this.#floatingToolbar.show(textLayer, boxes, this.direction === "ltr");
}

/**
Expand Down Expand Up @@ -1241,7 +1248,7 @@ class AnnotationEditorUIManager {
const selection = document.getSelection();
if (!selection || selection.isCollapsed) {
if (this.#selectedTextNode) {
this.#highlightToolbar?.hide();
this.#floatingToolbar?.hide();
this.#selectedTextNode = null;
this.#dispatchUpdateStates({
hasSelectedText: false,
Expand All @@ -1258,7 +1265,7 @@ class AnnotationEditorUIManager {
const textLayer = anchorElement.closest(".textLayer");
if (!textLayer) {
if (this.#selectedTextNode) {
this.#highlightToolbar?.hide();
this.#floatingToolbar?.hide();
this.#selectedTextNode = null;
this.#dispatchUpdateStates({
hasSelectedText: false,
Expand All @@ -1267,7 +1274,7 @@ class AnnotationEditorUIManager {
return;
}

this.#highlightToolbar?.hide();
this.#floatingToolbar?.hide();
this.#selectedTextNode = anchorNode;
this.#dispatchUpdateStates({
hasSelectedText: true,
Expand Down Expand Up @@ -1315,7 +1322,7 @@ class AnnotationEditorUIManager {
if (this.#mode === AnnotationEditorType.HIGHLIGHT) {
this.highlightSelection(methodOfCreation);
} else if (this.#enableHighlightFloatingButton) {
this.#displayHighlightToolbar();
this.#displayFloatingToolbar();
}
}

Expand Down Expand Up @@ -1606,6 +1613,9 @@ class AnnotationEditorUIManager {
case "highlightSelection":
this.highlightSelection("context_menu");
break;
case "commentSelection":
this.commentSelection("context_menu");
break;
}
}

Expand Down
24 changes: 12 additions & 12 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2591,7 +2591,7 @@ describe("FreeText Editor", () => {
await commit(page);

// Delete it in using the button.
await page.click(`${editorSelector} button.delete`);
await page.click(`${editorSelector} button.deleteButton`);
await page.waitForFunction(
sel => !document.querySelector(sel),
{},
Expand Down Expand Up @@ -2644,7 +2644,7 @@ describe("FreeText Editor", () => {
await selectAll(page);

// Delete it in using the button.
await page.focus(`${editorSelector} button.delete`);
await page.focus(`${editorSelector} button.deleteButton`);
await page.keyboard.press("Enter");
await page.waitForFunction(
sel => !document.querySelector(sel),
Expand Down Expand Up @@ -2885,8 +2885,8 @@ describe("FreeText Editor", () => {
await commit(page);
await waitForSerialized(page, 1);

await page.waitForSelector(`${editorSelector} button.delete`);
await page.click(`${editorSelector} button.delete`);
await page.waitForSelector(`${editorSelector} button.deleteButton`);
await page.click(`${editorSelector} button.deleteButton`);
await waitForSerialized(page, 0);

const twoToFourteen = Array.from(new Array(13).keys(), n => n + 2);
Expand Down Expand Up @@ -2935,8 +2935,8 @@ describe("FreeText Editor", () => {
await commit(page);
await waitForSerialized(page, 1);

await page.waitForSelector(`${editorSelector} button.delete`);
await page.click(`${editorSelector} button.delete`);
await page.waitForSelector(`${editorSelector} button.deleteButton`);
await page.click(`${editorSelector} button.deleteButton`);
await waitForSerialized(page, 0);

const twoToOne = Array.from(new Array(13).keys(), n => n + 2).concat(
Expand Down Expand Up @@ -3271,8 +3271,8 @@ describe("FreeText Editor", () => {
await commit(page);
await waitForSerialized(page, 1);

await page.waitForSelector(`${editorSelector} button.delete`);
await page.click(`${editorSelector} button.delete`);
await page.waitForSelector(`${editorSelector} button.deleteButton`);
await page.click(`${editorSelector} button.deleteButton`);
await waitForSerialized(page, 0);
await page.waitForSelector("#editorUndoBar", { visible: true });

Expand All @@ -3299,8 +3299,8 @@ describe("FreeText Editor", () => {
await commit(page);
await waitForSerialized(page, 1);

await page.waitForSelector(`${editorSelector} button.delete`);
await page.click(`${editorSelector} button.delete`);
await page.waitForSelector(`${editorSelector} button.deleteButton`);
await page.click(`${editorSelector} button.deleteButton`);
await waitForSerialized(page, 0);

await page.waitForFunction(() => {
Expand Down Expand Up @@ -3332,8 +3332,8 @@ describe("FreeText Editor", () => {
await commit(page);
await waitForSerialized(page, 1);

await page.waitForSelector(`${editorSelector} button.delete`);
await page.click(`${editorSelector} button.delete`);
await page.waitForSelector(`${editorSelector} button.deleteButton`);
await page.click(`${editorSelector} button.deleteButton`);
await waitForSerialized(page, 0);

await page.waitForSelector("#editorUndoBar", { visible: true });
Expand Down
Loading