1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

[Editor] Make the editor deserialization async

This commit is contained in:
Calixte Denizet 2024-10-03 16:22:02 +02:00
parent 4fb3adfc0f
commit 7bd3e5ccec
7 changed files with 49 additions and 26 deletions

View file

@ -244,7 +244,7 @@ class AnnotationEditorLayer {
* Enable pointer events on the main div in order to enable
* editor creation.
*/
enable() {
async enable() {
this.div.tabIndex = 0;
this.togglePointerEvents(true);
const annotationElementIds = new Set();
@ -271,7 +271,7 @@ class AnnotationEditorLayer {
if (annotationElementIds.has(editable.data.id)) {
continue;
}
const editor = this.deserialize(editable);
const editor = await this.deserialize(editable);
if (!editor) {
continue;
}
@ -657,11 +657,11 @@ class AnnotationEditorLayer {
* @param {Object} data
* @returns {AnnotationEditor | null}
*/
deserialize(data) {
async deserialize(data) {
return (
AnnotationEditorLayer.#editorTypes
(await AnnotationEditorLayer.#editorTypes
.get(data.annotationType ?? data.annotationEditorType)
?.deserialize(data, this, this.#uiManager) || null
?.deserialize(data, this, this.#uiManager)) || null
);
}

View file

@ -1371,9 +1371,9 @@ class AnnotationEditor {
* @param {Object} data
* @param {AnnotationEditorLayer} parent
* @param {AnnotationEditorUIManager} uiManager
* @returns {AnnotationEditor | null}
* @returns {Promise<AnnotationEditor | null>}
*/
static deserialize(data, parent, uiManager) {
static async deserialize(data, parent, uiManager) {
const editor = new this.prototype.constructor({
parent,
id: parent.getNextId(),

View file

@ -770,7 +770,7 @@ class FreeTextEditor extends AnnotationEditor {
}
/** @inheritdoc */
static deserialize(data, parent, uiManager) {
static async deserialize(data, parent, uiManager) {
let initialData = null;
if (data instanceof FreeTextAnnotationElement) {
const {
@ -807,7 +807,7 @@ class FreeTextEditor extends AnnotationEditor {
popupRef,
};
}
const editor = super.deserialize(data, parent, uiManager);
const editor = await super.deserialize(data, parent, uiManager);
editor.#fontSize = data.fontSize;
editor.#color = Util.makeHexColor(...data.color);
editor.#content = FreeTextEditor.#deserializeContent(data.value);

View file

@ -779,7 +779,7 @@ class HighlightEditor extends AnnotationEditor {
}
/** @inheritdoc */
static deserialize(data, parent, uiManager) {
static async deserialize(data, parent, uiManager) {
let initialData = null;
if (data instanceof HighlightAnnotationElement) {
const {
@ -832,7 +832,7 @@ class HighlightEditor extends AnnotationEditor {
}
const { color, quadPoints, inkLists, opacity } = data;
const editor = super.deserialize(data, parent, uiManager);
const editor = await super.deserialize(data, parent, uiManager);
editor.color = Util.makeHexColor(...color);
editor.#opacity = opacity || 1;

View file

@ -1149,11 +1149,11 @@ class InkEditor extends AnnotationEditor {
}
/** @inheritdoc */
static deserialize(data, parent, uiManager) {
static async deserialize(data, parent, uiManager) {
if (data instanceof InkAnnotationElement) {
return null;
}
const editor = super.deserialize(data, parent, uiManager);
const editor = await super.deserialize(data, parent, uiManager);
editor.thickness = data.thickness;
editor.color = Util.makeHexColor(...data.color);

View file

@ -768,11 +768,11 @@ class StampEditor extends AnnotationEditor {
}
/** @inheritdoc */
static deserialize(data, parent, uiManager) {
static async deserialize(data, parent, uiManager) {
if (data instanceof StampAnnotationElement) {
return null;
}
const editor = super.deserialize(data, parent, uiManager);
const editor = await super.deserialize(data, parent, uiManager);
const { rect, bitmapUrl, bitmapId, isSvg, accessibilityData } = data;
if (bitmapId && uiManager.imageManager.isValidId(bitmapId)) {
editor.#bitmapId = bitmapId;

View file

@ -621,6 +621,8 @@ class AnnotationEditorUIManager {
#viewer = null;
#updateModeCapability = null;
static TRANSLATE_SMALL = 1; // page units.
static TRANSLATE_BIG = 10; // page units.
@ -817,6 +819,9 @@ class AnnotationEditorUIManager {
}
destroy() {
this.#updateModeCapability?.resolve();
this.#updateModeCapability = null;
this.#abortController?.abort();
this.#abortController = null;
this._signal = null;
@ -1344,7 +1349,7 @@ class AnnotationEditorUIManager {
* Paste callback.
* @param {ClipboardEvent} event
*/
paste(event) {
async paste(event) {
event.preventDefault();
const { clipboardData } = event;
for (const item of clipboardData.items) {
@ -1378,7 +1383,7 @@ class AnnotationEditorUIManager {
try {
const newEditors = [];
for (const editor of data) {
const deserializedEditor = layer.deserialize(editor);
const deserializedEditor = await layer.deserialize(editor);
if (!deserializedEditor) {
return;
}
@ -1572,30 +1577,44 @@ class AnnotationEditorUIManager {
* @param {boolean} [isFromKeyboard] - true if the mode change is due to a
* keyboard action.
*/
updateMode(mode, editId = null, isFromKeyboard = false) {
async updateMode(mode, editId = null, isFromKeyboard = false) {
if (this.#mode === mode) {
return;
}
if (this.#updateModeCapability) {
await this.#updateModeCapability.promise;
if (!this.#updateModeCapability) {
// This ui manager has been destroyed.
return;
}
}
this.#updateModeCapability = Promise.withResolvers();
this.#mode = mode;
if (mode === AnnotationEditorType.NONE) {
this.setEditingState(false);
this.#disableAll();
this.#updateModeCapability.resolve();
return;
}
this.setEditingState(true);
this.#enableAll();
await this.#enableAll();
this.unselectAll();
for (const layer of this.#allLayers.values()) {
layer.updateMode(mode);
}
if (!editId && isFromKeyboard) {
this.addNewEditorFromKeyboard();
if (!editId) {
if (isFromKeyboard) {
this.addNewEditorFromKeyboard();
}
this.#updateModeCapability.resolve();
return;
}
if (!editId) {
return;
}
for (const editor of this.#allEditors.values()) {
if (editor.annotationElementId === editId) {
this.setSelected(editor);
@ -1603,6 +1622,8 @@ class AnnotationEditorUIManager {
break;
}
}
this.#updateModeCapability.resolve();
}
addNewEditorFromKeyboard() {
@ -1702,12 +1723,14 @@ class AnnotationEditorUIManager {
/**
* Enable all the layers.
*/
#enableAll() {
async #enableAll() {
if (!this.#isEnabled) {
this.#isEnabled = true;
const promises = [];
for (const layer of this.#allLayers.values()) {
layer.enable();
promises.push(layer.enable());
}
await Promise.all(promises);
for (const editor of this.#allEditors.values()) {
editor.enable();
}