web编辑器codemirror使用

web编辑器codemirror使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link href="https://cdn.bootcss.com/codemirror/5.28.0/theme/seti.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/codemirror/5.28.0/theme/solarized.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/codemirror/5.28.0/codemirror.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/codemirror/5.28.0/codemirror.min.js"></script>
<script src="https://cdn.bootcss.com/codemirror/5.28.0/mode/clike/clike.min.js"></script>
</head>
<body>
<textarea id="myCode">#input your Code here#</textarea>
<script type="text/javascript">
var editor = CodeMirror.fromTextArea(document.getElementById("myCode"), {
lineNumbers: true, /* 定义是否显示行号 */
mode: "javascript", /* 定义语法的类型,如果是html则为:text/html */
theme: "seti", /* 定义主题 */
//value: "设置显示的内容",
//lineSeparator: 'CRLF', /*指定换行符*/
indentUnit: 4, /*设置缩进的字符数,默认为2*/
smartIndent: true, /*自动缩进,默认为true*/
//tabSize: 8, /*tab字符的宽度,默认为4*/
lineWiseCopyCut: false, /*true时,如果当前没有选中文本,会自动选中当前行*/
dragDrop: false, /*是否可以被拖拽*/
autofocus: true
});
</script>
</body>
</html>

加入选择主题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link href="https://cdn.bootcss.com/codemirror/5.28.0/theme/seti.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/codemirror/5.28.0/theme/solarized.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/codemirror/5.28.0/codemirror.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/codemirror/5.28.0/codemirror.min.js"></script>
<script src="https://cdn.bootcss.com/codemirror/5.28.0/mode/clike/clike.min.js"></script>
<link href="https://cdn.bootcss.com/codemirror/5.28.0/theme/base16-light.min.css" rel="stylesheet">
</head>
<body>
<h2>Theme Demo</h2>
<textarea id="jsoncode" name="jsoncode" disabled="disabled" readonly="readonly">#code#</textarea>
<p>选择主题:
<select id="select" onchange="selectTheme()">
<option selected>default</option>
<option>solarized</option>
<option>seti</option>
<option>base16-light</option>
</select>
</p>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("jsoncode"), {
lineNumbers: true, /* 定义是否显示行号 */
lineWrapping: true, /*自动换行*/
styleActiveLine: true,
matchBrackets: true,
readOnly: true,
theme: "solarized" /* 定义主题 */
});
var input = document.getElementById("select");
function selectTheme() {
var theme = input.options[input.selectedIndex].textContent;
editor.setOption("theme", theme);
location.hash = "#" + theme;
}
var choice = (location.hash && location.hash.slice(1)) ||
(document.location.search &&
decodeURIComponent(document.location.search.slice(1)));
if (choice) {
input.value = choice;
editor.setOption("theme", choice);
}
CodeMirror.on(window, "hashchange", function() {
var theme = location.hash.slice(1);
if (theme) { input.value = theme; selectTheme(); }
});
</script>
</body>
</html>

支持语言列表

http://codemirror.net/mode/index.html

文章作者: kyren
文章链接: http://huluo666.github.io/2016/05/04/web编辑器codemirror使用/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Kyren's Blog