Skip to content

Commit

Permalink
[Editor] Make the editor deserialization async
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Oct 3, 2024
1 parent 4fb3adf commit 385bfb2
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 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
14 changes: 8 additions & 6 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,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 +1378,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,7 +1572,7 @@ 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;
}
Expand All @@ -1583,7 +1583,7 @@ class AnnotationEditorUIManager {
return;
}
this.setEditingState(true);
this.#enableAll();
await this.#enableAll();
this.unselectAll();
for (const layer of this.#allLayers.values()) {
layer.updateMode(mode);
Expand Down Expand Up @@ -1702,12 +1702,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 385bfb2

Please sign in to comment.