2020-12-10 22:45:14 +01:00
|
|
|
/* Copyright 2020 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-10-26 22:42:41 +02:00
|
|
|
import os from "os";
|
2024-08-16 18:06:53 +01:00
|
|
|
|
2023-10-26 22:42:41 +02:00
|
|
|
const isMac = os.platform() === "darwin";
|
|
|
|
|
2024-12-11 13:58:52 +01:00
|
|
|
function loadAndWait(filename, selector, zoom, setups, options, viewport) {
|
2023-10-12 13:16:58 +02:00
|
|
|
return Promise.all(
|
2020-12-10 22:45:14 +01:00
|
|
|
global.integrationSessions.map(async session => {
|
|
|
|
const page = await session.browser.newPage();
|
2021-09-08 18:00:03 +02:00
|
|
|
|
2024-12-11 13:58:52 +01:00
|
|
|
if (viewport) {
|
|
|
|
await page.setViewport(viewport);
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:00:03 +02:00
|
|
|
// In order to avoid errors because of checks which depend on
|
|
|
|
// a locale.
|
|
|
|
await page.evaluateOnNewDocument(() => {
|
|
|
|
Object.defineProperty(navigator, "language", {
|
|
|
|
get() {
|
|
|
|
return "en-US";
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Object.defineProperty(navigator, "languages", {
|
|
|
|
get() {
|
|
|
|
return ["en-US", "en"];
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-01-05 10:10:01 +01:00
|
|
|
let app_options = "";
|
|
|
|
if (options) {
|
2024-06-07 13:44:22 +02:00
|
|
|
const optionsObject =
|
|
|
|
typeof options === "function"
|
|
|
|
? await options(page, session.name)
|
|
|
|
: options;
|
|
|
|
|
2024-01-05 10:10:01 +01:00
|
|
|
// Options must be handled in app.js::_parseHashParams.
|
2024-06-07 13:44:22 +02:00
|
|
|
for (const [key, value] of Object.entries(optionsObject)) {
|
2024-01-05 10:10:01 +01:00
|
|
|
app_options += `&${key}=${encodeURIComponent(value)}`;
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 22:42:41 +02:00
|
|
|
const url = `${
|
|
|
|
global.integrationBaseUrl
|
2024-01-05 10:10:01 +01:00
|
|
|
}?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}${app_options}`;
|
2023-10-26 22:42:41 +02:00
|
|
|
|
2024-08-16 18:06:53 +01:00
|
|
|
if (setups) {
|
|
|
|
// page.evaluateOnNewDocument allows us to run code before the
|
|
|
|
// first js script is executed.
|
|
|
|
// The idea here is to set up some setters for PDFViewerApplication
|
|
|
|
// and EventBus, so we can inject some code to do whatever we want
|
|
|
|
// soon enough especially before the first event in the eventBus is
|
|
|
|
// dispatched.
|
2024-08-20 22:24:08 +01:00
|
|
|
const { prePageSetup, appSetup, earlySetup, eventBusSetup } = setups;
|
2024-08-16 18:06:53 +01:00
|
|
|
await prePageSetup?.(page);
|
2024-08-20 22:24:08 +01:00
|
|
|
if (earlySetup || appSetup || eventBusSetup) {
|
2024-08-16 18:06:53 +01:00
|
|
|
await page.evaluateOnNewDocument(
|
2024-08-20 22:24:08 +01:00
|
|
|
(eaSetup, aSetup, evSetup) => {
|
|
|
|
if (eaSetup) {
|
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
eval(`(${eaSetup})`)();
|
|
|
|
}
|
2024-08-16 18:06:53 +01:00
|
|
|
let app;
|
|
|
|
let eventBus;
|
|
|
|
Object.defineProperty(window, "PDFViewerApplication", {
|
|
|
|
get() {
|
|
|
|
return app;
|
|
|
|
},
|
|
|
|
set(newValue) {
|
|
|
|
app = newValue;
|
|
|
|
if (aSetup) {
|
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
eval(`(${aSetup})`)(app);
|
|
|
|
}
|
|
|
|
Object.defineProperty(app, "eventBus", {
|
|
|
|
get() {
|
|
|
|
return eventBus;
|
|
|
|
},
|
|
|
|
set(newV) {
|
|
|
|
eventBus = newV;
|
2024-08-20 22:24:08 +01:00
|
|
|
if (evSetup) {
|
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
eval(`(${evSetup})`)(eventBus);
|
|
|
|
}
|
2024-08-16 18:06:53 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2024-08-20 22:24:08 +01:00
|
|
|
earlySetup?.toString(),
|
2024-08-16 18:06:53 +01:00
|
|
|
appSetup?.toString(),
|
|
|
|
eventBusSetup?.toString()
|
|
|
|
);
|
|
|
|
}
|
2023-07-27 20:12:38 +02:00
|
|
|
}
|
|
|
|
|
2024-08-16 18:06:53 +01:00
|
|
|
await page.goto(url);
|
|
|
|
await setups?.postPageSetup?.(page);
|
|
|
|
|
2020-12-10 22:45:14 +01:00
|
|
|
await page.bringToFront();
|
2023-12-09 15:00:11 +01:00
|
|
|
if (selector) {
|
|
|
|
await page.waitForSelector(selector, {
|
|
|
|
timeout: 0,
|
|
|
|
});
|
|
|
|
}
|
2020-12-10 22:45:14 +01:00
|
|
|
return [session.name, page];
|
|
|
|
})
|
|
|
|
);
|
2023-10-12 13:16:58 +02:00
|
|
|
}
|
2020-12-10 22:45:14 +01:00
|
|
|
|
2023-12-06 15:27:31 +01:00
|
|
|
function createPromise(page, callback) {
|
|
|
|
return page.evaluateHandle(
|
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
cb => [new Promise(eval(`(${cb})`))],
|
|
|
|
callback.toString()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function awaitPromise(promise) {
|
|
|
|
return promise.evaluate(([p]) => p);
|
|
|
|
}
|
|
|
|
|
2023-10-12 13:16:58 +02:00
|
|
|
function closePages(pages) {
|
2024-06-23 12:37:43 +02:00
|
|
|
return Promise.all(pages.map(([_, page]) => closeSinglePage(page)));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function closeSinglePage(page) {
|
|
|
|
// Avoid to keep something from a previous test.
|
|
|
|
await page.evaluate(async () => {
|
|
|
|
await window.PDFViewerApplication.testingClose();
|
|
|
|
window.localStorage.clear();
|
|
|
|
});
|
|
|
|
await page.close({ runBeforeUnload: false });
|
2023-10-12 13:16:58 +02:00
|
|
|
}
|
2020-11-18 18:54:26 +01:00
|
|
|
|
2024-04-03 22:24:17 +02:00
|
|
|
async function waitForSandboxTrip(page) {
|
|
|
|
const handle = await page.evaluateHandle(() => [
|
|
|
|
new Promise(resolve => {
|
|
|
|
window.addEventListener("sandboxtripend", resolve, { once: true });
|
|
|
|
window.PDFViewerApplication.pdfScriptingManager.sandboxTrip();
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
await awaitPromise(handle);
|
|
|
|
}
|
|
|
|
|
2024-02-10 18:21:34 +01:00
|
|
|
function waitForTimeout(milliseconds) {
|
|
|
|
/**
|
|
|
|
* Wait for the given number of milliseconds.
|
|
|
|
*
|
|
|
|
* Note that waiting for an arbitrary time in tests is discouraged because it
|
|
|
|
* can easily cause intermittent failures, which is why this functionality is
|
|
|
|
* no longer provided by Puppeteer 22+ and we have to implement it ourselves
|
|
|
|
* for the remaining callers in the integration tests. We should avoid
|
|
|
|
* creating new usages of this function; instead please refer to the better
|
|
|
|
* alternatives at https://github.com/puppeteer/puppeteer/pull/11780.
|
|
|
|
*/
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(resolve, milliseconds);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-08 15:54:26 +02:00
|
|
|
async function clearInput(page, selector, waitForInputEvent = false) {
|
|
|
|
const action = async () => {
|
|
|
|
await page.click(selector);
|
|
|
|
await kbSelectAll(page);
|
|
|
|
await page.keyboard.press("Backspace");
|
|
|
|
await page.waitForFunction(
|
|
|
|
`document.querySelector('${selector}').value === ""`
|
|
|
|
);
|
|
|
|
};
|
|
|
|
return waitForInputEvent
|
|
|
|
? waitForEvent({
|
|
|
|
page,
|
|
|
|
eventName: "input",
|
|
|
|
action,
|
|
|
|
selector,
|
|
|
|
})
|
|
|
|
: action();
|
2023-10-12 13:16:58 +02:00
|
|
|
}
|
2022-06-17 22:01:20 +02:00
|
|
|
|
2024-07-11 22:31:55 +02:00
|
|
|
async function waitAndClick(page, selector, clickOptions = {}) {
|
|
|
|
await page.waitForSelector(selector, { visible: true });
|
|
|
|
await page.click(selector, clickOptions);
|
|
|
|
}
|
|
|
|
|
2025-02-18 15:24:01 +01:00
|
|
|
function waitForPointerUp(page) {
|
|
|
|
return createPromise(page, resolve => {
|
|
|
|
window.addEventListener("pointerup", resolve, { once: true });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-17 22:01:20 +02:00
|
|
|
function getSelector(id) {
|
|
|
|
return `[data-element-id="${id}"]`;
|
|
|
|
}
|
|
|
|
|
2024-05-23 17:53:58 +02:00
|
|
|
async function getRect(page, selector) {
|
|
|
|
// In Chrome something is wrong when serializing a `DomRect`,
|
|
|
|
// so we extract the values and return them ourselves.
|
2024-07-03 09:57:12 +02:00
|
|
|
await page.waitForSelector(selector, { visible: true });
|
2024-05-23 17:53:58 +02:00
|
|
|
return page.$eval(selector, el => {
|
|
|
|
const { x, y, width, height } = el.getBoundingClientRect();
|
|
|
|
return { x, y, width, height };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-17 22:01:20 +02:00
|
|
|
function getQuerySelector(id) {
|
|
|
|
return `document.querySelector('${getSelector(id)}')`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getComputedStyleSelector(id) {
|
|
|
|
return `getComputedStyle(${getQuerySelector(id)})`;
|
|
|
|
}
|
2023-10-12 13:16:58 +02:00
|
|
|
|
|
|
|
function getEditorSelector(n) {
|
|
|
|
return `#pdfjs_internal_editor_${n}`;
|
|
|
|
}
|
2022-07-22 12:13:47 +02:00
|
|
|
|
2024-10-02 14:36:35 +02:00
|
|
|
function getAnnotationSelector(id) {
|
|
|
|
return `[data-annotation-id="${id}"]`;
|
|
|
|
}
|
|
|
|
|
Fix flickering on text selection
When seleciting on a touch screen device, whenever the finger moves to a
blank area (so over `div.textLayer` directly rather than on a `<span>`),
the selection jumps to include all the text between the beginning of the
.textLayer and the selection side that is not being moved.
The existing selection flickering fix when using the mouse cannot be
trivially re-used on mobile, because when modifying a selection on
a touchscreen device Firefox will not emit any pointer event (and
Chrome will emit them inconsistently). Instead, we have to listen to the
'selectionchange' event.
The fix is different in Firefox and Chrome:
- on Firefox, we have to make sure that, when modifying the selection,
hovering on blank areas will hover on the .endOfContent element
rather than on the .textLayer element. This is done by adjusting the
z-indexes so that .endOfContent is above .textLayer.
- on Chrome, hovering on blank areas needs to trigger hovering on an
element that is either immediately after (or immediately before,
depending on which side of the selection the user is moving) the
currently selected text. This is done by moving the .endOfContent
element around between the correct `<span>`s in the text layer.
The new anti-flickering code is also used when selecting using a mouse:
the improvement in Firefox is only observable on multi-page selection,
while in Chrome it also affects selection within a single page.
After this commit, the `z-index`es inside .textLayer are as follows:
- .endOfContent has `z-index: 0`
- everything else has `z-index: 1`
- except for .markedContent, which have `z-index: 0`
and their contents have `z-index: 1`.
`.textLayer` has an explicit `z-index: 0` to introduce a new stacking context,
so that its contents are not drawn on top of `.annotationLayer`.
2024-04-11 12:50:57 +02:00
|
|
|
async function getSpanRectFromText(page, pageNumber, text) {
|
|
|
|
await page.waitForSelector(
|
|
|
|
`.page[data-page-number="${pageNumber}"] > .textLayer .endOfContent`
|
|
|
|
);
|
|
|
|
return page.evaluate(
|
|
|
|
(number, content) => {
|
|
|
|
for (const el of document.querySelectorAll(
|
2024-07-22 15:02:52 +02:00
|
|
|
`.page[data-page-number="${number}"] > .textLayer span:not(:has(> span))`
|
Fix flickering on text selection
When seleciting on a touch screen device, whenever the finger moves to a
blank area (so over `div.textLayer` directly rather than on a `<span>`),
the selection jumps to include all the text between the beginning of the
.textLayer and the selection side that is not being moved.
The existing selection flickering fix when using the mouse cannot be
trivially re-used on mobile, because when modifying a selection on
a touchscreen device Firefox will not emit any pointer event (and
Chrome will emit them inconsistently). Instead, we have to listen to the
'selectionchange' event.
The fix is different in Firefox and Chrome:
- on Firefox, we have to make sure that, when modifying the selection,
hovering on blank areas will hover on the .endOfContent element
rather than on the .textLayer element. This is done by adjusting the
z-indexes so that .endOfContent is above .textLayer.
- on Chrome, hovering on blank areas needs to trigger hovering on an
element that is either immediately after (or immediately before,
depending on which side of the selection the user is moving) the
currently selected text. This is done by moving the .endOfContent
element around between the correct `<span>`s in the text layer.
The new anti-flickering code is also used when selecting using a mouse:
the improvement in Firefox is only observable on multi-page selection,
while in Chrome it also affects selection within a single page.
After this commit, the `z-index`es inside .textLayer are as follows:
- .endOfContent has `z-index: 0`
- everything else has `z-index: 1`
- except for .markedContent, which have `z-index: 0`
and their contents have `z-index: 1`.
`.textLayer` has an explicit `z-index: 0` to introduce a new stacking context,
so that its contents are not drawn on top of `.annotationLayer`.
2024-04-11 12:50:57 +02:00
|
|
|
)) {
|
|
|
|
if (el.textContent === content) {
|
|
|
|
const { x, y, width, height } = el.getBoundingClientRect();
|
|
|
|
return { x, y, width, height };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
pageNumber,
|
|
|
|
text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-23 16:10:27 +02:00
|
|
|
async function waitForEvent({
|
2024-06-23 14:29:33 +02:00
|
|
|
page,
|
|
|
|
eventName,
|
2024-06-23 16:10:27 +02:00
|
|
|
action,
|
2024-06-23 14:29:33 +02:00
|
|
|
selector = null,
|
|
|
|
validator = null,
|
2024-06-23 16:10:27 +02:00
|
|
|
timeout = 5000,
|
|
|
|
}) {
|
2023-12-06 15:27:31 +01:00
|
|
|
const handle = await page.evaluateHandle(
|
2024-06-23 14:29:33 +02:00
|
|
|
(name, sel, validate, timeOut) => {
|
2024-06-23 14:25:52 +02:00
|
|
|
let callback = null,
|
|
|
|
timeoutId = null;
|
2024-06-23 14:29:33 +02:00
|
|
|
const element = sel ? document.querySelector(sel) : document;
|
2023-12-06 15:27:31 +01:00
|
|
|
return [
|
|
|
|
Promise.race([
|
|
|
|
new Promise(resolve => {
|
2024-06-23 14:29:33 +02:00
|
|
|
// The promise is resolved if the event fired in the context of the
|
|
|
|
// selector and, if a validator is defined, the event data satisfies
|
|
|
|
// the conditions of the validator function.
|
|
|
|
callback = e => {
|
2024-06-23 14:25:52 +02:00
|
|
|
if (timeoutId) {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
}
|
2024-06-23 14:29:33 +02:00
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
resolve(validate ? eval(`(${validate})`)(e) : true);
|
2024-06-23 14:25:52 +02:00
|
|
|
};
|
2024-06-23 14:29:33 +02:00
|
|
|
element.addEventListener(name, callback, { once: true });
|
2023-12-06 15:27:31 +01:00
|
|
|
}),
|
|
|
|
new Promise(resolve => {
|
2024-06-23 14:25:52 +02:00
|
|
|
timeoutId = setTimeout(() => {
|
2024-06-23 14:29:33 +02:00
|
|
|
element.removeEventListener(name, callback);
|
|
|
|
resolve(null);
|
2023-12-06 15:27:31 +01:00
|
|
|
}, timeOut);
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
eventName,
|
2024-06-23 14:29:33 +02:00
|
|
|
selector,
|
|
|
|
validator ? validator.toString() : null,
|
2023-12-06 15:27:31 +01:00
|
|
|
timeout
|
|
|
|
);
|
2024-06-23 16:10:27 +02:00
|
|
|
|
|
|
|
await action();
|
|
|
|
|
2024-06-23 14:29:33 +02:00
|
|
|
const success = await awaitPromise(handle);
|
|
|
|
if (success === null) {
|
2024-11-14 12:29:52 +01:00
|
|
|
console.warn(
|
|
|
|
`waitForEvent: ${eventName} didn't trigger within the timeout`
|
|
|
|
);
|
2024-06-23 14:29:33 +02:00
|
|
|
} else if (!success) {
|
2024-11-14 12:29:52 +01:00
|
|
|
console.warn(`waitForEvent: ${eventName} triggered, but validation failed`);
|
2023-09-27 22:35:57 +02:00
|
|
|
}
|
2022-10-25 15:28:22 +02:00
|
|
|
}
|
2022-12-06 16:16:24 +01:00
|
|
|
|
2023-10-12 13:16:58 +02:00
|
|
|
async function waitForStorageEntries(page, nEntries) {
|
|
|
|
return page.waitForFunction(
|
2022-12-06 16:16:24 +01:00
|
|
|
n => window.PDFViewerApplication.pdfDocument.annotationStorage.size === n,
|
|
|
|
{},
|
|
|
|
nEntries
|
|
|
|
);
|
2023-10-12 13:16:58 +02:00
|
|
|
}
|
2022-12-06 16:16:24 +01:00
|
|
|
|
2023-10-12 13:16:58 +02:00
|
|
|
async function waitForSerialized(page, nEntries) {
|
|
|
|
return page.waitForFunction(
|
2024-11-18 21:25:43 +01:00
|
|
|
n => {
|
|
|
|
try {
|
|
|
|
return (
|
|
|
|
(window.PDFViewerApplication.pdfDocument.annotationStorage
|
|
|
|
.serializable.map?.size ?? 0) === n
|
|
|
|
);
|
|
|
|
} catch {
|
|
|
|
// When serializing a stamp annotation with a SVG, the transfer
|
|
|
|
// can fail because of the SVG, so we just retry.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2022-12-06 16:16:24 +01:00
|
|
|
{},
|
2023-09-27 22:35:57 +02:00
|
|
|
nEntries
|
2022-12-06 16:16:24 +01:00
|
|
|
);
|
2023-10-12 13:16:58 +02:00
|
|
|
}
|
2023-09-27 22:35:57 +02:00
|
|
|
|
2024-06-19 17:20:42 +02:00
|
|
|
async function applyFunctionToEditor(page, editorId, func) {
|
|
|
|
return page.evaluate(
|
|
|
|
(id, f) => {
|
|
|
|
const editor =
|
|
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.getRawValue(
|
|
|
|
id
|
|
|
|
);
|
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
eval(`(${f})`)(editor);
|
|
|
|
},
|
|
|
|
editorId,
|
|
|
|
func.toString()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-01-18 19:21:55 +01:00
|
|
|
async function selectEditor(page, selector, count = 1) {
|
|
|
|
const editorRect = await getRect(page, selector);
|
|
|
|
await page.mouse.click(
|
|
|
|
editorRect.x + editorRect.width / 2,
|
|
|
|
editorRect.y + editorRect.height / 2,
|
|
|
|
{ count }
|
|
|
|
);
|
|
|
|
await waitForSelectedEditor(page, selector);
|
|
|
|
}
|
|
|
|
|
2023-10-12 13:16:58 +02:00
|
|
|
async function waitForSelectedEditor(page, selector) {
|
|
|
|
return page.waitForSelector(`${selector}.selectedEditor`);
|
|
|
|
}
|
2023-04-13 16:57:23 +02:00
|
|
|
|
2025-01-18 20:00:23 +01:00
|
|
|
async function unselectEditor(page, selector) {
|
|
|
|
await page.keyboard.press("Escape");
|
|
|
|
await waitForUnselectedEditor(page, selector);
|
|
|
|
}
|
|
|
|
|
2023-10-12 13:16:58 +02:00
|
|
|
async function waitForUnselectedEditor(page, selector) {
|
|
|
|
return page.waitForSelector(`${selector}:not(.selectedEditor)`);
|
|
|
|
}
|
2023-09-27 22:35:57 +02:00
|
|
|
|
2023-10-12 13:16:58 +02:00
|
|
|
async function mockClipboard(pages) {
|
|
|
|
return Promise.all(
|
2023-04-13 16:57:23 +02:00
|
|
|
pages.map(async ([_, page]) => {
|
|
|
|
await page.evaluate(() => {
|
|
|
|
let data = null;
|
|
|
|
const clipboard = {
|
|
|
|
writeText: async text => (data = text),
|
|
|
|
readText: async () => data,
|
|
|
|
};
|
|
|
|
Object.defineProperty(navigator, "clipboard", { value: clipboard });
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2023-10-12 13:16:58 +02:00
|
|
|
}
|
2023-06-06 17:18:02 +02:00
|
|
|
|
2024-06-21 20:37:01 +02:00
|
|
|
async function copy(page) {
|
2024-06-23 16:10:27 +02:00
|
|
|
await waitForEvent({
|
|
|
|
page,
|
|
|
|
eventName: "copy",
|
|
|
|
action: () => kbCopy(page),
|
|
|
|
});
|
2024-06-21 20:37:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function copyToClipboard(page, data) {
|
2024-03-26 21:17:36 +01:00
|
|
|
await page.evaluate(async dat => {
|
|
|
|
const items = Object.create(null);
|
|
|
|
for (const [type, value] of Object.entries(dat)) {
|
|
|
|
if (value.startsWith("data:")) {
|
|
|
|
const resp = await fetch(value);
|
|
|
|
items[type] = await resp.blob();
|
|
|
|
} else {
|
|
|
|
items[type] = new Blob([value], { type });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await navigator.clipboard.write([new ClipboardItem(items)]);
|
|
|
|
}, data);
|
2024-06-21 20:37:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function paste(page) {
|
2024-06-23 16:10:27 +02:00
|
|
|
await waitForEvent({
|
|
|
|
page,
|
|
|
|
eventName: "paste",
|
|
|
|
action: () => kbPaste(page),
|
|
|
|
});
|
2024-06-21 20:37:01 +02:00
|
|
|
}
|
2024-03-26 21:17:36 +01:00
|
|
|
|
2024-06-21 20:37:01 +02:00
|
|
|
async function pasteFromClipboard(page, selector = null) {
|
2024-06-23 14:29:33 +02:00
|
|
|
const validator = e => e.clipboardData.items.length !== 0;
|
2024-06-23 16:10:27 +02:00
|
|
|
await waitForEvent({
|
|
|
|
page,
|
|
|
|
eventName: "paste",
|
|
|
|
action: () => kbPaste(page),
|
|
|
|
selector,
|
|
|
|
validator,
|
|
|
|
});
|
2024-03-26 21:17:36 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 12:57:59 +02:00
|
|
|
async function getSerialized(page, filter = undefined) {
|
|
|
|
const values = await page.evaluate(() => {
|
2023-06-29 08:43:02 +02:00
|
|
|
const { map } =
|
|
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
|
2024-05-24 15:48:19 +02:00
|
|
|
if (!map) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const vals = Array.from(map.values());
|
|
|
|
for (const value of vals) {
|
|
|
|
for (const [k, v] of Object.entries(value)) {
|
|
|
|
// Puppeteer don't serialize typed array correctly, so we convert them
|
|
|
|
// to arrays.
|
|
|
|
if (ArrayBuffer.isView(v)) {
|
|
|
|
value[k] = Array.from(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vals;
|
2023-06-29 08:43:02 +02:00
|
|
|
});
|
2023-07-26 12:57:59 +02:00
|
|
|
return filter ? values.map(filter) : values;
|
|
|
|
}
|
2023-06-06 17:18:02 +02:00
|
|
|
|
2023-10-12 13:16:58 +02:00
|
|
|
async function getFirstSerialized(page, filter = undefined) {
|
|
|
|
return (await getSerialized(page, filter))[0];
|
|
|
|
}
|
2023-07-26 12:57:59 +02:00
|
|
|
|
2023-11-13 11:05:03 +01:00
|
|
|
function getAnnotationStorage(page) {
|
|
|
|
return page.evaluate(() =>
|
|
|
|
Object.fromEntries(
|
2025-04-09 13:42:22 +02:00
|
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable
|
|
|
|
.map || []
|
2023-11-13 11:05:03 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-13 16:14:50 +02:00
|
|
|
function waitForEntryInStorage(page, key, value, checker = (x, y) => x === y) {
|
2023-11-13 11:05:03 +01:00
|
|
|
return page.waitForFunction(
|
2024-05-13 16:14:50 +02:00
|
|
|
(k, v, c) => {
|
2023-11-13 11:05:03 +01:00
|
|
|
const { map } =
|
|
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
|
2024-05-13 16:14:50 +02:00
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
return map && eval(`(${c})`)(JSON.stringify(map.get(k)), v);
|
2023-11-13 11:05:03 +01:00
|
|
|
},
|
|
|
|
{},
|
|
|
|
key,
|
2024-05-13 16:14:50 +02:00
|
|
|
JSON.stringify(value),
|
|
|
|
checker.toString()
|
2023-11-13 11:05:03 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-06 17:18:02 +02:00
|
|
|
function getEditors(page, kind) {
|
|
|
|
return page.evaluate(aKind => {
|
|
|
|
const elements = document.querySelectorAll(`.${aKind}Editor`);
|
|
|
|
const results = [];
|
|
|
|
for (const { id } of elements) {
|
2024-12-22 18:28:50 +01:00
|
|
|
results.push(parseInt(id.split("_").at(-1)));
|
2023-06-06 17:18:02 +02:00
|
|
|
}
|
2024-12-22 18:28:50 +01:00
|
|
|
results.sort();
|
2023-06-06 17:18:02 +02:00
|
|
|
return results;
|
|
|
|
}, kind);
|
|
|
|
}
|
2023-07-06 15:32:11 +02:00
|
|
|
|
2025-01-19 18:59:55 +01:00
|
|
|
function getEditorDimensions(page, selector) {
|
|
|
|
return page.evaluate(sel => {
|
|
|
|
const { style } = document.querySelector(sel);
|
2023-07-13 18:31:08 +02:00
|
|
|
return {
|
|
|
|
left: style.left,
|
|
|
|
top: style.top,
|
|
|
|
width: style.width,
|
|
|
|
height: style.height,
|
|
|
|
};
|
2025-01-19 18:59:55 +01:00
|
|
|
}, selector);
|
2023-07-06 15:32:11 +02:00
|
|
|
}
|
|
|
|
|
2023-09-29 22:36:01 +02:00
|
|
|
async function serializeBitmapDimensions(page) {
|
|
|
|
await page.waitForFunction(() => {
|
|
|
|
try {
|
|
|
|
const map =
|
|
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable
|
|
|
|
.map;
|
|
|
|
return !!map;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-07-06 15:32:11 +02:00
|
|
|
return page.evaluate(() => {
|
|
|
|
const { map } =
|
|
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
|
|
|
|
return map
|
2024-01-21 15:47:39 +01:00
|
|
|
? Array.from(map.values(), x => ({
|
|
|
|
width: x.bitmap.width,
|
|
|
|
height: x.bitmap.height,
|
|
|
|
}))
|
2023-07-06 15:32:11 +02:00
|
|
|
: [];
|
|
|
|
});
|
|
|
|
}
|
2023-08-02 20:08:09 +02:00
|
|
|
|
2025-01-08 20:44:11 +01:00
|
|
|
async function dragAndDrop(page, selector, translations, steps = 1) {
|
2024-12-09 16:07:39 +01:00
|
|
|
const rect = await getRect(page, selector);
|
|
|
|
const startX = rect.x + rect.width / 2;
|
|
|
|
const startY = rect.y + rect.height / 2;
|
2023-08-02 20:08:09 +02:00
|
|
|
await page.mouse.move(startX, startY);
|
|
|
|
await page.mouse.down();
|
2024-12-09 16:07:39 +01:00
|
|
|
for (const [tX, tY] of translations) {
|
2025-01-08 20:44:11 +01:00
|
|
|
await page.mouse.move(startX + tX, startY + tY, { steps });
|
2024-12-09 16:07:39 +01:00
|
|
|
}
|
2023-08-02 20:08:09 +02:00
|
|
|
await page.mouse.up();
|
2023-09-27 22:35:57 +02:00
|
|
|
await page.waitForSelector("#viewer:not(.noUserSelect)");
|
2023-08-02 20:08:09 +02:00
|
|
|
}
|
2023-08-04 18:21:27 +02:00
|
|
|
|
2023-12-06 15:27:31 +01:00
|
|
|
function waitForAnnotationEditorLayer(page) {
|
|
|
|
return createPromise(page, resolve => {
|
|
|
|
window.PDFViewerApplication.eventBus.on(
|
|
|
|
"annotationeditorlayerrendered",
|
2024-05-21 14:41:07 +02:00
|
|
|
resolve,
|
|
|
|
{ once: true }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForAnnotationModeChanged(page) {
|
|
|
|
return createPromise(page, resolve => {
|
|
|
|
window.PDFViewerApplication.eventBus.on(
|
|
|
|
"annotationeditormodechanged",
|
|
|
|
resolve,
|
|
|
|
{ once: true }
|
2023-12-06 15:27:31 +01:00
|
|
|
);
|
2023-08-04 18:21:27 +02:00
|
|
|
});
|
|
|
|
}
|
2023-09-27 22:35:57 +02:00
|
|
|
|
2025-02-28 15:21:52 +01:00
|
|
|
function waitForPageRendered(page, pageNumber) {
|
|
|
|
return page.evaluateHandle(
|
|
|
|
number => [
|
|
|
|
new Promise(resolve => {
|
|
|
|
const { eventBus } = window.PDFViewerApplication;
|
|
|
|
eventBus.on("pagerendered", function handler(e) {
|
|
|
|
if (
|
|
|
|
!e.isDetailView &&
|
|
|
|
(number === undefined || e.pageNumber === number)
|
|
|
|
) {
|
|
|
|
resolve();
|
|
|
|
eventBus.off("pagerendered", handler);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
pageNumber
|
|
|
|
);
|
2024-05-21 14:41:07 +02:00
|
|
|
}
|
|
|
|
|
2024-12-08 22:16:41 +01:00
|
|
|
function waitForEditorMovedInDOM(page) {
|
|
|
|
return createPromise(page, resolve => {
|
|
|
|
window.PDFViewerApplication.eventBus.on("editormovedindom", resolve, {
|
|
|
|
once: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-27 22:35:57 +02:00
|
|
|
async function scrollIntoView(page, selector) {
|
2023-12-06 15:27:31 +01:00
|
|
|
const handle = await page.evaluateHandle(
|
|
|
|
sel => [
|
|
|
|
new Promise(resolve => {
|
2024-03-11 17:03:44 +01:00
|
|
|
const container = document.getElementById("viewerContainer");
|
|
|
|
if (container.scrollHeight <= container.clientHeight) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
container.addEventListener("scrollend", resolve, { once: true });
|
2023-12-06 15:27:31 +01:00
|
|
|
const element = document.querySelector(sel);
|
|
|
|
element.scrollIntoView({ behavior: "instant", block: "start" });
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
selector
|
|
|
|
);
|
|
|
|
return awaitPromise(handle);
|
2023-09-27 22:35:57 +02:00
|
|
|
}
|
2023-10-12 13:16:58 +02:00
|
|
|
|
2024-03-11 17:03:44 +01:00
|
|
|
async function firstPageOnTop(page) {
|
|
|
|
const handle = await page.evaluateHandle(() => [
|
|
|
|
new Promise(resolve => {
|
|
|
|
const container = document.getElementById("viewerContainer");
|
|
|
|
if (container.scrollTop === 0 && container.scrollLeft === 0) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
container.addEventListener("scrollend", resolve, { once: true });
|
|
|
|
container.scrollTo(0, 0);
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
return awaitPromise(handle);
|
|
|
|
}
|
|
|
|
|
2023-10-26 22:42:41 +02:00
|
|
|
async function hover(page, selector) {
|
2024-05-23 17:53:58 +02:00
|
|
|
const rect = await getRect(page, selector);
|
2023-10-26 22:42:41 +02:00
|
|
|
await page.mouse.move(rect.x + rect.width / 2, rect.y + rect.height / 2);
|
|
|
|
}
|
|
|
|
|
2024-07-11 18:00:36 +02:00
|
|
|
async function setCaretAt(page, pageNumber, text, position) {
|
|
|
|
await page.evaluate(
|
|
|
|
(pageN, string, pos) => {
|
|
|
|
for (const el of document.querySelectorAll(
|
|
|
|
`.page[data-page-number="${pageN}"] > .textLayer > span`
|
|
|
|
)) {
|
|
|
|
if (el.textContent === string) {
|
|
|
|
window.getSelection().setPosition(el.firstChild, pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
pageNumber,
|
|
|
|
text,
|
|
|
|
position
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-26 22:42:41 +02:00
|
|
|
const modifier = isMac ? "Meta" : "Control";
|
|
|
|
async function kbCopy(page) {
|
|
|
|
await page.keyboard.down(modifier);
|
|
|
|
await page.keyboard.press("c", { commands: ["Copy"] });
|
|
|
|
await page.keyboard.up(modifier);
|
|
|
|
}
|
|
|
|
async function kbPaste(page) {
|
|
|
|
await page.keyboard.down(modifier);
|
|
|
|
await page.keyboard.press("v", { commands: ["Paste"] });
|
|
|
|
await page.keyboard.up(modifier);
|
|
|
|
}
|
|
|
|
async function kbUndo(page) {
|
|
|
|
await page.keyboard.down(modifier);
|
|
|
|
await page.keyboard.press("z");
|
|
|
|
await page.keyboard.up(modifier);
|
|
|
|
}
|
|
|
|
async function kbRedo(page) {
|
|
|
|
if (isMac) {
|
|
|
|
await page.keyboard.down("Meta");
|
|
|
|
await page.keyboard.down("Shift");
|
|
|
|
await page.keyboard.press("z");
|
|
|
|
await page.keyboard.up("Shift");
|
|
|
|
await page.keyboard.up("Meta");
|
|
|
|
} else {
|
|
|
|
await page.keyboard.down("Control");
|
|
|
|
await page.keyboard.press("y");
|
|
|
|
await page.keyboard.up("Control");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function kbSelectAll(page) {
|
|
|
|
await page.keyboard.down(modifier);
|
|
|
|
await page.keyboard.press("a", { commands: ["SelectAll"] });
|
|
|
|
await page.keyboard.up(modifier);
|
|
|
|
}
|
|
|
|
async function kbModifierDown(page) {
|
|
|
|
await page.keyboard.down(modifier);
|
|
|
|
}
|
|
|
|
async function kbModifierUp(page) {
|
|
|
|
await page.keyboard.up(modifier);
|
|
|
|
}
|
|
|
|
async function kbGoToEnd(page) {
|
|
|
|
if (isMac) {
|
|
|
|
await page.keyboard.down("Meta");
|
|
|
|
await page.keyboard.press("ArrowDown", {
|
|
|
|
commands: ["MoveToEndOfDocument"],
|
|
|
|
});
|
|
|
|
await page.keyboard.up("Meta");
|
|
|
|
} else {
|
|
|
|
await page.keyboard.down("Control");
|
|
|
|
await page.keyboard.press("End");
|
|
|
|
await page.keyboard.up("Control");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function kbGoToBegin(page) {
|
|
|
|
if (isMac) {
|
|
|
|
await page.keyboard.down("Meta");
|
|
|
|
await page.keyboard.press("ArrowUp", {
|
|
|
|
commands: ["MoveToBeginningOfDocument"],
|
|
|
|
});
|
|
|
|
await page.keyboard.up("Meta");
|
|
|
|
} else {
|
|
|
|
await page.keyboard.down("Control");
|
|
|
|
await page.keyboard.press("Home");
|
|
|
|
await page.keyboard.up("Control");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function kbBigMoveLeft(page) {
|
|
|
|
if (isMac) {
|
|
|
|
await page.keyboard.down("Shift");
|
|
|
|
await page.keyboard.press("ArrowLeft");
|
|
|
|
await page.keyboard.up("Shift");
|
|
|
|
} else {
|
|
|
|
await page.keyboard.down("Control");
|
|
|
|
await page.keyboard.press("ArrowLeft");
|
|
|
|
await page.keyboard.up("Control");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function kbBigMoveRight(page) {
|
|
|
|
if (isMac) {
|
|
|
|
await page.keyboard.down("Shift");
|
|
|
|
await page.keyboard.press("ArrowRight");
|
|
|
|
await page.keyboard.up("Shift");
|
|
|
|
} else {
|
|
|
|
await page.keyboard.down("Control");
|
|
|
|
await page.keyboard.press("ArrowRight");
|
|
|
|
await page.keyboard.up("Control");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function kbBigMoveUp(page) {
|
|
|
|
if (isMac) {
|
|
|
|
await page.keyboard.down("Shift");
|
|
|
|
await page.keyboard.press("ArrowUp");
|
|
|
|
await page.keyboard.up("Shift");
|
|
|
|
} else {
|
|
|
|
await page.keyboard.down("Control");
|
|
|
|
await page.keyboard.press("ArrowUp");
|
|
|
|
await page.keyboard.up("Control");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function kbBigMoveDown(page) {
|
|
|
|
if (isMac) {
|
|
|
|
await page.keyboard.down("Shift");
|
|
|
|
await page.keyboard.press("ArrowDown");
|
|
|
|
await page.keyboard.up("Shift");
|
|
|
|
} else {
|
|
|
|
await page.keyboard.down("Control");
|
|
|
|
await page.keyboard.press("ArrowDown");
|
|
|
|
await page.keyboard.up("Control");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function kbDeleteLastWord(page) {
|
|
|
|
if (isMac) {
|
|
|
|
await page.keyboard.down("Alt");
|
|
|
|
await page.keyboard.press("Backspace");
|
|
|
|
await page.keyboard.up("Alt");
|
|
|
|
} else {
|
|
|
|
await page.keyboard.down("Control");
|
|
|
|
await page.keyboard.press("Backspace");
|
|
|
|
await page.keyboard.up("Control");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:03:44 +01:00
|
|
|
async function kbFocusNext(page) {
|
|
|
|
const handle = await createPromise(page, resolve => {
|
|
|
|
window.addEventListener("focusin", resolve, { once: true });
|
|
|
|
});
|
|
|
|
await page.keyboard.press("Tab");
|
|
|
|
await awaitPromise(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function kbFocusPrevious(page) {
|
|
|
|
const handle = await createPromise(page, resolve => {
|
|
|
|
window.addEventListener("focusin", resolve, { once: true });
|
|
|
|
});
|
|
|
|
await page.keyboard.down("Shift");
|
|
|
|
await page.keyboard.press("Tab");
|
|
|
|
await page.keyboard.up("Shift");
|
|
|
|
await awaitPromise(handle);
|
|
|
|
}
|
|
|
|
|
2024-10-19 02:19:46 +05:30
|
|
|
async function kbSave(page) {
|
|
|
|
await page.keyboard.down(modifier);
|
|
|
|
await page.keyboard.press("s");
|
|
|
|
await page.keyboard.up(modifier);
|
|
|
|
}
|
|
|
|
|
2024-05-24 19:11:37 +02:00
|
|
|
async function switchToEditor(name, page, disable = false) {
|
|
|
|
const modeChangedHandle = await createPromise(page, resolve => {
|
|
|
|
window.PDFViewerApplication.eventBus.on(
|
|
|
|
"annotationeditormodechanged",
|
|
|
|
resolve,
|
|
|
|
{ once: true }
|
|
|
|
);
|
|
|
|
});
|
2024-09-17 18:08:03 +02:00
|
|
|
await page.click(`#editor${name}Button`);
|
2024-05-24 19:11:37 +02:00
|
|
|
name = name.toLowerCase();
|
|
|
|
await page.waitForSelector(
|
|
|
|
".annotationEditorLayer" +
|
|
|
|
(disable ? `:not(.${name}Editing)` : `.${name}Editing`)
|
|
|
|
);
|
|
|
|
await awaitPromise(modeChangedHandle);
|
|
|
|
}
|
|
|
|
|
2025-02-23 18:49:14 +01:00
|
|
|
async function selectEditors(name, page) {
|
|
|
|
await kbSelectAll(page);
|
|
|
|
await page.waitForFunction(
|
|
|
|
() => !document.querySelector(`.${name}Editor:not(.selectedEditor)`)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-02-23 18:52:19 +01:00
|
|
|
async function clearEditors(name, page) {
|
|
|
|
await selectEditors(name, page);
|
|
|
|
await page.keyboard.press("Backspace");
|
|
|
|
await waitForStorageEntries(page, 0);
|
|
|
|
}
|
|
|
|
|
2024-11-29 16:22:44 +01:00
|
|
|
function waitForNoElement(page, selector) {
|
|
|
|
return page.waitForFunction(
|
|
|
|
sel => !document.querySelector(sel),
|
|
|
|
{},
|
|
|
|
selector
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-03-06 14:59:04 +01:00
|
|
|
function isCanvasMonochrome(page, pageNumber, rectangle, color) {
|
2024-12-01 21:15:57 +01:00
|
|
|
return page.evaluate(
|
2025-03-06 14:59:04 +01:00
|
|
|
(rect, pageN, col) => {
|
2024-12-01 21:15:57 +01:00
|
|
|
const canvas = document.querySelector(
|
|
|
|
`.page[data-page-number = "${pageN}"] .canvasWrapper canvas`
|
|
|
|
);
|
|
|
|
const canvasRect = canvas.getBoundingClientRect();
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
rect ||= canvasRect;
|
|
|
|
const { data } = ctx.getImageData(
|
|
|
|
rect.x - canvasRect.x,
|
|
|
|
rect.y - canvasRect.y,
|
|
|
|
rect.width,
|
|
|
|
rect.height
|
|
|
|
);
|
2025-03-06 14:59:04 +01:00
|
|
|
return new Uint32Array(data.buffer).every(x => x === col);
|
2024-12-01 21:15:57 +01:00
|
|
|
},
|
|
|
|
rectangle,
|
2025-03-06 14:59:04 +01:00
|
|
|
pageNumber,
|
|
|
|
color
|
2024-12-01 21:15:57 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-02-22 15:19:10 +01:00
|
|
|
async function getXY(page, selector) {
|
|
|
|
const rect = await getRect(page, selector);
|
|
|
|
return `${rect.x}::${rect.y}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForPositionChange(page, selector, xy) {
|
|
|
|
return page.waitForFunction(
|
|
|
|
(sel, currentXY) => {
|
|
|
|
const bbox = document.querySelector(sel).getBoundingClientRect();
|
|
|
|
return `${bbox.x}::${bbox.y}` !== currentXY;
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
selector,
|
|
|
|
xy
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function moveEditor(page, selector, n, pressKey) {
|
|
|
|
let xy = await getXY(page, selector);
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
const handle = await waitForEditorMovedInDOM(page);
|
|
|
|
await pressKey();
|
|
|
|
await awaitPromise(handle);
|
|
|
|
await waitForPositionChange(page, selector, xy);
|
|
|
|
xy = await getXY(page, selector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 13:16:58 +02:00
|
|
|
export {
|
2024-06-19 17:20:42 +02:00
|
|
|
applyFunctionToEditor,
|
2023-12-06 15:27:31 +01:00
|
|
|
awaitPromise,
|
2025-02-23 18:52:19 +01:00
|
|
|
clearEditors,
|
2023-10-12 13:16:58 +02:00
|
|
|
clearInput,
|
|
|
|
closePages,
|
2024-06-23 12:37:43 +02:00
|
|
|
closeSinglePage,
|
2024-06-21 20:37:01 +02:00
|
|
|
copy,
|
|
|
|
copyToClipboard,
|
2023-12-06 15:27:31 +01:00
|
|
|
createPromise,
|
2024-12-09 16:07:39 +01:00
|
|
|
dragAndDrop,
|
2024-03-11 17:03:44 +01:00
|
|
|
firstPageOnTop,
|
2024-10-02 14:36:35 +02:00
|
|
|
getAnnotationSelector,
|
2023-11-13 11:05:03 +01:00
|
|
|
getAnnotationStorage,
|
2023-10-12 13:16:58 +02:00
|
|
|
getComputedStyleSelector,
|
|
|
|
getEditorDimensions,
|
|
|
|
getEditors,
|
|
|
|
getEditorSelector,
|
|
|
|
getFirstSerialized,
|
|
|
|
getQuerySelector,
|
2024-05-23 17:53:58 +02:00
|
|
|
getRect,
|
2023-10-12 13:16:58 +02:00
|
|
|
getSelector,
|
|
|
|
getSerialized,
|
Fix flickering on text selection
When seleciting on a touch screen device, whenever the finger moves to a
blank area (so over `div.textLayer` directly rather than on a `<span>`),
the selection jumps to include all the text between the beginning of the
.textLayer and the selection side that is not being moved.
The existing selection flickering fix when using the mouse cannot be
trivially re-used on mobile, because when modifying a selection on
a touchscreen device Firefox will not emit any pointer event (and
Chrome will emit them inconsistently). Instead, we have to listen to the
'selectionchange' event.
The fix is different in Firefox and Chrome:
- on Firefox, we have to make sure that, when modifying the selection,
hovering on blank areas will hover on the .endOfContent element
rather than on the .textLayer element. This is done by adjusting the
z-indexes so that .endOfContent is above .textLayer.
- on Chrome, hovering on blank areas needs to trigger hovering on an
element that is either immediately after (or immediately before,
depending on which side of the selection the user is moving) the
currently selected text. This is done by moving the .endOfContent
element around between the correct `<span>`s in the text layer.
The new anti-flickering code is also used when selecting using a mouse:
the improvement in Firefox is only observable on multi-page selection,
while in Chrome it also affects selection within a single page.
After this commit, the `z-index`es inside .textLayer are as follows:
- .endOfContent has `z-index: 0`
- everything else has `z-index: 1`
- except for .markedContent, which have `z-index: 0`
and their contents have `z-index: 1`.
`.textLayer` has an explicit `z-index: 0` to introduce a new stacking context,
so that its contents are not drawn on top of `.annotationLayer`.
2024-04-11 12:50:57 +02:00
|
|
|
getSpanRectFromText,
|
2025-02-23 18:39:42 +01:00
|
|
|
getXY,
|
2023-10-26 22:42:41 +02:00
|
|
|
hover,
|
2025-03-06 14:59:04 +01:00
|
|
|
isCanvasMonochrome,
|
2023-10-26 22:42:41 +02:00
|
|
|
kbBigMoveDown,
|
|
|
|
kbBigMoveLeft,
|
|
|
|
kbBigMoveRight,
|
|
|
|
kbBigMoveUp,
|
|
|
|
kbDeleteLastWord,
|
2024-03-11 17:03:44 +01:00
|
|
|
kbFocusNext,
|
|
|
|
kbFocusPrevious,
|
2023-10-26 22:42:41 +02:00
|
|
|
kbGoToBegin,
|
|
|
|
kbGoToEnd,
|
|
|
|
kbModifierDown,
|
|
|
|
kbModifierUp,
|
|
|
|
kbRedo,
|
2024-10-19 02:19:46 +05:30
|
|
|
kbSave,
|
2023-10-26 22:42:41 +02:00
|
|
|
kbSelectAll,
|
|
|
|
kbUndo,
|
2023-10-12 13:16:58 +02:00
|
|
|
loadAndWait,
|
|
|
|
mockClipboard,
|
2025-02-22 15:19:10 +01:00
|
|
|
moveEditor,
|
2024-06-21 20:37:01 +02:00
|
|
|
paste,
|
2024-03-26 21:17:36 +01:00
|
|
|
pasteFromClipboard,
|
2023-10-12 13:16:58 +02:00
|
|
|
scrollIntoView,
|
2025-01-18 19:21:55 +01:00
|
|
|
selectEditor,
|
2025-02-23 18:49:14 +01:00
|
|
|
selectEditors,
|
2023-10-12 13:16:58 +02:00
|
|
|
serializeBitmapDimensions,
|
2024-07-11 18:00:36 +02:00
|
|
|
setCaretAt,
|
2024-05-24 19:11:37 +02:00
|
|
|
switchToEditor,
|
2025-01-18 20:00:23 +01:00
|
|
|
unselectEditor,
|
2024-07-11 22:31:55 +02:00
|
|
|
waitAndClick,
|
2023-10-12 13:16:58 +02:00
|
|
|
waitForAnnotationEditorLayer,
|
2024-05-21 14:41:07 +02:00
|
|
|
waitForAnnotationModeChanged,
|
2023-11-13 11:05:03 +01:00
|
|
|
waitForEntryInStorage,
|
2023-10-12 13:16:58 +02:00
|
|
|
waitForEvent,
|
2024-11-29 16:22:44 +01:00
|
|
|
waitForNoElement,
|
2024-05-21 14:41:07 +02:00
|
|
|
waitForPageRendered,
|
2025-02-18 15:24:01 +01:00
|
|
|
waitForPointerUp,
|
2024-04-03 22:24:17 +02:00
|
|
|
waitForSandboxTrip,
|
2023-10-12 13:16:58 +02:00
|
|
|
waitForSelectedEditor,
|
|
|
|
waitForSerialized,
|
|
|
|
waitForStorageEntries,
|
2024-02-10 18:21:34 +01:00
|
|
|
waitForTimeout,
|
2023-10-12 13:16:58 +02:00
|
|
|
waitForUnselectedEditor,
|
|
|
|
};
|