From 82d127883d0e9369e8227cb34ad2a859df6130ee Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 29 Nov 2022 12:14:40 +0100 Subject: [PATCH] Stop duplicating the `platform` getter in multiple files Currently both of the `AnnotationElement` and `KeyboardManager` classes contain *identical* `platform` getters, which seems like unnecessary duplication. With the pre-processor we can also limit the feature-testing to only GENERIC builds, since `navigator` should always be available in browsers. --- src/display/annotation_layer.js | 12 ++---------- src/display/editor/annotation_editor_layer.js | 8 ++++---- src/display/editor/editor.js | 6 +++--- src/display/editor/tools.js | 12 ++---------- src/shared/util.js | 13 +++++++++++++ 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index 2d311fafb..a0058754c 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -22,6 +22,7 @@ import { AnnotationBorderStyleType, AnnotationType, assert, + FeatureTest, LINE_FACTOR, shadow, unreachable, @@ -563,15 +564,6 @@ class AnnotationElement { } return fields; } - - static get platform() { - const platform = typeof navigator !== "undefined" ? navigator.platform : ""; - - return shadow(this, "platform", { - isWin: platform.includes("Win"), - isMac: platform.includes("Mac"), - }); - } } class LinkAnnotationElement extends AnnotationElement { @@ -904,7 +896,7 @@ class WidgetAnnotationElement extends AnnotationElement { } _getKeyModifier(event) { - const { isWin, isMac } = AnnotationElement.platform; + const { isWin, isMac } = FeatureTest.platform; return (isWin && event.ctrlKey) || (isMac && event.metaKey); } diff --git a/src/display/editor/annotation_editor_layer.js b/src/display/editor/annotation_editor_layer.js index 27c254e80..6d5fea76c 100644 --- a/src/display/editor/annotation_editor_layer.js +++ b/src/display/editor/annotation_editor_layer.js @@ -22,8 +22,8 @@ /** @typedef {import("../../web/text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */ /** @typedef {import("../../web/interfaces").IL10n} IL10n */ -import { bindEvents, KeyboardManager } from "./tools.js"; -import { AnnotationEditorType } from "../../shared/util.js"; +import { AnnotationEditorType, FeatureTest } from "../../shared/util.js"; +import { bindEvents } from "./tools.js"; import { FreeTextEditor } from "./freetext.js"; import { InkEditor } from "./ink.js"; @@ -429,7 +429,7 @@ class AnnotationEditorLayer { * @param {PointerEvent} event */ pointerup(event) { - const isMac = KeyboardManager.platform.isMac; + const { isMac } = FeatureTest.platform; if (event.button !== 0 || (event.ctrlKey && isMac)) { // Don't create an editor on right click. return; @@ -461,7 +461,7 @@ class AnnotationEditorLayer { * @param {PointerEvent} event */ pointerdown(event) { - const isMac = KeyboardManager.platform.isMac; + const { isMac } = FeatureTest.platform; if (event.button !== 0 || (event.ctrlKey && isMac)) { // Do nothing on right click. return; diff --git a/src/display/editor/editor.js b/src/display/editor/editor.js index f925b7f7d..60a1fcfb3 100644 --- a/src/display/editor/editor.js +++ b/src/display/editor/editor.js @@ -16,8 +16,8 @@ // eslint-disable-next-line max-len /** @typedef {import("./annotation_editor_layer.js").AnnotationEditorLayer} AnnotationEditorLayer */ -import { bindEvents, ColorManager, KeyboardManager } from "./tools.js"; -import { shadow, unreachable } from "../../shared/util.js"; +import { bindEvents, ColorManager } from "./tools.js"; +import { FeatureTest, shadow, unreachable } from "../../shared/util.js"; /** * @typedef {Object} AnnotationEditorParameters @@ -274,7 +274,7 @@ class AnnotationEditor { * @param {PointerEvent} event */ pointerdown(event) { - const isMac = KeyboardManager.platform.isMac; + const { isMac } = FeatureTest.platform; if (event.button !== 0 || (event.ctrlKey && isMac)) { // Avoid to focus this editor because of a non-left click. event.preventDefault(); diff --git a/src/display/editor/tools.js b/src/display/editor/tools.js index dd0629595..e502934b7 100644 --- a/src/display/editor/tools.js +++ b/src/display/editor/tools.js @@ -20,6 +20,7 @@ import { AnnotationEditorPrefix, AnnotationEditorType, + FeatureTest, shadow, Util, warn, @@ -211,7 +212,7 @@ class KeyboardManager { this.callbacks = new Map(); this.allKeys = new Set(); - const isMac = KeyboardManager.platform.isMac; + const { isMac } = FeatureTest.platform; for (const [keys, callback] of callbacks) { for (const key of keys) { const isMacKey = key.startsWith("mac+"); @@ -226,15 +227,6 @@ class KeyboardManager { } } - static get platform() { - const platform = typeof navigator !== "undefined" ? navigator.platform : ""; - - return shadow(this, "platform", { - isWin: platform.includes("Win"), - isMac: platform.includes("Mac"), - }); - } - /** * Serialize an event into a string in order to match a * potential key for a callback. diff --git a/src/shared/util.js b/src/shared/util.js index 6394e383c..c45ce201f 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -738,6 +738,19 @@ class FeatureTest { typeof OffscreenCanvas !== "undefined" ); } + + static get platform() { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && + typeof navigator === "undefined" + ) { + return shadow(this, "platform", { isWin: false, isMac: false }); + } + return shadow(this, "platform", { + isWin: navigator.platform.includes("Win"), + isMac: navigator.platform.includes("Mac"), + }); + } } const hexNumbers = [...Array(256).keys()].map(n =>