From 9c837b3361985c66c067c7655250ee96a64f571f Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Tue, 28 Jun 2022 15:05:11 -0400 Subject: [PATCH] Improve Monaco integration and use in csledit And remove timedtextarea.xml. Fixes #2665. --- chrome/content/scaffold/monaco/monaco.html | 72 +++-- chrome/content/scaffold/scaffold.js | 28 +- .../content/zotero/bindings/timedtextarea.xml | 293 ------------------ chrome/content/zotero/tools/csledit.js | 45 ++- chrome/content/zotero/tools/csledit.xhtml | 9 +- chrome/skin/default/zotero/zotero.css | 5 - 6 files changed, 83 insertions(+), 369 deletions(-) delete mode 100644 chrome/content/zotero/bindings/timedtextarea.xml diff --git a/chrome/content/scaffold/monaco/monaco.html b/chrome/content/scaffold/monaco/monaco.html index 22ccab4a1f..c329415144 100644 --- a/chrome/content/scaffold/monaco/monaco.html +++ b/chrome/content/scaffold/monaco/monaco.html @@ -21,43 +21,53 @@ diff --git a/chrome/content/scaffold/scaffold.js b/chrome/content/scaffold/scaffold.js index 6d495dac0e..6d1949696f 100644 --- a/chrome/content/scaffold/scaffold.js +++ b/chrome/content/scaffold/scaffold.js @@ -142,13 +142,21 @@ var Scaffold = new function () { var importWin = document.getElementById("editor-import").contentWindow; var codeWin = document.getElementById("editor-code").contentWindow; var testsWin = document.getElementById("editor-tests").contentWindow; - - _editors.import = importWin.editor; - _editors.importGlobal = importWin.globalEditor; - _editors.code = codeWin.editor; - _editors.codeGlobal = codeWin.globalEditor; - _editors.tests = testsWin.editor; - _editors.testsGlobal = testsWin.globalEditor; + + await Promise.all([ + importWin.loadMonaco({ language: 'plaintext' }).then(({ monaco, editor }) => { + _editors.importGlobal = monaco; + _editors.import = editor; + }), + codeWin.loadMonaco({ language: 'javascript' }).then(({ monaco, editor }) => { + _editors.codeGlobal = monaco; + _editors.code = editor; + }), + testsWin.loadMonaco({ language: 'json' }).then(({ monaco, editor }) => { + _editors.testsGlobal = monaco; + _editors.tests = editor; + }), + ]); this.initImportEditor(); this.initCodeEditor(); @@ -257,7 +265,7 @@ var Scaffold = new function () { this.initImportEditor = function () { let monaco = _editors.importGlobal, editor = _editors.import; - monaco.editor.setModelLanguage(editor.getModel(), 'plaintext'); + // Nothing to do here }; this.initCodeEditor = async function () { @@ -269,8 +277,6 @@ var Scaffold = new function () { editor.updateOptions({ lineNumbers: num => num + _linesOfMetadata - 1, - // clicking links doesn't actually work, so disable them (for now) - links: false }); monaco.languages.registerCodeLensProvider('javascript', this.createRunCodeLensProvider(monaco, editor)); @@ -295,8 +301,6 @@ var Scaffold = new function () { schemaValidation: 'error' }); - monaco.editor.setModelLanguage(editor.getModel(), 'json'); - editor.getModel().updateOptions({ insertSpaces: false }); diff --git a/chrome/content/zotero/bindings/timedtextarea.xml b/chrome/content/zotero/bindings/timedtextarea.xml deleted file mode 100644 index 35cd8759e4..0000000000 --- a/chrome/content/zotero/bindings/timedtextarea.xml +++ /dev/null @@ -1,293 +0,0 @@ - - - - - %textcontextDTD; - - - - - - - - - - - -]> - - - - - null - - - - - null - - - - return this.inputField.value; - - - - - - - - - - - - - - - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/chrome/content/zotero/tools/csledit.js b/chrome/content/zotero/tools/csledit.js index abb618b2ea..42f7250505 100644 --- a/chrome/content/zotero/tools/csledit.js +++ b/chrome/content/zotero/tools/csledit.js @@ -26,9 +26,11 @@ import FilePicker from 'zotero/modules/filePicker'; var Zotero_CSL_Editor = new function() { + let monaco, editor; + this.init = init; - this.handleKeyPress = handleKeyPress; this.loadCSL = loadCSL; + async function init() { await Zotero.Schema.schemaUpdatePromise; @@ -52,11 +54,6 @@ var Zotero_CSL_Editor = new function() { } } - if (currentStyle) { - // Call asynchronously, see note in Zotero.Styles - window.setTimeout(this.onStyleSelected.bind(this, currentStyle.styleID), 1); - } - var pageList = document.getElementById('zotero-csl-page-type'); var locators = Zotero.Cite.labels; for (let type of locators) { @@ -66,6 +63,20 @@ var Zotero_CSL_Editor = new function() { } pageList.selectedIndex = 0; + + let editorWin = document.getElementById("zotero-csl-editor-iframe").contentWindow; + let { monaco: _monaco, editor: _editor } = await editorWin.loadMonaco({ language: 'xml' }); + monaco = _monaco; + editor = _editor; + + editor.getModel().onDidChangeContent(Zotero.Utilities.debounce(() => { + this.onStyleModified(); + }, 250)); + + if (currentStyle) { + // Call asynchronously, see note in Zotero.Styles + window.setTimeout(this.onStyleSelected.bind(this, currentStyle.styleID), 1); + } } this.onStyleSelected = function(styleID) { @@ -84,10 +95,11 @@ var Zotero_CSL_Editor = new function() { this.refresh = function() { this.generateBibliography(this.loadStyleFromEditor()); } + + this.refreshDebounced = Zotero.Utilities.debounce(this.refresh, 250); this.save = async function () { - var editor = document.getElementById('zotero-csl-editor'); - var style = editor.value; + var style = editor.getValue(); var fp = new FilePicker(); fp.init(window, Zotero.getString('styles.editor.save'), fp.modeSave); fp.appendFilter("Citation Style Language", "*.csl"); @@ -109,21 +121,9 @@ var Zotero_CSL_Editor = new function() { } }; - function handleKeyPress(event) { - if (event.keyCode == 9 && - (!event.shiftKey && !event.metaKey && !event.altKey && !event.ctrlKey)) { - _insertText("\t"); - event.preventDefault(); - } - } - - function loadCSL(cslID) { - var editor = document.getElementById('zotero-csl-editor'); var style = Zotero.Styles.get(cslID); - editor.value = style.getXML(); - editor.cslID = cslID; - editor.doCommand(); + editor.setValue(style.getXML()); document.getElementById('zotero-csl-list').value = cslID; } @@ -131,7 +131,7 @@ var Zotero_CSL_Editor = new function() { var styleObject; try { styleObject = new Zotero.Style( - document.getElementById('zotero-csl-editor').value + editor.getValue() ); } catch(e) { document.getElementById('zotero-csl-preview-box') @@ -159,7 +159,6 @@ var Zotero_CSL_Editor = new function() { this.generateBibliography = function(style) { var iframe = document.getElementById('zotero-csl-preview-box'); - var editor = document.getElementById('zotero-csl-editor'); var items = Zotero.getActiveZoteroPane().getSelectedItems(); if (items.length == 0) { diff --git a/chrome/content/zotero/tools/csledit.xhtml b/chrome/content/zotero/tools/csledit.xhtml index 26019a0b19..ae83a45870 100644 --- a/chrome/content/zotero/tools/csledit.xhtml +++ b/chrome/content/zotero/tools/csledit.xhtml @@ -34,6 +34,7 @@ @@ -47,7 +48,7 @@