Skip to content

Commit 0de4d6d

Browse files
committed
Improved WYSIWYG code block behaviour via range of fixes
- Fixed issues with new code blocks breaking or acting odd due to misnamed contenteditable attribute. - Helped fix issue where code blocks may show in a strage blank state due to timing within shadow dom loading. - Fixed some function timing issues where some functions required their async predecessor to have finished. Tested rather heavily in firefox and brave. Fixes #3292
1 parent 06f694b commit 0de4d6d

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

resources/js/components/code-editor.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ class CodeEditor {
5959
this.languageInput.value = language;
6060
this.callback = callback;
6161

62-
this.show();
63-
this.updateEditorMode(language);
64-
65-
window.importVersioned('code').then(Code => {
66-
Code.setContent(this.editor, code);
67-
});
62+
this.show()
63+
.then(() => this.updateEditorMode(language))
64+
.then(() => window.importVersioned('code'))
65+
.then(Code => Code.setContent(this.editor, code));
6866
}
6967

7068
async show() {

resources/js/wysiwyg/plugin-codeeditor.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,35 @@ function defineCodeBlockCustomElement(editor) {
9191
}
9292

9393
connectedCallback() {
94+
const connectedTime = Date.now();
9495
if (this.cm) {
9596
return;
9697
}
9798

99+
this.cleanChildContent();
100+
98101
const container = this.shadowRoot.querySelector('.CodeMirrorContainer');
99-
importVersioned('code').then(Code => {
102+
const renderCodeMirror = (Code) => {
100103
this.cm = Code.wysiwygView(container, this.getContent(), this.getLanguage());
104+
Code.updateLayout(this.cm);
105+
};
106+
107+
window.importVersioned('code').then((Code) => {
108+
const timeout = (Date.now() - connectedTime < 20) ? 20 : 0;
109+
setTimeout(() => renderCodeMirror(Code), timeout);
101110
});
102111
}
112+
113+
cleanChildContent() {
114+
const pre = this.querySelector('pre');
115+
if (!pre) return;
116+
117+
for (const preChild of pre.childNodes) {
118+
if (preChild.nodeName === '#text' && preChild.textContent === '') {
119+
preChild.remove();
120+
}
121+
}
122+
}
103123
}
104124

105125
win.customElements.define('code-block', CodeBlockElement);
@@ -130,15 +150,13 @@ function register(editor, url) {
130150
} else {
131151
const textContent = editor.selection.getContent({format: 'text'});
132152
showPopup(editor, textContent, '', (newCode, newLang) => {
133-
const wrap = doc.createElement('code-block');
134153
const pre = doc.createElement('pre');
135154
const code = doc.createElement('code');
136155
code.classList.add(`language-${newLang}`);
137156
code.innerText = newCode;
138157
pre.append(code);
139-
wrap.append(pre);
140158

141-
editor.insertContent(wrap.outerHTML);
159+
editor.insertContent(pre.outerHTML);
142160
});
143161
}
144162
});
@@ -168,7 +186,7 @@ function register(editor, url) {
168186

169187
editor.parser.addNodeFilter('code-block', function(elms) {
170188
for (const el of elms) {
171-
el.attr('content-editable', 'false');
189+
el.attr('contenteditable', 'false');
172190
}
173191
});
174192

0 commit comments

Comments
 (0)