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/go/go';
9 import 'codemirror/mode/htmlmixed/htmlmixed';
10 import 'codemirror/mode/javascript/javascript';
11 import 'codemirror/mode/julia/julia';
12 import 'codemirror/mode/lua/lua';
13 import 'codemirror/mode/haskell/haskell';
14 import 'codemirror/mode/markdown/markdown';
15 import 'codemirror/mode/mllike/mllike';
16 import 'codemirror/mode/nginx/nginx';
17 import 'codemirror/mode/php/php';
18 import 'codemirror/mode/powershell/powershell';
19 import 'codemirror/mode/properties/properties';
20 import 'codemirror/mode/python/python';
21 import 'codemirror/mode/ruby/ruby';
22 import 'codemirror/mode/rust/rust';
23 import 'codemirror/mode/shell/shell';
24 import 'codemirror/mode/sql/sql';
25 import 'codemirror/mode/toml/toml';
26 import 'codemirror/mode/xml/xml';
27 import 'codemirror/mode/yaml/yaml';
30 import 'codemirror/addon/scroll/scrollpastend';
32 // Mapping of possible languages or formats from user input to their codemirror modes.
33 // Value can be a mode string or a function that will receive the code content & return the mode string.
34 // The function option is used in the event the exact mode could be dynamic depending on the code.
39 scala: 'text/x-scala',
40 kotlin: 'text/x-kotlin',
41 'c++': 'text/x-c++src',
42 'c#': 'text/x-csharp',
43 csharp: 'text/x-csharp',
50 javascript: 'javascript',
51 json: {name: 'javascript', json: true},
61 powershell: 'powershell',
62 properties: 'properties',
65 return content.includes('<?php') ? 'php' : 'text/x-php';
84 * Highlight pre elements on a page
86 function highlight() {
87 let codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
88 for (let i = 0; i < codeBlocks.length; i++) {
89 highlightElem(codeBlocks[i]);
94 * Add code highlighting to a single element.
95 * @param {HTMLElement} elem
97 function highlightElem(elem) {
98 const innerCodeElem = elem.querySelector('code[class^=language-]');
99 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
100 const content = elem.textContent.trim();
103 if (innerCodeElem !== null) {
104 const langName = innerCodeElem.className.replace('language-', '');
105 mode = getMode(langName, content);
108 const cm = CodeMirror(function(elt) {
109 elem.parentNode.replaceChild(elt, elem);
123 * Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
126 function addCopyIcon(cmInstance) {
127 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>`;
128 const copyButton = document.createElement('div');
129 copyButton.classList.add('CodeMirror-copy');
130 copyButton.innerHTML = copyIcon;
131 cmInstance.display.wrapper.appendChild(copyButton);
133 const clipboard = new Clipboard(copyButton, {
134 text: function(trigger) {
135 return cmInstance.getValue()
139 clipboard.on('success', event => {
140 copyButton.classList.add('success');
142 copyButton.classList.remove('success');
148 * Search for a codemirror code based off a user suggestion
149 * @param {String} suggestion
150 * @param {String} content
153 function getMode(suggestion, content) {
154 suggestion = suggestion.trim().replace(/^\./g, '').toLowerCase();
156 const modeMapType = typeof modeMap[suggestion];
158 if (modeMapType === 'undefined') {
162 if (modeMapType === 'function') {
163 return modeMap[suggestion](content);
166 return modeMap[suggestion];
170 * Ge the theme to use for CodeMirror instances.
171 * @returns {*|string}
173 function getTheme() {
174 return window.codeTheme || 'base16-light';
178 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
179 * Manages a textarea element to hold code content.
180 * @param {HTMLElement} elem
181 * @returns {{wrap: Element, editor: *}}
183 function wysiwygView(elem) {
184 const doc = elem.ownerDocument;
185 const codeElem = elem.querySelector('code');
187 let lang = (elem.className || '').replace('language-', '');
188 if (lang === '' && codeElem) {
189 lang = (codeElem.className || '').replace('language-', '')
192 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
193 const content = elem.textContent;
194 const newWrap = doc.createElement('div');
195 const newTextArea = doc.createElement('textarea');
197 newWrap.className = 'CodeMirrorContainer';
198 newWrap.setAttribute('data-lang', lang);
199 newWrap.setAttribute('dir', 'ltr');
200 newTextArea.style.display = 'none';
201 elem.parentNode.replaceChild(newWrap, elem);
203 newWrap.appendChild(newTextArea);
204 newWrap.contentEditable = false;
205 newTextArea.textContent = content;
207 let cm = CodeMirror(function(elt) {
208 newWrap.appendChild(elt);
211 mode: getMode(lang, content),
220 return {wrap: newWrap, editor: cm};
224 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
225 * @param {HTMLElement} elem
226 * @param {String} modeSuggestion
229 function popupEditor(elem, modeSuggestion) {
230 const content = elem.textContent;
232 return CodeMirror(function(elt) {
233 elem.parentNode.insertBefore(elt, elem);
234 elem.style.display = 'none';
237 mode: getMode(modeSuggestion, content),
245 * Set the mode of a codemirror instance.
247 * @param modeSuggestion
249 function setMode(cmInstance, modeSuggestion, content) {
250 cmInstance.setOption('mode', getMode(modeSuggestion, content));
254 * Set the content of a cm instance.
258 function setContent(cmInstance, codeContent) {
259 cmInstance.setValue(codeContent);
261 updateLayout(cmInstance);
266 * Update the layout (codemirror refresh) of a cm instance.
269 function updateLayout(cmInstance) {
270 cmInstance.refresh();
274 * Get a CodeMirror instance to use for the markdown editor.
275 * @param {HTMLElement} elem
278 function markdownEditor(elem) {
279 const content = elem.textContent;
289 window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {config});
291 return CodeMirror(function (elt) {
292 elem.parentNode.insertBefore(elt, elem);
293 elem.style.display = 'none';
298 * Get the 'meta' key dependant on the user's system.
301 function getMetaKey() {
302 let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
303 return mac ? "Cmd" : "Ctrl";
307 highlight: highlight,
308 wysiwygView: wysiwygView,
309 popupEditor: popupEditor,
311 setContent: setContent,
312 updateLayout: updateLayout,
313 markdownEditor: markdownEditor,
314 getMetaKey: getMetaKey,