mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-05-03 15:59:57 +00:00
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
This commit is contained in:
parent
06f694bad2
commit
0de4d6d223
2 changed files with 27 additions and 11 deletions
resources/js
|
@ -59,12 +59,10 @@ class CodeEditor {
|
||||||
this.languageInput.value = language;
|
this.languageInput.value = language;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
|
|
||||||
this.show();
|
this.show()
|
||||||
this.updateEditorMode(language);
|
.then(() => this.updateEditorMode(language))
|
||||||
|
.then(() => window.importVersioned('code'))
|
||||||
window.importVersioned('code').then(Code => {
|
.then(Code => Code.setContent(this.editor, code));
|
||||||
Code.setContent(this.editor, code);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async show() {
|
async show() {
|
||||||
|
|
|
@ -91,15 +91,35 @@ function defineCodeBlockCustomElement(editor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
|
const connectedTime = Date.now();
|
||||||
if (this.cm) {
|
if (this.cm) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.cleanChildContent();
|
||||||
|
|
||||||
const container = this.shadowRoot.querySelector('.CodeMirrorContainer');
|
const container = this.shadowRoot.querySelector('.CodeMirrorContainer');
|
||||||
importVersioned('code').then(Code => {
|
const renderCodeMirror = (Code) => {
|
||||||
this.cm = Code.wysiwygView(container, this.getContent(), this.getLanguage());
|
this.cm = Code.wysiwygView(container, this.getContent(), this.getLanguage());
|
||||||
|
Code.updateLayout(this.cm);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.importVersioned('code').then((Code) => {
|
||||||
|
const timeout = (Date.now() - connectedTime < 20) ? 20 : 0;
|
||||||
|
setTimeout(() => renderCodeMirror(Code), timeout);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanChildContent() {
|
||||||
|
const pre = this.querySelector('pre');
|
||||||
|
if (!pre) return;
|
||||||
|
|
||||||
|
for (const preChild of pre.childNodes) {
|
||||||
|
if (preChild.nodeName === '#text' && preChild.textContent === '') {
|
||||||
|
preChild.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
win.customElements.define('code-block', CodeBlockElement);
|
win.customElements.define('code-block', CodeBlockElement);
|
||||||
|
@ -130,15 +150,13 @@ function register(editor, url) {
|
||||||
} else {
|
} else {
|
||||||
const textContent = editor.selection.getContent({format: 'text'});
|
const textContent = editor.selection.getContent({format: 'text'});
|
||||||
showPopup(editor, textContent, '', (newCode, newLang) => {
|
showPopup(editor, textContent, '', (newCode, newLang) => {
|
||||||
const wrap = doc.createElement('code-block');
|
|
||||||
const pre = doc.createElement('pre');
|
const pre = doc.createElement('pre');
|
||||||
const code = doc.createElement('code');
|
const code = doc.createElement('code');
|
||||||
code.classList.add(`language-${newLang}`);
|
code.classList.add(`language-${newLang}`);
|
||||||
code.innerText = newCode;
|
code.innerText = newCode;
|
||||||
pre.append(code);
|
pre.append(code);
|
||||||
wrap.append(pre);
|
|
||||||
|
|
||||||
editor.insertContent(wrap.outerHTML);
|
editor.insertContent(pre.outerHTML);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -168,7 +186,7 @@ function register(editor, url) {
|
||||||
|
|
||||||
editor.parser.addNodeFilter('code-block', function(elms) {
|
editor.parser.addNodeFilter('code-block', function(elms) {
|
||||||
for (const el of elms) {
|
for (const el of elms) {
|
||||||
el.attr('content-editable', 'false');
|
el.attr('contenteditable', 'false');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue