Skip to content

Commit

Permalink
🐛 fix #217
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Mar 11, 2020
1 parent bd0b933 commit 731d2fe
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/ts/wysiwyg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {getRenderElementNextNode, modifyPre} from "./inlineTag";
import {input} from "./input";
import {insertHTML} from "./insertHTML";
import {processCodeRender, showCode} from "./processCodeRender";
import {isHeadingMD, isHrMD, isToC} from "./processMD";
import {isHeadingMD, isHrMD, isToC, renderToc} from "./processMD";
import {setRangeByWbr} from "./setRangeByWbr";

class WYSIWYG {
Expand Down Expand Up @@ -312,6 +312,7 @@ class WYSIWYG {
if (blockElement && blockElement.tagName.indexOf("H") === 0 && blockElement.textContent === ""
&& blockElement.tagName.length === 2) {
// heading 为空删除 https://github.com/Vanessa219/vditor/issues/150
renderToc(this.element);
return;
}
input(vditor, getSelection().getRangeAt(0).cloneRange(), event);
Expand Down Expand Up @@ -365,6 +366,7 @@ class WYSIWYG {
if (blockElement.tagName.indexOf("H") === 0 && blockElement.textContent === ""
&& blockElement.tagName.length === 2) {
// heading 为空删除 https://github.com/Vanessa219/vditor/issues/150
renderToc(this.element);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ts/wysiwyg/processMD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export const renderToc = (editorElement: HTMLPreElement) => {
}
let tocHTML = "";
Array.from(editorElement.children).forEach((item: HTMLElement) => {
if (item.tagName.indexOf("H") === 0 && item.tagName.length === 2) {
if (item.tagName.indexOf("H") === 0 && item.tagName.length === 2 && item.textContent.trim() !== "") {
const space = new Array((parseInt(item.tagName.substring(1), 10) - 1) * 2).fill(" ").join("");
tocHTML += `${space}<span data-type="toc-h">${item.textContent.trim()}</span><br>`;
}
});
tocElement.innerHTML = tocHTML;
tocElement.innerHTML = tocHTML || "[ToC]";
};
2 changes: 2 additions & 0 deletions src/ts/wysiwyg/setHeading.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {hasClosestBlock} from "../util/hasClosest";
import {setRangeByWbr} from "./setRangeByWbr";
import {renderToc} from "./processMD";

export const setHeading = (vditor: IVditor, tagName: string) => {
const range = getSelection().getRangeAt(0);
Expand All @@ -23,6 +24,7 @@ export const setHeading = (vditor: IVditor, tagName: string) => {
blockElement.outerHTML = `<${tagName} data-block="0">${blockElement.innerHTML.trim()}</${tagName}>`;
}
setRangeByWbr(vditor.wysiwyg.element, range);
renderToc(vditor.wysiwyg.element);
}
};

Expand Down

0 comments on commit 731d2fe

Please sign in to comment.