1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

[api-minor] Deprecate getGlobalEventBus and update the "viewer components" examples accordingly

To avoid outright breaking third-party usages of the "viewer components" the `getGlobalEventBus` functionality is left intact, but a deprecation message is printed if the function is invoked.

The various examples are updated to *explicitly* initialize an `EventBus` instance, and provide that when initializing the relevant viewer components.
This commit is contained in:
Jonas Jenwald 2020-02-26 17:16:30 +01:00
parent 965ebe63fd
commit 9a437a158f
16 changed files with 68 additions and 34 deletions

View file

@ -18,7 +18,7 @@ import {
animationStarted,
AutoPrintRegExp,
DEFAULT_SCALE_VALUE,
getGlobalEventBus,
EventBus,
getPDFFileNameFromURL,
isValidRotation,
isValidScrollMode,
@ -343,7 +343,7 @@ const PDFViewerApplication = {
const eventBus =
appConfig.eventBus ||
getGlobalEventBus(AppOptions.get("eventBusDispatchToDOM"));
new EventBus({ dispatchToDOM: AppOptions.get("eventBusDispatchToDOM") });
this.eventBus = eventBus;
const pdfRenderingQueue = new PDFRenderingQueue();

View file

@ -1088,17 +1088,20 @@ class BaseViewer {
* @param {HTMLDivElement} textLayerDiv
* @param {number} pageIndex
* @param {PageViewport} viewport
* @param {boolean} enhanceTextSelection
* @param {EventBus} eventBus
* @returns {TextLayerBuilder}
*/
createTextLayerBuilder(
textLayerDiv,
pageIndex,
viewport,
enhanceTextSelection = false
enhanceTextSelection = false,
eventBus
) {
return new TextLayerBuilder({
textLayerDiv,
eventBus: this.eventBus,
eventBus,
pageIndex,
viewport,
findController: this.isInPresentationMode ? null : this.findController,

View file

@ -146,13 +146,15 @@ class IPDFTextLayerFactory {
* @param {number} pageIndex
* @param {PageViewport} viewport
* @param {boolean} enhanceTextSelection
* @param {EventBus} eventBus
* @returns {TextLayerBuilder}
*/
createTextLayerBuilder(
textLayerDiv,
pageIndex,
viewport,
enhanceTextSelection = false
enhanceTextSelection = false,
eventBus
) {}
}

View file

@ -25,7 +25,7 @@ const MATCHES_COUNT_LIMIT = 1000;
* is done by PDFFindController.
*/
class PDFFindBar {
constructor(options, eventBus = getGlobalEventBus(), l10n = NullL10n) {
constructor(options, eventBus, l10n = NullL10n) {
this.opened = false;
this.bar = options.bar || null;
@ -38,7 +38,7 @@ class PDFFindBar {
this.findResultsCount = options.findResultsCount || null;
this.findPreviousButton = options.findPreviousButton || null;
this.findNextButton = options.findNextButton || null;
this.eventBus = eventBus;
this.eventBus = eventBus || getGlobalEventBus();
this.l10n = l10n;
// Add event listeners to the DOM elements.

View file

@ -67,9 +67,9 @@ class PDFFindController {
/**
* @param {PDFFindControllerOptions} options
*/
constructor({ linkService, eventBus = getGlobalEventBus() }) {
constructor({ linkService, eventBus }) {
this._linkService = linkService;
this._eventBus = eventBus;
this._eventBus = eventBus || getGlobalEventBus();
this._reset();
eventBus.on("findbarclose", this._onFindBarClose.bind(this));

View file

@ -444,7 +444,8 @@ class PDFPageView {
textLayerDiv,
this.id - 1,
this.viewport,
this.textLayerMode === TextLayerMode.ENABLE_ENHANCE
this.textLayerMode === TextLayerMode.ENABLE_ENHANCE,
this.eventBus
);
}
this.textLayer = textLayer;

View file

@ -21,12 +21,7 @@ import {
DefaultTextLayerFactory,
TextLayerBuilder,
} from "./text_layer_builder.js";
import {
EventBus,
getGlobalEventBus,
NullL10n,
ProgressBar,
} from "./ui_utils.js";
import { EventBus, NullL10n, ProgressBar } from "./ui_utils.js";
import { PDFLinkService, SimpleLinkService } from "./pdf_link_service.js";
import { DownloadManager } from "./download_manager.js";
import { GenericL10n } from "./genericl10n.js";
@ -41,9 +36,6 @@ const pdfjsVersion = PDFJSDev.eval("BUNDLE_VERSION");
// eslint-disable-next-line no-unused-vars
const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD");
// For backwards compatibility, ensure that events are re-dispatched to the DOM.
getGlobalEventBus(/* dispatchToDOM = */ true);
export {
PDFViewer,
PDFSinglePageViewer,

View file

@ -444,19 +444,22 @@ class DefaultTextLayerFactory {
* @param {number} pageIndex
* @param {PageViewport} viewport
* @param {boolean} enhanceTextSelection
* @param {EventBus} eventBus
* @returns {TextLayerBuilder}
*/
createTextLayerBuilder(
textLayerDiv,
pageIndex,
viewport,
enhanceTextSelection = false
enhanceTextSelection = false,
eventBus
) {
return new TextLayerBuilder({
textLayerDiv,
pageIndex,
viewport,
enhanceTextSelection,
eventBus,
});
}
}

View file

@ -757,9 +757,8 @@ const animationStarted = new Promise(function(resolve) {
});
/**
* Simple event bus for an application. Listeners are attached using the
* `on` and `off` methods. To raise an event, the `dispatch` method shall be
* used.
* Simple event bus for an application. Listeners are attached using the `on`
* and `off` methods. To raise an event, the `dispatch` method shall be used.
*/
class EventBus {
constructor({ dispatchToDOM = false } = {}) {
@ -832,6 +831,9 @@ class EventBus {
let globalEventBus = null;
function getGlobalEventBus(dispatchToDOM = false) {
console.error(
"getGlobalEventBus is deprecated, use a manually created EventBus instance instead."
);
if (!globalEventBus) {
globalEventBus = new EventBus({ dispatchToDOM });
}

View file

@ -60,7 +60,7 @@ function getViewerConfiguration() {
appContainer: document.body,
mainContainer: document.getElementById("viewerContainer"),
viewerContainer: document.getElementById("viewer"),
eventBus: null, // Using global event bus with (optional) DOM events.
eventBus: null,
toolbar: {
container: document.getElementById("toolbarViewer"),
numPages: document.getElementById("numPages"),