Skip to content

Commit 9cd78aa

Browse files
author
wangfupeng
committed
1.修复源码编辑时的两个bug: 2.修复操作img、table不触发onchange事件的bug
1 parent 1a173b8 commit 9cd78aa

8 files changed

Lines changed: 140 additions & 17 deletions

File tree

‎dist/js/wangEditor.js‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4005,7 +4005,11 @@ _e(function (E, $) {
40054005
value = value.replace(/<script[\s\S]*?<\/script>/ig, '');
40064006
}
40074007
// 赋值
4008-
$txt.html(value);
4008+
try {
4009+
$txt.html(value);
4010+
} catch (ex) {
4011+
// 更新 html 源码出错,一般都是取消了 js 过滤之后,js报错导致的
4012+
}
40094013
}
40104014

40114015
// 定义click事件
@@ -4028,6 +4032,11 @@ _e(function (E, $) {
40284032
// 赋值
40294033
$code.val($txt.html());
40304034

4035+
// 监控变化
4036+
$code.on('change', function (e) {
4037+
updateValue();
4038+
});
4039+
40314040
// 渲染
40324041
$txt.after($code).hide();
40334042
$code.show();
@@ -7379,6 +7388,7 @@ _e(function (E, $) {
73797388
var editor = this;
73807389
var txt = editor.txt;
73817390
var $txt = txt.$txt;
7391+
var html = '';
73827392
// 说明:设置了 max-height 之后,$txt.parent() 负责滚动处理
73837393
var $currentTxt = editor.useMaxHeight ? $txt.parent() : $txt;
73847394
var $currentTable;
@@ -7414,8 +7424,20 @@ _e(function (E, $) {
74147424
// 统一执行命令的方法
74157425
var commandFn;
74167426
function command(e, callback) {
7427+
// 执行命令之前,先存储html内容
7428+
html = $txt.html();
7429+
// 监控内容变化
7430+
var cb = function () {
7431+
if (callback) {
7432+
callback();
7433+
}
7434+
if (html !== $txt.html()) {
7435+
$txt.change();
7436+
}
7437+
};
7438+
// 执行命令
74177439
if (commandFn) {
7418-
editor.customCommand(e, commandFn, callback);
7440+
editor.customCommand(e, commandFn, cb);
74197441
}
74207442
}
74217443

@@ -7558,6 +7580,7 @@ _e(function (E, $) {
75587580
var lang = editor.config.lang;
75597581
var txt = editor.txt;
75607582
var $txt = txt.$txt;
7583+
var html = '';
75617584
// 说明:设置了 max-height 之后,$txt.parent() 负责滚动处理
75627585
var $currentTxt = editor.useMaxHeight ? $txt.parent() : $txt;
75637586
var $editorContainer = editor.$editorContainer;
@@ -7603,6 +7626,9 @@ _e(function (E, $) {
76037626
if (url != null) {
76047627
currentLink = url;
76057628
}
7629+
if (html !== $txt.html()) {
7630+
$txt.change();
7631+
}
76067632
};
76077633
var $link;
76087634
var inLink = false;
@@ -7643,6 +7669,9 @@ _e(function (E, $) {
76437669

76447670
// 执行命令
76457671
if (commandFn) {
7672+
// 记录下执行命令之前的html内容
7673+
html = $txt.html();
7674+
// 执行命令
76467675
editor.customCommand(e, commandFn, callback);
76477676
}
76487677
}
@@ -7689,8 +7718,20 @@ _e(function (E, $) {
76897718
// 统一执行命令的方法
76907719
var commandFn;
76917720
function customCommand(e, callback) {
7721+
var cb;
7722+
// 记录下执行命令之前的html内容
7723+
html = $txt.html();
7724+
cb = function () {
7725+
if (callback) {
7726+
callback();
7727+
}
7728+
if (html !== $txt.html()) {
7729+
$txt.change();
7730+
}
7731+
};
7732+
// 执行命令
76927733
if (commandFn) {
7693-
editor.customCommand(e, commandFn, callback);
7734+
editor.customCommand(e, commandFn, cb);
76947735
}
76957736
}
76967737

‎dist/js/wangEditor.min.js‎

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

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "wangeditor",
33
"title": "wangEditor",
4-
"version": "2.1.20",
4+
"version": "2.1.21",
55
"description": "wangEditor - 基于javascript和css开发的 Web富文本编辑器, 轻量、简洁、易用、开源免费",
66
"homepage": "http://wangeditor.github.io/",
77
"author": {

‎src/js/parts/00870-editor-add-menu-source.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ _e(function (E, $) {
3535
value = value.replace(/<script[\s\S]*?<\/script>/ig, '');
3636
}
3737
// 赋值
38-
$txt.html(value);
38+
try {
39+
$txt.html(value);
40+
} catch (ex) {
41+
// 更新 html 源码出错,一般都是取消了 js 过滤之后,js报错导致的
42+
}
3943
}
4044

4145
// 定义click事件
@@ -58,6 +62,11 @@ _e(function (E, $) {
5862
// 赋值
5963
$code.val($txt.html());
6064

65+
// 监控变化
66+
$code.on('change', function (e) {
67+
updateValue();
68+
});
69+
6170
// 渲染
6271
$txt.after($code).hide();
6372
$code.show();

‎src/js/parts/0p030-txt-table-toolbar.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ _e(function (E, $) {
55
var editor = this;
66
var txt = editor.txt;
77
var $txt = txt.$txt;
8+
var html = '';
89
// 说明:设置了 max-height 之后,$txt.parent() 负责滚动处理
910
var $currentTxt = editor.useMaxHeight ? $txt.parent() : $txt;
1011
var $currentTable;
@@ -40,8 +41,20 @@ _e(function (E, $) {
4041
// 统一执行命令的方法
4142
var commandFn;
4243
function command(e, callback) {
44+
// 执行命令之前,先存储html内容
45+
html = $txt.html();
46+
// 监控内容变化
47+
var cb = function () {
48+
if (callback) {
49+
callback();
50+
}
51+
if (html !== $txt.html()) {
52+
$txt.change();
53+
}
54+
};
55+
// 执行命令
4356
if (commandFn) {
44-
editor.customCommand(e, commandFn, callback);
57+
editor.customCommand(e, commandFn, cb);
4558
}
4659
}
4760

‎src/js/parts/0p040-txt-img-toolbar.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _e(function (E, $) {
1010
var lang = editor.config.lang;
1111
var txt = editor.txt;
1212
var $txt = txt.$txt;
13+
var html = '';
1314
// 说明:设置了 max-height 之后,$txt.parent() 负责滚动处理
1415
var $currentTxt = editor.useMaxHeight ? $txt.parent() : $txt;
1516
var $editorContainer = editor.$editorContainer;
@@ -55,6 +56,9 @@ _e(function (E, $) {
5556
if (url != null) {
5657
currentLink = url;
5758
}
59+
if (html !== $txt.html()) {
60+
$txt.change();
61+
}
5862
};
5963
var $link;
6064
var inLink = false;
@@ -95,6 +99,9 @@ _e(function (E, $) {
9599

96100
// 执行命令
97101
if (commandFn) {
102+
// 记录下执行命令之前的html内容
103+
html = $txt.html();
104+
// 执行命令
98105
editor.customCommand(e, commandFn, callback);
99106
}
100107
}
@@ -141,8 +148,20 @@ _e(function (E, $) {
141148
// 统一执行命令的方法
142149
var commandFn;
143150
function customCommand(e, callback) {
151+
var cb;
152+
// 记录下执行命令之前的html内容
153+
html = $txt.html();
154+
cb = function () {
155+
if (callback) {
156+
callback();
157+
}
158+
if (html !== $txt.html()) {
159+
$txt.change();
160+
}
161+
};
162+
// 执行命令
144163
if (commandFn) {
145-
editor.customCommand(e, commandFn, callback);
164+
editor.customCommand(e, commandFn, cb);
146165
}
147166
}
148167

‎src/js/wangEditor.js‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4005,7 +4005,11 @@ _e(function (E, $) {
40054005
value = value.replace(/<script[\s\S]*?<\/script>/ig, '');
40064006
}
40074007
// 赋值
4008-
$txt.html(value);
4008+
try {
4009+
$txt.html(value);
4010+
} catch (ex) {
4011+
// 更新 html 源码出错,一般都是取消了 js 过滤之后,js报错导致的
4012+
}
40094013
}
40104014

40114015
// 定义click事件
@@ -4028,6 +4032,11 @@ _e(function (E, $) {
40284032
// 赋值
40294033
$code.val($txt.html());
40304034

4035+
// 监控变化
4036+
$code.on('change', function (e) {
4037+
updateValue();
4038+
});
4039+
40314040
// 渲染
40324041
$txt.after($code).hide();
40334042
$code.show();
@@ -7379,6 +7388,7 @@ _e(function (E, $) {
73797388
var editor = this;
73807389
var txt = editor.txt;
73817390
var $txt = txt.$txt;
7391+
var html = '';
73827392
// 说明:设置了 max-height 之后,$txt.parent() 负责滚动处理
73837393
var $currentTxt = editor.useMaxHeight ? $txt.parent() : $txt;
73847394
var $currentTable;
@@ -7414,8 +7424,20 @@ _e(function (E, $) {
74147424
// 统一执行命令的方法
74157425
var commandFn;
74167426
function command(e, callback) {
7427+
// 执行命令之前,先存储html内容
7428+
html = $txt.html();
7429+
// 监控内容变化
7430+
var cb = function () {
7431+
if (callback) {
7432+
callback();
7433+
}
7434+
if (html !== $txt.html()) {
7435+
$txt.change();
7436+
}
7437+
};
7438+
// 执行命令
74177439
if (commandFn) {
7418-
editor.customCommand(e, commandFn, callback);
7440+
editor.customCommand(e, commandFn, cb);
74197441
}
74207442
}
74217443

@@ -7558,6 +7580,7 @@ _e(function (E, $) {
75587580
var lang = editor.config.lang;
75597581
var txt = editor.txt;
75607582
var $txt = txt.$txt;
7583+
var html = '';
75617584
// 说明:设置了 max-height 之后,$txt.parent() 负责滚动处理
75627585
var $currentTxt = editor.useMaxHeight ? $txt.parent() : $txt;
75637586
var $editorContainer = editor.$editorContainer;
@@ -7603,6 +7626,9 @@ _e(function (E, $) {
76037626
if (url != null) {
76047627
currentLink = url;
76057628
}
7629+
if (html !== $txt.html()) {
7630+
$txt.change();
7631+
}
76067632
};
76077633
var $link;
76087634
var inLink = false;
@@ -7643,6 +7669,9 @@ _e(function (E, $) {
76437669

76447670
// 执行命令
76457671
if (commandFn) {
7672+
// 记录下执行命令之前的html内容
7673+
html = $txt.html();
7674+
// 执行命令
76467675
editor.customCommand(e, commandFn, callback);
76477676
}
76487677
}
@@ -7689,8 +7718,20 @@ _e(function (E, $) {
76897718
// 统一执行命令的方法
76907719
var commandFn;
76917720
function customCommand(e, callback) {
7721+
var cb;
7722+
// 记录下执行命令之前的html内容
7723+
html = $txt.html();
7724+
cb = function () {
7725+
if (callback) {
7726+
callback();
7727+
}
7728+
if (html !== $txt.html()) {
7729+
$txt.change();
7730+
}
7731+
};
7732+
// 执行命令
76927733
if (commandFn) {
7693-
editor.customCommand(e, commandFn, callback);
7734+
editor.customCommand(e, commandFn, cb);
76947735
}
76957736
}
76967737

‎test/index.html‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<body style="padding:0 20px;">
2020
<p>wangEditor2.0 支持 IE8 浏览器</p>
2121
<div id="editor-container" class="container">
22-
<div id="editor-trigger"><p>请输入内容</p><p>&lt;script&gt;&lt;/script&gt;</p></div>
22+
<div id="editor-trigger"><p>请输入内容</p></div>
2323
<!-- <textarea id="editor-trigger" style="display:none;">
2424
<p>请输入内容...</p>
2525
</textarea> -->
@@ -118,9 +118,9 @@
118118
// });
119119

120120
// onchange 事件
121-
// editor.onchange = function () {
122-
// console.log(this.$txt.html());
123-
// };
121+
editor.onchange = function () {
122+
console.log(this.$txt.html());
123+
};
124124

125125
// 取消过滤js
126126
// editor.config.jsFilter = false;

0 commit comments

Comments
 (0)