Skip to content

Commit

Permalink
🎨 sv mode #807
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Nov 10, 2020
1 parent 308a239 commit e8153de
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class Vditor extends VditorMethod {
/** 获取评论 ID */
public getCommentIds() {
if (this.vditor.currentMode !== "wysiwyg") {
return;
return [];
}
let ids: string[] = [];
this.vditor.wysiwyg.element.querySelectorAll(".vditor-comment").forEach((item) => {
Expand Down
5 changes: 4 additions & 1 deletion src/ts/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ export class Preview {
vditor.tip.hide();
vditor.preview.element.removeAttribute("data-type");
}
vditor.preview.element.querySelector(".vditor-comment--focus").classList.remove("vditor-comment--focus");
const cmtFocusElement = vditor.preview.element.querySelector(".vditor-comment--focus");
if (cmtFocusElement) {
cmtFocusElement.classList.remove("vditor-comment--focus");
}
codeRender(vditor.preview.element.lastElementChild as HTMLElement, vditor.options.lang);
highlightRender(vditor.options.preview.hljs, vditor.preview.element.lastElementChild as HTMLElement,
vditor.options.cdn);
Expand Down
58 changes: 42 additions & 16 deletions src/ts/sv/inputEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,35 +128,61 @@ export const inputEvent = (vditor: IVditor, event?: InputEvent) => {
html = blockElement.previousElementSibling.textContent + html;
blockElement.previousElementSibling.remove();
}

// 添加链接引用
const allLinkRefDefsElement = vditor.sv.element.querySelector("[data-type='link-ref-defs-block']");
if (allLinkRefDefsElement && !blockElement.isEqualNode(allLinkRefDefsElement.parentElement)) {
html += allLinkRefDefsElement.parentElement.textContent;
allLinkRefDefsElement.parentElement.remove();
}
vditor.sv.element.querySelectorAll("[data-type='link-ref-defs-block']").forEach((item, index) => {
if (index === 0 && item && !(blockElement as HTMLElement).isEqualNode(item.parentElement)) {
html += "\n" + item.parentElement.textContent;
item.parentElement.remove();
}
});

// 添加脚注
const allFootnoteElement = vditor.sv.element.querySelector("[data-type='footnotes-link']");
if (allFootnoteElement && !blockElement.isEqualNode(allFootnoteElement.parentElement)) {
html += allFootnoteElement.parentElement.textContent;
allFootnoteElement.parentElement.remove();
}
vditor.sv.element.querySelectorAll("[data-type='footnotes-link']").forEach((item, index) => {
if (index === 0 && item && !(blockElement as HTMLElement).isEqualNode(item.parentElement)) {
html += "\n" + item.parentElement.textContent;
item.parentElement.remove();
}
});
}
html = processSpinVditorSVDOM(html, vditor);
if (isSVElement) {
blockElement.innerHTML = html;
} else {
blockElement.outerHTML = html;
}

const allLinkRefDefsElement = vditor.sv.element.querySelector("[data-type='link-ref-defs-block']");
if (allLinkRefDefsElement) {
vditor.sv.element.insertAdjacentElement("beforeend", allLinkRefDefsElement.parentElement);
let firstLinkRefDefElement: Element;
const allLinkRefDefsElement = vditor.sv.element.querySelectorAll("[data-type='link-ref-defs-block']");
allLinkRefDefsElement.forEach((item, index) => {
if (index === 0) {
firstLinkRefDefElement = item.parentElement;
} else {
firstLinkRefDefElement.lastElementChild.remove();
firstLinkRefDefElement.insertAdjacentHTML("beforeend", `${item.parentElement.innerHTML}`);
item.parentElement.remove();
}
});
if (allLinkRefDefsElement.length > 0) {
vditor.sv.element.insertAdjacentElement("beforeend", firstLinkRefDefElement);
}

const allFootnoteElement = vditor.sv.element.querySelector("[data-type='footnotes-link']");
if (allFootnoteElement) {
vditor.sv.element.insertAdjacentElement("beforeend", allFootnoteElement.parentElement);
// 脚注合并后添加的末尾
let firstFootnoteElement: Element;
const allFootnoteElement = vditor.sv.element.querySelectorAll("[data-type='footnotes-link']");
allFootnoteElement.forEach((item, index) => {
if (index === 0) {
firstFootnoteElement = item.parentElement;
} else {
firstFootnoteElement.lastElementChild.remove();
firstFootnoteElement.insertAdjacentHTML("beforeend", `${item.parentElement.innerHTML}`);
item.parentElement.remove();
}
});
if (allFootnoteElement.length > 0) {
vditor.sv.element.insertAdjacentElement("beforeend", firstFootnoteElement);
}

setRangeByWbr(vditor.sv.element, range);

scrollCenter(vditor);
Expand Down
58 changes: 29 additions & 29 deletions src/ts/wysiwyg/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,6 @@ export const input = (vditor: IVditor, range: Range, event?: InputEvent) => {
} else {
blockElement.outerHTML = html;

let firstLinkRefDefElement: Element;
const allLinkRefDefsElement = vditor.ir.element.querySelectorAll("[data-type='link-ref-defs-block']");
allLinkRefDefsElement.forEach((item, index) => {
if (index === 0) {
firstLinkRefDefElement = item;
} else {
firstLinkRefDefElement.insertAdjacentHTML("beforeend", item.innerHTML);
item.remove();
}
});
if (allLinkRefDefsElement.length > 0) {
vditor.ir.element.insertAdjacentElement("beforeend", allLinkRefDefsElement[0]);
}

// 脚注合并后添加的末尾
let firstFootnoteElement: Element;
const allFootnoteElement = vditor.wysiwyg.element.querySelectorAll("[data-type='footnotes-block']");
allFootnoteElement.forEach((item, index) => {
if (index === 0) {
firstFootnoteElement = item;
} else {
firstFootnoteElement.insertAdjacentHTML("beforeend", item.innerHTML);
item.remove();
}
});
if (allFootnoteElement.length > 0) {
vditor.ir.element.insertAdjacentElement("beforeend", allFootnoteElement[0]);
}

if (footnoteElement) {
// 更新正文中的 tip
const footnoteItemElement = hasTopClosestByTag(vditor.wysiwyg.element.querySelector("wbr"), "LI");
Expand All @@ -171,6 +142,35 @@ export const input = (vditor: IVditor, range: Range, event?: InputEvent) => {
}
}

let firstLinkRefDefElement: Element;
const allLinkRefDefsElement = vditor.wysiwyg.element.querySelectorAll("[data-type='link-ref-defs-block']");
allLinkRefDefsElement.forEach((item, index) => {
if (index === 0) {
firstLinkRefDefElement = item;
} else {
firstLinkRefDefElement.insertAdjacentHTML("beforeend", item.innerHTML);
item.remove();
}
});
if (allLinkRefDefsElement.length > 0) {
vditor.wysiwyg.element.insertAdjacentElement("beforeend", allLinkRefDefsElement[0]);
}

// 脚注合并后添加的末尾
let firstFootnoteElement: Element;
const allFootnoteElement = vditor.wysiwyg.element.querySelectorAll("[data-type='footnotes-block']");
allFootnoteElement.forEach((item, index) => {
if (index === 0) {
firstFootnoteElement = item;
} else {
firstFootnoteElement.insertAdjacentHTML("beforeend", item.innerHTML);
item.remove();
}
});
if (allFootnoteElement.length > 0) {
vditor.wysiwyg.element.insertAdjacentElement("beforeend", allFootnoteElement[0]);
}

if (hasClosestByHeadings(blockElement) || html.startsWith("<h") || event?.inputType === "deleteContentBackward"
|| event?.inputType === "deleteContentForward") {
renderToc(vditor);
Expand Down

0 comments on commit e8153de

Please sign in to comment.