Browse Source

improve(plugin): add types and api of scrolling to block in page

charlie 4 years ago
parent
commit
981bcf5ec6
4 changed files with 43 additions and 28 deletions
  1. 10 5
      libs/src/LSPlugin.ts
  2. 17 11
      libs/src/LSPlugin.user.ts
  3. 8 8
      src/main/frontend/handler/plugin.cljs
  4. 8 4
      src/main/logseq/api.cljs

+ 10 - 5
libs/src/LSPlugin.ts

@@ -177,8 +177,8 @@ export interface IAppProxy {
   getCurrentGraph: () => Promise<AppGraphInfo | null>
 
   // router
-  pushState: (k: string, params?: {}) => void
-  replaceState: (k: string, params?: {}) => void
+  pushState: (k: string, params?: Record<string, any>, query?: Record<string, any>) => void
+  replaceState: (k: string, params?: Record<string, any>, query?: Record<string, any>) => void
 
   // ui
   showMsg: (content: string, status?: 'success' | 'warning' | string) => void
@@ -187,12 +187,12 @@ export interface IAppProxy {
   registerUIItem: (
     type: 'toolbar' | 'pagebar',
     opts: { key: string, template: string }
-  ) => boolean
+  ) => void
 
   registerPageMenuItem: (
     tag: string,
     action: (e: IHookEvent & { page: string }) => void
-  ) => unknown
+  ) => void
 
   // events
   onCurrentGraphChanged: IUserHook
@@ -231,7 +231,7 @@ export interface IEditorProxy extends Record<string, any> {
   registerSlashCommand: (
     tag: string,
     action: BlockCommandCallback | Array<SlashCommandAction>
-  ) => boolean
+  ) => unknown
 
   /**
    * register a custom command in the block context menu (triggered by right clicking the block dot)
@@ -339,6 +339,11 @@ export interface IEditorProxy extends Record<string, any> {
   getBlockProperty: (block: BlockIdentity, key: string) => Promise<any>
 
   getBlockProperties: (block: BlockIdentity) => Promise<any>
+
+  scrollToBlockInPage: (
+    pageName: BlockPageName,
+    blockId: BlockIdentity
+  ) => void
 }
 
 /**

+ 17 - 11
libs/src/LSPlugin.user.ts

@@ -10,7 +10,7 @@ import {
   BlockCommandCallback,
   StyleString,
   ThemeOptions,
-  UIOptions, IHookEvent
+  UIOptions, IHookEvent, BlockIdentity, BlockPageName
 } from './LSPlugin'
 import Debug from 'debug'
 import * as CSS from 'csstype'
@@ -24,6 +24,7 @@ declare global {
   }
 }
 
+const PROXY_CONTINUE = Symbol.for('proxy-continue')
 const debug = Debug('LSPlugin:user')
 
 /**
@@ -67,15 +68,13 @@ const app: Partial<IAppProxy> = {
       method: 'register-plugin-ui-item',
       args: [pid, type, opts]
     })
-
-    return false
   },
 
   registerPageMenuItem (
     this: LSPluginUser,
     tag: string,
     action: (e: IHookEvent & { page: string }) => void
-  ): unknown {
+  ) {
     if (typeof action !== 'function') {
       return false
     }
@@ -88,8 +87,6 @@ const app: Partial<IAppProxy> = {
       type, {
         key, label
       }, action)
-
-    return false
   }
 }
 
@@ -142,15 +139,13 @@ const editor: Partial<IEditorProxy> = {
       method: 'register-plugin-slash-command',
       args: [this.baseInfo.id, [tag, actions]]
     })
-
-    return false
   },
 
   registerBlockContextMenuItem (
     this: LSPluginUser,
     tag: string,
     action: BlockCommandCallback
-  ): unknown {
+  ) {
     if (typeof action !== 'function') {
       return false
     }
@@ -163,8 +158,19 @@ const editor: Partial<IEditorProxy> = {
       type, {
         key, label
       }, action)
+  },
 
-    return false
+  scrollToBlockInPage (
+    this: LSPluginUser,
+    pageName: BlockPageName,
+    blockId: BlockIdentity
+  ) {
+    const anchor = `block-content-` + blockId
+    this.App.pushState(
+      'page',
+      { name: pageName },
+      { anchor }
+    )
   }
 }
 
@@ -363,7 +369,7 @@ export class LSPluginUser extends EventEmitter<LSPluginUserEvents> implements IL
         return function (this: any, ...args: any) {
           if (origMethod) {
             const ret = origMethod.apply(that, args)
-            if (ret === false) return
+            if (ret !== PROXY_CONTINUE) return
           }
 
           // Handle hook

+ 8 - 8
src/main/frontend/handler/plugin.cljs

@@ -40,11 +40,11 @@
 
 (defn register-plugin-slash-command
   [pid [cmd actions]]
-  (prn (if-let [pid (keyword pid)]
-         (when (contains? (:plugin/installed-plugins @state/state) pid)
-           (do (swap! state/state update-in [:plugin/installed-commands pid]
-                      (fnil merge {}) (hash-map cmd (mapv #(conj % {:pid pid}) actions)))
-               true)))))
+  (when-let [pid (keyword pid)]
+    (when (contains? (:plugin/installed-plugins @state/state) pid)
+      (do (swap! state/state update-in [:plugin/installed-commands pid]
+                 (fnil merge {}) (hash-map cmd (mapv #(conj % {:pid pid}) actions)))
+          true))))
 
 (defn unregister-plugin-slash-command
   [pid]
@@ -52,8 +52,8 @@
 
 (defn register-plugin-simple-command
   ;; action => [:action-key :event-key]
-  [pid {:keys [key label type] :as cmd}  action]
-  (if-let [pid (keyword pid)]
+  [pid {:keys [key label type] :as cmd} action]
+  (when-let [pid (keyword pid)]
     (when (contains? (:plugin/installed-plugins @state/state) pid)
       (do (swap! state/state update-in [:plugin/simple-commands pid]
                  (fnil conj []) [type cmd action pid])
@@ -66,7 +66,7 @@
 (defn register-plugin-ui-item
   [pid {:keys [key type template] :as opts}]
   (when-let [pid (keyword pid)]
-    (when (or true (contains? (:plugin/installed-plugins @state/state) pid))
+    (when (contains? (:plugin/installed-plugins @state/state) pid)
       (do (swap! state/state update-in [:plugin/installed-ui-items pid]
                  (fnil conj []) [type opts pid])
           true))))

+ 8 - 4
src/main/logseq/api.cljs

@@ -242,14 +242,18 @@
       (js/apis.openExternal url))))
 
 (def ^:export push_state
-  (fn [^js k ^js params]
+  (fn [^js k ^js params ^js query]
     (rfe/push-state
-     (keyword k) (bean/->clj params))))
+     (keyword k)
+     (bean/->clj params)
+     (bean/->clj query))))
 
 (def ^:export replace_state
-  (fn [^js k ^js params]
+  (fn [^js k ^js params ^js query]
     (rfe/replace-state
-     (keyword k) (bean/->clj params))))
+     (keyword k)
+     (bean/->clj params)
+     (bean/->clj query))))
 
 ;; editor
 (def ^:export check_editing