From f9e52a852e247d3b0cb3f0ba091a8a20f0b40804 Mon Sep 17 00:00:00 2001 From: heiyehk <33891067+heiyehk@users.noreply.github.com> Date: Sat, 2 Sep 2023 12:04:42 +0800 Subject: [PATCH] =?UTF-8?q?Pref(=E4=BC=98=E5=8C=96)preview.actions?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E4=B8=8D=E6=B8=B2=E6=9F=93=20(#1462)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: 在没有action的情况下则不进行dom操作 * pref: update * style: 退回原格式 * bugfix: 修复分屏下不显示 --- src/ts/preview/index.ts | 71 +++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/ts/preview/index.ts b/src/ts/preview/index.ts index 556ad032..63beadc0 100644 --- a/src/ts/preview/index.ts +++ b/src/ts/preview/index.ts @@ -98,46 +98,49 @@ export class Preview { break; } } - actionElement.innerHTML = actionHtml.join(""); if (actions.length === 0) { actionElement.style.display = "none"; - } - this.element.appendChild(actionElement); - this.element.appendChild(previewElement); + 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.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"); + 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"); }); - btn.classList.add("vditor-preview__action--current"); - }); + } } public render(vditor: IVditor, value?: string) {