Merge pull request from Snuffleupagus/rm-toolbar-reset-events

Remove the internal "toolbarreset"/"secondarytoolbarreset" events and slightly re-factor the code
This commit is contained in:
Tim van der Meij 2023-12-29 12:47:20 +01:00 committed by GitHub
commit 9e14d04fd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 148 additions and 168 deletions

@ -52,14 +52,15 @@ import { PagesCountLimit } from "./pdf_viewer.js";
*/ */
class SecondaryToolbar { class SecondaryToolbar {
#opts;
/** /**
* @param {SecondaryToolbarOptions} options * @param {SecondaryToolbarOptions} options
* @param {EventBus} eventBus * @param {EventBus} eventBus
*/ */
constructor(options, eventBus) { constructor(options, eventBus) {
this.toolbar = options.toolbar; this.#opts = options;
this.toggleButton = options.toggleButton; const buttons = [
this.buttons = [
{ {
element: options.presentationModeButton, element: options.presentationModeButton,
eventName: "presentationmode", eventName: "presentationmode",
@ -141,28 +142,19 @@ class SecondaryToolbar {
}, },
]; ];
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
this.buttons.push({ buttons.push({
element: options.openFileButton, element: options.openFileButton,
eventName: "openfile", eventName: "openfile",
close: true, close: true,
}); });
} }
this.items = {
firstPage: options.firstPageButton,
lastPage: options.lastPageButton,
pageRotateCw: options.pageRotateCwButton,
pageRotateCcw: options.pageRotateCcwButton,
};
this.eventBus = eventBus; this.eventBus = eventBus;
this.opened = false; this.opened = false;
// Bind the event listeners for click, cursor tool, and scroll/spread mode // Bind the event listeners for click, cursor tool, and scroll/spread mode
// actions. // actions.
this.#bindClickListeners(); this.#bindListeners(buttons);
this.#bindCursorToolsListener(options);
this.#bindScrollModeListener(options);
this.#bindSpreadModeListener(options);
this.reset(); this.reset();
} }
@ -190,30 +182,40 @@ class SecondaryToolbar {
this.#updateUIState(); this.#updateUIState();
// Reset the Scroll/Spread buttons too, since they're document specific. // Reset the Scroll/Spread buttons too, since they're document specific.
this.eventBus.dispatch("secondarytoolbarreset", { source: this }); this.#scrollModeChanged({ mode: ScrollMode.VERTICAL });
this.#spreadModeChanged({ mode: SpreadMode.NONE });
} }
#updateUIState() { #updateUIState() {
this.items.firstPage.disabled = this.pageNumber <= 1; const {
this.items.lastPage.disabled = this.pageNumber >= this.pagesCount; firstPageButton,
this.items.pageRotateCw.disabled = this.pagesCount === 0; lastPageButton,
this.items.pageRotateCcw.disabled = this.pagesCount === 0; pageRotateCwButton,
pageRotateCcwButton,
} = this.#opts;
firstPageButton.disabled = this.pageNumber <= 1;
lastPageButton.disabled = this.pageNumber >= this.pagesCount;
pageRotateCwButton.disabled = this.pagesCount === 0;
pageRotateCcwButton.disabled = this.pagesCount === 0;
} }
#bindClickListeners() { #bindListeners(buttons) {
const { eventBus } = this;
const { toggleButton } = this.#opts;
// Button to toggle the visibility of the secondary toolbar. // Button to toggle the visibility of the secondary toolbar.
this.toggleButton.addEventListener("click", this.toggle.bind(this)); toggleButton.addEventListener("click", this.toggle.bind(this));
// All items within the secondary toolbar. // All items within the secondary toolbar.
for (const { element, eventName, close, eventDetails } of this.buttons) { for (const { element, eventName, close, eventDetails } of buttons) {
element.addEventListener("click", evt => { element.addEventListener("click", evt => {
if (eventName !== null) { if (eventName !== null) {
this.eventBus.dispatch(eventName, { source: this, ...eventDetails }); eventBus.dispatch(eventName, { source: this, ...eventDetails });
} }
if (close) { if (close) {
this.close(); this.close();
} }
this.eventBus.dispatch("reporttelemetry", { eventBus.dispatch("reporttelemetry", {
source: this, source: this,
details: { details: {
type: "buttons", type: "buttons",
@ -222,72 +224,58 @@ class SecondaryToolbar {
}); });
}); });
} }
eventBus._on("cursortoolchanged", this.#cursorToolChanged.bind(this));
eventBus._on("scrollmodechanged", this.#scrollModeChanged.bind(this));
eventBus._on("spreadmodechanged", this.#spreadModeChanged.bind(this));
} }
#bindCursorToolsListener({ cursorSelectToolButton, cursorHandToolButton }) { #cursorToolChanged({ tool }) {
this.eventBus._on("cursortoolchanged", ({ tool }) => { const { cursorSelectToolButton, cursorHandToolButton } = this.#opts;
toggleCheckedBtn(cursorSelectToolButton, tool === CursorTool.SELECT);
toggleCheckedBtn(cursorHandToolButton, tool === CursorTool.HAND); toggleCheckedBtn(cursorSelectToolButton, tool === CursorTool.SELECT);
}); toggleCheckedBtn(cursorHandToolButton, tool === CursorTool.HAND);
} }
#bindScrollModeListener({ #scrollModeChanged({ mode }) {
scrollPageButton, const {
scrollVerticalButton, scrollPageButton,
scrollHorizontalButton, scrollVerticalButton,
scrollWrappedButton, scrollHorizontalButton,
spreadNoneButton, scrollWrappedButton,
spreadOddButton, spreadNoneButton,
spreadEvenButton, spreadOddButton,
}) { spreadEvenButton,
const scrollModeChanged = ({ mode }) => { } = this.#opts;
toggleCheckedBtn(scrollPageButton, mode === ScrollMode.PAGE);
toggleCheckedBtn(scrollVerticalButton, mode === ScrollMode.VERTICAL);
toggleCheckedBtn(scrollHorizontalButton, mode === ScrollMode.HORIZONTAL);
toggleCheckedBtn(scrollWrappedButton, mode === ScrollMode.WRAPPED);
// Permanently *disable* the Scroll buttons when PAGE-scrolling is being toggleCheckedBtn(scrollPageButton, mode === ScrollMode.PAGE);
// enforced for *very* long/large documents; please see the `BaseViewer`. toggleCheckedBtn(scrollVerticalButton, mode === ScrollMode.VERTICAL);
const forceScrollModePage = toggleCheckedBtn(scrollHorizontalButton, mode === ScrollMode.HORIZONTAL);
this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE; toggleCheckedBtn(scrollWrappedButton, mode === ScrollMode.WRAPPED);
scrollPageButton.disabled = forceScrollModePage;
scrollVerticalButton.disabled = forceScrollModePage;
scrollHorizontalButton.disabled = forceScrollModePage;
scrollWrappedButton.disabled = forceScrollModePage;
// Temporarily *disable* the Spread buttons when horizontal scrolling is // Permanently *disable* the Scroll buttons when PAGE-scrolling is being
// enabled, since the non-default Spread modes doesn't affect the layout. // enforced for *very* long/large documents; please see the `BaseViewer`.
const isHorizontal = mode === ScrollMode.HORIZONTAL; const forceScrollModePage =
spreadNoneButton.disabled = isHorizontal; this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE;
spreadOddButton.disabled = isHorizontal; scrollPageButton.disabled = forceScrollModePage;
spreadEvenButton.disabled = isHorizontal; scrollVerticalButton.disabled = forceScrollModePage;
}; scrollHorizontalButton.disabled = forceScrollModePage;
this.eventBus._on("scrollmodechanged", scrollModeChanged); scrollWrappedButton.disabled = forceScrollModePage;
this.eventBus._on("secondarytoolbarreset", evt => { // Temporarily *disable* the Spread buttons when horizontal scrolling is
if (evt.source === this) { // enabled, since the non-default Spread modes doesn't affect the layout.
scrollModeChanged({ mode: ScrollMode.VERTICAL }); const isHorizontal = mode === ScrollMode.HORIZONTAL;
} spreadNoneButton.disabled = isHorizontal;
}); spreadOddButton.disabled = isHorizontal;
spreadEvenButton.disabled = isHorizontal;
} }
#bindSpreadModeListener({ #spreadModeChanged({ mode }) {
spreadNoneButton, const { spreadNoneButton, spreadOddButton, spreadEvenButton } = this.#opts;
spreadOddButton,
spreadEvenButton,
}) {
const spreadModeChanged = ({ mode }) => {
toggleCheckedBtn(spreadNoneButton, mode === SpreadMode.NONE);
toggleCheckedBtn(spreadOddButton, mode === SpreadMode.ODD);
toggleCheckedBtn(spreadEvenButton, mode === SpreadMode.EVEN);
};
this.eventBus._on("spreadmodechanged", spreadModeChanged);
this.eventBus._on("secondarytoolbarreset", evt => { toggleCheckedBtn(spreadNoneButton, mode === SpreadMode.NONE);
if (evt.source === this) { toggleCheckedBtn(spreadOddButton, mode === SpreadMode.ODD);
spreadModeChanged({ mode: SpreadMode.NONE }); toggleCheckedBtn(spreadEvenButton, mode === SpreadMode.EVEN);
}
});
} }
open() { open() {
@ -295,7 +283,9 @@ class SecondaryToolbar {
return; return;
} }
this.opened = true; this.opened = true;
toggleExpandedBtn(this.toggleButton, true, this.toolbar);
const { toggleButton, toolbar } = this.#opts;
toggleExpandedBtn(toggleButton, true, toolbar);
} }
close() { close() {
@ -303,7 +293,9 @@ class SecondaryToolbar {
return; return;
} }
this.opened = false; this.opened = false;
toggleExpandedBtn(this.toggleButton, false, this.toolbar);
const { toggleButton, toolbar } = this.#opts;
toggleExpandedBtn(toggleButton, false, toolbar);
} }
toggle() { toggle() {

@ -43,14 +43,16 @@ import {
*/ */
class Toolbar { class Toolbar {
#opts;
/** /**
* @param {ToolbarOptions} options * @param {ToolbarOptions} options
* @param {EventBus} eventBus * @param {EventBus} eventBus
*/ */
constructor(options, eventBus) { constructor(options, eventBus) {
this.toolbar = options.container; this.#opts = options;
this.eventBus = eventBus; this.eventBus = eventBus;
this.buttons = [ const buttons = [
{ element: options.previous, eventName: "previouspage" }, { element: options.previous, eventName: "previouspage" },
{ element: options.next, eventName: "nextpage" }, { element: options.next, eventName: "nextpage" },
{ element: options.zoomIn, eventName: "zoomin" }, { element: options.zoomIn, eventName: "zoomin" },
@ -106,22 +108,12 @@ class Toolbar {
}, },
}, },
]; ];
this.items = {
numPages: options.numPages,
pageNumber: options.pageNumber,
scaleSelect: options.scaleSelect,
customScaleOption: options.customScaleOption,
previous: options.previous,
next: options.next,
zoomIn: options.zoomIn,
zoomOut: options.zoomOut,
};
// Bind the event listeners for click and various other actions. // Bind the event listeners for click and various other actions.
this.#bindListeners(options); this.#bindListeners(buttons);
if (options.editorHighlightColorPicker) { if (options.editorHighlightColorPicker) {
this.eventBus._on( eventBus._on(
"annotationeditoruimanager", "annotationeditoruimanager",
({ uiManager }) => { ({ uiManager }) => {
this.#setAnnotationEditorUIManager( this.#setAnnotationEditorUIManager(
@ -172,18 +164,19 @@ class Toolbar {
this.updateLoadingIndicatorState(); this.updateLoadingIndicatorState();
// Reset the Editor buttons too, since they're document specific. // Reset the Editor buttons too, since they're document specific.
this.eventBus.dispatch("toolbarreset", { source: this }); this.#editorModeChanged({ mode: AnnotationEditorType.DISABLE });
} }
#bindListeners(options) { #bindListeners(buttons) {
const { pageNumber, scaleSelect } = this.items; const { eventBus } = this;
const { pageNumber, scaleSelect } = this.#opts;
const self = this; const self = this;
// The buttons within the toolbar. // The buttons within the toolbar.
for (const { element, eventName, eventDetails } of this.buttons) { for (const { element, eventName, eventDetails } of buttons) {
element.addEventListener("click", evt => { element.addEventListener("click", evt => {
if (eventName !== null) { if (eventName !== null) {
this.eventBus.dispatch(eventName, { eventBus.dispatch(eventName, {
source: this, source: this,
...eventDetails, ...eventDetails,
// evt.detail is the number of clicks. // evt.detail is the number of clicks.
@ -197,7 +190,7 @@ class Toolbar {
this.select(); this.select();
}); });
pageNumber.addEventListener("change", function () { pageNumber.addEventListener("change", function () {
self.eventBus.dispatch("pagenumberchanged", { eventBus.dispatch("pagenumberchanged", {
source: self, source: self,
value: this.value, value: this.value,
}); });
@ -207,15 +200,14 @@ class Toolbar {
if (this.value === "custom") { if (this.value === "custom") {
return; return;
} }
self.eventBus.dispatch("scalechanged", { eventBus.dispatch("scalechanged", {
source: self, source: self,
value: this.value, value: this.value,
}); });
}); });
// Here we depend on browsers dispatching the "click" event *after* the // Here we depend on browsers dispatching the "click" event *after* the
// "change" event, when the <select>-element changes. // "change" event, when the <select>-element changes.
scaleSelect.addEventListener("click", function (evt) { scaleSelect.addEventListener("click", function ({ target }) {
const target = evt.target;
// Remove focus when an <option>-element was *clicked*, to improve the UX // Remove focus when an <option>-element was *clicked*, to improve the UX
// for mouse users (fixes bug 1300525 and issue 4923). // for mouse users (fixes bug 1300525 and issue 4923).
if ( if (
@ -228,95 +220,92 @@ class Toolbar {
// Suppress context menus for some controls. // Suppress context menus for some controls.
scaleSelect.oncontextmenu = noContextMenu; scaleSelect.oncontextmenu = noContextMenu;
this.#bindEditorToolsListener(options); eventBus._on(
"annotationeditormodechanged",
this.#editorModeChanged.bind(this)
);
} }
#bindEditorToolsListener({ #editorModeChanged({ mode }) {
editorFreeTextButton, const {
editorFreeTextParamsToolbar, editorFreeTextButton,
editorHighlightButton, editorFreeTextParamsToolbar,
editorHighlightParamsToolbar, editorHighlightButton,
editorInkButton, editorHighlightParamsToolbar,
editorInkParamsToolbar, editorInkButton,
editorStampButton, editorInkParamsToolbar,
editorStampParamsToolbar, editorStampButton,
}) { editorStampParamsToolbar,
const editorModeChanged = ({ mode }) => { } = this.#opts;
toggleCheckedBtn(
editorFreeTextButton,
mode === AnnotationEditorType.FREETEXT,
editorFreeTextParamsToolbar
);
toggleCheckedBtn(
editorHighlightButton,
mode === AnnotationEditorType.HIGHLIGHT,
editorHighlightParamsToolbar
);
toggleCheckedBtn(
editorInkButton,
mode === AnnotationEditorType.INK,
editorInkParamsToolbar
);
toggleCheckedBtn(
editorStampButton,
mode === AnnotationEditorType.STAMP,
editorStampParamsToolbar
);
const isDisable = mode === AnnotationEditorType.DISABLE; toggleCheckedBtn(
editorFreeTextButton.disabled = isDisable; editorFreeTextButton,
editorHighlightButton.disabled = isDisable; mode === AnnotationEditorType.FREETEXT,
editorInkButton.disabled = isDisable; editorFreeTextParamsToolbar
editorStampButton.disabled = isDisable; );
}; toggleCheckedBtn(
this.eventBus._on("annotationeditormodechanged", editorModeChanged); editorHighlightButton,
mode === AnnotationEditorType.HIGHLIGHT,
editorHighlightParamsToolbar
);
toggleCheckedBtn(
editorInkButton,
mode === AnnotationEditorType.INK,
editorInkParamsToolbar
);
toggleCheckedBtn(
editorStampButton,
mode === AnnotationEditorType.STAMP,
editorStampParamsToolbar
);
this.eventBus._on("toolbarreset", evt => { const isDisable = mode === AnnotationEditorType.DISABLE;
if (evt.source === this) { editorFreeTextButton.disabled = isDisable;
editorModeChanged({ mode: AnnotationEditorType.DISABLE }); editorHighlightButton.disabled = isDisable;
} editorInkButton.disabled = isDisable;
}); editorStampButton.disabled = isDisable;
} }
#updateUIState(resetNumPages = false) { #updateUIState(resetNumPages = false) {
const { pageNumber, pagesCount, pageScaleValue, pageScale, items } = this; const { pageNumber, pagesCount, pageScaleValue, pageScale } = this;
const opts = this.#opts;
if (resetNumPages) { if (resetNumPages) {
if (this.hasPageLabels) { if (this.hasPageLabels) {
items.pageNumber.type = "text"; opts.pageNumber.type = "text";
items.numPages.setAttribute("data-l10n-id", "pdfjs-page-of-pages"); opts.numPages.setAttribute("data-l10n-id", "pdfjs-page-of-pages");
} else { } else {
items.pageNumber.type = "number"; opts.pageNumber.type = "number";
items.numPages.setAttribute("data-l10n-id", "pdfjs-of-pages"); opts.numPages.setAttribute("data-l10n-id", "pdfjs-of-pages");
items.numPages.setAttribute( opts.numPages.setAttribute(
"data-l10n-args", "data-l10n-args",
JSON.stringify({ pagesCount }) JSON.stringify({ pagesCount })
); );
} }
items.pageNumber.max = pagesCount; opts.pageNumber.max = pagesCount;
} }
if (this.hasPageLabels) { if (this.hasPageLabels) {
items.pageNumber.value = this.pageLabel; opts.pageNumber.value = this.pageLabel;
items.numPages.setAttribute( opts.numPages.setAttribute(
"data-l10n-args", "data-l10n-args",
JSON.stringify({ pageNumber, pagesCount }) JSON.stringify({ pageNumber, pagesCount })
); );
} else { } else {
items.pageNumber.value = pageNumber; opts.pageNumber.value = pageNumber;
} }
items.previous.disabled = pageNumber <= 1; opts.previous.disabled = pageNumber <= 1;
items.next.disabled = pageNumber >= pagesCount; opts.next.disabled = pageNumber >= pagesCount;
items.zoomOut.disabled = pageScale <= MIN_SCALE; opts.zoomOut.disabled = pageScale <= MIN_SCALE;
items.zoomIn.disabled = pageScale >= MAX_SCALE; opts.zoomIn.disabled = pageScale >= MAX_SCALE;
let predefinedValueFound = false; let predefinedValueFound = false;
for (const option of items.scaleSelect.options) { for (const option of opts.scaleSelect.options) {
if (option.value !== pageScaleValue) { if (option.value !== pageScaleValue) {
option.selected = false; option.selected = false;
continue; continue;
@ -325,8 +314,8 @@ class Toolbar {
predefinedValueFound = true; predefinedValueFound = true;
} }
if (!predefinedValueFound) { if (!predefinedValueFound) {
items.customScaleOption.selected = true; opts.customScaleOption.selected = true;
items.customScaleOption.setAttribute( opts.customScaleOption.setAttribute(
"data-l10n-args", "data-l10n-args",
JSON.stringify({ JSON.stringify({
scale: Math.round(pageScale * 10000) / 100, scale: Math.round(pageScale * 10000) / 100,
@ -336,8 +325,7 @@ class Toolbar {
} }
updateLoadingIndicatorState(loading = false) { updateLoadingIndicatorState(loading = false) {
const { pageNumber } = this.items; const { pageNumber } = this.#opts;
pageNumber.classList.toggle("loading", loading); pageNumber.classList.toggle("loading", loading);
} }
} }