Skip to content

Commit

Permalink
🐛 fix #411
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed May 14, 2020
1 parent 9a87e5a commit 1c95824
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

### v3.2.6 / 2020-05-xx

* [411](https://github.com/Vanessa219/vditor/issues/411) 复制到微信公众号后代码块背景丢失 `修复缺陷`
* [410](https://github.com/Vanessa219/vditor/issues/410) not delete inline code(firfox) `修复缺陷`
* [405](https://github.com/Vanessa219/vditor/issues/405) translated mindmap into Korean `文档相关`

Expand Down
4 changes: 4 additions & 0 deletions src/assets/scss/_reset.scss
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@
}
}

pre {
margin: 1em 0;
}

pre > code {
margin: 0;
font-size: 85%;
Expand Down
51 changes: 30 additions & 21 deletions src/ts/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ export class Preview {
previewElement.classList.add(vditor.options.classes.preview);
}
previewElement.style.maxWidth = vditor.options.preview.maxWidth + "px";
previewElement.addEventListener("copy", () => {
// 防止背景色被粘贴到公众号中
this.element.style.backgroundColor = "#fff";
setTimeout(() => {
this.element.style.backgroundColor = "var(--textarea-background-color)";
});
previewElement.addEventListener("copy", (event) => {
const tempElement = document.createElement("div");
tempElement.className = "vditor-reset";
tempElement.appendChild(getSelection().getRangeAt(0).cloneContents());

this.copyToWechat(vditor, tempElement);
event.preventDefault();
});

const actionElement = document.createElement("div");
Expand All @@ -57,21 +58,7 @@ export class Preview {
}

if (type === "mp-wechat") {
// fix math render
document.querySelectorAll(".katex-html .base").forEach((item: HTMLElement) => {
item.style.display = "initial";
});

// 防止背景色被粘贴到公众号中
this.element.style.backgroundColor = "#fff";
const range = this.element.lastElementChild.ownerDocument.createRange();
range.selectNode(this.element.lastElementChild);
setSelectionFocus(range);
document.execCommand("copy");
vditor.tip.show("已复制,可到微信公众号平台进行粘贴");
range.collapse(true);

this.element.style.backgroundColor = "var(--textarea-background-color)";
this.copyToWechat(vditor, this.element.lastElementChild.cloneNode(true) as HTMLElement);
return;
}

Expand Down Expand Up @@ -185,4 +172,26 @@ export class Preview {
abcRender(vditor.preview.element.lastElementChild as HTMLElement, vditor.options.cdn);
mediaRender(vditor.preview.element.lastElementChild as HTMLElement);
}

private copyToWechat(vditor: IVditor, copyElement: HTMLElement) {
// fix math render
copyElement.querySelectorAll(".katex-html .base").forEach((item: HTMLElement) => {
item.style.display = "initial";
});
// 防止背景色被粘贴到公众号中
copyElement.style.backgroundColor = "#fff";
// 代码背景
copyElement.querySelectorAll("code").forEach((item) => {
item.style.backgroundImage = "none";
});
this.element.append(copyElement);

const range = copyElement.ownerDocument.createRange();
range.selectNode(copyElement);
setSelectionFocus(range);
document.execCommand("copy");

this.element.lastElementChild.remove();
vditor.tip.show("已复制,可到微信公众号平台进行粘贴");
}
}

0 comments on commit 1c95824

Please sign in to comment.