Commit 052744ed authored by Jérome Perrin's avatar Jérome Perrin

monaco: various changes

* format code
* implement python annotations
parent a2dffbb8
<div id="monaco-container" style="width:100%;height:800px;border:1px solid grey;"></div> <div id="monaco-container" style="width:100%;height:800px;border:1px solid grey;"></div>
<script tal:content='python: "window.monacoEditorWebPackResourceBaseUrl = " + modules["json"].dumps(options["portal_url"]) + " + \"/monaco-editor/\""'></script>
<textarea tal:attributes="id options/field_id; <textarea tal:attributes="id options/field_id;
name options/field_id" style="display:none" tal:content="options/content" tal:condition="options/field_id | nothing"> name options/field_id"
style="display:none"
tal:content="options/content"
tal:condition="options/field_id | nothing">
</textarea> </textarea>
<script tal:content='python: "var portal_url=" + modules["json"].dumps(options.get("portal_url"))'></script>
<script tal:content='python: "var field_id=" + modules["json"].dumps(options.get("field_id"))'></script> <script tal:content='python: "var field_id=" + modules["json"].dumps(options.get("field_id"))'></script>
<script tal:content='python: "var mode=" + modules["json"].dumps(options["mode"])'></script> <script tal:content='python: "var mode=" + modules["json"].dumps(options["mode"])'></script>
<script tal:content='python: "var textarea_selector=" + modules["json"].dumps(options.get("textarea_selector"))'></script> <script tal:content='python: "var textarea_selector=" + modules["json"].dumps(options.get("textarea_selector"))'></script>
<script tal:content='python: "var bound_names=" + modules["json"].dumps(options.get("bound_names"))'></script>
<script tal:content='python: "window.monacoEditorWebPackResourceBaseUrl = " + modules["json"].dumps(options["portal_url"]) + " + \"/monaco-editor/\""'></script>
<script> <script>
/* we need to defer import for the monacoEditorWebPackResourceBaseUrl trick to work as expected in ZMI */ /* we need to defer import for the monacoEditorWebPackResourceBaseUrl trick to work as expected in ZMI */
var $script = document.createElement('script'); var $script = document.createElement("script");
$script.src = window.monacoEditorWebPackResourceBaseUrl + '/monaco-editor/app.bundle.js'; $script.src =
window.monacoEditorWebPackResourceBaseUrl + "/monaco-editor/app.bundle.js";
document.head.appendChild($script); document.head.appendChild($script);
$script.onload = function() { $script.onload = function() {
var $textarea =
var $textarea = document.querySelector(textarea_selector) || document.getElementById(field_id); document.querySelector(textarea_selector) ||
document.getElementById(field_id);
if (textarea_selector) { if (textarea_selector) {
/* ZMI mode */ /* ZMI mode */
var $monacoContainer = document.getElementById('monaco-container'); /* create a div instead of the default textarea */
var $monacoContainer = document.getElementById("monaco-container");
$monacoContainer.parentNode.removeChild($monacoContainer); $monacoContainer.parentNode.removeChild($monacoContainer);
$textarea.parentNode.appendChild($monacoContainer); $textarea.parentNode.appendChild($monacoContainer);
$monacoContainer.style.width = ($textarea.parentNode.offsetWidth - 10) + "px"; $monacoContainer.style.width = $textarea.parentNode.offsetWidth - 10 + "px";
$monacoContainer.style.height = $textarea.parentNode.offsetHeight + "px"; $monacoContainer.style.height = $textarea.parentNode.offsetHeight + "px";
$textarea.style.display = "none"; $textarea.style.display = "none";
...@@ -37,48 +48,99 @@ $script.onload = function() { ...@@ -37,48 +48,99 @@ $script.onload = function() {
$textarea.parentNode.title = ""; $textarea.parentNode.title = "";
function saveDocument() { function saveDocument() {
clickSaveButton('Base_edit'); clickSaveButton("Base_edit");
document.getElementById("main_form").submit() document.getElementById("main_form").submit();
} }
} }
var editor = monaco.editor.create(document.getElementById('monaco-container'), { var editor = monaco.editor.create(
value: $textarea.value, document.getElementById("monaco-container"),
language: mode, {
multiCursorModifier: 'ctrlCmd', /* because Alt+Click is LeftClick on ChromeOS */ value: $textarea.value,
language: mode,
multiCursorModifier:
"ctrlCmd" /* because Alt+Click is LeftClick on ChromeOS */,
/* XXX audoindent experiments */ formatOnType: true
autoIndent: true, }
formatOnPaste: true, );
formatOnType: true,
});
if (mode == "python") { if (mode == "python") {
editor.model.updateOptions({ tabSize: 2 }); editor.model.updateOptions({ tabSize: 2 });
monaco.languages.setLanguageConfiguration( monaco.languages.setLanguageConfiguration("python", {
"python", indentationRules: { increaseIndentPattern: /^.*:$/ }
{ indentationRules: { increaseIndentPattern: /^.*:$/, }}); });
// TODO unindent rule
} }
editor.model.onDidChangeContent((event) => { var timeout = null;
function checkPythonSourceCode() {
const data = new FormData();
const checker_parameters = {
code: editor.getValue()
};
// ZMI python scripts.
if (bound_names) {
checker_parameters["bound_names"] = JSON.parse(bound_names);
checker_parameters["params"] = document.querySelector(
'input[name="params"]'
).value;
}
data.append("data", JSON.stringify(checker_parameters));
fetch(portal_url + "/ERP5Site_checkPythonSourceCodeAsJSON", {
method: "POST",
body: data
})
.then(response => response.json())
.then(data => {
monaco.editor.setModelMarkers(
editor.model,
"pylint",
data["annotations"].map(annotation => {
return {
startLineNumber: annotation.row + 1,
endLineNumber: annotation.row + 1,
startColumn: annotation.col,
endColumn: Infinity,
message: annotation.text,
severity:
annotation.type === "error"
? monaco.Severity.Error
: monaco.Severity.Warning
};
})
);
timeout = null;
});
}
editor.model.onDidChangeContent(event => {
$textarea.value = editor.getValue(); $textarea.value = editor.getValue();
changed = true; /* global variable used in erp5.js for onbeforeunload event */ changed = true; /* global variable used in erp5.js for onbeforeunload event */
if (mode == "python") {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(checkPythonSourceCode, 300);
}
}); });
if (mode === "python") {
// Check a first time when loading document.
checkPythonSourceCode();
}
editor.addAction({ editor.addAction({
id: 'save', id: "save",
label: 'Save', label: "Save",
keybindings: [ keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S],
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S,
],
precondition: null, precondition: null,
keybindingContext: null, keybindingContext: null,
contextMenuGroupId: 'navigation', contextMenuGroupId: "navigation",
contextMenuOrder: 1.5, contextMenuOrder: 1.5,
run: function(ed) { run: function(ed) {
return saveDocument(); return saveDocument();
} }
}); });
};
window.monacoEditor = editor; // XXX debug
}
</script> </script>
\ No newline at end of file
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