Remove unnecessary closure in src/display/text_layer.js, and use standard classes

With modern JavaScript modules, where you explicitly list the properties that should be exported, it's no longer necessary to wrap all of the code in a closure.[1]

This patch also tries to clean-up/improve a couple of the existing JSDoc-comments.

---
[1] This reduces the size, even of the *built* `pdf.js` file, since there's now a lot less unnecessary whitespace.
This commit is contained in:
Jonas Jenwald 2021-05-05 16:36:18 +02:00
parent 52961197d3
commit 9a1758c6b8

View File

@ -42,17 +42,6 @@ import {
* selection enhancement.
*/
/**
* @typedef {Object} TextLayerRenderTask
* @property {Promise<void>} promise
* @property {() => void} cancel
* @property {(expandDivs: boolean) => void} expandTextDivs
*/
/**
* @type {(renderParameters: TextLayerRenderParameters) => TextLayerRenderTask}
*/
const renderTextLayer = (function renderTextLayerClosure() {
const MAX_TEXT_DIVS_TO_RENDER = 100000;
const DEFAULT_FONT_SIZE = 30;
const DEFAULT_FONT_ASCENT = 0.8;
@ -138,7 +127,6 @@ const renderTextLayer = (function renderTextLayerClosure() {
scale: 1,
};
textDiv.textContent = geom.str;
task._textDivs.push(textDiv);
const tx = Util.transform(task._viewport.transform, geom.transform);
@ -168,6 +156,7 @@ const renderTextLayer = (function renderTextLayerClosure() {
// Keeps screen readers from pausing on every new text span.
textDiv.setAttribute("role", "presentation");
textDiv.textContent = geom.str;
// geom.dir may be 'ttb' for vertical texts.
textDiv.dir = geom.dir;
@ -561,17 +550,8 @@ const renderTextLayer = (function renderTextLayerClosure() {
}
}
/**
* Text layer rendering task.
*
* @param {TextContent} textContent
* @param {HTMLElement} container
* @param {PageViewport} viewport
* @param {Array} textDivs
* @param {boolean} enhanceTextSelection
* @private
*/
function TextLayerRenderTask({
class TextLayerRenderTask {
constructor({
textContent,
textContentStream,
container,
@ -616,12 +596,19 @@ const renderTextLayer = (function renderTextLayerClosure() {
/* Avoid "Uncaught promise" messages in the console. */
});
}
TextLayerRenderTask.prototype = {
/**
* Promise for textLayer rendering task completion.
* @type {Promise<void>}
*/
get promise() {
return this._capability.promise;
},
}
cancel: function TextLayer_cancel() {
/**
* Cancel rendering of the textLayer.
*/
cancel() {
this._canceled = true;
if (this._reader) {
this._reader.cancel(new AbortException("TextLayer task cancelled."));
@ -632,8 +619,11 @@ const renderTextLayer = (function renderTextLayerClosure() {
this._renderTimer = null;
}
this._capability.reject(new Error("TextLayer task cancelled."));
},
}
/**
* @private
*/
_processItems(items, styleCache) {
for (let i = 0, len = items.length; i < len; i++) {
if (items[i].str === undefined) {
@ -656,8 +646,11 @@ const renderTextLayer = (function renderTextLayerClosure() {
this._textContentItemsStr.push(items[i].str);
appendText(this, items[i], styleCache, this._layoutTextCtx);
}
},
}
/**
* @private
*/
_layoutText(textDiv) {
const textDivProperties = this._textDivProperties.get(textDiv);
@ -700,9 +693,12 @@ const renderTextLayer = (function renderTextLayerClosure() {
br.setAttribute("role", "presentation");
this._container.appendChild(br);
}
},
}
_render: function TextLayer_render(timeout) {
/**
* @private
*/
_render(timeout = 0) {
const capability = createPromiseCapability();
let styleCache = Object.create(null);
@ -759,9 +755,12 @@ const renderTextLayer = (function renderTextLayerClosure() {
}, timeout);
}
}, this._capability.reject);
},
}
expandTextDivs: function TextLayer_expandTextDivs(expandDivs) {
/**
* @param {boolean} [expandDivs]
*/
expandTextDivs(expandDivs = false) {
if (!this._enhanceTextSelection || !this._renderingDone) {
return;
}
@ -820,10 +819,13 @@ const renderTextLayer = (function renderTextLayerClosure() {
div.style.transform = divProps.originalTransform;
}
}
},
};
}
}
// eslint-disable-next-line no-shadow
/**
* @param {TextLayerRenderParameters} renderParameters
* @returns {TextLayerRenderTask}
*/
function renderTextLayer(renderParameters) {
const task = new TextLayerRenderTask({
textContent: renderParameters.textContent,
@ -838,7 +840,4 @@ const renderTextLayer = (function renderTextLayerClosure() {
return task;
}
return renderTextLayer;
})();
export { renderTextLayer };