Browse Source

:art: https://github.com/siyuan-note/siyuan/issues/5066 add index for menu

Vanessa 2 năm trước cách đây
mục cha
commit
19dd0cd796
4 tập tin đã thay đổi với 15 bổ sung8 xóa
  1. 1 1
      app/src/asset/index.ts
  2. 11 4
      app/src/menus/Menu.ts
  3. 2 3
      app/src/plugin/API.ts
  4. 1 0
      app/src/types/index.d.ts

+ 1 - 1
app/src/asset/index.ts

@@ -33,7 +33,7 @@ export class Asset extends Model {
         this.element.addEventListener("click", (event) => {
             setPanelFocus(this.element.parentElement.parentElement);
             this.app.plugins.forEach(item => {
-                item.eventBus.emit("click-pdf", event);
+                item.eventBus.emit("click-pdf", {event});
             });
         });
         if (typeof this.pdfId === "string") {

+ 11 - 4
app/src/menus/Menu.ts

@@ -82,13 +82,13 @@ export class Menu {
         }
     }
 
-    public addSeparator() {
-        this.addItem({type: "separator"});
+    public addSeparator(index?: number) {
+        this.addItem({type: "separator", index});
     }
 
     public addItem(option: IMenu) {
         const menuItem = new MenuItem(option);
-        this.append(menuItem.element);
+        this.append(menuItem.element, option.index);
         return menuItem.element;
     }
 
@@ -111,10 +111,17 @@ export class Menu {
         window.siyuan.menus.menu.element.removeAttribute("data-name");    // 标识再次点击不消失
     }
 
-    public append(element?: HTMLElement) {
+    public append(element?: HTMLElement, index?: number) {
         if (!element) {
             return;
         }
+        if (typeof index === "number") {
+            const insertElement = this.element.querySelectorAll(".b3-menu__items > .b3-menu__separator")[index]
+            if (insertElement) {
+                insertElement.before(element);
+                return;
+            }
+        }
         this.element.lastElementChild.append(element);
     }
 

+ 2 - 3
app/src/plugin/API.ts

@@ -2,7 +2,6 @@ import {confirmDialog} from "../dialog/confirmDialog";
 import {Plugin} from "./index";
 import {showMessage} from "../dialog/message";
 import {Dialog} from "../dialog";
-import {MenuItem} from "../menus/Menu";
 import {Menu as SiyuanMenu} from "../menus/Menu";
 import {fetchGet, fetchPost, fetchSyncPost} from "../util/fetch";
 import {isMobile} from "../util/functions";
@@ -42,11 +41,11 @@ export class Menu {
         return this.menu.addItem(option);
     }
 
-    addSeparator() {
+    addSeparator(index?: number) {
         if (this.isOpen) {
             return;
         }
-        this.menu.addSeparator();
+        this.menu.addSeparator(index);
     }
 
     open(options: { x: number, y: number, h?: number, w?: number, isLeft: false }) {

+ 1 - 0
app/src/types/index.d.ts

@@ -724,6 +724,7 @@ declare interface IMenu {
     iconHTML?: string
     current?: boolean
     bind?: (element: HTMLElement) => void
+    index?: number
 }
 
 declare interface IBazaarItem {