Skip to content

Commit

Permalink
🎨 fix #1640
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Aug 9, 2024
1 parent eaec29c commit 98dfe17
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 升级
* 3.9
* 添加 insertMD 方法
* 添加 options.upload.renderLinkDest
* 添加 options.preview.math.mathJaxOptions
* 添加 hljs.langs
Expand All @@ -16,6 +17,10 @@

* [open issues](https://github.com/Vanessa219/vditor/issues)

### v3.10.6 / 2024-08

* [添加 `insertMD` 方法](https://github.com/Vanessa219/vditor/issues/1640) `引入特性`

### v3.10.5 / 2024-08-09

* [添加 `upload.renderLinkDest` 用于扩展粘贴时图片链接的处理](https://github.com/Vanessa219/vditor/issues/1186) `引入特性`
Expand Down
21 changes: 19 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {Preview} from "./ts/preview/index";
import {Resize} from "./ts/resize/index";
import {Editor} from "./ts/sv/index";
import {inputEvent} from "./ts/sv/inputEvent";
import {processAfterRender as processSVAfterRender} from "./ts/sv/process";
import {processAfterRender as processSVAfterRender, processPaste} from "./ts/sv/process";
import {Tip} from "./ts/tip/index";
import {Toolbar} from "./ts/toolbar/index";
import {disableToolbar, hidePanel} from "./ts/toolbar/setToolbar";
Expand All @@ -30,11 +30,12 @@ import {addScript, addScriptSync} from "./ts/util/addScript";
import {getSelectText} from "./ts/util/getSelectText";
import {Options} from "./ts/util/Options";
import {processCodeRender} from "./ts/util/processCode";
import {getCursorPosition, getEditorRange} from "./ts/util/selection";
import {getCursorPosition, getEditorRange, insertHTML} from "./ts/util/selection";
import {afterRenderEvent} from "./ts/wysiwyg/afterRenderEvent";
import {WYSIWYG} from "./ts/wysiwyg/index";
import {input} from "./ts/wysiwyg/input";
import {renderDomByMd} from "./ts/wysiwyg/renderDomByMd";
import {execAfterRender} from "./ts/util/fixBrowserBehavior";

class Vditor extends VditorMethod {
public readonly version: string;
Expand Down Expand Up @@ -268,6 +269,8 @@ class Vditor extends VditorMethod {
public insertValue(value: string, render = true) {
const range = getEditorRange(this.vditor);
range.collapse(true);
// https://github.com/Vanessa219/vditor/issues/716
// https://github.com/Vanessa219/vditor/issues/917
const tmpElement = document.createElement("template");
tmpElement.innerHTML = value;
range.insertNode(tmpElement.content.cloneNode(true));
Expand All @@ -290,6 +293,20 @@ class Vditor extends VditorMethod {
}
}

/** 在焦点处插入 Markdown */
public insertMD(md: string) {
// https://github.com/Vanessa219/vditor/issues/1640
if (this.vditor.currentMode === "ir") {
insertHTML(this.vditor.lute.Md2VditorIRDOM(md), this.vditor);
} else if (this.vditor.currentMode === "wysiwyg") {
insertHTML(this.vditor.lute.Md2VditorDOM(md), this.vditor);
} else {
processPaste(this.vditor, md);
}
this.vditor.outline.render(this.vditor);
execAfterRender(this.vditor);
}

/** 设置编辑器内容 */
public setValue(markdown: string, clearStack = false) {
if (this.vditor.currentMode === "sv") {
Expand Down

0 comments on commit 98dfe17

Please sign in to comment.