diff --git a/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-hint.js b/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-hint.js index 48b8f48c..ff925a51 100644 --- a/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-hint.js +++ b/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-hint.js @@ -33,7 +33,11 @@ /// /// -/** @param {localStorageCustomAssistantItem} assistantData */ +/** + * @param {localStorageCustomAssistantItem} assistantData + * @constructor + * @extends CustomAnnotator + */ function AssistantHint(annotationPopup, assistantData) { CustomAnnotator.call(this, annotationPopup, assistantData); diff --git a/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-replace-hint.js b/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-replace-hint.js index 2bb62250..378b483f 100644 --- a/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-replace-hint.js +++ b/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-replace-hint.js @@ -33,7 +33,11 @@ /// /// -/** @param {localStorageCustomAssistantItem} assistantData */ +/** + * @param {localStorageCustomAssistantItem} assistantData + * @constructor + * @extends CustomAnnotator + */ function AssistantReplaceHint(annotationPopup, assistantData) { CustomAnnotator.call(this, annotationPopup, assistantData); diff --git a/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-replace.js b/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-replace.js index 7718d8ac..630523be 100644 --- a/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-replace.js +++ b/sdkjs-plugins/content/ai/scripts/custom-annotations/assistant-replace.js @@ -33,7 +33,11 @@ /// /// -/** @param {localStorageCustomAssistantItem} assistantData */ +/** + * @param {localStorageCustomAssistantItem} assistantData + * @constructor + * @extends CustomAnnotator + */ function AssistantReplace(annotationPopup, assistantData) { CustomAnnotator.call(this, annotationPopup, assistantData); diff --git a/sdkjs-plugins/content/ai/scripts/custom-annotations/custom-annotator.js b/sdkjs-plugins/content/ai/scripts/custom-annotations/custom-annotator.js index 8bbf0d97..23146d26 100644 --- a/sdkjs-plugins/content/ai/scripts/custom-annotations/custom-annotator.js +++ b/sdkjs-plugins/content/ai/scripts/custom-annotations/custom-annotator.js @@ -31,7 +31,12 @@ */ /// +/// +/** + * @constructor + * @extends TextAnnotator + */ function CustomAnnotator(annotationPopup, assistantData) { TextAnnotator.call(this, annotationPopup); @@ -72,4 +77,4 @@ CustomAnnotator.prototype._handleNewRangePositions = async function(range, paraI let annotRange = this.getAnnotationRangeObj(paraId, rangeId); Asc.Editor.callMethod("RemoveAnnotationRange", [annotRange]); } -}; \ No newline at end of file +}; diff --git a/sdkjs-plugins/content/ai/scripts/custom-annotations/manager.js b/sdkjs-plugins/content/ai/scripts/custom-annotations/manager.js index 15cd9711..8b131abe 100644 --- a/sdkjs-plugins/content/ai/scripts/custom-annotations/manager.js +++ b/sdkjs-plugins/content/ai/scripts/custom-annotations/manager.js @@ -42,7 +42,7 @@ class CustomAssistantManager { constructor() { /** - * @type {Map} + * @type {Map} */ this._customAssistants = new Map(); this._isCustomAssistantInit = new Map(); @@ -53,15 +53,13 @@ class CustomAssistantManager { /** * @param {localStorageCustomAssistantItem} assistantData - * @returns */ createAssistant(assistantData) { - let assistant = this._customAssistants.get(assistantData.id); - if (assistant) { - assistant.assistantData = assistantData; - assistant.type = assistantData.type; - return assistant; + if (this._customAssistants.has(assistantData.id)) { + return this._updateAssistant(assistantData); } + /** @type {Assistant | null} */ + let assistant = null; switch (assistantData.type) { case 0: assistant = new AssistantHint(customAnnotationPopup, assistantData); @@ -72,10 +70,9 @@ class CustomAssistantManager { case 2: assistant = new AssistantReplace(customAnnotationPopup, assistantData); break; - default: - throw new Error( - `Unknown assistant type: ${assistantData.type}` - ); + } + if (!assistant) { + throw new Error("Unknown custom assistant type: " + assistantData.type); } this._isCustomAssistantInit.set(assistantData.id, false); @@ -84,6 +81,34 @@ class CustomAssistantManager { return assistant; } + /** + * @param {localStorageCustomAssistantItem} assistantData + */ + _updateAssistant(assistantData) { + let assistant = this._customAssistants.get(assistantData.id); + if (!assistant) { + throw new Error("Custom assistant not found: " + assistantData.id); + } + assistant.assistantData = assistantData; + assistant.type = assistantData.type; + + const isRunning = this._isCustomAssistantRunning.get(assistantData.id); + + this._paragraphsStack.forEach((value, paraId) => { + assistant.onChangeParagraph( + paraId, + value.recalcId, + value.text, + value.annotations + ); + }); + const paragraphIdsToUpdate = [...assistant.checked]; + if (isRunning) { + assistant.checkParagraphs(paragraphIdsToUpdate); + } + + return assistant; + } /** @param {string} assistantId */ deleteAssistant(assistantId) { diff --git a/sdkjs-plugins/content/ai/scripts/custom-annotations/types.js b/sdkjs-plugins/content/ai/scripts/custom-annotations/types.js index e84bb7a2..389e672a 100644 --- a/sdkjs-plugins/content/ai/scripts/custom-annotations/types.js +++ b/sdkjs-plugins/content/ai/scripts/custom-annotations/types.js @@ -62,3 +62,6 @@ * @typedef {ReplaceHintInfoForPopup | HintInfoForPopup | ReplaceInfoForPopup} InfoForPopup */ +/** + * @typedef {CustomAnnotator & TextAnnotator & AssistantHint & AssistantReplaceHint & AssistantReplace} Assistant + */ \ No newline at end of file diff --git a/sdkjs-plugins/content/ai/scripts/text-annotations/text-annotator.js b/sdkjs-plugins/content/ai/scripts/text-annotations/text-annotator.js index 68a1b835..a5edcc98 100644 --- a/sdkjs-plugins/content/ai/scripts/text-annotations/text-annotator.js +++ b/sdkjs-plugins/content/ai/scripts/text-annotations/text-annotator.js @@ -40,6 +40,7 @@ function TextAnnotator(annotatorPopup) /** @type {Object.} */ this.waitParagraphs = {}; this.paraToCheck = new Set(); + /** @type {Set} */ this.checked = new Set(); // was checked on the previous request this.type = -1;