Commit 0bfc33fd authored by Jérome Perrin's avatar Jérome Perrin

monaco_editor: allow inclusion of external js

By using a magic comment such as

```javascript
   // 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.

/reviewed-on !969
parent d692e641
...@@ -117,6 +117,35 @@ $script.onload = function() { ...@@ -117,6 +117,35 @@ $script.onload = function() {
monaco.languages.html.htmlDefaults.options.format.tabSize = 2; monaco.languages.html.htmlDefaults.options.format.tabSize = 2;
monaco.languages.html.htmlDefaults.options.format.insertSpaces = true; 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', { monaco.languages.registerDocumentFormattingEditProvider('javascript', {
provideDocumentFormattingEdits(model, options, token) { provideDocumentFormattingEdits(model, options, token) {
const text = prettier.format(model.getValue(), { const text = prettier.format(model.getValue(), {
...@@ -149,25 +178,6 @@ $script.onload = function() { ...@@ -149,25 +178,6 @@ $script.onload = function() {
module: monaco.languages.typescript.ModuleKind.UMD 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; var timeout = null;
// minimal pollyfil for AbortController // minimal pollyfil for AbortController
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment