Skip to content

Commit

Permalink
v3.1.0 支持粘贴时过滤图片
Browse files Browse the repository at this point in the history
  • Loading branch information
wangfupeng1988 committed Mar 7, 2018
1 parent 455b48f commit fddf2c7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 9 deletions.
5 changes: 3 additions & 2 deletions ISSUE.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@
### v3.1.0

- [done] 视图增加 “字体” “字号”(字体需要文档说明)
- 外露的修改图片大小的按钮
- https://github.com/wangfupeng1988/wangEditor/issues/512
- [done] 支持粘贴内容时忽略图片

### 近期计划解决

- 外露的修改图片大小的按钮
- **处理粘贴数据时,要把 pasteHtml 先转换成 vnode 之类的结构化对象,然后再递归处理**
- 撤销的兼容性问题(会误伤其他编辑器或者 input textarea 等),考虑用 onchange 记录 undo 和 redo 的内容(但是得考虑直接修改 dom 的情况,如 quote code img list table 菜单)
- 列表撤销会删除一行?https://github.com/wangfupeng1988/wangEditor/issues/1131
- 页面中有 input 等输入标签时,undo redo 会误伤 https://github.com/wangfupeng1988/wangEditor/issues/1024
Expand Down
6 changes: 6 additions & 0 deletions docs/usage/03-config/06-paste.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

当从其他网页复制文本内容粘贴到编辑器中,编辑器会默认过滤掉复制文本中自带的样式,目的是让粘贴后的文本变得更加简洁和轻量。用户可通过`editor.customConfig.pasteFilterStyle = false`手动关闭掉粘贴样式的过滤。

## 忽略粘贴内容中的图片

从其他页面复制过来的内容,除了包含文字还可能包含图片,这些图片一般都是外域的(可能会有盗链)。此时如果想要忽略图片,即只粘贴文字不粘贴图片,可以使用`editor.customConfig.pasteIgnoreImg = true`来控制。默认是可以粘贴图片的。

## 自定义处理粘贴的文本内容

使用者可通过`editor.customConfig.pasteTextHandle`对粘贴的文本内容进行自定义的过滤、处理等操作,然后返回处理之后的文本内容。编辑器最终会粘贴用户处理之后并且返回的的内容。
Expand All @@ -23,6 +27,8 @@
var editor = new E('#div1')
// 关闭粘贴样式的过滤
editor.customConfig.pasteFilterStyle = false
// 忽略粘贴内容中的图片
editor.customConfig.pasteIgnoreImg = true
// 自定义处理粘贴的文本内容
editor.customConfig.pasteTextHandle = function (content) {
// content 即粘贴过来的内容(html 或 纯文本),可进行自定义处理然后返回
Expand Down
13 changes: 11 additions & 2 deletions release/wangEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,9 @@ var config = {
// 粘贴过滤样式,默认开启
pasteFilterStyle: true,

// 粘贴内容时,忽略图片。默认关闭
pasteIgnoreImg: false,

// 对粘贴的文字进行自定义处理,返回处理后的结果。编辑器会将处理后的结果粘贴到编辑区域中。
// IE 暂时不支持
pasteTextHandle: function pasteTextHandle(content) {
Expand Down Expand Up @@ -3031,7 +3034,7 @@ function getPasteText(e) {
}

// 获取粘贴的html
function getPasteHtml(e, filterStyle) {
function getPasteHtml(e, filterStyle, ignoreImg) {
var clipboardData = e.clipboardData || e.originalEvent && e.originalEvent.clipboardData;
var pasteText = void 0,
pasteHtml = void 0;
Expand Down Expand Up @@ -3061,6 +3064,11 @@ function getPasteHtml(e, filterStyle) {
// 过滤 data-xxx 属性
pasteHtml = pasteHtml.replace(/\s?data-.+?=('|").+?('|")/igm, '');

if (ignoreImg) {
// 忽略图片
pasteHtml = pasteHtml.replace(/<img.+?>/igm, '');
}

if (filterStyle) {
// 过滤样式
pasteHtml = pasteHtml.replace(/\s?(class|style)=('|").+?('|")/igm, '');
Expand Down Expand Up @@ -3428,6 +3436,7 @@ Text.prototype = {
var config = editor.config;
var pasteFilterStyle = config.pasteFilterStyle;
var pasteTextHandle = config.pasteTextHandle;
var ignoreImg = config.pasteIgnoreImg;
var $textElem = editor.$textElem;

// 粘贴图片、文本的事件,每次只能执行一个
Expand Down Expand Up @@ -3462,7 +3471,7 @@ Text.prototype = {
}

// 获取粘贴的文字
var pasteHtml = getPasteHtml(e, pasteFilterStyle);
var pasteHtml = getPasteHtml(e, pasteFilterStyle, ignoreImg);
var pasteText = getPasteText(e);
pasteText = pasteText.replace(/\n/gm, '<br>');

Expand Down
4 changes: 2 additions & 2 deletions release/wangEditor.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion release/wangEditor.min.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ const config = {
// 粘贴过滤样式,默认开启
pasteFilterStyle: true,

// 粘贴内容时,忽略图片。默认关闭
pasteIgnoreImg: false,

// 对粘贴的文字进行自定义处理,返回处理后的结果。编辑器会将处理后的结果粘贴到编辑区域中。
// IE 暂时不支持
pasteTextHandle: function (content) {
Expand Down
3 changes: 2 additions & 1 deletion src/js/text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ Text.prototype = {
const config = editor.config
const pasteFilterStyle = config.pasteFilterStyle
const pasteTextHandle = config.pasteTextHandle
const ignoreImg = config.pasteIgnoreImg
const $textElem = editor.$textElem

// 粘贴图片、文本的事件,每次只能执行一个
Expand Down Expand Up @@ -368,7 +369,7 @@ Text.prototype = {
}

// 获取粘贴的文字
let pasteHtml = getPasteHtml(e, pasteFilterStyle)
let pasteHtml = getPasteHtml(e, pasteFilterStyle, ignoreImg)
let pasteText = getPasteText(e)
pasteText = pasteText.replace(/\n/gm, '<br>')

Expand Down
7 changes: 6 additions & 1 deletion src/js/util/paste-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function getPasteText(e) {
}

// 获取粘贴的html
export function getPasteHtml(e, filterStyle) {
export function getPasteHtml(e, filterStyle, ignoreImg) {
const clipboardData = e.clipboardData || (e.originalEvent && e.originalEvent.clipboardData)
let pasteText, pasteHtml
if (clipboardData == null) {
Expand Down Expand Up @@ -49,6 +49,11 @@ export function getPasteHtml(e, filterStyle) {
// 过滤 data-xxx 属性
pasteHtml = pasteHtml.replace(/\s?data-.+?=('|").+?('|")/igm, '')

if (ignoreImg) {
// 忽略图片
pasteHtml = pasteHtml.replace(/<img.+?>/igm, '')
}

if (filterStyle) {
// 过滤样式
pasteHtml = pasteHtml.replace(/\s?(class|style)=('|").+?('|")/igm, '')
Expand Down

0 comments on commit fddf2c7

Please sign in to comment.