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);
60 * Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
61 * @param {EditorView} editorView
63 function addCopyIcon(editorView) {
64 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>`;
65 const copyButton = document.createElement('button');
66 copyButton.setAttribute('type', 'button')
67 copyButton.classList.add('cm-copy-button');
68 copyButton.innerHTML = copyIcon;
69 editorView.dom.appendChild(copyButton);
71 copyButton.addEventListener('click', event => {
72 copyTextToClipboard(editorView.state.doc.toString());
73 copyButton.classList.add('success');
75 copyButton.classList.remove('success');
81 * Ge the theme to use for CodeMirror instances.
86 const darkMode = document.documentElement.classList.contains('dark-mode');
87 return window.codeTheme || (darkMode ? 'darcula' : 'default');
91 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
92 * Manages a textarea element to hold code content.
93 * @param {HTMLElement} cmContainer
94 * @param {String} content
95 * @param {String} language
96 * @returns {{wrap: Element, editor: *}}
98 export function wysiwygView(cmContainer, content, language) {
99 return CodeMirror(cmContainer, {
101 mode: getMode(language, content),
111 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
112 * @param {HTMLElement} elem
113 * @param {String} modeSuggestion
116 export function popupEditor(elem, modeSuggestion) {
117 const content = elem.textContent;
119 return CodeMirror(function(elt) {
120 elem.parentNode.insertBefore(elt, elem);
121 elem.style.display = 'none';
124 mode: getMode(modeSuggestion, content),
132 * Create an inline editor to replace the given textarea.
133 * @param {HTMLTextAreaElement} textArea
134 * @param {String} mode
135 * @returns {CodeMirror3}
137 export function inlineEditor(textArea, mode) {
138 return CodeMirror.fromTextArea(textArea, {
139 mode: getMode(mode, textArea.value),
147 * Set the language mode of a codemirror EditorView.
149 * @param {EditorView} ev
150 * @param {string} modeSuggestion
151 * @param {string} content
153 export function setMode(ev, modeSuggestion, content) {
154 updateViewLanguage(ev, modeSuggestion, content);
158 * Set the content of a cm instance.
162 export function setContent(cmInstance, codeContent) {
163 cmInstance.setValue(codeContent);
165 updateLayout(cmInstance);
170 * Update the layout (codemirror refresh) of a cm instance.
173 export function updateLayout(cmInstance) {
174 cmInstance.refresh();
178 * Get a CodeMirror instance to use for the markdown editor.
179 * @param {HTMLElement} elem
180 * @param {function} onChange
181 * @param {object} domEventHandlers
182 * @param {Array} keyBindings
185 export function markdownEditor(elem, onChange, domEventHandlers, keyBindings) {
186 const content = elem.textContent;
188 parent: elem.parentNode,
191 ...editor('markdown'),
192 EditorView.updateListener.of((v) => {
195 EditorView.domEventHandlers(domEventHandlers),
196 keymap.of(keyBindings),
200 // Emit a pre-event public event to allow tweaking of the configure before view creation.
201 window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {cmEditorViewConfig: config});
203 // Create editor view, hide original input
204 const ev = createView(config);
205 setMode(ev, 'markdown', '');
206 elem.style.display = 'none';