Skip to content

Commit

Permalink
🎨 #1462
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Sep 2, 2023
1 parent f9e52a8 commit d53f336
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

### v3.9.4 / 2023-09

* [preview.actions 为空不渲染工具栏](https://github.com/Vanessa219/vditor/issues/1453) `改进功能`
* [MathJax 支持传入其他配置项](https://github.com/Vanessa219/vditor/issues/1453) `引入特性`

### v3.9.5 / 2023-08-29
Expand Down
81 changes: 38 additions & 43 deletions src/ts/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ export class Preview {
}
}
});

this.element.appendChild(previewElement);
const actions = vditor.options.preview.actions;
if (actions.length === 0) {
return;
}
const actionElement = document.createElement("div");
actionElement.className = "vditor-preview__action";
const actionHtml: string[] = [];
Expand Down Expand Up @@ -98,49 +101,41 @@ export class Preview {
break;
}
}
if (actions.length === 0) {
actionElement.style.display = "none";
this.element.appendChild(previewElement);
previewElement.style.width = "auto";
} else {
actionElement.innerHTML = actionHtml.join("");
this.element.appendChild(actionElement);
this.element.appendChild(previewElement);

actionElement.addEventListener(getEventName(), (event) => {
const btn = hasClosestByTag(event.target as HTMLElement, "BUTTON");
if (!btn) {
return;
}
const type = btn.getAttribute("data-type");
const actionCustom = actions.find((w: IPreviewActionCustom) => w?.key === type) as IPreviewActionCustom;
if (actionCustom) {
actionCustom.click(type);
return;
}
actionElement.innerHTML = actionHtml.join("");
this.element.appendChild(actionElement);
actionElement.addEventListener(getEventName(), (event) => {
const btn = hasClosestByTag(event.target as HTMLElement, "BUTTON");
if (!btn) {
return;
}
const type = btn.getAttribute("data-type");
const actionCustom = actions.find((w: IPreviewActionCustom) => w?.key === type) as IPreviewActionCustom;
if (actionCustom) {
actionCustom.click(type);
return;
}

if (type === "mp-wechat" || type === "zhihu") {
this.copyToX(vditor, this.element.lastElementChild.cloneNode(true) as HTMLElement, type);
return;
}
if (type === "mp-wechat" || type === "zhihu") {
this.copyToX(vditor, this.element.lastElementChild.cloneNode(true) as HTMLElement, type);
return;
}

if (type === "desktop") {
previewElement.style.width = "auto";
} else if (type === "tablet") {
previewElement.style.width = "780px";
} else {
previewElement.style.width = "360px";
}
if (previewElement.scrollWidth > previewElement.parentElement.clientWidth) {
previewElement.style.width = "auto";
}
this.render(vditor);
actionElement.querySelectorAll("button").forEach((item) => {
item.classList.remove("vditor-preview__action--current");
});
btn.classList.add("vditor-preview__action--current");
if (type === "desktop") {
previewElement.style.width = "auto";
} else if (type === "tablet") {
previewElement.style.width = "780px";
} else {
previewElement.style.width = "360px";
}
if (previewElement.scrollWidth > previewElement.parentElement.clientWidth) {
previewElement.style.width = "auto";
}
this.render(vditor);
actionElement.querySelectorAll("button").forEach((item) => {
item.classList.remove("vditor-preview__action--current");
});
}
btn.classList.add("vditor-preview__action--current");
});
}

public render(vditor: IVditor, value?: string) {
Expand Down Expand Up @@ -278,7 +273,7 @@ export class Preview {
setSelectionFocus(range);
document.execCommand("copy");
this.element.lastElementChild.remove();
vditor.tip.show(['zhihu', 'mp-wechat'].includes(type)? `已复制,可到${type === "zhihu" ? "知乎" : "微信公众号平台"}进行粘贴`: `已复制到剪切板`);

vditor.tip.show(['zhihu', 'mp-wechat'].includes(type) ? `已复制,可到${type === "zhihu" ? "知乎" : "微信公众号平台"}进行粘贴` : `已复制到剪切板`);
}
}

0 comments on commit d53f336

Please sign in to comment.