Просмотр исходного кода

improve(plugin): route changed hook & more types

charlie 4 лет назад
Родитель
Сommit
fa77070e59
3 измененных файлов с 12 добавлено и 9 удалено
  1. 8 7
      libs/src/LSPlugin.d.ts
  2. 3 1
      src/main/frontend/handler/route.cljs
  3. 1 1
      src/main/logseq/api.cljs

+ 8 - 7
libs/src/LSPlugin.d.ts

@@ -61,15 +61,15 @@ type IHookEvent = {
   [key: string]: any
 }
 
-type IUserHook = (callback: (e: IHookEvent) => void) => void
-type IUserSlotHook = (callback: (e: IHookEvent & UISlotIdentity) => void) => void
+type IUserHook<E = any> = (callback: (e: IHookEvent & E) => void) => void
+type IUserSlotHook<E = any> = (callback: (e: IHookEvent & UISlotIdentity & E) => void) => void
 
 type BlockID = number
 type BlockUUID = string
 
 type IEntityID = { id: BlockID }
 
-interface AppConfigs {
+interface AppUserConfigs {
   preferredFormat: 'markdown' | 'org'
   preferredLanguage: string
   preferredWorkflow: string
@@ -99,12 +99,12 @@ interface BlockEntity {
 }
 
 type BlockIdentity = BlockUUID | Pick<BlockEntity, 'uuid'>
-type SlashCommandActionTag = 'editor/input' | 'editor/hook' | 'editor/clear-current-slash'
-type SlashCommandAction = [SlashCommandActionTag, ...args: any]
+type SlashCommandActionCmd = 'editor/input' | 'editor/hook' | 'editor/clear-current-slash'
+type SlashCommandAction = [cmd: SlashCommandActionCmd, ...args: any]
 
 interface IAppProxy {
-  getUserState: () => Promise<any>
-  getAppConfigs: () => Promise<AppConfigs>
+  getUserInfo: () => Promise<any>
+  getUserConfigs: () => Promise<AppUserConfigs>
 
   // router
   pushState: (k: string, params?: {}) => void
@@ -118,6 +118,7 @@ interface IAppProxy {
   onThemeModeChanged: IUserHook
   onPageFileMounted: IUserSlotHook
   onBlockRendererMounted: IUserSlotHook
+  onRouteChanged: IUserHook<{ path: string, template: string }>
 }
 
 interface IEditorProxy {

+ 3 - 1
src/main/frontend/handler/route.cljs

@@ -3,6 +3,7 @@
             [reitit.frontend.easy :as rfe]
             [reitit.frontend.history :as rfh]
             [frontend.state :as state]
+            [frontend.handler.plugin :as plugin-handler]
             [goog.dom :as gdom]
             [frontend.handler.ui :as ui-handler]
             [frontend.db :as db]
@@ -95,7 +96,8 @@
     (update-page-label! route)
     (when-let [anchor (get-in route [:query-params :anchor])]
       (jump-to-anchor! anchor)
-      (util/scroll-to-top))))
+      (util/scroll-to-top))
+    (plugin-handler/hook-plugin-app :route-changed (select-keys route [:template :path :parameters]))))
 
 (defn go-to-search!
   [search-mode]

+ 1 - 1
src/main/logseq/api.cljs

@@ -43,7 +43,7 @@
         (js/console.error "[parse hiccup error]" e) input))))
 
 ;; base
-(def ^:export get_app_configs
+(def ^:export get_user_configs
   (fn []
     (bean/->js
       (normalize-keyword-for-json