X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/0de4d6d22342fa5a84393e5e18ff59fe42cbb876..refs/pull/3830/head:/resources/js/wysiwyg/plugin-codeeditor.js diff --git a/resources/js/wysiwyg/plugin-codeeditor.js b/resources/js/wysiwyg/plugin-codeeditor.js index b0640a450..66441c87e 100644 --- a/resources/js/wysiwyg/plugin-codeeditor.js +++ b/resources/js/wysiwyg/plugin-codeeditor.js @@ -39,16 +39,16 @@ function defineCodeBlockCustomElement(editor) { constructor() { super(); this.attachShadow({mode: 'open'}); - const linkElem = document.createElement('link'); - linkElem.setAttribute('rel', 'stylesheet'); - linkElem.setAttribute('href', window.baseUrl('/dist/styles.css')); + + const stylesToCopy = document.querySelectorAll('link[rel="stylesheet"]:not([media="print"])'); + const copiedStyles = Array.from(stylesToCopy).map(styleEl => styleEl.cloneNode(false)); const cmContainer = document.createElement('div'); cmContainer.style.pointerEvents = 'none'; cmContainer.contentEditable = 'false'; cmContainer.classList.add('CodeMirrorContainer'); - this.shadowRoot.append(linkElem, cmContainer); + this.shadowRoot.append(...copiedStyles, cmContainer); } getLanguage() { @@ -86,7 +86,13 @@ function defineCodeBlockCustomElement(editor) { getContent() { const code = this.querySelector('code') || this.querySelector('pre'); const tempEl = document.createElement('pre'); - tempEl.innerHTML = code.innerHTML.replace().replace(//gi ,'\n').replace(/\ufeff/g, ''); + tempEl.innerHTML = code.innerHTML.replace(/\ufeff/g, ''); + + const brs = tempEl.querySelectorAll('br'); + for (const br of brs) { + br.replaceWith('\n'); + } + return tempEl.textContent; } @@ -97,11 +103,16 @@ function defineCodeBlockCustomElement(editor) { } this.cleanChildContent(); + const content = this.getContent(); + const lines = content.split('\n').length; + const height = (lines * 19.2) + 18 + 24; + this.style.height = `${height}px`; const container = this.shadowRoot.querySelector('.CodeMirrorContainer'); const renderCodeMirror = (Code) => { - this.cm = Code.wysiwygView(container, this.getContent(), this.getLanguage()); - Code.updateLayout(this.cm); + this.cm = Code.wysiwygView(container, content, this.getLanguage()); + setTimeout(() => Code.updateLayout(this.cm), 10); + setTimeout(() => this.style.height = null, 12); }; window.importVersioned('code').then((Code) => { @@ -142,6 +153,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; @@ -171,7 +190,7 @@ function register(editor, url) { editor.on('PreInit', () => { editor.parser.addNodeFilter('pre', function(elms) { for (const el of elms) { - const wrapper = new tinymce.html.Node.create('code-block', { + const wrapper = tinymce.html.Node.create('code-block', { contenteditable: 'false', }); @@ -197,6 +216,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); });