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/dart/dart';
8 import 'codemirror/mode/diff/diff';
9 import 'codemirror/mode/fortran/fortran';
10 import 'codemirror/mode/go/go';
11 import 'codemirror/mode/haskell/haskell';
12 import 'codemirror/mode/htmlmixed/htmlmixed';
13 import 'codemirror/mode/javascript/javascript';
14 import 'codemirror/mode/julia/julia';
15 import 'codemirror/mode/lua/lua';
16 import 'codemirror/mode/markdown/markdown';
17 import 'codemirror/mode/mllike/mllike';
18 import 'codemirror/mode/nginx/nginx';
19 import 'codemirror/mode/octave/octave';
20 import 'codemirror/mode/perl/perl';
21 import 'codemirror/mode/pascal/pascal';
22 import 'codemirror/mode/php/php';
23 import 'codemirror/mode/powershell/powershell';
24 import 'codemirror/mode/properties/properties';
25 import 'codemirror/mode/python/python';
26 import 'codemirror/mode/ruby/ruby';
27 import 'codemirror/mode/rust/rust';
28 import 'codemirror/mode/shell/shell';
29 import 'codemirror/mode/sql/sql';
30 import 'codemirror/mode/stex/stex';
31 import 'codemirror/mode/swift/swift';
32 import 'codemirror/mode/toml/toml';
33 import 'codemirror/mode/vb/vb';
34 import 'codemirror/mode/vbscript/vbscript';
35 import 'codemirror/mode/xml/xml';
36 import 'codemirror/mode/yaml/yaml';
39 import 'codemirror/addon/scroll/scrollpastend';
41 // Mapping of possible languages or formats from user input to their codemirror modes.
42 // Value can be a mode string or a function that will receive the code content & return the mode string.
43 // The function option is used in the event the exact mode could be dynamic depending on the code.
49 scala: 'text/x-scala',
50 kotlin: 'text/x-kotlin',
51 'c++': 'text/x-c++src',
52 'c#': 'text/x-csharp',
53 csharp: 'text/x-csharp',
54 dart: 'application/dart',
58 'f#': 'text/x-fsharp',
59 fsharp: 'text/x-fsharp',
65 javascript: 'text/javascript',
66 json: 'application/json',
67 js: 'text/javascript',
69 julia: 'text/x-julia',
72 matlab: 'text/x-octave',
78 octave: 'text/x-octave',
81 powershell: 'powershell',
82 properties: 'properties',
83 ocaml: 'text/x-ocaml',
84 pascal: 'text/x-pascal',
87 return content.includes('<?php') ? 'php' : 'text/x-php';
99 swift: 'text/x-swift',
101 ts: 'text/typescript',
102 typescript: 'text/typescript',
104 vbscript: 'vbscript',
105 'vb.net': 'text/x-vb',
113 * Highlight pre elements on a page
115 export function highlight() {
116 const codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
117 for (const codeBlock of codeBlocks) {
118 highlightElem(codeBlock);
123 * Highlight all code blocks within the given parent element
124 * @param {HTMLElement} parent
126 export function highlightWithin(parent) {
127 const codeBlocks = parent.querySelectorAll('pre');
128 for (const codeBlock of codeBlocks) {
129 highlightElem(codeBlock);
134 * Add code highlighting to a single element.
135 * @param {HTMLElement} elem
137 function highlightElem(elem) {
138 const innerCodeElem = elem.querySelector('code[class^=language-]');
139 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
140 const content = elem.textContent.trimEnd();
143 if (innerCodeElem !== null) {
144 const langName = innerCodeElem.className.replace('language-', '');
145 mode = getMode(langName, content);
148 const cm = CodeMirror(function(elt) {
149 elem.parentNode.replaceChild(elt, elem);
163 * Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
166 function addCopyIcon(cmInstance) {
167 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>`;
168 const copyButton = document.createElement('div');
169 copyButton.classList.add('CodeMirror-copy');
170 copyButton.innerHTML = copyIcon;
171 cmInstance.display.wrapper.appendChild(copyButton);
173 const clipboard = new Clipboard(copyButton, {
174 text: function(trigger) {
175 return cmInstance.getValue()
179 clipboard.on('success', event => {
180 copyButton.classList.add('success');
182 copyButton.classList.remove('success');
188 * Search for a codemirror code based off a user suggestion
189 * @param {String} suggestion
190 * @param {String} content
193 function getMode(suggestion, content) {
194 suggestion = suggestion.trim().replace(/^\./g, '').toLowerCase();
196 const modeMapType = typeof modeMap[suggestion];
198 if (modeMapType === 'undefined') {
202 if (modeMapType === 'function') {
203 return modeMap[suggestion](content);
206 return modeMap[suggestion];
210 * Ge the theme to use for CodeMirror instances.
211 * @returns {*|string}
213 function getTheme() {
214 const darkMode = document.documentElement.classList.contains('dark-mode');
215 return window.codeTheme || (darkMode ? 'darcula' : 'default');
219 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
220 * Manages a textarea element to hold code content.
221 * @param {HTMLElement} cmContainer
222 * @param {String} content
223 * @param {String} language
224 * @returns {{wrap: Element, editor: *}}
226 export function wysiwygView(cmContainer, content, language) {
227 return CodeMirror(cmContainer, {
229 mode: getMode(language, content),
239 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
240 * @param {HTMLElement} elem
241 * @param {String} modeSuggestion
244 export function popupEditor(elem, modeSuggestion) {
245 const content = elem.textContent;
247 return CodeMirror(function(elt) {
248 elem.parentNode.insertBefore(elt, elem);
249 elem.style.display = 'none';
252 mode: getMode(modeSuggestion, content),
260 * Create an inline editor to replace the given textarea.
261 * @param {HTMLTextAreaElement} textArea
262 * @param {String} mode
263 * @returns {CodeMirror3}
265 export function inlineEditor(textArea, mode) {
266 return CodeMirror.fromTextArea(textArea, {
267 mode: getMode(mode, textArea.value),
275 * Set the mode of a codemirror instance.
277 * @param modeSuggestion
279 export function setMode(cmInstance, modeSuggestion, content) {
280 cmInstance.setOption('mode', getMode(modeSuggestion, content));
284 * Set the content of a cm instance.
288 export function setContent(cmInstance, codeContent) {
289 cmInstance.setValue(codeContent);
291 updateLayout(cmInstance);
296 * Update the layout (codemirror refresh) of a cm instance.
299 export function updateLayout(cmInstance) {
300 cmInstance.refresh();
304 * Get a CodeMirror instance to use for the markdown editor.
305 * @param {HTMLElement} elem
308 export function markdownEditor(elem) {
309 const content = elem.textContent;
319 window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {config});
321 return CodeMirror(function (elt) {
322 elem.parentNode.insertBefore(elt, elem);
323 elem.style.display = 'none';
328 * Get the 'meta' key dependent on the user's system.
331 export function getMetaKey() {
332 let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
333 return mac ? "Cmd" : "Ctrl";