Skip to content

Commit

Permalink
🐛 fix #218
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Mar 12, 2020
1 parent 9e463c1 commit fc55cc5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

### v2.3.0 / 未发布

* [218](https://github.com/Vanessa219/vditor/issues/218) 所见即所得模式下,insertValue渲染*和~的时候似乎不是很正确 `修复缺陷`
* [217](https://github.com/Vanessa219/vditor/issues/217) ToC 添加悬浮菜单及 bug 修复 `修复缺陷`
* [216](https://github.com/Vanessa219/vditor/issues/216) subtoolbar 向下溢出 `修复缺陷`
* [215](https://github.com/Vanessa219/vditor/issues/215) lists when last enter `修复缺陷`
Expand Down
26 changes: 7 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ import {getMarkdown} from "./ts/util/getMarkdown";
import {Options} from "./ts/util/Options";
import {setPreviewMode} from "./ts/util/setPreviewMode";
import {WYSIWYG} from "./ts/wysiwyg";
import {afterRenderEvent} from "./ts/wysiwyg/afterRenderEvent";
import {insertHTML} from "./ts/wysiwyg/insertHTML";
import {processCodeRender} from "./ts/wysiwyg/processCodeRender";
import {renderDomByMd} from "./ts/wysiwyg/renderDomByMd";
import {input} from "./ts/wysiwyg/input";

class Vditor {

Expand Down Expand Up @@ -277,33 +275,23 @@ class Vditor {
if (this.vditor.currentMode === "markdown") {
insertText(this.vditor, value, "");
} else {
let range
if (getSelection().rangeCount === 0) {
this.vditor.wysiwyg.element.focus();
range = getSelection().getRangeAt(0);
} else {
const range = getSelection().getRangeAt(0);
range = getSelection().getRangeAt(0);
if (!this.vditor.wysiwyg.element.contains(range.startContainer)) {
this.vditor.wysiwyg.element.focus();
} else {
range.collapse(true);
}
}

document.execCommand("insertHTML", false, value);

if (render) {
const vditorDomHTML = this.vditor.lute.Md2VditorDOM(value);
insertHTML(vditorDomHTML, this.vditor);

this.vditor.wysiwyg.element.querySelectorAll(".vditor-wysiwyg__block")
.forEach((blockElement: HTMLElement) => {
processCodeRender(blockElement, this.vditor);
});

afterRenderEvent(this.vditor, {
enableAddUndoStack: true,
enableHint: false,
enableInput: false,
});
} else {
document.execCommand("insertHTML", false, value);
input(this.vditor, range);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ts/wysiwyg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ class WYSIWYG {
});

// 中文处理
this.element.addEventListener("compositionstart", (event: IHTMLInputEvent) => {
this.element.addEventListener("compositionstart", (event: InputEvent) => {
this.composingLock = true;
});

this.element.addEventListener("compositionend", (event: IHTMLInputEvent) => {
this.element.addEventListener("compositionend", (event: InputEvent) => {
const blockElement = hasClosestBlock(getSelection().getRangeAt(0).startContainer);
if (blockElement && blockElement.tagName.indexOf("H") === 0 && blockElement.textContent === ""
&& blockElement.tagName.length === 2) {
Expand All @@ -321,7 +321,7 @@ class WYSIWYG {
input(vditor, getSelection().getRangeAt(0).cloneRange(), event);
});

this.element.addEventListener("input", (event: IHTMLInputEvent) => {
this.element.addEventListener("input", (event: InputEvent) => {
if (this.preventInput) {
this.preventInput = false;
return;
Expand Down
5 changes: 3 additions & 2 deletions src/ts/wysiwyg/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {processCodeRender} from "./processCodeRender";
import {renderToc} from "./processMD";
import {setRangeByWbr} from "./setRangeByWbr";

export const input = (vditor: IVditor, range: Range, event: IHTMLInputEvent) => {
export const input = (vditor: IVditor, range: Range, event?: InputEvent) => {
let blockElement = hasClosestBlock(range.startContainer);

// 列表需要到最顶层
Expand All @@ -30,7 +30,7 @@ export const input = (vditor: IVditor, range: Range, event: IHTMLInputEvent) =>
// 代码块前为空行,按下向后删除键,代码块内容会被删除
renderElement.outerHTML = `<p data-block="0">${renderElement.textContent}</p>`;
}
} else if (event.inputType !== "formatItalic"
} else if (event && event.inputType !== "formatItalic"
&& event.inputType !== "deleteByDrag"
&& event.inputType !== "insertFromDrop"
&& event.inputType !== "formatBold"
Expand All @@ -41,6 +41,7 @@ export const input = (vditor: IVditor, range: Range, event: IHTMLInputEvent) =>
&& event.inputType !== "formatOutdent"
&& event.inputType !== "formatIndent"
&& event.inputType !== "" // document.execCommand('unlink', false)
|| !event
) {
const previousAEmptyElement = previoueIsEmptyA(range.startContainer);
if (previousAEmptyElement) {
Expand Down

0 comments on commit fc55cc5

Please sign in to comment.