Skip to content

Commit

Permalink
🐛 fix # 412
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed May 14, 2020
1 parent 1c95824 commit b817184
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -66,6 +66,7 @@

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

* [412](https://github.com/Vanessa219/vditor/issues/412) 预览界面大纲无法定位 `修复缺陷`
* [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
19 changes: 9 additions & 10 deletions src/ts/markdown/outlineRender.ts
Expand Up @@ -16,14 +16,9 @@ export const outlineRender = (contentElement: HTMLElement, targetElement: Elemen
}
});
targetElement.innerHTML = tocHTML;
if (targetElement.getAttribute("data-render") === "true") {
return;
}
targetElement.setAttribute("data-render", "true");
targetElement.addEventListener("click", (event: Event & { target: HTMLElement }) => {
const itemElement = event.target;
if (itemElement.classList.contains("vditor-outline__item")) {
const id = itemElement.getAttribute("data-id");
targetElement.querySelectorAll(".vditor-outline__item").forEach((item) => {
item.addEventListener("click", (event: Event & { target: HTMLElement }) => {
const id = item.getAttribute("data-id");
if (vditor) {
if (vditor.options.height === "auto") {
let windowScrollY = document.getElementById(id).offsetTop + vditor.element.offsetTop;
Expand All @@ -35,11 +30,15 @@ export const outlineRender = (contentElement: HTMLElement, targetElement: Elemen
if (vditor.element.offsetTop < window.scrollY) {
window.scrollTo(window.scrollX, vditor.element.offsetTop);
}
vditor[vditor.currentMode].element.scrollTop = document.getElementById(id).offsetTop;
if (vditor.element.querySelector('.vditor-preview').contains(contentElement)) {
contentElement.parentElement.scrollTop = document.getElementById(id).offsetTop;
} else {
contentElement.scrollTop = document.getElementById(id).offsetTop;
}
}
} else {
window.scrollTo(window.scrollX, document.getElementById(id).offsetTop);
}
}
});
});
};
7 changes: 6 additions & 1 deletion src/ts/toolbar/Preview.ts
Expand Up @@ -2,6 +2,7 @@ import {Constants} from "../constants";
import {getEventName} from "../util/compatibility";
import {MenuItem} from "./MenuItem";
import {disableToolbar, enableToolbar, hidePanel} from "./setToolbar";
import {renderOutline} from "../util/fixBrowserBehavior";

export class Preview extends MenuItem {
constructor(vditor: IVditor, menuItem: IMenuItem) {
Expand All @@ -17,7 +18,7 @@ export class Preview extends MenuItem {
return;
}

const toolbars = Constants.EDIT_TOOLBARS.concat(["both", "format", "edit-mode", "outline", "devtools"]);
const toolbars = Constants.EDIT_TOOLBARS.concat(["both", "format", "edit-mode", "devtools"]);
if (btnElement.classList.contains("vditor-menu--current")) {
btnElement.classList.remove("vditor-menu--current");
if (vditor.currentMode === "sv") {
Expand All @@ -32,6 +33,7 @@ export class Preview extends MenuItem {
vditor.preview.element.style.display = "none";
}
enableToolbar(vditor.toolbar.elements, toolbars);
renderOutline(vditor);
} else {
disableToolbar(vditor.toolbar.elements, toolbars);
vditor.preview.element.style.display = "block";
Expand All @@ -43,6 +45,9 @@ export class Preview extends MenuItem {
vditor.preview.render(vditor);
btnElement.classList.add("vditor-menu--current");
hidePanel(vditor, ["subToolbar", "hint", "popover"]);
setTimeout(() => {
renderOutline(vditor);
}, vditor.options.preview.delay);
}
});
}
Expand Down
10 changes: 8 additions & 2 deletions src/ts/util/fixBrowserBehavior.ts
Expand Up @@ -387,8 +387,14 @@ export const isToC = (text: string) => {
export const renderOutline = (vditor: IVditor) => {
const outlineElement = vditor.element.querySelector(".vditor-outline") as HTMLElement;
if (outlineElement && outlineElement.style.display === "block") {
outlineRender(vditor[vditor.currentMode].element,
outlineElement.querySelector(".vditor-outline__content"), vditor);
const previewElement: HTMLElement = vditor.element.querySelector('.vditor-preview')
if (previewElement && previewElement.style.display === "block") {
outlineRender(previewElement.lastElementChild as HTMLElement,
outlineElement.querySelector(".vditor-outline__content"), vditor);
} else {
outlineRender(vditor[vditor.currentMode].element,
outlineElement.querySelector(".vditor-outline__content"), vditor);
}
}
};

Expand Down

0 comments on commit b817184

Please sign in to comment.