1 import {EditorView, keymap} from "@codemirror/view";
3 import {copyTextToClipboard} from "../services/clipboard.js"
4 import {viewer, editor} from "./setups.js";
5 import {createView, updateViewLanguage} from "./views.js";
8 * Highlight pre elements on a page
10 export function highlight() {
11 const codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
12 for (const codeBlock of codeBlocks) {
13 highlightElem(codeBlock);
18 * Highlight all code blocks within the given parent element
19 * @param {HTMLElement} parent
21 export function highlightWithin(parent) {
22 const codeBlocks = parent.querySelectorAll('pre');
23 for (const codeBlock of codeBlocks) {
24 highlightElem(codeBlock);
29 * Add code highlighting to a single element.
30 * @param {HTMLElement} elem
32 function highlightElem(elem) {
33 const innerCodeElem = elem.querySelector('code[class^=language-]');
34 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
35 const content = elem.textContent.trimEnd();
38 if (innerCodeElem !== null) {
39 langName = innerCodeElem.className.replace('language-', '');
42 const wrapper = document.createElement('div');
43 elem.parentNode.insertBefore(wrapper, elem);
45 const ev = createView({
48 extensions: viewer(wrapper),
51 setMode(ev, langName, content);
57 * Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
58 * @param {EditorView} editorView
60 function addCopyIcon(editorView) {
61 const copyIcon = `<svg viewBox="0 0 24 24" width="16" height="16" xmlns="http://www.w3.org/2000/svg"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>`;
62 const checkIcon = `<svg viewBox="0 0 24 24" width="16" height="16" xmlns="http://www.w3.org/2000/svg"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/></svg>`;
63 const copyButton = document.createElement('button');
64 copyButton.setAttribute('type', 'button')
65 copyButton.classList.add('cm-copy-button');
66 copyButton.innerHTML = copyIcon;
67 editorView.dom.appendChild(copyButton);
69 const notifyTime = 620;
70 const transitionTime = 60;
71 copyButton.addEventListener('click', event => {
72 copyTextToClipboard(editorView.state.doc.toString());
73 copyButton.classList.add('success');
76 copyButton.innerHTML = checkIcon;
77 }, transitionTime / 2);
80 copyButton.classList.remove('success');
84 copyButton.innerHTML = copyIcon;
85 }, notifyTime + (transitionTime / 2));
90 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
91 * Manages a textarea element to hold code content.
92 * @param {HTMLElement} cmContainer
93 * @param {ShadowRoot} shadowRoot
94 * @param {String} content
95 * @param {String} language
96 * @returns {EditorView}
98 export function wysiwygView(cmContainer, shadowRoot, content, language) {
99 // Monkey-patch so that the container document window "CSSStyleSheet" is used instead of the outer window document.
100 // Needed otherwise codemirror fails to apply styles due to a window mismatch when creating a new "CSSStyleSheet" instance.
101 // Opened: https://github.com/codemirror/dev/issues/1133
102 const originalCSSStyleSheetReference = window.CSSStyleSheet;
103 window.CSSStyleSheet = cmContainer.ownerDocument.defaultView.CSSStyleSheet;
105 const ev = createView({
108 extensions: viewer(cmContainer),
112 window.CSSStyleSheet = originalCSSStyleSheetReference;
113 setMode(ev, language, content);
120 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
121 * @param {HTMLElement} elem
122 * @param {String} modeSuggestion
125 export function popupEditor(elem, modeSuggestion) {
126 const content = elem.textContent;
128 return CodeMirror(function(elt) {
129 elem.parentNode.insertBefore(elt, elem);
130 elem.style.display = 'none';
133 mode: getMode(modeSuggestion, content),
141 * Create an inline editor to replace the given textarea.
142 * @param {HTMLTextAreaElement} textArea
143 * @param {String} mode
144 * @returns {EditorView}
146 export function inlineEditor(textArea, mode) {
147 const content = textArea.value;
149 parent: textArea.parentNode,
152 ...editor(textArea.parentElement),
153 EditorView.updateListener.of((v) => {
155 textArea.value = v.state.doc.toString();
161 // Create editor view, hide original input
162 const ev = createView(config);
163 setMode(ev, mode, content);
164 textArea.style.display = 'none';
170 * Set the language mode of a codemirror EditorView.
172 * @param {EditorView} ev
173 * @param {string} modeSuggestion
174 * @param {string} content
176 export function setMode(ev, modeSuggestion, content) {
177 updateViewLanguage(ev, modeSuggestion, content);
181 * Set the content of a cm instance.
182 * @param {EditorView} ev
185 export function setContent(ev, codeContent) {
186 const doc = ev.state.doc;
187 doc.replace(0, doc.length, codeContent);
191 * Get a CodeMirror instance to use for the markdown editor.
192 * @param {HTMLElement} elem
193 * @param {function} onChange
194 * @param {object} domEventHandlers
195 * @param {Array} keyBindings
198 export function markdownEditor(elem, onChange, domEventHandlers, keyBindings) {
199 const content = elem.textContent;
201 parent: elem.parentNode,
204 ...editor(elem.parentElement),
205 EditorView.updateListener.of((v) => {
208 EditorView.domEventHandlers(domEventHandlers),
209 keymap.of(keyBindings),
213 // Emit a pre-event public event to allow tweaking of the configure before view creation.
214 window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {cmEditorViewConfig: config});
216 // Create editor view, hide original input
217 const ev = createView(config);
218 setMode(ev, 'markdown', '');
219 elem.style.display = 'none';