|
|
@@ -2,7 +2,7 @@ import {hasClosestByHeadings} from "../util/hasClosestByHeadings";
|
|
|
|
|
|
export const outlineRender = (contentElement: HTMLElement, targetElement: Element, vditor?: IVditor) => {
|
|
|
let tocHTML = "";
|
|
|
- Array.from(contentElement.children).forEach((item: HTMLElement) => {
|
|
|
+ Array.from(contentElement.children).forEach((item: HTMLElement, index) => {
|
|
|
if (hasClosestByHeadings(item)) {
|
|
|
const headingNo = parseInt(item.tagName.substring(1), 10);
|
|
|
const space = new Array((headingNo - 1) * 2).fill(" ").join("");
|
|
|
@@ -12,6 +12,9 @@ export const outlineRender = (contentElement: HTMLElement, targetElement: Elemen
|
|
|
} else {
|
|
|
text = item.textContent.trim();
|
|
|
}
|
|
|
+ const lastIndex = item.id.lastIndexOf("_");
|
|
|
+ const lastId = item.id.substring(0, lastIndex === -1 ? undefined : lastIndex);
|
|
|
+ item.id = lastId + "_" + index;
|
|
|
tocHTML += `<div data-id="${item.id}" class="vditor-outline__item">${space}${text}</div>`;
|
|
|
}
|
|
|
});
|
|
|
@@ -30,7 +33,7 @@ export const outlineRender = (contentElement: HTMLElement, targetElement: Elemen
|
|
|
if (vditor.element.offsetTop < window.scrollY) {
|
|
|
window.scrollTo(window.scrollX, vditor.element.offsetTop);
|
|
|
}
|
|
|
- if (vditor.element.querySelector('.vditor-preview').contains(contentElement)) {
|
|
|
+ if (vditor.element.querySelector(".vditor-preview").contains(contentElement)) {
|
|
|
contentElement.parentElement.scrollTop = document.getElementById(id).offsetTop;
|
|
|
} else {
|
|
|
contentElement.scrollTop = document.getElementById(id).offsetTop;
|