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