Commit c133fa43 authored by Jérome Perrin's avatar Jérome Perrin

monaco_editor: enable prettier as a js code formatter

This is triggered by using "Format document" from context menu.

Include prettier 1.14.0 Copyright 2017-2018 James Long
https://github.com/prettier/prettier/
parent e02dfde7
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
<script src="rsvp.js"></script> <script src="rsvp.js"></script>
<script src="renderjs.js"></script> <script src="renderjs.js"></script>
<script src="monaco-editor/prettier@1.14.0/standalone.js"></script>
<script src="monaco-editor/prettier@1.14.0/parser-babylon.js"></script>
<script> <script>
/* /*
......
...@@ -88,8 +88,30 @@ ...@@ -88,8 +88,30 @@
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 (this.state.model_language === 'javascript') { if (this.state.model_language === 'javascript') {
// prettier as a formatting provider
monaco.languages.registerDocumentFormattingEditProvider(
'javascript',
{
provideDocumentFormattingEdits(model, options, token) {
const text = prettier.format(model.getValue(), {
parser: 'babylon',
plugins: [prettierPlugins.babylon],
// see http://json.schemastore.org/prettierrc for supported options.
singleQuote: true,
tabWidth: 2
});
return [
{
range: model.getFullModelRange(),
text
}
];
}
}
);
// Type mapping for Nexedi libraries // Type mapping for Nexedi libraries
function addExtraLibrary(script_name, lib_name) { function addExtraLibrary(script_name, lib_name) {
return fetch(script_name) return fetch(script_name)
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Folder" module="OFS.Folder"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_objects</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>prettier@1.14.0</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>parser-babylon.js</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/javascript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>parser-babylon.js</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>standalone.js</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/javascript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>standalone.js</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -52,8 +52,11 @@ ...@@ -52,8 +52,11 @@
tal:content='python: "window.monacoEditorWebPackResourceBaseUrl = " + modules["json"].dumps(options["portal_url"]) + " + \"/monaco-editor/\""'> tal:content='python: "window.monacoEditorWebPackResourceBaseUrl = " + modules["json"].dumps(options["portal_url"]) + " + \"/monaco-editor/\""'>
</script> </script>
<script tal:attributes="src string:${options/portal_url}/monaco-editor/prettier@1.14.0/standalone.js"></script>
<script tal:attributes="src string:${options/portal_url}/monaco-editor/prettier@1.14.0/parser-babylon.js"></script>
<script charset="utf-8"> <script charset="utf-8">
/* 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 = $script.src =
window.monacoEditorWebPackResourceBaseUrl + "/monaco-editor/app.bundle.min.js"; window.monacoEditorWebPackResourceBaseUrl + "/monaco-editor/app.bundle.min.js";
...@@ -114,6 +117,24 @@ $script.onload = function() { ...@@ -114,6 +117,24 @@ $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;
} }
monaco.languages.registerDocumentFormattingEditProvider('javascript', {
provideDocumentFormattingEdits(model, options, token) {
const text = prettier.format(model.getValue(), {
parser: 'babylon',
plugins: [prettierPlugins.babylon],
// see http://json.schemastore.org/prettierrc for supported options.
singleQuote: true,
tabWidth: 2
});
return [
{
range: model.getFullModelRange(),
text,
},
];
},
});
// Type mapping for Nexedi libraries // Type mapping for Nexedi libraries
function addExtraLibrary(script_name, lib_name) { function addExtraLibrary(script_name, lib_name) {
......
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