2017-05-28 08:16:21 -04:00
|
|
|
require('codemirror/mode/css/css');
|
|
|
|
require('codemirror/mode/clike/clike');
|
2017-07-01 11:10:52 -04:00
|
|
|
require('codemirror/mode/diff/diff');
|
2017-05-28 08:16:21 -04:00
|
|
|
require('codemirror/mode/go/go');
|
|
|
|
require('codemirror/mode/htmlmixed/htmlmixed');
|
|
|
|
require('codemirror/mode/javascript/javascript');
|
|
|
|
require('codemirror/mode/markdown/markdown');
|
|
|
|
require('codemirror/mode/nginx/nginx');
|
|
|
|
require('codemirror/mode/php/php');
|
|
|
|
require('codemirror/mode/powershell/powershell');
|
|
|
|
require('codemirror/mode/python/python');
|
|
|
|
require('codemirror/mode/ruby/ruby');
|
|
|
|
require('codemirror/mode/shell/shell');
|
|
|
|
require('codemirror/mode/sql/sql');
|
|
|
|
require('codemirror/mode/toml/toml');
|
|
|
|
require('codemirror/mode/xml/xml');
|
|
|
|
require('codemirror/mode/yaml/yaml');
|
|
|
|
|
|
|
|
const CodeMirror = require('codemirror');
|
|
|
|
|
2017-06-17 07:41:18 -04:00
|
|
|
const modeMap = {
|
|
|
|
css: 'css',
|
|
|
|
c: 'clike',
|
|
|
|
java: 'clike',
|
|
|
|
scala: 'clike',
|
|
|
|
kotlin: 'clike',
|
|
|
|
'c++': 'clike',
|
|
|
|
'c#': 'clike',
|
|
|
|
csharp: 'clike',
|
2017-07-01 11:10:52 -04:00
|
|
|
diff: 'diff',
|
2017-06-17 07:41:18 -04:00
|
|
|
go: 'go',
|
|
|
|
html: 'htmlmixed',
|
|
|
|
javascript: 'javascript',
|
|
|
|
json: {name: 'javascript', json: true},
|
|
|
|
js: 'javascript',
|
|
|
|
php: 'php',
|
|
|
|
md: 'markdown',
|
|
|
|
mdown: 'markdown',
|
|
|
|
markdown: 'markdown',
|
|
|
|
nginx: 'nginx',
|
|
|
|
powershell: 'powershell',
|
|
|
|
py: 'python',
|
|
|
|
python: 'python',
|
|
|
|
ruby: 'ruby',
|
|
|
|
rb: 'ruby',
|
|
|
|
shell: 'shell',
|
2017-07-01 11:10:52 -04:00
|
|
|
sh: 'shell',
|
2017-06-17 07:41:18 -04:00
|
|
|
bash: 'shell',
|
|
|
|
toml: 'toml',
|
|
|
|
sql: 'sql',
|
|
|
|
xml: 'xml',
|
|
|
|
yaml: 'yaml',
|
|
|
|
yml: 'yaml',
|
|
|
|
};
|
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Highlight pre elements on a page
|
|
|
|
*/
|
|
|
|
function highlight() {
|
2017-09-10 11:14:04 -04:00
|
|
|
let codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
|
2017-05-28 08:16:21 -04:00
|
|
|
for (let i = 0; i < codeBlocks.length; i++) {
|
2017-06-17 10:07:55 -04:00
|
|
|
highlightElem(codeBlocks[i]);
|
|
|
|
}
|
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) {
|
|
|
|
let innerCodeElem = elem.querySelector('code[class^=language-]');
|
|
|
|
let mode = '';
|
|
|
|
if (innerCodeElem !== null) {
|
|
|
|
let langName = innerCodeElem.className.replace('language-', '');
|
2017-07-01 08:23:46 -04:00
|
|
|
mode = getMode(langName);
|
2017-05-28 08:16:21 -04:00
|
|
|
}
|
2017-06-17 10:07:55 -04:00
|
|
|
elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
|
2017-09-10 11:14:04 -04:00
|
|
|
let content = elem.textContent.trim();
|
2017-06-17 10:07:55 -04:00
|
|
|
|
|
|
|
CodeMirror(function(elt) {
|
|
|
|
elem.parentNode.replaceChild(elt, elem);
|
|
|
|
}, {
|
|
|
|
value: content,
|
|
|
|
mode: mode,
|
|
|
|
lineNumbers: true,
|
2017-09-02 08:34:37 -04:00
|
|
|
theme: getTheme(),
|
2017-06-17 10:07:55 -04:00
|
|
|
readOnly: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-01 08:23:46 -04:00
|
|
|
/**
|
|
|
|
* Search for a codemirror code based off a user suggestion
|
|
|
|
* @param suggestion
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function getMode(suggestion) {
|
|
|
|
suggestion = suggestion.trim().replace(/^\./g, '').toLowerCase();
|
|
|
|
return (typeof modeMap[suggestion] !== 'undefined') ? modeMap[suggestion] : '';
|
|
|
|
}
|
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Ge the theme to use for CodeMirror instances.
|
|
|
|
* @returns {*|string}
|
|
|
|
*/
|
|
|
|
function getTheme() {
|
|
|
|
return window.codeTheme || 'base16-light';
|
|
|
|
}
|
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.
|
|
|
|
* @param {HTMLElement} elem
|
|
|
|
* @returns {{wrap: Element, editor: *}}
|
|
|
|
*/
|
|
|
|
function wysiwygView(elem) {
|
2017-06-17 10:07:55 -04:00
|
|
|
let doc = elem.ownerDocument;
|
2017-07-01 08:23:46 -04:00
|
|
|
let codeElem = elem.querySelector('code');
|
|
|
|
|
|
|
|
let lang = (elem.className || '').replace('language-', '');
|
|
|
|
if (lang === '' && codeElem) {
|
|
|
|
lang = (codeElem.className || '').replace('language-', '')
|
|
|
|
}
|
|
|
|
|
2017-06-17 10:07:55 -04:00
|
|
|
elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
|
|
|
|
let content = elem.textContent;
|
|
|
|
let newWrap = doc.createElement('div');
|
|
|
|
let newTextArea = doc.createElement('textarea');
|
|
|
|
|
|
|
|
newWrap.className = 'CodeMirrorContainer';
|
2017-07-01 08:23:46 -04:00
|
|
|
newWrap.setAttribute('data-lang', lang);
|
2017-06-17 10:07:55 -04:00
|
|
|
newTextArea.style.display = 'none';
|
|
|
|
elem.parentNode.replaceChild(newWrap, elem);
|
|
|
|
|
|
|
|
newWrap.appendChild(newTextArea);
|
|
|
|
newWrap.contentEditable = false;
|
|
|
|
newTextArea.textContent = content;
|
|
|
|
|
|
|
|
let cm = CodeMirror(function(elt) {
|
|
|
|
newWrap.appendChild(elt);
|
|
|
|
}, {
|
|
|
|
value: content,
|
2017-07-01 08:23:46 -04:00
|
|
|
mode: getMode(lang),
|
2017-06-17 10:07:55 -04:00
|
|
|
lineNumbers: true,
|
2017-09-02 08:34:37 -04:00
|
|
|
theme: getTheme(),
|
2017-06-17 10:07:55 -04:00
|
|
|
readOnly: true
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
cm.refresh();
|
|
|
|
}, 300);
|
2017-07-01 08:23:46 -04:00
|
|
|
return {wrap: newWrap, editor: cm};
|
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 to show in the WYSIWYG pop-up editor
|
|
|
|
* @param {HTMLElement} elem
|
|
|
|
* @param {String} modeSuggestion
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function popupEditor(elem, modeSuggestion) {
|
2017-07-01 08:23:46 -04:00
|
|
|
let content = elem.textContent;
|
|
|
|
|
|
|
|
return CodeMirror(function(elt) {
|
|
|
|
elem.parentNode.insertBefore(elt, elem);
|
|
|
|
elem.style.display = 'none';
|
|
|
|
}, {
|
|
|
|
value: content,
|
|
|
|
mode: getMode(modeSuggestion),
|
|
|
|
lineNumbers: true,
|
2017-09-02 08:34:37 -04:00
|
|
|
theme: getTheme(),
|
2017-07-01 08:23:46 -04:00
|
|
|
lineWrapping: true
|
|
|
|
});
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-07-01 08:23:46 -04:00
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Set the mode of a codemirror instance.
|
|
|
|
* @param cmInstance
|
|
|
|
* @param modeSuggestion
|
|
|
|
*/
|
|
|
|
function setMode(cmInstance, modeSuggestion) {
|
2017-07-01 08:23:46 -04:00
|
|
|
cmInstance.setOption('mode', getMode(modeSuggestion));
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the content of a cm instance.
|
|
|
|
* @param cmInstance
|
|
|
|
* @param codeContent
|
|
|
|
*/
|
|
|
|
function setContent(cmInstance, codeContent) {
|
2017-07-01 08:23:46 -04:00
|
|
|
cmInstance.setValue(codeContent);
|
|
|
|
setTimeout(() => {
|
|
|
|
cmInstance.refresh();
|
|
|
|
}, 10);
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-06-17 10:07:55 -04:00
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Get a CodeMirror instace to use for the markdown editor.
|
|
|
|
* @param {HTMLElement} elem
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function markdownEditor(elem) {
|
2017-05-28 11:02:46 -04:00
|
|
|
let content = elem.textContent;
|
|
|
|
|
2017-07-01 08:23:46 -04:00
|
|
|
return CodeMirror(function (elt) {
|
2017-05-28 11:02:46 -04:00
|
|
|
elem.parentNode.insertBefore(elt, elem);
|
|
|
|
elem.style.display = 'none';
|
|
|
|
}, {
|
|
|
|
value: content,
|
2017-07-01 08:23:46 -04:00
|
|
|
mode: "markdown",
|
2017-05-28 11:02:46 -04:00
|
|
|
lineNumbers: true,
|
2017-09-02 08:34:37 -04:00
|
|
|
theme: getTheme(),
|
2017-05-28 11:02:46 -04:00
|
|
|
lineWrapping: true
|
|
|
|
});
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-05-28 11:02:46 -04:00
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
/**
|
|
|
|
* Get the 'meta' key dependant on the user's system.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function getMetaKey() {
|
2017-07-02 14:35:13 -04:00
|
|
|
let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
|
|
|
|
return mac ? "Cmd" : "Ctrl";
|
2017-09-02 08:34:37 -04:00
|
|
|
}
|
2017-05-28 11:02:46 -04:00
|
|
|
|
2017-09-02 08:34:37 -04:00
|
|
|
module.exports = {
|
|
|
|
highlight: highlight,
|
|
|
|
highlightElem: highlightElem,
|
|
|
|
wysiwygView: wysiwygView,
|
|
|
|
popupEditor: popupEditor,
|
|
|
|
setMode: setMode,
|
|
|
|
setContent: setContent,
|
|
|
|
markdownEditor: markdownEditor,
|
|
|
|
getMetaKey: getMetaKey,
|
|
|
|
};
|