* @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()
});
const win = doc.defaultView;
class CodeBlockElement extends win.HTMLElement {
+
+ /**
+ * @type {?SimpleEditorInterface}
+ */
+ editor = null;
+
constructor() {
super();
this.attachShadow({mode: 'open'});
cmContainer.style.pointerEvents = 'none';
cmContainer.contentEditable = 'false';
cmContainer.classList.add('CodeMirrorContainer');
+ cmContainer.classList.toggle('dark-mode', document.documentElement.classList.contains('dark-mode'));
this.shadowRoot.append(...copiedStyles, cmContainer);
}
}
setContent(content, language) {
- if (this.cm) {
- importVersioned('code').then(Code => {
- Code.setContent(this.cm, content);
- Code.setMode(this.cm, language, content);
- });
+ if (this.editor) {
+ this.editor.setContent(content);
+ this.editor.setMode(language, content);
}
let pre = this.querySelector('pre');
connectedCallback() {
const connectedTime = Date.now();
- if (this.cm) {
+ if (this.editor) {
return;
}
this.style.height = `${height}px`;
const container = this.shadowRoot.querySelector('.CodeMirrorContainer');
- const renderCodeMirror = (Code) => {
- this.cm = Code.wysiwygView(container, content, this.getLanguage());
- setTimeout(() => Code.updateLayout(this.cm), 10);
+ const renderEditor = (Code) => {
+ this.editor = Code.wysiwygView(container, this.shadowRoot, content, this.getLanguage());
setTimeout(() => this.style.height = null, 12);
};
window.importVersioned('code').then((Code) => {
const timeout = (Date.now() - connectedTime < 20) ? 20 : 0;
- setTimeout(() => renderCodeMirror(Code), timeout);
+ setTimeout(() => renderEditor(Code), timeout);
});
}