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 * Ge the theme to use for CodeMirror instances.
95 const darkMode = document.documentElement.classList.contains('dark-mode');
96 return window.codeTheme || (darkMode ? 'darcula' : 'default');
100 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
101 * Manages a textarea element to hold code content.
102 * @param {HTMLElement} cmContainer
103 * @param {String} content
104 * @param {String} language
105 * @returns {EditorView}
107 export function wysiwygView(cmContainer, content, language) {
108 const ev = createView({
111 extensions: viewer(cmContainer),
114 setMode(ev, language, content);
121 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
122 * @param {HTMLElement} elem
123 * @param {String} modeSuggestion
126 export function popupEditor(elem, modeSuggestion) {
127 const content = elem.textContent;
129 return CodeMirror(function(elt) {
130 elem.parentNode.insertBefore(elt, elem);
131 elem.style.display = 'none';
134 mode: getMode(modeSuggestion, content),
142 * Create an inline editor to replace the given textarea.
143 * @param {HTMLTextAreaElement} textArea
144 * @param {String} mode
145 * @returns {EditorView}
147 export function inlineEditor(textArea, mode) {
148 const content = textArea.value;
150 parent: textArea.parentNode,
153 ...editor(textArea.parentElement),
154 EditorView.updateListener.of((v) => {
156 textArea.value = v.state.doc.toString();
162 // Create editor view, hide original input
163 const ev = createView(config);
164 setMode(ev, mode, content);
165 textArea.style.display = 'none';
171 * Set the language mode of a codemirror EditorView.
173 * @param {EditorView} ev
174 * @param {string} modeSuggestion
175 * @param {string} content
177 export function setMode(ev, modeSuggestion, content) {
178 updateViewLanguage(ev, modeSuggestion, content);
182 * Set the content of a cm instance.
186 export function setContent(cmInstance, codeContent) {
187 cmInstance.setValue(codeContent);
189 updateLayout(cmInstance);
194 * Update the layout (codemirror refresh) of a cm instance.
197 export function updateLayout(cmInstance) {
198 cmInstance.refresh();
202 * Get a CodeMirror instance to use for the markdown editor.
203 * @param {HTMLElement} elem
204 * @param {function} onChange
205 * @param {object} domEventHandlers
206 * @param {Array} keyBindings
209 export function markdownEditor(elem, onChange, domEventHandlers, keyBindings) {
210 const content = elem.textContent;
212 parent: elem.parentNode,
215 ...editor(elem.parentElement),
216 EditorView.updateListener.of((v) => {
219 EditorView.domEventHandlers(domEventHandlers),
220 keymap.of(keyBindings),
224 // Emit a pre-event public event to allow tweaking of the configure before view creation.
225 window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {cmEditorViewConfig: config});
227 // Create editor view, hide original input
228 const ev = createView(config);
229 setMode(ev, 'markdown', '');
230 elem.style.display = 'none';