2018-11-10 05:17:35 +08:00
|
|
|
import CodeMirror from "codemirror";
|
2018-11-11 00:08:33 +08:00
|
|
|
import Clipboard from "clipboard/dist/clipboard.min";
|
2018-09-23 06:24:51 +08:00
|
|
|
|
|
|
|
// Modes
|
2018-11-10 05:17:35 +08:00
|
|
|
import 'codemirror/mode/css/css';
|
|
|
|
import 'codemirror/mode/clike/clike';
|
|
|
|
import 'codemirror/mode/diff/diff';
|
|
|
|
import 'codemirror/mode/go/go';
|
|
|
|
import 'codemirror/mode/htmlmixed/htmlmixed';
|
|
|
|
import 'codemirror/mode/javascript/javascript';
|
2019-03-22 16:42:02 +08:00
|
|
|
import 'codemirror/mode/julia/julia';
|
2019-03-02 16:52:14 +08:00
|
|
|
import 'codemirror/mode/lua/lua';
|
2019-03-22 16:42:02 +08:00
|
|
|
import 'codemirror/mode/haskell/haskell';
|
2018-11-10 05:17:35 +08:00
|
|
|
import 'codemirror/mode/markdown/markdown';
|
2019-03-22 16:42:02 +08:00
|
|
|
import 'codemirror/mode/mllike/mllike';
|
2018-11-10 05:17:35 +08:00
|
|
|
import 'codemirror/mode/nginx/nginx';
|
|
|
|
import 'codemirror/mode/php/php';
|
|
|
|
import 'codemirror/mode/powershell/powershell';
|
|
|
|
import 'codemirror/mode/python/python';
|
|
|
|
import 'codemirror/mode/ruby/ruby';
|
2019-03-22 16:42:02 +08:00
|
|
|
import 'codemirror/mode/rust/rust';
|
2018-11-10 05:17:35 +08:00
|
|
|
import 'codemirror/mode/shell/shell';
|
|
|
|
import 'codemirror/mode/sql/sql';
|
|
|
|
import 'codemirror/mode/toml/toml';
|
|
|
|
import 'codemirror/mode/xml/xml';
|
|
|
|
import 'codemirror/mode/yaml/yaml';
|
2017-05-28 20:16:21 +08:00
|
|
|
|
2018-09-23 06:24:51 +08:00
|
|
|
// Addons
|
2018-11-10 05:17:35 +08:00
|
|
|
import 'codemirror/addon/scroll/scrollpastend';
|
2017-05-28 20:16:21 +08:00
|
|
|
|
2017-06-17 19:41:18 +08:00
|
|
|
const modeMap = {
|
|
|
|
css: 'css',
|
2018-05-20 16:32:15 +08: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',
|
2017-07-01 23:10:52 +08:00
|
|
|
diff: 'diff',
|
2017-06-17 19:41:18 +08:00
|
|
|
go: 'go',
|
2019-03-22 16:42:02 +08:00
|
|
|
haskell: 'haskell',
|
|
|
|
hs: 'haskell',
|
2017-06-17 19:41:18 +08:00
|
|
|
html: 'htmlmixed',
|
|
|
|
javascript: 'javascript',
|
|
|
|
json: {name: 'javascript', json: true},
|
|
|
|
js: 'javascript',
|
2019-03-22 16:42:02 +08:00
|
|
|
jl: 'julia',
|
|
|
|
julia: 'julia',
|
2019-03-02 16:52:14 +08:00
|
|
|
lua: 'lua',
|
2017-06-17 19:41:18 +08:00
|
|
|
md: 'markdown',
|
|
|
|
mdown: 'markdown',
|
|
|
|
markdown: 'markdown',
|
2019-03-22 16:42:02 +08:00
|
|
|
ml: 'mllike',
|
2017-06-17 19:41:18 +08:00
|
|
|
nginx: 'nginx',
|
|
|
|
powershell: 'powershell',
|
2019-03-22 16:42:02 +08:00
|
|
|
ocaml: 'mllike',
|
2019-03-02 16:52:14 +08:00
|
|
|
php: 'php',
|
2017-06-17 19:41:18 +08:00
|
|
|
py: 'python',
|
|
|
|
python: 'python',
|
|
|
|
ruby: 'ruby',
|
2019-03-22 16:42:02 +08:00
|
|
|
rust: 'rust',
|
2017-06-17 19:41:18 +08:00
|
|
|
rb: 'ruby',
|
2019-03-22 16:42:02 +08:00
|
|
|
rs: 'rust',
|
2017-06-17 19:41:18 +08:00
|
|
|
shell: 'shell',
|
2017-07-01 23:10:52 +08:00
|
|
|
sh: 'shell',
|
2017-06-17 19:41:18 +08:00
|
|
|
bash: 'shell',
|
|
|
|
toml: 'toml',
|
2018-12-16 23:38:49 +08:00
|
|
|
sql: 'text/x-sql',
|
2017-06-17 19:41:18 +08:00
|
|
|
xml: 'xml',
|
|
|
|
yaml: 'yaml',
|
|
|
|
yml: 'yaml',
|
|
|
|
};
|
|
|
|
|
2017-09-02 20:34:37 +08:00
|
|
|
/**
|
|
|
|
* Highlight pre elements on a page
|
|
|
|
*/
|
|
|
|
function highlight() {
|
2017-09-10 23:14:04 +08:00
|
|
|
let codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
|
2017-05-28 20:16:21 +08:00
|
|
|
for (let i = 0; i < codeBlocks.length; i++) {
|
2017-06-17 22:07:55 +08:00
|
|
|
highlightElem(codeBlocks[i]);
|
|
|
|
}
|
2017-09-02 20:34:37 +08:00
|
|
|
}
|
2017-05-28 20:16:21 +08:00
|
|
|
|
2017-09-02 20:34:37 +08:00
|
|
|
/**
|
|
|
|
* Add code highlighting to a single element.
|
|
|
|
* @param {HTMLElement} elem
|
|
|
|
*/
|
2017-06-17 22:07:55 +08: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 20:23:46 +08:00
|
|
|
mode = getMode(langName);
|
2017-05-28 20:16:21 +08:00
|
|
|
}
|
2017-06-17 22:07:55 +08:00
|
|
|
elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
|
2017-09-10 23:14:04 +08:00
|
|
|
let content = elem.textContent.trim();
|
2017-06-17 22:07:55 +08:00
|
|
|
|
2018-06-09 17:41:01 +08:00
|
|
|
let cm = CodeMirror(function(elt) {
|
2017-06-17 22:07:55 +08:00
|
|
|
elem.parentNode.replaceChild(elt, elem);
|
|
|
|
}, {
|
|
|
|
value: content,
|
|
|
|
mode: mode,
|
|
|
|
lineNumbers: true,
|
2017-09-02 20:34:37 +08:00
|
|
|
theme: getTheme(),
|
2017-06-17 22:07:55 +08:00
|
|
|
readOnly: true
|
|
|
|
});
|
2018-06-09 17:41:01 +08: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 21:55:33 +08:00
|
|
|
}, 240);
|
2018-06-09 17:41:01 +08:00
|
|
|
});
|
2017-06-17 22:07:55 +08:00
|
|
|
}
|
|
|
|
|
2017-07-01 20:23:46 +08: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 20:34:37 +08:00
|
|
|
/**
|
|
|
|
* Ge the theme to use for CodeMirror instances.
|
|
|
|
* @returns {*|string}
|
|
|
|
*/
|
|
|
|
function getTheme() {
|
|
|
|
return window.codeTheme || 'base16-light';
|
|
|
|
}
|
2017-05-28 20:16:21 +08:00
|
|
|
|
2017-09-02 20:34:37 +08: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 22:07:55 +08:00
|
|
|
let doc = elem.ownerDocument;
|
2017-07-01 20:23:46 +08:00
|
|
|
let codeElem = elem.querySelector('code');
|
|
|
|
|
|
|
|
let lang = (elem.className || '').replace('language-', '');
|
|
|
|
if (lang === '' && codeElem) {
|
|
|
|
lang = (codeElem.className || '').replace('language-', '')
|
|
|
|
}
|
|
|
|
|
2017-06-17 22:07:55 +08: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 20:23:46 +08:00
|
|
|
newWrap.setAttribute('data-lang', lang);
|
2018-09-22 20:18:26 +08:00
|
|
|
newWrap.setAttribute('dir', 'ltr');
|
2017-06-17 22:07:55 +08: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 20:23:46 +08:00
|
|
|
mode: getMode(lang),
|
2017-06-17 22:07:55 +08:00
|
|
|
lineNumbers: true,
|
2017-09-02 20:34:37 +08:00
|
|
|
theme: getTheme(),
|
2017-06-17 22:07:55 +08:00
|
|
|
readOnly: true
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
cm.refresh();
|
|
|
|
}, 300);
|
2017-07-01 20:23:46 +08:00
|
|
|
return {wrap: newWrap, editor: cm};
|
2017-09-02 20:34:37 +08:00
|
|
|
}
|
2017-05-28 20:16:21 +08:00
|
|
|
|
2017-09-02 20:34:37 +08: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 20:23:46 +08: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 20:34:37 +08:00
|
|
|
theme: getTheme(),
|
2017-07-01 20:23:46 +08:00
|
|
|
lineWrapping: true
|
|
|
|
});
|
2017-09-02 20:34:37 +08:00
|
|
|
}
|
2017-07-01 20:23:46 +08:00
|
|
|
|
2017-09-02 20:34:37 +08:00
|
|
|
/**
|
|
|
|
* Set the mode of a codemirror instance.
|
|
|
|
* @param cmInstance
|
|
|
|
* @param modeSuggestion
|
|
|
|
*/
|
|
|
|
function setMode(cmInstance, modeSuggestion) {
|
2017-07-01 20:23:46 +08:00
|
|
|
cmInstance.setOption('mode', getMode(modeSuggestion));
|
2017-09-02 20:34:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the content of a cm instance.
|
|
|
|
* @param cmInstance
|
|
|
|
* @param codeContent
|
|
|
|
*/
|
|
|
|
function setContent(cmInstance, codeContent) {
|
2017-07-01 20:23:46 +08:00
|
|
|
cmInstance.setValue(codeContent);
|
|
|
|
setTimeout(() => {
|
|
|
|
cmInstance.refresh();
|
|
|
|
}, 10);
|
2017-09-02 20:34:37 +08:00
|
|
|
}
|
2017-06-17 22:07:55 +08:00
|
|
|
|
2017-09-02 20:34:37 +08:00
|
|
|
/**
|
|
|
|
* Get a CodeMirror instace to use for the markdown editor.
|
|
|
|
* @param {HTMLElement} elem
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function markdownEditor(elem) {
|
2017-05-28 23:02:46 +08:00
|
|
|
let content = elem.textContent;
|
|
|
|
|
2017-07-01 20:23:46 +08:00
|
|
|
return CodeMirror(function (elt) {
|
2017-05-28 23:02:46 +08:00
|
|
|
elem.parentNode.insertBefore(elt, elem);
|
|
|
|
elem.style.display = 'none';
|
|
|
|
}, {
|
|
|
|
value: content,
|
2017-07-01 20:23:46 +08:00
|
|
|
mode: "markdown",
|
2017-05-28 23:02:46 +08:00
|
|
|
lineNumbers: true,
|
2017-09-02 20:34:37 +08:00
|
|
|
theme: getTheme(),
|
2018-09-23 06:24:51 +08:00
|
|
|
lineWrapping: true,
|
|
|
|
scrollPastEnd: true,
|
2017-05-28 23:02:46 +08:00
|
|
|
});
|
2017-09-02 20:34:37 +08:00
|
|
|
}
|
2017-05-28 23:02:46 +08:00
|
|
|
|
2017-09-02 20:34:37 +08:00
|
|
|
/**
|
|
|
|
* Get the 'meta' key dependant on the user's system.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function getMetaKey() {
|
2017-07-03 02:35:13 +08:00
|
|
|
let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
|
|
|
|
return mac ? "Cmd" : "Ctrl";
|
2017-09-02 20:34:37 +08:00
|
|
|
}
|
2017-05-28 23:02:46 +08:00
|
|
|
|
2018-11-10 05:17:35 +08:00
|
|
|
export default {
|
2017-09-02 20:34:37 +08:00
|
|
|
highlight: highlight,
|
|
|
|
wysiwygView: wysiwygView,
|
|
|
|
popupEditor: popupEditor,
|
|
|
|
setMode: setMode,
|
|
|
|
setContent: setContent,
|
|
|
|
markdownEditor: markdownEditor,
|
|
|
|
getMetaKey: getMetaKey,
|
2019-03-22 16:42:02 +08:00
|
|
|
};
|