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/scheme/scheme';
29 import 'codemirror/mode/shell/shell';
30 import 'codemirror/mode/smarty/smarty';
31 import 'codemirror/mode/sql/sql';
32 import 'codemirror/mode/stex/stex';
33 import 'codemirror/mode/swift/swift';
34 import 'codemirror/mode/toml/toml';
35 import 'codemirror/mode/twig/twig';
36 import 'codemirror/mode/vb/vb';
37 import 'codemirror/mode/vbscript/vbscript';
38 import 'codemirror/mode/xml/xml';
39 import 'codemirror/mode/yaml/yaml';
42 import 'codemirror/addon/scroll/scrollpastend';
44 // Mapping of possible languages or formats from user input to their codemirror modes.
45 // Value can be a mode string or a function that will receive the code content & return the mode string.
46 // The function option is used in the event the exact mode could be dynamic depending on the code.
52 scala: 'text/x-scala',
53 kotlin: 'text/x-kotlin',
54 'c++': 'text/x-c++src',
55 'c#': 'text/x-csharp',
56 csharp: 'text/x-csharp',
57 dart: 'application/dart',
61 'f#': 'text/x-fsharp',
62 fsharp: 'text/x-fsharp',
68 javascript: 'text/javascript',
69 json: 'application/json',
70 js: 'text/javascript',
72 julia: 'text/x-julia',
75 matlab: 'text/x-octave',
80 mssql: 'text/x-mssql',
81 mysql: 'text/x-mysql',
83 octave: 'text/x-octave',
86 powershell: 'powershell',
87 properties: 'properties',
88 ocaml: 'text/x-ocaml',
89 pascal: 'text/x-pascal',
92 return content.includes('<?php') ? 'php' : 'text/x-php';
94 pgsql: 'text/x-pgsql',
95 'pl/sql': 'text/x-plsql',
96 postgresql: 'text/x-pgsql',
108 sqlite: 'text/x-sqlite',
109 stext: 'text/x-stex',
110 swift: 'text/x-swift',
112 ts: 'text/typescript',
114 typescript: 'text/typescript',
116 vbscript: 'vbscript',
117 'vb.net': 'text/x-vb',
125 * Highlight pre elements on a page
127 export function highlight() {
128 const codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
129 for (const codeBlock of codeBlocks) {
130 highlightElem(codeBlock);
135 * Highlight all code blocks within the given parent element
136 * @param {HTMLElement} parent
138 export function highlightWithin(parent) {
139 const codeBlocks = parent.querySelectorAll('pre');
140 for (const codeBlock of codeBlocks) {
141 highlightElem(codeBlock);
146 * Add code highlighting to a single element.
147 * @param {HTMLElement} elem
149 function highlightElem(elem) {
150 const innerCodeElem = elem.querySelector('code[class^=language-]');
151 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
152 const content = elem.textContent.trimEnd();
155 if (innerCodeElem !== null) {
156 const langName = innerCodeElem.className.replace('language-', '');
157 mode = getMode(langName, content);
160 const cm = CodeMirror(function(elt) {
161 elem.parentNode.replaceChild(elt, elem);
175 * Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
178 function addCopyIcon(cmInstance) {
179 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>`;
180 const copyButton = document.createElement('div');
181 copyButton.classList.add('CodeMirror-copy');
182 copyButton.innerHTML = copyIcon;
183 cmInstance.display.wrapper.appendChild(copyButton);
185 const clipboard = new Clipboard(copyButton, {
186 text: function(trigger) {
187 return cmInstance.getValue()
191 clipboard.on('success', event => {
192 copyButton.classList.add('success');
194 copyButton.classList.remove('success');
200 * Search for a codemirror code based off a user suggestion
201 * @param {String} suggestion
202 * @param {String} content
205 function getMode(suggestion, content) {
206 suggestion = suggestion.trim().replace(/^\./g, '').toLowerCase();
208 const modeMapType = typeof modeMap[suggestion];
210 if (modeMapType === 'undefined') {
214 if (modeMapType === 'function') {
215 return modeMap[suggestion](content);
218 return modeMap[suggestion];
222 * Ge the theme to use for CodeMirror instances.
223 * @returns {*|string}
225 function getTheme() {
226 const darkMode = document.documentElement.classList.contains('dark-mode');
227 return window.codeTheme || (darkMode ? 'darcula' : 'default');
231 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
232 * Manages a textarea element to hold code content.
233 * @param {HTMLElement} cmContainer
234 * @param {String} content
235 * @param {String} language
236 * @returns {{wrap: Element, editor: *}}
238 export function wysiwygView(cmContainer, content, language) {
239 return CodeMirror(cmContainer, {
241 mode: getMode(language, content),
251 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
252 * @param {HTMLElement} elem
253 * @param {String} modeSuggestion
256 export function popupEditor(elem, modeSuggestion) {
257 const content = elem.textContent;
259 return CodeMirror(function(elt) {
260 elem.parentNode.insertBefore(elt, elem);
261 elem.style.display = 'none';
264 mode: getMode(modeSuggestion, content),
272 * Create an inline editor to replace the given textarea.
273 * @param {HTMLTextAreaElement} textArea
274 * @param {String} mode
275 * @returns {CodeMirror3}
277 export function inlineEditor(textArea, mode) {
278 return CodeMirror.fromTextArea(textArea, {
279 mode: getMode(mode, textArea.value),
287 * Set the mode of a codemirror instance.
289 * @param modeSuggestion
291 export function setMode(cmInstance, modeSuggestion, content) {
292 cmInstance.setOption('mode', getMode(modeSuggestion, content));
296 * Set the content of a cm instance.
300 export function setContent(cmInstance, codeContent) {
301 cmInstance.setValue(codeContent);
303 updateLayout(cmInstance);
308 * Update the layout (codemirror refresh) of a cm instance.
311 export function updateLayout(cmInstance) {
312 cmInstance.refresh();
316 * Get a CodeMirror instance to use for the markdown editor.
317 * @param {HTMLElement} elem
320 export function markdownEditor(elem) {
321 const content = elem.textContent;
331 window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {config});
333 return CodeMirror(function (elt) {
334 elem.parentNode.insertBefore(elt, elem);
335 elem.style.display = 'none';
340 * Get the 'meta' key dependent on the user's system.
343 export function getMetaKey() {
344 let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
345 return mac ? "Cmd" : "Ctrl";