Skip to content

Commit

Permalink
🐛 fix #43
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Jan 7, 2020
1 parent 4a3dd44 commit 897b749
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

### v2.0.13 / 2020-01-06

* [43](https://github.com/Vanessa219/vditor/issues/43) When copy & paste the link `修复缺陷`
* [39](https://github.com/Vanessa219/vditor/issues/39) 所见即所得模式录音bug `修复缺陷`
* [38](https://github.com/Vanessa219/vditor/issues/38) 有序列表顺序错误 `修复缺陷`
* [37](https://github.com/Vanessa219/vditor/issues/37) 为 wysiwyg 代码块添加快捷键 `引入特性`
Expand Down
16 changes: 9 additions & 7 deletions src/ts/wysiwyg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ class WYSIWYG {
let textHTML = event.clipboardData.getData("text/html");
const textPlain = event.clipboardData.getData("text/plain");

// 浏览器地址栏拷贝处理
if (textHTML.replace(/<(|\/)(html|body|meta)[^>]*?>/ig, "").trim() ===
`<a href="${textPlain}">${textPlain}</a>` ||
textHTML.replace(/<(|\/)(html|body|meta)[^>]*?>/ig, "").trim() ===
`<!--StartFragment--><a href="${textPlain}">${textPlain}</a><!--EndFragment-->`) {
textHTML = '';
}

// process word
const doc = new DOMParser().parseFromString(textHTML, "text/html");
if (doc.body) {
Expand Down Expand Up @@ -126,14 +134,8 @@ class WYSIWYG {
uploadFiles(vditor, event.clipboardData.files);
} else if (textPlain.trim() !== "" && event.clipboardData.files.length === 0) {
log("Md2VditorDOM", textPlain, "argument", vditor.options.debugger);
let vditorDomHTML = vditor.lute.Md2VditorDOM(textPlain);
const vditorDomHTML = vditor.lute.Md2VditorDOM(textPlain);
log("Md2VditorDOM", vditorDomHTML, "result", vditor.options.debugger);
const tempElement = document.createElement("div");
tempElement.innerHTML = vditorDomHTML;
const pElements = tempElement.querySelectorAll("p");
if (pElements.length === 1) {
vditorDomHTML = pElements[0].innerHTML.trim();
}
insertHTML(vditorDomHTML, vditor);
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/ts/wysiwyg/insertHTML.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
export const insertHTML = (html: string, vditor: IVditor) => {
// 使用 lute 方法会添加 p 元素,只有一个 p 元素的时候进行删除
const tempElement = document.createElement('div')
tempElement.innerHTML = html
const pElements = tempElement.querySelectorAll("p");
if (pElements.length === 1) {
html = pElements[0].innerHTML.trim();
}

const pasteElement = document.createElement("template");
pasteElement.innerHTML = html;

const range = getSelection().getRangeAt(0);
if (!range.collapsed) {
vditor.wysiwyg.preventInput = true;
document.execCommand("delete", false, "");

}
range.insertNode(pasteElement.content.cloneNode(true));
range.collapse(false);
Expand Down

0 comments on commit 897b749

Please sign in to comment.