diff --git a/libs/src/LSPlugin.ts b/libs/src/LSPlugin.ts index 83962e0d8..f5aed843b 100644 --- a/libs/src/LSPlugin.ts +++ b/libs/src/LSPlugin.ts @@ -177,8 +177,8 @@ export interface IAppProxy { getCurrentGraph: () => Promise // router - pushState: (k: string, params?: {}) => void - replaceState: (k: string, params?: {}) => void + pushState: (k: string, params?: Record, query?: Record) => void + replaceState: (k: string, params?: Record, query?: Record) => 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 { registerSlashCommand: ( tag: string, action: BlockCommandCallback | Array - ) => 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 { getBlockProperty: (block: BlockIdentity, key: string) => Promise getBlockProperties: (block: BlockIdentity) => Promise + + scrollToBlockInPage: ( + pageName: BlockPageName, + blockId: BlockIdentity + ) => void } /** diff --git a/libs/src/LSPlugin.user.ts b/libs/src/LSPlugin.user.ts index 34ff304d1..fb13c1e27 100644 --- a/libs/src/LSPlugin.user.ts +++ b/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 = { 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 = { type, { key, label }, action) - - return false } } @@ -142,15 +139,13 @@ const editor: Partial = { 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 = { 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 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 diff --git a/src/main/frontend/handler/plugin.cljs b/src/main/frontend/handler/plugin.cljs index 487709407..04034892f 100644 --- a/src/main/frontend/handler/plugin.cljs +++ b/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)))) diff --git a/src/main/logseq/api.cljs b/src/main/logseq/api.cljs index 92652a961..939abbe34 100644 --- a/src/main/logseq/api.cljs +++ b/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