2018-11-09 16:17:35 -05:00
|
|
|
import CodeMirror from "codemirror";
|
2018-11-10 11:08:33 -05:00
|
|
|
import Clipboard from "clipboard/dist/clipboard.min";
|
2018-09-22 18:24:51 -04:00
|
|
|
|
|
|
|
// Modes
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/css/css';
|
|
|
|
import 'codemirror/mode/clike/clike';
|
2022-11-10 08:38:56 -05:00
|
|
|
import 'codemirror/mode/dart/dart';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/diff/diff';
|
2020-02-07 07:45:19 -05:00
|
|
|
import 'codemirror/mode/fortran/fortran';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/go/go';
|
2020-02-07 07:45:19 -05:00
|
|
|
import 'codemirror/mode/haskell/haskell';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/htmlmixed/htmlmixed';
|
|
|
|
import 'codemirror/mode/javascript/javascript';
|
2019-03-22 04:42:02 -04:00
|
|
|
import 'codemirror/mode/julia/julia';
|
2019-03-02 03:52:14 -05:00
|
|
|
import 'codemirror/mode/lua/lua';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/markdown/markdown';
|
2019-03-22 04:42:02 -04:00
|
|
|
import 'codemirror/mode/mllike/mllike';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/nginx/nginx';
|
2022-09-27 13:52:21 -04:00
|
|
|
import 'codemirror/mode/octave/octave';
|
2020-01-19 00:41:18 -05:00
|
|
|
import 'codemirror/mode/perl/perl';
|
2020-02-07 07:45:19 -05:00
|
|
|
import 'codemirror/mode/pascal/pascal';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/php/php';
|
|
|
|
import 'codemirror/mode/powershell/powershell';
|
2019-09-15 20:20:11 -04:00
|
|
|
import 'codemirror/mode/properties/properties';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/python/python';
|
|
|
|
import 'codemirror/mode/ruby/ruby';
|
2019-03-22 04:42:02 -04:00
|
|
|
import 'codemirror/mode/rust/rust';
|
2023-01-26 06:26:20 -05:00
|
|
|
import 'codemirror/mode/scheme/scheme';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/shell/shell';
|
2022-11-29 08:53:41 -05:00
|
|
|
import 'codemirror/mode/smarty/smarty';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/sql/sql';
|
2022-05-30 13:41:40 -04:00
|
|
|
import 'codemirror/mode/stex/stex';
|
2022-11-12 03:44:25 -05:00
|
|
|
import 'codemirror/mode/swift/swift';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/toml/toml';
|
2022-11-29 08:53:41 -05:00
|
|
|
import 'codemirror/mode/twig/twig';
|
2021-08-04 15:56:34 -04:00
|
|
|
import 'codemirror/mode/vb/vb';
|
2020-10-02 09:14:29 -04:00
|
|
|
import 'codemirror/mode/vbscript/vbscript';
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/mode/xml/xml';
|
|
|
|
import 'codemirror/mode/yaml/yaml';
|
2017-05-28 08:16:21 -04:00
|
|
|
|
2018-09-22 18:24:51 -04:00
|
|
|
// Addons
|
2018-11-09 16:17:35 -05:00
|
|
|
import 'codemirror/addon/scroll/scrollpastend';
|
2017-05-28 08:16:21 -04:00
|
|
|
|
2019-12-07 11:23:44 -05:00
|
|
|
// Mapping of possible languages or formats from user input to their codemirror modes.
|
|
|
|
// Value can be a mode string or a function that will receive the code content & return the mode string.
|
|
|
|
// The function option is used in the event the exact mode could be dynamic depending on the code.
|
2017-06-17 07:41:18 -04:00
|
|
|
const modeMap = {
|
2022-07-24 16:15:43 -04:00
|
|
|
bash: 'shell',
|
2017-06-17 07:41:18 -04:00
|
|
|
css: 'css',
|
2018-05-20 04:32:15 -04:00
|
|
|
c: 'text/x-csrc',
|
|
|
|
java: 'text/x-java',
|
|
|
|
scala: 'text/x-scala',
|
|
|
|
kotlin: 'text/x-kotlin',
|
|
|
|
'c++': 'text/x-c++src',
|
|
|
|
'c#': 'text/x-csharp',
|
|
|
|
csharp: 'text/x-csharp',
|
2022-11-10 08:38:56 -05:00
|
|
|
dart: 'application/dart',
|
2017-07-01 11:10:52 -04:00
|
|
|
diff: 'diff',
|
2020-02-07 07:45:19 -05:00
|
|
|
for: 'fortran',
|
|
|
|
fortran: 'fortran',
|
2022-06-20 12:49:56 -04:00
|
|
|
'f#': 'text/x-fsharp',
|
|
|
|
fsharp: 'text/x-fsharp',
|
2017-06-17 07:41:18 -04:00
|
|
|
go: 'go',
|
2019-03-22 04:42:02 -04:00
|
|
|
haskell: 'haskell',
|
|
|
|
hs: 'haskell',
|
2017-06-17 07:41:18 -04:00
|
|
|
html: 'htmlmixed',
|
2019-10-17 16:16:55 -04:00
|
|
|
ini: 'properties',
|
2022-06-20 12:49:56 -04:00
|
|
|
javascript: 'text/javascript',
|
|
|
|
json: 'application/json',
|
|
|
|
js: 'text/javascript',
|
|
|
|
jl: 'text/x-julia',
|
|
|
|
julia: 'text/x-julia',
|
2022-05-30 13:41:40 -04:00
|
|
|
latex: 'text/x-stex',
|
2019-03-02 03:52:14 -05:00
|
|
|
lua: 'lua',
|
2022-09-27 13:52:21 -04:00
|
|
|
matlab: 'text/x-octave',
|
2017-06-17 07:41:18 -04:00
|
|
|
md: 'markdown',
|
|
|
|
mdown: 'markdown',
|
|
|
|
markdown: 'markdown',
|
2019-03-22 04:42:02 -04:00
|
|
|
ml: 'mllike',
|
2023-01-26 06:26:20 -05:00
|
|
|
mssql: 'text/x-mssql',
|
|
|
|
mysql: 'text/x-mysql',
|
2017-06-17 07:41:18 -04:00
|
|
|
nginx: 'nginx',
|
2022-09-27 13:52:21 -04:00
|
|
|
octave: 'text/x-octave',
|
2020-01-19 00:41:18 -05:00
|
|
|
perl: 'perl',
|
|
|
|
pl: 'perl',
|
2017-06-17 07:41:18 -04:00
|
|
|
powershell: 'powershell',
|
2019-09-15 20:24:47 -04:00
|
|
|
properties: 'properties',
|
2022-06-20 12:49:56 -04:00
|
|
|
ocaml: 'text/x-ocaml',
|
2019-12-27 12:14:34 -05:00
|
|
|
pascal: 'text/x-pascal',
|
|
|
|
pas: 'text/x-pascal',
|
2019-12-07 11:23:44 -05:00
|
|
|
php: (content) => {
|
|
|
|
return content.includes('<?php') ? 'php' : 'text/x-php';
|
|
|
|
},
|
2023-01-26 06:26:20 -05:00
|
|
|
pgsql: 'text/x-pgsql',
|
|
|
|
'pl/sql': 'text/x-plsql',
|
|
|
|
postgresql: 'text/x-pgsql',
|
2017-06-17 07:41:18 -04:00
|
|
|
py: 'python',
|
|
|
|
python: 'python',
|
|
|
|
ruby: 'ruby',
|
2019-03-22 04:42:02 -04:00
|
|
|
rust: 'rust',
|
2017-06-17 07:41:18 -04:00
|
|
|
rb: 'ruby',
|
2019-03-22 04:42:02 -04:00
|
|
|
rs: 'rust',
|
2023-01-26 06:26:20 -05:00
|
|
|
scheme: 'scheme',
|
2017-06-17 07:41:18 -04:00
|
|
|
shell: 'shell',
|
2017-07-01 11:10:52 -04:00
|
|
|
sh: 'shell',
|
2022-11-29 08:53:41 -05:00
|
|
|
smarty: 'smarty',
|
2022-11-12 03:44:25 -05:00
|
|
|
sql: 'text/x-sql',
|
2023-01-26 06:26:20 -05:00
|
|
|
sqlite: 'text/x-sqlite',
|
2022-05-30 13:41:40 -04:00
|
|
|
stext: 'text/x-stex',
|
2022-11-12 03:44:25 -05:00
|
|
|
swift: 'text/x-swift',
|
2017-06-17 07:41:18 -04:00
|
|
|
toml: 'toml',
|
2022-06-20 12:49:56 -04:00
|
|
|
ts: 'text/typescript',
|
2022-11-29 08:53:41 -05:00
|
|
|
twig: 'twig',
|
2022-06-20 12:49:56 -04:00
|
|
|
typescript: 'text/typescript',
|
2020-10-13 17:44:33 -04:00
|
|
|
vbs: 'vbscript',
|
2020-10-02 09:14:29 -04:00
|
|
|
vbscript: 'vbscript',
|
2021-08-04 15:56:34 -04:00
|
|
|
'vb.net': 'text/x-vb',
|
|
|
|
vbnet: 'text/x-vb',
|
2017-06-17 07:41:18 -04:00
|
|
|
xml: 'xml',
|
|
|
|
yaml: 'yaml',
|
|
|
|
yml: 'yaml',
|
|
|
|
};
|
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Highlight pre elements on a page
|
|
|
|
*/
|
2022-02-08 06:10:01 -05:00
|
|
|
export function highlight() {
|
2020-01-15 15:18:02 -05:00
|
|
|
const codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
|
|
|
|
for (const codeBlock of codeBlocks) {
|
|
|
|
highlightElem(codeBlock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Highlight all code blocks within the given parent element
|
|
|
|
* @param {HTMLElement} parent
|
|
|
|
*/
|
2022-02-08 06:10:01 -05:00
|
|
|
export function highlightWithin(parent) {
|
2020-01-15 15:18:02 -05:00
|
|
|
const codeBlocks = parent.querySelectorAll('pre');
|
|
|
|
for (const codeBlock of codeBlocks) {
|
|
|
|
highlightElem(codeBlock);
|
2017-06-17 10:07:55 -04:00
|
|
|
}
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-05-28 08:16:21 -04:00
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Add code highlighting to a single element.
|
|
|
|
* @param {HTMLElement} elem
|
|
|
|
*/
|
2017-06-17 10:07:55 -04:00
|
|
|
function highlightElem(elem) {
|
2019-12-07 11:23:44 -05:00
|
|
|
const innerCodeElem = elem.querySelector('code[class^=language-]');
|
|
|
|
elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
|
2020-02-15 09:24:55 -05:00
|
|
|
const content = elem.textContent.trimEnd();
|
2019-12-07 11:23:44 -05:00
|
|
|
|
2017-06-17 10:07:55 -04:00
|
|
|
let mode = '';
|
|
|
|
if (innerCodeElem !== null) {
|
2019-12-07 11:23:44 -05:00
|
|
|
const langName = innerCodeElem.className.replace('language-', '');
|
|
|
|
mode = getMode(langName, content);
|
2017-05-28 08:16:21 -04:00
|
|
|
}
|
2017-06-17 10:07:55 -04:00
|
|
|
|
2019-12-07 11:23:44 -05:00
|
|
|
const cm = CodeMirror(function(elt) {
|
2017-06-17 10:07:55 -04:00
|
|
|
elem.parentNode.replaceChild(elt, elem);
|
|
|
|
}, {
|
|
|
|
value: content,
|
|
|
|
mode: mode,
|
|
|
|
lineNumbers: true,
|
2019-10-17 09:41:39 -04:00
|
|
|
lineWrapping: false,
|
2017-09-02 08:34:37 -04:00
|
|
|
theme: getTheme(),
|
2017-06-17 10:07:55 -04:00
|
|
|
readOnly: true
|
|
|
|
});
|
2018-06-09 05:41:01 -04:00
|
|
|
|
|
|
|
addCopyIcon(cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
|
|
|
|
* @param cmInstance
|
|
|
|
*/
|
|
|
|
function addCopyIcon(cmInstance) {
|
|
|
|
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>`;
|
|
|
|
const copyButton = document.createElement('div');
|
|
|
|
copyButton.classList.add('CodeMirror-copy');
|
|
|
|
copyButton.innerHTML = copyIcon;
|
|
|
|
cmInstance.display.wrapper.appendChild(copyButton);
|
|
|
|
|
|
|
|
const clipboard = new Clipboard(copyButton, {
|
|
|
|
text: function(trigger) {
|
|
|
|
return cmInstance.getValue()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
clipboard.on('success', event => {
|
|
|
|
copyButton.classList.add('success');
|
|
|
|
setTimeout(() => {
|
|
|
|
copyButton.classList.remove('success');
|
2018-09-22 09:55:33 -04:00
|
|
|
}, 240);
|
2018-06-09 05:41:01 -04:00
|
|
|
});
|
2017-06-17 10:07:55 -04:00
|
|
|
}
|
|
|
|
|
2017-07-01 08:23:46 -04:00
|
|
|
/**
|
|
|
|
* Search for a codemirror code based off a user suggestion
|
2019-12-07 11:23:44 -05:00
|
|
|
* @param {String} suggestion
|
|
|
|
* @param {String} content
|
2017-07-01 08:23:46 -04:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2019-12-07 11:23:44 -05:00
|
|
|
function getMode(suggestion, content) {
|
2017-07-01 08:23:46 -04:00
|
|
|
suggestion = suggestion.trim().replace(/^\./g, '').toLowerCase();
|
2019-12-07 11:23:44 -05:00
|
|
|
|
|
|
|
const modeMapType = typeof modeMap[suggestion];
|
|
|
|
|
|
|
|
if (modeMapType === 'undefined') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modeMapType === 'function') {
|
|
|
|
return modeMap[suggestion](content);
|
|
|
|
}
|
|
|
|
|
|
|
|
return modeMap[suggestion];
|
2017-07-01 08:23:46 -04:00
|
|
|
}
|
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Ge the theme to use for CodeMirror instances.
|
|
|
|
* @returns {*|string}
|
|
|
|
*/
|
|
|
|
function getTheme() {
|
2020-04-10 17:38:29 -04:00
|
|
|
const darkMode = document.documentElement.classList.contains('dark-mode');
|
|
|
|
return window.codeTheme || (darkMode ? 'darcula' : 'default');
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-05-28 08:16:21 -04:00
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Create a CodeMirror instance for showing inside the WYSIWYG editor.
|
|
|
|
* Manages a textarea element to hold code content.
|
2022-02-09 14:24:27 -05:00
|
|
|
* @param {HTMLElement} cmContainer
|
|
|
|
* @param {String} content
|
|
|
|
* @param {String} language
|
2017-09-02 08:34:37 -04:00
|
|
|
* @returns {{wrap: Element, editor: *}}
|
|
|
|
*/
|
2022-02-09 14:24:27 -05:00
|
|
|
export function wysiwygView(cmContainer, content, language) {
|
|
|
|
return CodeMirror(cmContainer, {
|
2017-06-17 10:07:55 -04:00
|
|
|
value: content,
|
2022-02-09 14:24:27 -05:00
|
|
|
mode: getMode(language, content),
|
2017-06-17 10:07:55 -04:00
|
|
|
lineNumbers: true,
|
2019-10-17 09:41:39 -04:00
|
|
|
lineWrapping: false,
|
2017-09-02 08:34:37 -04:00
|
|
|
theme: getTheme(),
|
2017-06-17 10:07:55 -04:00
|
|
|
readOnly: true
|
|
|
|
});
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-05-28 08:16:21 -04:00
|
|
|
|
2022-01-04 06:54:24 -05:00
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Create a CodeMirror instance to show in the WYSIWYG pop-up editor
|
|
|
|
* @param {HTMLElement} elem
|
|
|
|
* @param {String} modeSuggestion
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2022-02-08 06:10:01 -05:00
|
|
|
export function popupEditor(elem, modeSuggestion) {
|
2019-12-07 11:23:44 -05:00
|
|
|
const content = elem.textContent;
|
2017-07-01 08:23:46 -04:00
|
|
|
|
|
|
|
return CodeMirror(function(elt) {
|
|
|
|
elem.parentNode.insertBefore(elt, elem);
|
|
|
|
elem.style.display = 'none';
|
|
|
|
}, {
|
|
|
|
value: content,
|
2019-12-07 11:23:44 -05:00
|
|
|
mode: getMode(modeSuggestion, content),
|
2017-07-01 08:23:46 -04:00
|
|
|
lineNumbers: true,
|
2019-10-17 09:41:39 -04:00
|
|
|
lineWrapping: false,
|
2019-08-15 00:45:48 -04:00
|
|
|
theme: getTheme()
|
2017-07-01 08:23:46 -04:00
|
|
|
});
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-07-01 08:23:46 -04:00
|
|
|
|
2022-05-17 12:39:31 -04:00
|
|
|
/**
|
|
|
|
* Create an inline editor to replace the given textarea.
|
|
|
|
* @param {HTMLTextAreaElement} textArea
|
|
|
|
* @param {String} mode
|
|
|
|
* @returns {CodeMirror3}
|
|
|
|
*/
|
|
|
|
export function inlineEditor(textArea, mode) {
|
|
|
|
return CodeMirror.fromTextArea(textArea, {
|
|
|
|
mode: getMode(mode, textArea.value),
|
|
|
|
lineNumbers: true,
|
|
|
|
lineWrapping: false,
|
|
|
|
theme: getTheme(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Set the mode of a codemirror instance.
|
|
|
|
* @param cmInstance
|
|
|
|
* @param modeSuggestion
|
|
|
|
*/
|
2022-02-08 06:10:01 -05:00
|
|
|
export function setMode(cmInstance, modeSuggestion, content) {
|
2019-12-07 11:23:44 -05:00
|
|
|
cmInstance.setOption('mode', getMode(modeSuggestion, content));
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the content of a cm instance.
|
|
|
|
* @param cmInstance
|
|
|
|
* @param codeContent
|
|
|
|
*/
|
2022-02-08 06:10:01 -05:00
|
|
|
export function setContent(cmInstance, codeContent) {
|
2017-07-01 08:23:46 -04:00
|
|
|
cmInstance.setValue(codeContent);
|
|
|
|
setTimeout(() => {
|
2019-12-07 11:54:34 -05:00
|
|
|
updateLayout(cmInstance);
|
2017-07-01 08:23:46 -04:00
|
|
|
}, 10);
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-06-17 10:07:55 -04:00
|
|
|
|
2019-12-07 11:54:34 -05:00
|
|
|
/**
|
|
|
|
* Update the layout (codemirror refresh) of a cm instance.
|
|
|
|
* @param cmInstance
|
|
|
|
*/
|
2022-02-08 06:10:01 -05:00
|
|
|
export function updateLayout(cmInstance) {
|
2019-12-07 11:54:34 -05:00
|
|
|
cmInstance.refresh();
|
|
|
|
}
|
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
2019-10-16 13:01:35 -04:00
|
|
|
* Get a CodeMirror instance to use for the markdown editor.
|
2017-09-02 08:34:37 -04:00
|
|
|
* @param {HTMLElement} elem
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2022-02-08 06:10:01 -05:00
|
|
|
export function markdownEditor(elem) {
|
2019-10-16 13:01:35 -04:00
|
|
|
const content = elem.textContent;
|
|
|
|
const config = {
|
2017-05-28 11:02:46 -04:00
|
|
|
value: content,
|
2017-07-01 08:23:46 -04:00
|
|
|
mode: "markdown",
|
2017-05-28 11:02:46 -04:00
|
|
|
lineNumbers: true,
|
2018-09-22 18:24:51 -04:00
|
|
|
lineWrapping: true,
|
2019-08-15 00:45:48 -04:00
|
|
|
theme: getTheme(),
|
2018-09-22 18:24:51 -04:00
|
|
|
scrollPastEnd: true,
|
2019-10-16 13:01:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
window.$events.emitPublic(elem, 'editor-markdown-cm::pre-init', {config});
|
|
|
|
|
|
|
|
return CodeMirror(function (elt) {
|
|
|
|
elem.parentNode.insertBefore(elt, elem);
|
|
|
|
elem.style.display = 'none';
|
|
|
|
}, config);
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-05-28 11:02:46 -04:00
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
2022-02-08 06:10:01 -05:00
|
|
|
* Get the 'meta' key dependent on the user's system.
|
2017-09-02 08:34:37 -04:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-02-08 06:10:01 -05:00
|
|
|
export function getMetaKey() {
|
2017-07-02 14:35:13 -04:00
|
|
|
let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
|
|
|
|
return mac ? "Cmd" : "Ctrl";
|
2022-02-08 06:10:01 -05:00
|
|
|
}
|