Merge pull request #18843 from calixteman/editor_async_deser
[Editor] Make the editor deserialization async
This commit is contained in:
commit
85586c5859
@ -244,7 +244,7 @@ class AnnotationEditorLayer {
|
|||||||
* Enable pointer events on the main div in order to enable
|
* Enable pointer events on the main div in order to enable
|
||||||
* editor creation.
|
* editor creation.
|
||||||
*/
|
*/
|
||||||
enable() {
|
async enable() {
|
||||||
this.div.tabIndex = 0;
|
this.div.tabIndex = 0;
|
||||||
this.togglePointerEvents(true);
|
this.togglePointerEvents(true);
|
||||||
const annotationElementIds = new Set();
|
const annotationElementIds = new Set();
|
||||||
@ -271,7 +271,7 @@ class AnnotationEditorLayer {
|
|||||||
if (annotationElementIds.has(editable.data.id)) {
|
if (annotationElementIds.has(editable.data.id)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const editor = this.deserialize(editable);
|
const editor = await this.deserialize(editable);
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -657,11 +657,11 @@ class AnnotationEditorLayer {
|
|||||||
* @param {Object} data
|
* @param {Object} data
|
||||||
* @returns {AnnotationEditor | null}
|
* @returns {AnnotationEditor | null}
|
||||||
*/
|
*/
|
||||||
deserialize(data) {
|
async deserialize(data) {
|
||||||
return (
|
return (
|
||||||
AnnotationEditorLayer.#editorTypes
|
(await AnnotationEditorLayer.#editorTypes
|
||||||
.get(data.annotationType ?? data.annotationEditorType)
|
.get(data.annotationType ?? data.annotationEditorType)
|
||||||
?.deserialize(data, this, this.#uiManager) || null
|
?.deserialize(data, this, this.#uiManager)) || null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1371,9 +1371,9 @@ class AnnotationEditor {
|
|||||||
* @param {Object} data
|
* @param {Object} data
|
||||||
* @param {AnnotationEditorLayer} parent
|
* @param {AnnotationEditorLayer} parent
|
||||||
* @param {AnnotationEditorUIManager} uiManager
|
* @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({
|
const editor = new this.prototype.constructor({
|
||||||
parent,
|
parent,
|
||||||
id: parent.getNextId(),
|
id: parent.getNextId(),
|
||||||
|
|||||||
@ -770,7 +770,7 @@ class FreeTextEditor extends AnnotationEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritdoc */
|
/** @inheritdoc */
|
||||||
static deserialize(data, parent, uiManager) {
|
static async deserialize(data, parent, uiManager) {
|
||||||
let initialData = null;
|
let initialData = null;
|
||||||
if (data instanceof FreeTextAnnotationElement) {
|
if (data instanceof FreeTextAnnotationElement) {
|
||||||
const {
|
const {
|
||||||
@ -807,7 +807,7 @@ class FreeTextEditor extends AnnotationEditor {
|
|||||||
popupRef,
|
popupRef,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const editor = super.deserialize(data, parent, uiManager);
|
const editor = await super.deserialize(data, parent, uiManager);
|
||||||
editor.#fontSize = data.fontSize;
|
editor.#fontSize = data.fontSize;
|
||||||
editor.#color = Util.makeHexColor(...data.color);
|
editor.#color = Util.makeHexColor(...data.color);
|
||||||
editor.#content = FreeTextEditor.#deserializeContent(data.value);
|
editor.#content = FreeTextEditor.#deserializeContent(data.value);
|
||||||
|
|||||||
@ -779,7 +779,7 @@ class HighlightEditor extends AnnotationEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritdoc */
|
/** @inheritdoc */
|
||||||
static deserialize(data, parent, uiManager) {
|
static async deserialize(data, parent, uiManager) {
|
||||||
let initialData = null;
|
let initialData = null;
|
||||||
if (data instanceof HighlightAnnotationElement) {
|
if (data instanceof HighlightAnnotationElement) {
|
||||||
const {
|
const {
|
||||||
@ -832,7 +832,7 @@ class HighlightEditor extends AnnotationEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { color, quadPoints, inkLists, opacity } = data;
|
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.color = Util.makeHexColor(...color);
|
||||||
editor.#opacity = opacity || 1;
|
editor.#opacity = opacity || 1;
|
||||||
|
|||||||
@ -1149,11 +1149,11 @@ class InkEditor extends AnnotationEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritdoc */
|
/** @inheritdoc */
|
||||||
static deserialize(data, parent, uiManager) {
|
static async deserialize(data, parent, uiManager) {
|
||||||
if (data instanceof InkAnnotationElement) {
|
if (data instanceof InkAnnotationElement) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const editor = super.deserialize(data, parent, uiManager);
|
const editor = await super.deserialize(data, parent, uiManager);
|
||||||
|
|
||||||
editor.thickness = data.thickness;
|
editor.thickness = data.thickness;
|
||||||
editor.color = Util.makeHexColor(...data.color);
|
editor.color = Util.makeHexColor(...data.color);
|
||||||
|
|||||||
@ -768,11 +768,11 @@ class StampEditor extends AnnotationEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritdoc */
|
/** @inheritdoc */
|
||||||
static deserialize(data, parent, uiManager) {
|
static async deserialize(data, parent, uiManager) {
|
||||||
if (data instanceof StampAnnotationElement) {
|
if (data instanceof StampAnnotationElement) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const editor = super.deserialize(data, parent, uiManager);
|
const editor = await super.deserialize(data, parent, uiManager);
|
||||||
const { rect, bitmapUrl, bitmapId, isSvg, accessibilityData } = data;
|
const { rect, bitmapUrl, bitmapId, isSvg, accessibilityData } = data;
|
||||||
if (bitmapId && uiManager.imageManager.isValidId(bitmapId)) {
|
if (bitmapId && uiManager.imageManager.isValidId(bitmapId)) {
|
||||||
editor.#bitmapId = bitmapId;
|
editor.#bitmapId = bitmapId;
|
||||||
|
|||||||
@ -621,6 +621,8 @@ class AnnotationEditorUIManager {
|
|||||||
|
|
||||||
#viewer = null;
|
#viewer = null;
|
||||||
|
|
||||||
|
#updateModeCapability = null;
|
||||||
|
|
||||||
static TRANSLATE_SMALL = 1; // page units.
|
static TRANSLATE_SMALL = 1; // page units.
|
||||||
|
|
||||||
static TRANSLATE_BIG = 10; // page units.
|
static TRANSLATE_BIG = 10; // page units.
|
||||||
@ -817,6 +819,9 @@ class AnnotationEditorUIManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
this.#updateModeCapability?.resolve();
|
||||||
|
this.#updateModeCapability = null;
|
||||||
|
|
||||||
this.#abortController?.abort();
|
this.#abortController?.abort();
|
||||||
this.#abortController = null;
|
this.#abortController = null;
|
||||||
this._signal = null;
|
this._signal = null;
|
||||||
@ -1344,7 +1349,7 @@ class AnnotationEditorUIManager {
|
|||||||
* Paste callback.
|
* Paste callback.
|
||||||
* @param {ClipboardEvent} event
|
* @param {ClipboardEvent} event
|
||||||
*/
|
*/
|
||||||
paste(event) {
|
async paste(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const { clipboardData } = event;
|
const { clipboardData } = event;
|
||||||
for (const item of clipboardData.items) {
|
for (const item of clipboardData.items) {
|
||||||
@ -1378,7 +1383,7 @@ class AnnotationEditorUIManager {
|
|||||||
try {
|
try {
|
||||||
const newEditors = [];
|
const newEditors = [];
|
||||||
for (const editor of data) {
|
for (const editor of data) {
|
||||||
const deserializedEditor = layer.deserialize(editor);
|
const deserializedEditor = await layer.deserialize(editor);
|
||||||
if (!deserializedEditor) {
|
if (!deserializedEditor) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1572,30 +1577,44 @@ class AnnotationEditorUIManager {
|
|||||||
* @param {boolean} [isFromKeyboard] - true if the mode change is due to a
|
* @param {boolean} [isFromKeyboard] - true if the mode change is due to a
|
||||||
* keyboard action.
|
* keyboard action.
|
||||||
*/
|
*/
|
||||||
updateMode(mode, editId = null, isFromKeyboard = false) {
|
async updateMode(mode, editId = null, isFromKeyboard = false) {
|
||||||
if (this.#mode === mode) {
|
if (this.#mode === mode) {
|
||||||
return;
|
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;
|
this.#mode = mode;
|
||||||
if (mode === AnnotationEditorType.NONE) {
|
if (mode === AnnotationEditorType.NONE) {
|
||||||
this.setEditingState(false);
|
this.setEditingState(false);
|
||||||
this.#disableAll();
|
this.#disableAll();
|
||||||
|
|
||||||
|
this.#updateModeCapability.resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.setEditingState(true);
|
this.setEditingState(true);
|
||||||
this.#enableAll();
|
await this.#enableAll();
|
||||||
this.unselectAll();
|
this.unselectAll();
|
||||||
for (const layer of this.#allLayers.values()) {
|
for (const layer of this.#allLayers.values()) {
|
||||||
layer.updateMode(mode);
|
layer.updateMode(mode);
|
||||||
}
|
}
|
||||||
if (!editId && isFromKeyboard) {
|
if (!editId) {
|
||||||
this.addNewEditorFromKeyboard();
|
if (isFromKeyboard) {
|
||||||
|
this.addNewEditorFromKeyboard();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#updateModeCapability.resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!editId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (const editor of this.#allEditors.values()) {
|
for (const editor of this.#allEditors.values()) {
|
||||||
if (editor.annotationElementId === editId) {
|
if (editor.annotationElementId === editId) {
|
||||||
this.setSelected(editor);
|
this.setSelected(editor);
|
||||||
@ -1603,6 +1622,8 @@ class AnnotationEditorUIManager {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.#updateModeCapability.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
addNewEditorFromKeyboard() {
|
addNewEditorFromKeyboard() {
|
||||||
@ -1702,12 +1723,14 @@ class AnnotationEditorUIManager {
|
|||||||
/**
|
/**
|
||||||
* Enable all the layers.
|
* Enable all the layers.
|
||||||
*/
|
*/
|
||||||
#enableAll() {
|
async #enableAll() {
|
||||||
if (!this.#isEnabled) {
|
if (!this.#isEnabled) {
|
||||||
this.#isEnabled = true;
|
this.#isEnabled = true;
|
||||||
|
const promises = [];
|
||||||
for (const layer of this.#allLayers.values()) {
|
for (const layer of this.#allLayers.values()) {
|
||||||
layer.enable();
|
promises.push(layer.enable());
|
||||||
}
|
}
|
||||||
|
await Promise.all(promises);
|
||||||
for (const editor of this.#allEditors.values()) {
|
for (const editor of this.#allEditors.values()) {
|
||||||
editor.enable();
|
editor.enable();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user