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

pull/2455/head
charlie 2021-07-17 15:36:55 +08:00 committed by Tienson Qin
parent 5c426ec603
commit 981bcf5ec6
4 changed files with 43 additions and 28 deletions

View File

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

View File

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

View File

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

View File

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