Enable auto-formatting of the entire code-base using Prettier (issue 11444)

Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).

Prettier is being used for a couple of reasons:

 - To be consistent with `mozilla-central`, where Prettier is already in use across the tree.

 - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.

Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.

*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.

(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
This commit is contained in:
Jonas Jenwald 2019-12-25 15:59:37 +01:00
parent 8ec1dfde49
commit de36b2aaba
205 changed files with 40024 additions and 31859 deletions

View file

@ -14,8 +14,8 @@
*/
/* eslint no-var: error, prefer-const: error */
import { getGlobalEventBus } from './ui_utils';
import { renderTextLayer } from 'pdfjs-lib';
import { getGlobalEventBus } from "./ui_utils";
import { renderTextLayer } from "pdfjs-lib";
const EXPAND_DIVS_TIMEOUT = 300; // ms
@ -37,8 +37,14 @@ const EXPAND_DIVS_TIMEOUT = 300; // ms
* also provides a way to highlight text that is being searched for.
*/
class TextLayerBuilder {
constructor({ textLayerDiv, eventBus, pageIndex, viewport,
findController = null, enhanceTextSelection = false, }) {
constructor({
textLayerDiv,
eventBus,
pageIndex,
viewport,
findController = null,
enhanceTextSelection = false,
}) {
this.textLayerDiv = textLayerDiv;
this.eventBus = eventBus || getGlobalEventBus();
this.textContent = null;
@ -65,12 +71,12 @@ class TextLayerBuilder {
this.renderingDone = true;
if (!this.enhanceTextSelection) {
const endOfContent = document.createElement('div');
endOfContent.className = 'endOfContent';
const endOfContent = document.createElement("div");
endOfContent.className = "endOfContent";
this.textLayerDiv.appendChild(endOfContent);
}
this.eventBus.dispatch('textlayerrendered', {
this.eventBus.dispatch("textlayerrendered", {
source: this,
pageNumber: this.pageNumber,
numTextDivs: this.textDivs.length,
@ -101,22 +107,27 @@ class TextLayerBuilder {
timeout,
enhanceTextSelection: this.enhanceTextSelection,
});
this.textLayerRenderTask.promise.then(() => {
this.textLayerDiv.appendChild(textLayerFrag);
this._finishRendering();
this._updateMatches();
}, function (reason) {
// Cancelled or failed to render text layer; skipping errors.
});
this.textLayerRenderTask.promise.then(
() => {
this.textLayerDiv.appendChild(textLayerFrag);
this._finishRendering();
this._updateMatches();
},
function(reason) {
// Cancelled or failed to render text layer; skipping errors.
}
);
if (!this._onUpdateTextLayerMatches) {
this._onUpdateTextLayerMatches = (evt) => {
this._onUpdateTextLayerMatches = evt => {
if (evt.pageIndex === this.pageIdx || evt.pageIndex === -1) {
this._updateMatches();
}
};
this.eventBus.on('updatetextlayermatches',
this._onUpdateTextLayerMatches);
this.eventBus.on(
"updatetextlayermatches",
this._onUpdateTextLayerMatches
);
}
}
@ -129,8 +140,10 @@ class TextLayerBuilder {
this.textLayerRenderTask = null;
}
if (this._onUpdateTextLayerMatches) {
this.eventBus.off('updatetextlayermatches',
this._onUpdateTextLayerMatches);
this.eventBus.off(
"updatetextlayermatches",
this._onUpdateTextLayerMatches
);
this._onUpdateTextLayerMatches = null;
}
}
@ -150,9 +163,10 @@ class TextLayerBuilder {
if (!matches) {
return [];
}
const { findController, textContentItemsStr, } = this;
const { findController, textContentItemsStr } = this;
let i = 0, iIndex = 0;
let i = 0,
iIndex = 0;
const end = textContentItemsStr.length - 1;
const queryLen = findController.state.query.length;
const result = [];
@ -162,14 +176,13 @@ class TextLayerBuilder {
let matchIdx = matches[m];
// Loop over the divIdxs.
while (i !== end &&
matchIdx >= (iIndex + textContentItemsStr[i].length)) {
while (i !== end && matchIdx >= iIndex + textContentItemsStr[i].length) {
iIndex += textContentItemsStr[i].length;
i++;
}
if (i === textContentItemsStr.length) {
console.error('Could not find a matching mapping');
console.error("Could not find a matching mapping");
}
const match = {
@ -180,16 +193,17 @@ class TextLayerBuilder {
};
// Calculate the end position.
if (matchesLength) { // Multiterm search.
if (matchesLength) {
// Multiterm search.
matchIdx += matchesLength[m];
} else { // Phrase search.
} else {
// Phrase search.
matchIdx += queryLen;
}
// Somewhat the same array as above, but use > instead of >= to get
// the end position right.
while (i !== end &&
matchIdx > (iIndex + textContentItemsStr[i].length)) {
while (i !== end && matchIdx > iIndex + textContentItemsStr[i].length) {
iIndex += textContentItemsStr[i].length;
i++;
}
@ -208,9 +222,9 @@ class TextLayerBuilder {
if (matches.length === 0) {
return;
}
const { findController, pageIdx, textContentItemsStr, textDivs, } = this;
const { findController, pageIdx, textContentItemsStr, textDivs } = this;
const isSelectedPage = (pageIdx === findController.selected.pageIdx);
const isSelectedPage = pageIdx === findController.selected.pageIdx;
const selectedMatchIdx = findController.selected.matchIdx;
const highlightAll = findController.state.highlightAll;
let prevEnd = null;
@ -221,17 +235,19 @@ class TextLayerBuilder {
function beginText(begin, className) {
const divIdx = begin.divIdx;
textDivs[divIdx].textContent = '';
textDivs[divIdx].textContent = "";
appendTextToDiv(divIdx, 0, begin.offset, className);
}
function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
const div = textDivs[divIdx];
const content =
textContentItemsStr[divIdx].substring(fromOffset, toOffset);
const content = textContentItemsStr[divIdx].substring(
fromOffset,
toOffset
);
const node = document.createTextNode(content);
if (className) {
const span = document.createElement('span');
const span = document.createElement("span");
span.className = className;
span.appendChild(node);
div.appendChild(span);
@ -240,7 +256,8 @@ class TextLayerBuilder {
div.appendChild(node);
}
let i0 = selectedMatchIdx, i1 = i0 + 1;
let i0 = selectedMatchIdx,
i1 = i0 + 1;
if (highlightAll) {
i0 = 0;
i1 = matches.length;
@ -253,10 +270,11 @@ class TextLayerBuilder {
const match = matches[i];
const begin = match.begin;
const end = match.end;
const isSelected = (isSelectedPage && i === selectedMatchIdx);
const highlightSuffix = (isSelected ? ' selected' : '');
const isSelected = isSelectedPage && i === selectedMatchIdx;
const highlightSuffix = isSelected ? " selected" : "";
if (isSelected) { // Attempt to scroll the selected match into view.
if (isSelected) {
// Attempt to scroll the selected match into view.
findController.scrollMatchIntoView({
element: textDivs[begin.divIdx],
pageIndex: pageIdx,
@ -277,15 +295,23 @@ class TextLayerBuilder {
}
if (begin.divIdx === end.divIdx) {
appendTextToDiv(begin.divIdx, begin.offset, end.offset,
'highlight' + highlightSuffix);
appendTextToDiv(
begin.divIdx,
begin.offset,
end.offset,
"highlight" + highlightSuffix
);
} else {
appendTextToDiv(begin.divIdx, begin.offset, infinity.offset,
'highlight begin' + highlightSuffix);
appendTextToDiv(
begin.divIdx,
begin.offset,
infinity.offset,
"highlight begin" + highlightSuffix
);
for (let n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
textDivs[n0].className = 'highlight middle' + highlightSuffix;
textDivs[n0].className = "highlight middle" + highlightSuffix;
}
beginText(end, 'highlight end' + highlightSuffix);
beginText(end, "highlight end" + highlightSuffix);
}
prevEnd = end;
}
@ -301,7 +327,11 @@ class TextLayerBuilder {
return;
}
const {
findController, matches, pageIdx, textContentItemsStr, textDivs,
findController,
matches,
pageIdx,
textContentItemsStr,
textDivs,
} = this;
let clearedUntilDivIdx = -1;
@ -312,7 +342,7 @@ class TextLayerBuilder {
for (let n = begin, end = match.end.divIdx; n <= end; n++) {
const div = textDivs[n];
div.textContent = textContentItemsStr[n];
div.className = '';
div.className = "";
}
clearedUntilDivIdx = match.end.divIdx + 1;
}
@ -340,46 +370,55 @@ class TextLayerBuilder {
const div = this.textLayerDiv;
let expandDivsTimer = null;
div.addEventListener('mousedown', (evt) => {
div.addEventListener("mousedown", evt => {
if (this.enhanceTextSelection && this.textLayerRenderTask) {
this.textLayerRenderTask.expandTextDivs(true);
if ((typeof PDFJSDev === 'undefined' ||
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) &&
expandDivsTimer) {
if (
(typeof PDFJSDev === "undefined" ||
!PDFJSDev.test("FIREFOX || MOZCENTRAL")) &&
expandDivsTimer
) {
clearTimeout(expandDivsTimer);
expandDivsTimer = null;
}
return;
}
const end = div.querySelector('.endOfContent');
const end = div.querySelector(".endOfContent");
if (!end) {
return;
}
if (typeof PDFJSDev === 'undefined' ||
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
if (
typeof PDFJSDev === "undefined" ||
!PDFJSDev.test("FIREFOX || MOZCENTRAL")
) {
// On non-Firefox browsers, the selection will feel better if the height
// of the `endOfContent` div is adjusted to start at mouse click
// location. This avoids flickering when the selection moves up.
// However it does not work when selection is started on empty space.
let adjustTop = evt.target !== div;
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
adjustTop = adjustTop && window.getComputedStyle(end).
getPropertyValue('-moz-user-select') !== 'none';
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
adjustTop =
adjustTop &&
window
.getComputedStyle(end)
.getPropertyValue("-moz-user-select") !== "none";
}
if (adjustTop) {
const divBounds = div.getBoundingClientRect();
const r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
end.style.top = (r * 100).toFixed(2) + '%';
end.style.top = (r * 100).toFixed(2) + "%";
}
}
end.classList.add('active');
end.classList.add("active");
});
div.addEventListener('mouseup', () => {
div.addEventListener("mouseup", () => {
if (this.enhanceTextSelection && this.textLayerRenderTask) {
if (typeof PDFJSDev === 'undefined' ||
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
if (
typeof PDFJSDev === "undefined" ||
!PDFJSDev.test("FIREFOX || MOZCENTRAL")
) {
expandDivsTimer = setTimeout(() => {
if (this.textLayerRenderTask) {
this.textLayerRenderTask.expandTextDivs(false);
@ -392,15 +431,17 @@ class TextLayerBuilder {
return;
}
const end = div.querySelector('.endOfContent');
const end = div.querySelector(".endOfContent");
if (!end) {
return;
}
if (typeof PDFJSDev === 'undefined' ||
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
end.style.top = '';
if (
typeof PDFJSDev === "undefined" ||
!PDFJSDev.test("FIREFOX || MOZCENTRAL")
) {
end.style.top = "";
}
end.classList.remove('active');
end.classList.remove("active");
});
}
}
@ -416,8 +457,12 @@ class DefaultTextLayerFactory {
* @param {boolean} enhanceTextSelection
* @returns {TextLayerBuilder}
*/
createTextLayerBuilder(textLayerDiv, pageIndex, viewport,
enhanceTextSelection = false) {
createTextLayerBuilder(
textLayerDiv,
pageIndex,
viewport,
enhanceTextSelection = false
) {
return new TextLayerBuilder({
textLayerDiv,
pageIndex,
@ -427,7 +472,4 @@ class DefaultTextLayerFactory {
}
}
export {
TextLayerBuilder,
DefaultTextLayerFactory,
};
export { TextLayerBuilder, DefaultTextLayerFactory };