1 import {EditorView, keymap} from "@codemirror/view";
2 import {copyTextToClipboard} from "../services/clipboard.js"
5 import {viewer, editor} from "./setups.js";
6 import {createView, updateViewLanguage} from "./views.js";
9 * Highlight pre elements on a page
11 export function highlight() {
12 const codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
13 for (const codeBlock of codeBlocks) {
14 highlightElem(codeBlock);
19 * Highlight all code blocks within the given parent element
20 * @param {HTMLElement} parent
22 export function highlightWithin(parent) {
23 const codeBlocks = parent.querySelectorAll('pre');
24 for (const codeBlock of codeBlocks) {
25 highlightElem(codeBlock);
30 * Add code highlighting to a single element.
31 * @param {HTMLElement} elem
33 function highlightElem(elem) {
34 const innerCodeElem = elem.querySelector('code[class^=language-]');
35 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
36 const content = elem.textContent.trimEnd();
39 if (innerCodeElem !== null) {
40 langName = innerCodeElem.className.replace('language-', '');
43 const wrapper = document.createElement('div');
44 elem.parentNode.insertBefore(wrapper, elem);
46 const ev = createView({
51 setMode(ev, langName, content);
59 * Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
60 * @param {EditorView} editorView
62 function addCopyIcon(editorView) {
63 const copyIcon = `<svg viewBox="0 0 24 24" width="16" height="16" xmlns="http://www.w3.org/2000/svg"><path d="M0 0h24v24H0z" fill="none"/><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>`;
64 const copyButton = document.createElement('button');
65 copyButton.setAttribute('type', 'button')
66 copyButton.classList.add('cm-copy-button');
67 copyButton.innerHTML = copyIcon;
68 editorView.dom.appendChild(copyButton);
70 copyButton.addEventListener('click', event => {
71 copyTextToClipboard(editorView.state.doc.toString());
72 copyButton.classList.add('success');
74 copyButton.classList.remove('success');
80 * Ge the theme to use for CodeMirror instances.
84 const darkMode = document.documentElement.classList.contains('dark-mode');
85 return window.codeTheme || (darkMode ? 'darcula' : 'default');
89 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
90 * Manages a textarea element to hold code content.
91 * @param {HTMLElement} cmContainer
92 * @param {String} content
93 * @param {String} language
94 * @returns {{wrap: Element, editor: *}}
96 export function wysiwygView(cmContainer, content, language) {
97 return CodeMirror(cmContainer, {
99 mode: getMode(language, content),
109 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
110 * @param {HTMLElement} elem
111 * @param {String} modeSuggestion
114 export function popupEditor(elem, modeSuggestion) {
115 const content = elem.textContent;
117 return CodeMirror(function(elt) {
118 elem.parentNode.insertBefore(elt, elem);
119 elem.style.display = 'none';
122 mode: getMode(modeSuggestion, content),
130 * Create an inline editor to replace the given textarea.
131 * @param {HTMLTextAreaElement} textArea
132 * @param {String} mode
133 * @returns {CodeMirror3}
135 export function inlineEditor(textArea, mode) {
136 return CodeMirror.fromTextArea(textArea, {
137 mode: getMode(mode, textArea.value),
145 * Set the language mode of a codemirror EditorView.
147 * @param {EditorView} ev
148 * @param {string} modeSuggestion
149 * @param {string} content
151 export function setMode(ev, modeSuggestion, content) {
152 updateViewLanguage(ev, modeSuggestion, content);
156 * Set the content of a cm instance.
160 export function setContent(cmInstance, codeContent) {
161 cmInstance.setValue(codeContent);
163 updateLayout(cmInstance);
168 * Update the layout (codemirror refresh) of a cm instance.
171 export function updateLayout(cmInstance) {
172 cmInstance.refresh();
176 * Get a CodeMirror instance to use for the markdown editor.
177 * @param {HTMLElement} elem
178 * @param {function} onChange
179 * @param {object} domEventHandlers
180 * @param {Array} keyBindings
183 export function markdownEditor(elem, onChange, domEventHandlers, keyBindings) {
184 const content = elem.textContent;
186 parent: elem.parentNode,
189 ...editor('markdown'),
190 EditorView.updateListener.of((v) => {
193 EditorView.domEventHandlers(domEventHandlers),
194 keymap.of(keyBindings),
198 // Emit a pre-event public event to allow tweaking of the configure before view creation.
199 window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {cmEditorViewConfig: config});
201 // Create editor view, hide original input
202 const ev = createView(config);
203 elem.style.display = 'none';