From 03d2350613ad7d236c3406ad19439d8ffae7c5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= Date: Mon, 21 Oct 2019 08:33:09 +0200 Subject: [PATCH] monaco_editor: allow inclusion of external js By using a magic comment such as // erp5_monaco_editor_include: ../other.js other we can tell monaco editor that other.js will be loaded before the current javascript file, which helps the typescript compiler in diagnosing errors and providing completions. rsvp, renderjs and jio are still always loaded because they are used in almost all our javscripts. --- .../monaco_editor_support.zpt | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/bt5/erp5_monaco_editor/SkinTemplateItem/portal_skins/erp5_monaco_editor/monaco_editor_support.zpt b/bt5/erp5_monaco_editor/SkinTemplateItem/portal_skins/erp5_monaco_editor/monaco_editor_support.zpt index 59b1f359487..314b42c3a4f 100644 --- a/bt5/erp5_monaco_editor/SkinTemplateItem/portal_skins/erp5_monaco_editor/monaco_editor_support.zpt +++ b/bt5/erp5_monaco_editor/SkinTemplateItem/portal_skins/erp5_monaco_editor/monaco_editor_support.zpt @@ -117,6 +117,35 @@ $script.onload = function() { monaco.languages.html.htmlDefaults.options.format.tabSize = 2; monaco.languages.html.htmlDefaults.options.format.insertSpaces = true; } + if (mode === "javascript") { + // load an external library in namespace of the edited .js + function addExtraLibrary(script_name, lib_name) { + return fetch(script_name) + .then(function(resp) { + return resp.text(); + }) + .then(function(script_code) { + monaco.languages.typescript.javascriptDefaults.addExtraLib( + script_code, + lib_name + ); + }); + } + let lines = $textarea.value.split('\n'); + // type mapping for nexedi libraries, plus external references from current document. + let extra_libraries = [ + addExtraLibrary('../monaco-rsvp.d.ts', 'rsvp'), + addExtraLibrary('../monaco-renderjs.d.ts', 'renderjs'), + addExtraLibrary('../monaco-jio.d.ts', 'jio')] + + for(let i = 0; i < lines.length; i++){ + let match = lines[i].match(/erp5_monaco_editor_include\:\s*([^\s]+)\s+([a-zA-Z0-9]+)/); + if (match){ + addExtraLibrary(match[1], match[2]); + } + } + Promise.all(extra_libraries); + } monaco.languages.registerDocumentFormattingEditProvider('javascript', { provideDocumentFormattingEdits(model, options, token) { const text = prettier.format(model.getValue(), { @@ -149,25 +178,6 @@ $script.onload = function() { module: monaco.languages.typescript.ModuleKind.UMD }); - // Type mapping for Nexedi libraries - function addExtraLibrary(script_name, lib_name) { - return fetch(script_name) - .then(function(resp) { - return resp.text(); - }) - .then(function(script_code) { - monaco.languages.typescript.javascriptDefaults.addExtraLib( - script_code, - lib_name - ); - }); - } - Promise.all([ - addExtraLibrary('../monaco-rsvp.d.ts', 'rsvp'), - addExtraLibrary('../monaco-renderjs.d.ts', 'renderjs'), - addExtraLibrary('../monaco-jio.d.ts', 'jio')]); - - var timeout = null; // minimal pollyfil for AbortController -- 2.30.9