]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/plugin-codeeditor.js
CM6: Got WYSIWYG code blocks working
[bookstack] / resources / js / wysiwyg / plugin-codeeditor.js
index 5377f564e862aef84bde451d1707d221eb875ad7..7eff5c279d4b28647856b570e9fcefb0b2ed4ed2 100644 (file)
@@ -9,7 +9,7 @@ function elemIsCodeBlock(elem) {
  * @param {function(string, string)} callback (Receives (code: string,language: string)
  */
 function showPopup(editor, code, language, callback) {
-    window.components.first('code-editor').open(code, language, (newCode, newLang) => {
+    window.$components.first('code-editor').open(code, language, (newCode, newLang) => {
         callback(newCode, newLang)
         editor.focus()
     });
@@ -110,8 +110,7 @@ function defineCodeBlockCustomElement(editor) {
 
             const container = this.shadowRoot.querySelector('.CodeMirrorContainer');
             const renderCodeMirror = (Code) => {
-                this.cm = Code.wysiwygView(container, content, this.getLanguage());
-                setTimeout(() => Code.updateLayout(this.cm), 10);
+                this.cm = Code.wysiwygView(container, this.shadowRoot, content, this.getLanguage());
                 setTimeout(() => this.style.height = null, 12);
             };
 
@@ -153,6 +152,14 @@ function register(editor, url) {
         }
     });
 
+    editor.ui.registry.addButton('editcodeeditor', {
+        tooltip: 'Edit code block',
+        icon: 'edit-block',
+        onAction() {
+            editor.execCommand('codeeditor');
+        }
+    });
+
     editor.addCommand('codeeditor', () => {
         const selectedNode = editor.selection.getNode();
         const doc = selectedNode.ownerDocument;
@@ -208,6 +215,15 @@ function register(editor, url) {
         });
     });
 
+    editor.ui.registry.addContextToolbar('codeeditor', {
+        predicate: function (node) {
+            return node.nodeName.toLowerCase() === 'code-block';
+        },
+        items: 'editcodeeditor',
+        position: 'node',
+        scope: 'node'
+    });
+
     editor.on('PreInit', () => {
         defineCodeBlockCustomElement(editor);
     });