1 import CodeMirror from "codemirror";
2 import Clipboard from "clipboard/dist/clipboard.min";
5 import 'codemirror/mode/css/css';
6 import 'codemirror/mode/clike/clike';
7 import 'codemirror/mode/diff/diff';
8 import 'codemirror/mode/fortran/fortran';
9 import 'codemirror/mode/go/go';
10 import 'codemirror/mode/haskell/haskell';
11 import 'codemirror/mode/htmlmixed/htmlmixed';
12 import 'codemirror/mode/javascript/javascript';
13 import 'codemirror/mode/julia/julia';
14 import 'codemirror/mode/lua/lua';
15 import 'codemirror/mode/markdown/markdown';
16 import 'codemirror/mode/mllike/mllike';
17 import 'codemirror/mode/nginx/nginx';
18 import 'codemirror/mode/perl/perl';
19 import 'codemirror/mode/pascal/pascal';
20 import 'codemirror/mode/php/php';
21 import 'codemirror/mode/powershell/powershell';
22 import 'codemirror/mode/properties/properties';
23 import 'codemirror/mode/python/python';
24 import 'codemirror/mode/ruby/ruby';
25 import 'codemirror/mode/rust/rust';
26 import 'codemirror/mode/shell/shell';
27 import 'codemirror/mode/sql/sql';
28 import 'codemirror/mode/toml/toml';
29 import 'codemirror/mode/vbscript/vbscript';
30 import 'codemirror/mode/xml/xml';
31 import 'codemirror/mode/yaml/yaml';
34 import 'codemirror/addon/scroll/scrollpastend';
36 // Mapping of possible languages or formats from user input to their codemirror modes.
37 // Value can be a mode string or a function that will receive the code content & return the mode string.
38 // The function option is used in the event the exact mode could be dynamic depending on the code.
43 scala: 'text/x-scala',
44 kotlin: 'text/x-kotlin',
45 'c++': 'text/x-c++src',
46 'c#': 'text/x-csharp',
47 csharp: 'text/x-csharp',
56 javascript: 'javascript',
57 json: {name: 'javascript', json: true},
69 powershell: 'powershell',
70 properties: 'properties',
72 pascal: 'text/x-pascal',
75 return content.includes('<?php') ? 'php' : 'text/x-php';
95 * Highlight pre elements on a page
97 function highlight() {
98 const codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
99 for (const codeBlock of codeBlocks) {
100 highlightElem(codeBlock);
105 * Highlight all code blocks within the given parent element
106 * @param {HTMLElement} parent
108 function highlightWithin(parent) {
109 const codeBlocks = parent.querySelectorAll('pre');
110 for (const codeBlock of codeBlocks) {
111 highlightElem(codeBlock);
116 * Add code highlighting to a single element.
117 * @param {HTMLElement} elem
119 function highlightElem(elem) {
120 const innerCodeElem = elem.querySelector('code[class^=language-]');
121 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
122 const content = elem.textContent.trimEnd();
125 if (innerCodeElem !== null) {
126 const langName = innerCodeElem.className.replace('language-', '');
127 mode = getMode(langName, content);
130 const cm = CodeMirror(function(elt) {
131 elem.parentNode.replaceChild(elt, elem);
145 * Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
148 function addCopyIcon(cmInstance) {
149 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>`;
150 const copyButton = document.createElement('div');
151 copyButton.classList.add('CodeMirror-copy');
152 copyButton.innerHTML = copyIcon;
153 cmInstance.display.wrapper.appendChild(copyButton);
155 const clipboard = new Clipboard(copyButton, {
156 text: function(trigger) {
157 return cmInstance.getValue()
161 clipboard.on('success', event => {
162 copyButton.classList.add('success');
164 copyButton.classList.remove('success');
170 * Search for a codemirror code based off a user suggestion
171 * @param {String} suggestion
172 * @param {String} content
175 function getMode(suggestion, content) {
176 suggestion = suggestion.trim().replace(/^\./g, '').toLowerCase();
178 const modeMapType = typeof modeMap[suggestion];
180 if (modeMapType === 'undefined') {
184 if (modeMapType === 'function') {
185 return modeMap[suggestion](content);
188 return modeMap[suggestion];
192 * Ge the theme to use for CodeMirror instances.
193 * @returns {*|string}
195 function getTheme() {
196 const darkMode = document.documentElement.classList.contains('dark-mode');
197 return window.codeTheme || (darkMode ? 'darcula' : 'default');
201 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
202 * Manages a textarea element to hold code content.
203 * @param {HTMLElement} elem
204 * @returns {{wrap: Element, editor: *}}
206 function wysiwygView(elem) {
207 const doc = elem.ownerDocument;
208 const codeElem = elem.querySelector('code');
210 let lang = (elem.className || '').replace('language-', '');
211 if (lang === '' && codeElem) {
212 lang = (codeElem.className || '').replace('language-', '')
215 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
216 const content = elem.textContent;
217 const newWrap = doc.createElement('div');
218 const newTextArea = doc.createElement('textarea');
220 newWrap.className = 'CodeMirrorContainer';
221 newWrap.setAttribute('data-lang', lang);
222 newWrap.setAttribute('dir', 'ltr');
223 newTextArea.style.display = 'none';
224 elem.parentNode.replaceChild(newWrap, elem);
226 newWrap.appendChild(newTextArea);
227 newWrap.contentEditable = false;
228 newTextArea.textContent = content;
230 let cm = CodeMirror(function(elt) {
231 newWrap.appendChild(elt);
234 mode: getMode(lang, content),
243 return {wrap: newWrap, editor: cm};
247 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
248 * @param {HTMLElement} elem
249 * @param {String} modeSuggestion
252 function popupEditor(elem, modeSuggestion) {
253 const content = elem.textContent;
255 return CodeMirror(function(elt) {
256 elem.parentNode.insertBefore(elt, elem);
257 elem.style.display = 'none';
260 mode: getMode(modeSuggestion, content),
268 * Set the mode of a codemirror instance.
270 * @param modeSuggestion
272 function setMode(cmInstance, modeSuggestion, content) {
273 cmInstance.setOption('mode', getMode(modeSuggestion, content));
277 * Set the content of a cm instance.
281 function setContent(cmInstance, codeContent) {
282 cmInstance.setValue(codeContent);
284 updateLayout(cmInstance);
289 * Update the layout (codemirror refresh) of a cm instance.
292 function updateLayout(cmInstance) {
293 cmInstance.refresh();
297 * Get a CodeMirror instance to use for the markdown editor.
298 * @param {HTMLElement} elem
301 function markdownEditor(elem) {
302 const content = elem.textContent;
312 window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {config});
314 return CodeMirror(function (elt) {
315 elem.parentNode.insertBefore(elt, elem);
316 elem.style.display = 'none';
321 * Get the 'meta' key dependant on the user's system.
324 function getMetaKey() {
325 let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
326 return mac ? "Cmd" : "Ctrl";
330 highlight: highlight,
331 highlightWithin: highlightWithin,
332 wysiwygView: wysiwygView,
333 popupEditor: popupEditor,
335 setContent: setContent,
336 updateLayout: updateLayout,
337 markdownEditor: markdownEditor,
338 getMetaKey: getMetaKey,