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';
28 import 'codemirror/mode/pascal/pascal';
31 import 'codemirror/addon/scroll/scrollpastend';
33 // Mapping of possible languages or formats from user input to their codemirror modes.
34 // Value can be a mode string or a function that will receive the code content & return the mode string.
35 // The function option is used in the event the exact mode could be dynamic depending on the code.
40 scala: 'text/x-scala',
41 kotlin: 'text/x-kotlin',
42 'c++': 'text/x-c++src',
43 'c#': 'text/x-csharp',
44 csharp: 'text/x-csharp',
51 javascript: 'javascript',
52 json: {name: 'javascript', json: true},
62 powershell: 'powershell',
63 properties: 'properties',
65 pascal: 'text/x-pascal',
68 return content.includes('<?php') ? 'php' : 'text/x-php';
87 * Highlight pre elements on a page
89 function highlight() {
90 const codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
91 for (const codeBlock of codeBlocks) {
92 highlightElem(codeBlock);
97 * Highlight all code blocks within the given parent element
98 * @param {HTMLElement} parent
100 function highlightWithin(parent) {
101 const codeBlocks = parent.querySelectorAll('pre');
102 for (const codeBlock of codeBlocks) {
103 highlightElem(codeBlock);
108 * Add code highlighting to a single element.
109 * @param {HTMLElement} elem
111 function highlightElem(elem) {
112 const innerCodeElem = elem.querySelector('code[class^=language-]');
113 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
114 const content = elem.textContent.trimEnd();
117 if (innerCodeElem !== null) {
118 const langName = innerCodeElem.className.replace('language-', '');
119 mode = getMode(langName, content);
122 const cm = CodeMirror(function(elt) {
123 elem.parentNode.replaceChild(elt, elem);
137 * Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
140 function addCopyIcon(cmInstance) {
141 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>`;
142 const copyButton = document.createElement('div');
143 copyButton.classList.add('CodeMirror-copy');
144 copyButton.innerHTML = copyIcon;
145 cmInstance.display.wrapper.appendChild(copyButton);
147 const clipboard = new Clipboard(copyButton, {
148 text: function(trigger) {
149 return cmInstance.getValue()
153 clipboard.on('success', event => {
154 copyButton.classList.add('success');
156 copyButton.classList.remove('success');
162 * Search for a codemirror code based off a user suggestion
163 * @param {String} suggestion
164 * @param {String} content
167 function getMode(suggestion, content) {
168 suggestion = suggestion.trim().replace(/^\./g, '').toLowerCase();
170 const modeMapType = typeof modeMap[suggestion];
172 if (modeMapType === 'undefined') {
176 if (modeMapType === 'function') {
177 return modeMap[suggestion](content);
180 return modeMap[suggestion];
184 * Ge the theme to use for CodeMirror instances.
185 * @returns {*|string}
187 function getTheme() {
188 return window.codeTheme || 'default';
192 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
193 * Manages a textarea element to hold code content.
194 * @param {HTMLElement} elem
195 * @returns {{wrap: Element, editor: *}}
197 function wysiwygView(elem) {
198 const doc = elem.ownerDocument;
199 const codeElem = elem.querySelector('code');
201 let lang = (elem.className || '').replace('language-', '');
202 if (lang === '' && codeElem) {
203 lang = (codeElem.className || '').replace('language-', '')
206 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
207 const content = elem.textContent;
208 const newWrap = doc.createElement('div');
209 const newTextArea = doc.createElement('textarea');
211 newWrap.className = 'CodeMirrorContainer';
212 newWrap.setAttribute('data-lang', lang);
213 newWrap.setAttribute('dir', 'ltr');
214 newTextArea.style.display = 'none';
215 elem.parentNode.replaceChild(newWrap, elem);
217 newWrap.appendChild(newTextArea);
218 newWrap.contentEditable = false;
219 newTextArea.textContent = content;
221 let cm = CodeMirror(function(elt) {
222 newWrap.appendChild(elt);
225 mode: getMode(lang, content),
234 return {wrap: newWrap, editor: cm};
238 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
239 * @param {HTMLElement} elem
240 * @param {String} modeSuggestion
243 function popupEditor(elem, modeSuggestion) {
244 const content = elem.textContent;
246 return CodeMirror(function(elt) {
247 elem.parentNode.insertBefore(elt, elem);
248 elem.style.display = 'none';
251 mode: getMode(modeSuggestion, content),
259 * Set the mode of a codemirror instance.
261 * @param modeSuggestion
263 function setMode(cmInstance, modeSuggestion, content) {
264 cmInstance.setOption('mode', getMode(modeSuggestion, content));
268 * Set the content of a cm instance.
272 function setContent(cmInstance, codeContent) {
273 cmInstance.setValue(codeContent);
275 updateLayout(cmInstance);
280 * Update the layout (codemirror refresh) of a cm instance.
283 function updateLayout(cmInstance) {
284 cmInstance.refresh();
288 * Get a CodeMirror instance to use for the markdown editor.
289 * @param {HTMLElement} elem
292 function markdownEditor(elem) {
293 const content = elem.textContent;
303 window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {config});
305 return CodeMirror(function (elt) {
306 elem.parentNode.insertBefore(elt, elem);
307 elem.style.display = 'none';
312 * Get the 'meta' key dependant on the user's system.
315 function getMetaKey() {
316 let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
317 return mac ? "Cmd" : "Ctrl";
321 highlight: highlight,
322 highlightWithin: highlightWithin,
323 wysiwygView: wysiwygView,
324 popupEditor: popupEditor,
326 setContent: setContent,
327 updateLayout: updateLayout,
328 markdownEditor: markdownEditor,
329 getMetaKey: getMetaKey,