Skip to content

Commit

Permalink
Merge pull request #18843 from calixteman/editor_async_deser
Browse files Browse the repository at this point in the history
[Editor] Make the editor deserialization async
  • Loading branch information
calixteman authored Oct 3, 2024
2 parents 4fb3adf + 7bd3e5c commit 85586c5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/display/editor/annotation_editor_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/freetext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/ink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/stamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 34 additions & 11 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ class AnnotationEditorUIManager {

#viewer = null;

#updateModeCapability = null;

static TRANSLATE_SMALL = 1; // page units.

static TRANSLATE_BIG = 10; // page units.
Expand Down Expand Up @@ -817,6 +819,9 @@ class AnnotationEditorUIManager {
}

destroy() {
this.#updateModeCapability?.resolve();
this.#updateModeCapability = null;

this.#abortController?.abort();
this.#abortController = null;
this._signal = null;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -1572,37 +1577,53 @@ 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();
return;
}

if (!editId) {
if (isFromKeyboard) {
this.addNewEditorFromKeyboard();
}

this.#updateModeCapability.resolve();
return;
}

for (const editor of this.#allEditors.values()) {
if (editor.annotationElementId === editId) {
this.setSelected(editor);
editor.enterInEditMode();
break;
}
}

this.#updateModeCapability.resolve();
}

addNewEditorFromKeyboard() {
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 85586c5

Please sign in to comment.