Przeglądaj źródła

:art: https://github.com/Vanessa219/vditor/pull/1462

Vanessa 2 lat temu
rodzic
commit
d53f336bdf
2 zmienionych plików z 39 dodań i 43 usunięć
  1. 1 0
      CHANGELOG.md
  2. 38 43
      src/ts/preview/index.ts

+ 1 - 0
CHANGELOG.md

@@ -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

+ 38 - 43
src/ts/preview/index.ts

@@ -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[] = [];
@@ -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) {
@@ -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" ? "知乎" : "微信公众号平台"}进行粘贴` : `已复制到剪切板`);
     }
 }