Skip to content

Commit fe97b1a

Browse files
1.3.8 发布
可集成 highlight 插件实现代码高亮
1 parent 9d560b6 commit fe97b1a

35 files changed

Lines changed: 794 additions & 68 deletions

‎dist/js/wangEditor-1.3.7.min.js‎

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 93 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ $.extend($E, {
300300
//视频
301301
'videoEmbed': '<embed src="{src}" allowFullScreen="true" quality="high" width="{width}" height="{height}" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>',
302302
//代码块
303-
'codePre': '<pre style="border:1px solid #ccc; background-color: #f5f5f5; padding: 10px; margin: 5px 0px; line-height: 1.4; font-size: 0.8em; font-family: Menlo, Monaco, Consolas; border-radius: 4px; -moz-border-radius: 4px; -webkit-border-radius: 4px;">{content}</pre><p><br></p>'
303+
'codePre': '<pre style="border:1px solid #ccc; background-color: #f5f5f5; padding: 10px; margin: 5px 0px; line-height: 1.4; font-size: 0.8em; font-family: Menlo, Monaco, Consolas; border-radius: 4px; -moz-border-radius: 4px; -webkit-border-radius: 4px;"><code>{content}</code></pre><p><br></p>',
304+
//代码块(highlight插件)
305+
'codePreWidthHightLight': '<pre><code class="{lang}">{content}</code></pre>'
304306
},
305307

306308
//表情配置(1.gif, 2.gif, 3.gif ... 100.gif)
@@ -1034,6 +1036,11 @@ $.extend($E.fn, {
10341036
//获取editor Container
10351037
'getEditorContainer': function(){
10361038
return this.$editorContainer;
1039+
},
1040+
1041+
//让编辑器失去焦点
1042+
'blur': function(){
1043+
this.$txt.blur();
10371044
}
10381045
});
10391046
$.extend($E.fn, {
@@ -1909,28 +1916,93 @@ $.extend($E.fn, {
19091916
'cssClass': 'icon-wangEditor-terminal',
19101917
'modal': function(editor){
19111918
var txtId = $E.getUniqeId(),
1919+
selectId = $E.getUniqeId(),
19121920
btnId = $E.getUniqeId(),
1913-
content = '<p>请输入代码:</p>' +
1921+
content = '<p>请输入代码:{selectLangs}</p>' + // selectLangs 待填充语言列表下拉框
19141922
'<div><textarea id="' + txtId + '" style="width:100%; height:100px;"></textarea></div>' +
19151923
'<button id="' + btnId + '" type="button" class="wangEditor-modal-btn">插入</button>',
1916-
$simpleCode_modal = $(
1917-
$E.htmlTemplates.modal.replace('{content}', content)
1918-
);
1924+
selectLangs = '<select id="' + selectId + '">{content}</select>', // content 待填充语言列表
1925+
$simpleCode_modal;
1926+
1927+
//获取语言列表(没有则返回 false)
1928+
var hashljs = false, //是否有高亮插件的标记。默认false
1929+
hljs = window.hljs, //引用了 highlightJS 插件之后才能得到
1930+
listLanguages = hljs && hljs.listLanguages,
1931+
langs = typeof listLanguages === 'function' && listLanguages(),
1932+
langsStr = '';
1933+
1934+
if(langs && typeof langs === 'object' && langs.length){
1935+
//确定 langs 是数组
1936+
1937+
//循环数组,获取语言列表
1938+
$.each(langs, function(key, lang){
1939+
langsStr = langsStr +
1940+
'<option value="' + lang + '">' +
1941+
lang +
1942+
'</option>';
1943+
});
1944+
1945+
//语言下拉菜单
1946+
selectLangs = selectLangs.replace('{content}', langsStr); //插入语言选项
1947+
content = content.replace('{selectLangs}', selectLangs); //插入下拉菜单
1948+
1949+
//标记为有高亮插件
1950+
hashljs = true;
19191951

1952+
}else{
1953+
//无高亮插件,则不显示语言下拉菜单
1954+
content = content.replace('{selectLangs}', '');
1955+
}
1956+
1957+
//生成modal,填充内容
1958+
$simpleCode_modal = $(
1959+
$E.htmlTemplates.modal.replace('{content}', content)
1960+
);
1961+
1962+
//插入代码
19201963
$simpleCode_modal.find('#' + btnId).click(function(e){
1964+
//获取并处理代码块
19211965
var code = $.trim($('#' + txtId).val()),
1966+
//callback回调事件
19221967
simpleCode_callback = function(){
1968+
//清空代码区域
19231969
$('#' + txtId).val('');
1970+
1971+
//高亮显示代码
1972+
if( hashljs ){
1973+
$('pre code').each(function(i, block) {
1974+
// hljs 上面已经定义
1975+
hljs.highlightBlock(block);
1976+
});
1977+
}
19241978
};
19251979

1980+
//获取语言
1981+
var lang;
1982+
if(hashljs){
1983+
lang = $('#' + selectId).val();
1984+
}
1985+
19261986
if(code && code !== ''){
19271987
//替换特殊字符
19281988
code = code.replace(/&/gm, '&amp;')
19291989
.replace(/</gm, '&lt;')
1930-
.replace(/>/gm, '&gt;')
1931-
.replace(/\n/gm, '<br>')
1932-
.replace(/\s{1}/gm, '&nbsp;');
1933-
code = $E.htmlTemplates.codePre.replace('{content}', code);
1990+
.replace(/>/gm, '&gt;');
1991+
1992+
//说明:之前的代码,集成highlight时注释,暂且保留
1993+
// .replace(/\n/gm, '<br>')
1994+
// .replace(/\s{1}/gm, '&nbsp;');
1995+
1996+
if(hashljs){
1997+
//高亮代码块
1998+
code = $E.htmlTemplates.codePreWidthHightLight
1999+
.replace('{lang}', lang)
2000+
.replace('{content}', code);
2001+
}else{
2002+
//普通代码块
2003+
code = $E.htmlTemplates.codePre.replace('{content}', code);
2004+
}
2005+
19342006
editor.command(e, 'insertHTML', code, simpleCode_callback);
19352007
}
19362008
});
@@ -2108,13 +2180,23 @@ $.extend($E.fn, {
21082180
'type': 'btn',
21092181
'hotKey': 'ctrl+z', //例如'ctrl+z'/'ctrl,shift+z'/'ctrl,shift,alt+z'/'ctrl,shift,alt,meta+z',支持这四种情况。只有type==='btn'的情况下,才可以使用快捷键
21102182
'cssClass': 'icon-wangEditor-ccw',
2111-
'command': 'commonUndo'
2183+
'command': 'commonUndo',
2184+
'callback': function(editor){
2185+
//撤销时,会发生光标不准确的问题
2186+
//因此,撤销时,让编辑器失去焦点
2187+
editor.blur();
2188+
}
21122189
},
21132190
'redo': {
21142191
'title': '重复',
21152192
'type': 'btn',
21162193
'cssClass': 'icon-wangEditor-cw',
2117-
'command': 'commonRedo'
2194+
'command': 'commonRedo',
2195+
'callback': function(editor){
2196+
//redo时,会发生光标不准确的问题
2197+
//因此,redo时,让编辑器失去焦点
2198+
editor.blur();
2199+
}
21182200
},
21192201
'viewSourceCode': {
21202202
'title': '查看源码',

‎dist/js/wangEditor-1.3.8.min.js‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/docs.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ ul{
7373
}
7474
#container h2{
7575
margin-top: 30px;
76+
margin-bottom: 15px;
77+
border-bottom: 3px solid #999;
7678
}
7779
#container h3{
7880
margin-top: 15px;
81+
margin-bottom: 10px;
82+
border-bottom: 1px solid #ccc;
7983
}
8084
#container .alert{
8185
background-color: yellow;

‎docs/images/highlight1.png‎

195 KB
Loading

‎docs/images/highlight2.png‎

78.3 KB
Loading

‎docs/images/highlight3.png‎

94.2 KB
Loading

‎docs/index.html‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<li><a href="#menu-config">自定义配置菜单</a></li>
2929
<li><a href="#onchange">添加change监听事件</a></li>
3030
<li><a href="#expressions">自定义配置表情图标</a></li>
31+
<li><a href="#highlight">配置代码高亮</a></li>
3132
</ul>
3233
<a href="#upload-img" class="group-title">图片上传</a>
3334
<ul>
@@ -332,6 +333,92 @@ <h3>自定义配置表情图标</h3>
332333
发挥你的想象力,不必拘泥于这一个例子。
333334
</p>
334335
</div>
336+
<div id="highlight">
337+
<h3>配置代码高亮</h3>
338+
<p>
339+
wangEditor自带的代码插入功能,比较简单,也没有高亮功能。
340+
下面介绍一种简单的方法,几分钟即可让wangEditor拥有代码高亮功能
341+
———— wangEditor可以集成
342+
<a href="https://highlightjs.org/" target="_blank">
343+
hightlight
344+
</a>
345+
插件来实现代码高亮。
346+
可查看
347+
<a href="../test/highlightJS/test.html" target="_blank">
348+
在线的demo
349+
</a>
350+
</p>
351+
<p>
352+
<br>
353+
<b>第一步,你需要下载 hightlight 插件</b>
354+
</p>
355+
<p>
356+
国外网站访问比较慢,你可以快速<a href="http://pan.baidu.com/s/1mgIQjH2" target="_blank">下载作者整理好的</a>,
357+
但是,它只能支持20多种常用语言。
358+
<span class="alert">想要支持更多语言,</span>
359+
请点击
360+
<a href="https://highlightjs.org/download/">
361+
https://highlightjs.org/download/
362+
</a>
363+
里面有上百种语言供你选择、打包下载。
364+
</p>
365+
<img src="images/highlight1.png" style="width:400px;">
366+
<p>
367+
下载之后,可以得到一个 highlight.pack.js 和一个 style 文件夹,其中有很多个 css 文件。
368+
从这些 css 文件名,你就可以看出,每一个 css 文件就是一个主题包。
369+
<span class="alert">想要哪种代码高亮的样式主题,就引用哪一个 css 到页面中!</span>
370+
</p>
371+
<img src="images/highlight2.png" style="width:300px;">
372+
373+
<p>
374+
<br>
375+
<b>第二步,在页面中引用 highlight 的 css 和 js 文件</b>
376+
</p>
377+
<pre>
378+
&lt;!--在 head 中引用 css 文件--&gt;
379+
<span class="alert">&lt;!--style文件夹中,每一个css就是一个样式主题,可自行选择--&gt;</span>
380+
&lt;link rel="stylesheet" type="text/css" href="highlightJS/styles/github.css"&gt;
381+
382+
...省略若干内容...
383+
384+
&lt;!--在 body 最下方引用 javascript 文件--&gt;
385+
&lt;!--引用jquery、highlight.pack.js--&gt;
386+
&lt;script type="text/javascript" src="js/jquery-1.10.2.min.js"&gt;&lt;/script&gt;
387+
&lt;script type="text/javascript" src="highlightJS/highlight.pack.js"&gt;&lt;/script&gt;
388+
</pre>
389+
390+
<p>
391+
<br>
392+
<b>第三步,执行 hightlight 的方法</b>
393+
</p>
394+
<pre>
395+
&lt;!-- 引用jquery、highlight.pack.js 之后,再执行如下代码--&gt;
396+
&lt;script type="text/javascript"&gt;
397+
$(function(){
398+
$('pre code').each(function(i, block) {
399+
hljs.highlightBlock(block);
400+
});
401+
});
402+
&lt;/script&gt;
403+
</pre>
404+
<p>
405+
至此,hightlight 插件已经集成到了页面中。
406+
</p>
407+
<p>
408+
那么如何将它集成进 wangEditor 呢 ———— 答案:<b>不用集成!
409+
你只需要在这个页面的基础上做一个 wangEditor 编辑器即可,
410+
wangEditor会自动识别你是否引用了 highlight 插件。</b>
411+
</p>
412+
<p>
413+
不会的同学,可参考<a href="#start" target="_blank">开始使用</a>一节
414+
</p>
415+
<p>
416+
<br>
417+
以上都做完了,你会发现 wangEditor 的“插入代码”界面中,
418+
多了选择语言的下拉框,插入代码后,也有了高亮效果,快去试试吧!
419+
</p>
420+
<img src="images/highlight3.png" style="width:400px;">
421+
</div>
335422
<div id="upload-img">
336423
<h2>图片上传</h2>
337424
<p>
@@ -1153,7 +1240,7 @@ <h3>定制开发</h3>
11531240
<script type="text/javascript" src="../dist/js/jquery-1.10.2.min.js"></script>
11541241
<script type="text/javascript">
11551242
$(function(){
1156-
var version = '1.3.7',
1243+
var version = '1.3.8',
11571244
css = 'wangEditor-' + version + '.css',
11581245
js = 'wangEditor-' + version + '.js',
11591246
js_min = 'wangEditor-' + version + '.min.js'

‎docs/parts/001-sidebar.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<li><a href="#menu-config">自定义配置菜单</a></li>
2121
<li><a href="#onchange">添加change监听事件</a></li>
2222
<li><a href="#expressions">自定义配置表情图标</a></li>
23+
<li><a href="#highlight">配置代码高亮</a></li>
2324
</ul>
2425
<a href="#upload-img" class="group-title">图片上传</a>
2526
<ul>

0 commit comments

Comments
 (0)