From 9dd69b04b8bcb32abe9db60627a52690629bcb52 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Tue, 21 Jun 2022 12:01:06 +0100 Subject: [PATCH] Fixed code snippets being added as single line TinyMCE was adding attributes to
elements within code blocks which would then not be converted to newlines by our code regex match. This changes the conversion to use dom querying instead. Fixes #3507 --- resources/js/wysiwyg/plugin-codeeditor.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/resources/js/wysiwyg/plugin-codeeditor.js b/resources/js/wysiwyg/plugin-codeeditor.js index 82ab7d3c8..4123fb783 100644 --- a/resources/js/wysiwyg/plugin-codeeditor.js +++ b/resources/js/wysiwyg/plugin-codeeditor.js @@ -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; } @@ -104,6 +110,7 @@ function defineCodeBlockCustomElement(editor) { const container = this.shadowRoot.querySelector('.CodeMirrorContainer'); const renderCodeMirror = (Code) => { + console.log({content}); this.cm = Code.wysiwygView(container, content, this.getLanguage()); Code.updateLayout(this.cm); setTimeout(() => { @@ -159,6 +166,7 @@ function register(editor, url) { showPopup(editor, textContent, '', (newCode, newLang) => { const pre = doc.createElement('pre'); const code = doc.createElement('code'); + console.log(newCode); code.classList.add(`language-${newLang}`); code.innerText = newCode; pre.append(code);