Ver Fonte

fix: electron menu handler(Redo, Undo, Copy) (#8466)

* fix(electron): menu undo & redo not working
* refactor(electron): use editMenu from electron src code
* chore(electron): add FIXME comments
* fix(electron): menu copy & cut not working
* remove: custom fn in menu cut
* fix: add accelerator to menu copy item
situ2001 há 2 anos atrás
pai
commit
29da7386cb
2 ficheiros alterados com 48 adições e 2 exclusões
  1. 36 1
      src/electron/electron/core.cljs
  2. 12 1
      src/main/electron/listener.cljs

+ 36 - 1
src/electron/electron/core.cljs

@@ -228,7 +228,42 @@
                                   (if mac?
                                     {:role "close"}
                                     {:role "quit"})]}
-                       {:role "editMenu"}
+                       {:role "editMenu"
+                        ;; https://github.com/electron/electron/blob/85f41d59aceabbdeee1fdec75770249c6335e73a/lib/browser/api/menu-item-roles.ts#L239-L276
+                        :submenu (concat
+                                  [{:label "Undo"
+                                    :click (fn []
+                                             (let [browser-window ^js/BrowserWindow @*win
+                                                   web-contents (.-webContents browser-window)]
+                                               (.send web-contents "invokeEditorHandler" "undo")))
+                                    :accelerator "CommandOrControl+Z"}
+                                   {:label "Redo"
+                                    :click (fn []
+                                             (let [browser-window ^js/BrowserWindow @*win
+                                                   web-contents (.-webContents browser-window)]
+                                               (.send web-contents "invokeEditorHandler" "redo")))
+                                    :accelerator "Shift+CommandOrControl+Z"}
+                                   {:type "separator"}
+                                   {:role "cut"}
+                                   {:label "Copy"
+                                    :click (fn []
+                                             (let [browser-window ^js/BrowserWindow @*win
+                                                   web-contents (.-webContents browser-window)]
+                                               (.send web-contents "invokeEditorHandler" "copy")))
+                                    :accelerator "CommandOrControl+C"}
+                                   {:role "paste"}]
+
+                                  (if mac?
+                                    [{:role "pasteAndMatchStyle"}
+                                     {:role "delete"}
+                                     {:role "selectAll"} ;; FIXME not work as expected
+                                     {:type "separator"}
+                                     {:label "Speech"
+                                      :submenu [{:role "startSpeaking"},
+                                                {:role "stopSpeaking"}]}]
+                                    [{:role "delete"}
+                                     {:type "separator"}
+                                     {:role "selectAll"}]))}
                        {:role "viewMenu"}
                        {:role "windowMenu"})
         ;; Windows has no about role

+ 12 - 1
src/main/electron/listener.cljs

@@ -12,6 +12,7 @@
             [frontend.fs.watcher-handler :as watcher-handler]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.file-sync :as file-sync-handler]
+            [frontend.handler.history :as history]
             [frontend.handler.notification :as notification]
             [frontend.handler.repo :as repo-handler]
             [frontend.handler.route :as route-handler]
@@ -181,7 +182,17 @@
 
   (js/window.apis.on "syncAPIServerState"
                      (fn [^js data]
-                       (state/set-state! :electron/server (bean/->clj data)))))
+                       (state/set-state! :electron/server (bean/->clj data))))
+
+  (js/window.apis.on "invokeEditorHandler"
+                     (fn [action]
+                       (println "invokeEditorHandler with action:" action)
+                       (case action
+                         "undo" (history/undo! nil)
+                         "redo" (history/redo! nil)
+                         "copy" (editor-handler/shortcut-copy nil)
+                         "cut" (editor-handler/shortcut-cut nil) ;; FIXME this handler relies on arg Event
+                         ))))
 
 (defn listen!
   []