From 88280641ac6f42207bd56533e1f6224abb03d487 Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Thu, 24 Jun 2021 11:32:43 +0300 Subject: [PATCH] Avoid setTimeout call on every note-editor context menu popup Fixes #2111 --- chrome/content/zotero/xpcom/editorInstance.js | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/xpcom/editorInstance.js b/chrome/content/zotero/xpcom/editorInstance.js index 9b4b4a5826..b379ca950b 100644 --- a/chrome/content/zotero/xpcom/editorInstance.js +++ b/chrome/content/zotero/xpcom/editorInstance.js @@ -661,9 +661,7 @@ class EditorInstance { } case 'openContextMenu': { let { x, y, pos, itemGroups } = message; - // If `contenteditable` area wasn't focused before, the spell checker - // might not be fully initialized on right-click - setTimeout(() => this._openPopup(x, y, pos, itemGroups), 50); + this._openPopup(x, y, pos, itemGroups); return; } case 'return': { @@ -764,7 +762,7 @@ class EditorInstance { return attachment.key; } - _openPopup(x, y, pos, itemGroups) { + async _openPopup(x, y, pos, itemGroups) { let appendItems = (parentNode, itemGroups) => { for (let itemGroup of itemGroups) { for (let item of itemGroup) { @@ -811,6 +809,23 @@ class EditorInstance { // Spell checker let spellChecker = this._getSpellChecker(); + // If `contenteditable` area wasn't focused before, the spell checker + // might not be fully initialized on right-click. + // The wait time depends on system performance/load + let n = 0; + // Wait for 200ms + while (n++ < 20) { + try { + if (spellChecker.mInlineSpellChecker.spellChecker.GetCurrentDictionary()) { + break; + } + } + catch (e) { + break; + } + await Zotero.Promise.delay(10); + } + // Separator var separator = this._popup.ownerDocument.createElement('menuseparator'); this._popup.appendChild(separator);