LSPlugin.d.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import EventEmitter from 'eventemitter3'
  2. import * as CSS from 'csstype'
  3. import { LSPluginCaller } from './LSPlugin.caller'
  4. import { LSPluginUser } from './LSPlugin.user'
  5. type PluginLocalIdentity = string
  6. type ThemeOptions = {
  7. name: string
  8. url: string
  9. description?: string
  10. mode?: 'dark' | 'light'
  11. [key: string]: any
  12. }
  13. type StyleString = string;
  14. type StyleOptions = {
  15. key?: string
  16. style: StyleString
  17. }
  18. type UIBaseOptions = {
  19. key?: string
  20. replace?: boolean
  21. template: string
  22. }
  23. type UIPathIdentity = {
  24. path: string // dom selector
  25. }
  26. type UISlotIdentity = {
  27. slot: string // slot key
  28. }
  29. type UISlotOptions = UIBaseOptions & UISlotIdentity
  30. type UIPathOptions = UIBaseOptions & UIPathIdentity
  31. type UIOptions = UIPathOptions | UISlotOptions
  32. interface LSPluginPkgConfig {
  33. id: PluginLocalIdentity
  34. mode: 'shadow' | 'iframe'
  35. themes: Array<ThemeOptions>
  36. icon: string
  37. }
  38. interface LSPluginBaseInfo {
  39. id: string // should be unique
  40. mode: 'shadow' | 'iframe'
  41. settings: {
  42. disabled: boolean
  43. [key: string]: any
  44. },
  45. [key: string]: any
  46. }
  47. type IHookEvent = {
  48. [key: string]: any
  49. }
  50. type IUserHook<E = any> = (callback: (e: IHookEvent & E) => void) => void
  51. type IUserSlotHook<E = any> = (callback: (e: IHookEvent & UISlotIdentity & E) => void) => void
  52. type BlockID = number
  53. type BlockUUID = string
  54. type BlockUUIDTuple = ['uuid', BlockUUID]
  55. type IEntityID = { id: BlockID }
  56. interface AppUserConfigs {
  57. preferredFormat: 'markdown' | 'org'
  58. preferredLanguage: string
  59. preferredWorkflow: string
  60. [key: string]: any
  61. }
  62. interface BlockEntity {
  63. id: number // db id
  64. uuid: string
  65. anchor: string
  66. body: any
  67. children: Array<BlockEntity | BlockUUIDTuple>
  68. container: string
  69. content: string
  70. format: 'markdown' | 'org'
  71. file: IEntityID
  72. left: IEntityID
  73. level: number
  74. meta: { timestamps: any, properties: any, startPos: number, endPos: number }
  75. page: IEntityID
  76. parent: IEntityID
  77. title: Array<any>
  78. unordered: boolean
  79. [key: string]: any
  80. }
  81. type BlockIdentity = BlockUUID | Pick<BlockEntity, 'uuid'>
  82. type BlockPageName = string
  83. type SlashCommandActionCmd =
  84. 'editor/input'
  85. | 'editor/hook'
  86. | 'editor/clear-current-slash'
  87. | 'editor/restore-saved-cursor'
  88. type SlashCommandAction = [cmd: SlashCommandActionCmd, ...args: any]
  89. type BlockCommandCallback = (e: IHookEvent & { uuid: BlockUUID }) => Promise<void>
  90. interface IAppProxy {
  91. getUserInfo: () => Promise<any>
  92. getUserConfigs: () => Promise<AppUserConfigs>
  93. // router
  94. pushState: (k: string, params?: {}) => void
  95. replaceState: (k: string, params?: {}) => void
  96. // ui
  97. showMsg: (content: string, status?: 'success' | 'warning' | string) => void
  98. setZoomFactor: (factor: number) => void
  99. // events
  100. onThemeModeChanged: IUserHook<{ mode: 'dark' | 'light' }>
  101. onBlockRendererMounted: IUserSlotHook<{ uuid: BlockUUID }>
  102. onRouteChanged: IUserHook<{ path: string, template: string }>
  103. onSidebarVisibleChanged: IUserHook<{ visible: boolean }>
  104. }
  105. interface IEditorProxy {
  106. registerSlashCommand: (tag: string, action: BlockCommandCallback | Array<SlashCommandAction>) => boolean
  107. registerBlockContextMenu: (tag: string, action: BlockCommandCallback) => boolean
  108. // block related APIs
  109. checkEditing: () => Promise<BlockUUID | boolean>
  110. insertAtEditingCursor: (content: string) => Promise<void>
  111. getCurrentPage: () => Promise<Partial<BlockEntity> | null>
  112. getCurrentBlock: () => Promise<BlockEntity | null>
  113. getCurrentBlockContent: () => Promise<string>
  114. getCurrentPageBlocksTree: () => Promise<Array<BlockEntity>>
  115. getPageBlocksTree: (pageName: BlockPageName) => Promise<Array<BlockEntity>>
  116. insertBlock: (srcBlock: BlockIdentity, content: string, opts?: Partial<{ before: boolean, sibling: boolean, props: {} }>) => Promise<BlockEntity | null>
  117. updateBlock: (srcBlock: BlockIdentity, content: string, opts?: Partial<{ props: {} }>) => Promise<void>
  118. removeBlock: (srcBlock: BlockIdentity, opts?: Partial<{ includeChildren: boolean }>) => Promise<void>
  119. getBlock: (srcBlock: BlockIdentity, opts?: Partial<{ includeChildren: boolean }>) => Promise<BlockEntity>
  120. moveBlock: (srcBlock: BlockIdentity, targetBlock: BlockIdentity, opts?: Partial<{ before: boolean, children: boolean }>) => Promise<void>
  121. editBlock: (srcBlock: BlockIdentity, opts?: { pos: number }) => Promise<void>
  122. upsertBlockProperty: (block: BlockIdentity, key: string, value: any) => Promise<void>
  123. removeBlockProperty: (block: BlockIdentity, key: string) => Promise<void>
  124. getBlockProperty: (block: BlockIdentity, key: string) => Promise<any>
  125. getBlockProperties: (block: BlockIdentity) => Promise<any>
  126. }
  127. interface IDBProxy {
  128. datascriptQuery: <T = any>(query: string) => Promise<T>
  129. }
  130. interface ILSPluginThemeManager extends EventEmitter {
  131. themes: Map<PluginLocalIdentity, Array<ThemeOptions>>
  132. registerTheme (id: PluginLocalIdentity, opt: ThemeOptions): Promise<void>
  133. unregisterTheme (id: PluginLocalIdentity): Promise<void>
  134. selectTheme (opt?: ThemeOptions): Promise<void>
  135. }
  136. type LSPluginUserEvents = 'ui:visible:changed' | 'settings:changed'
  137. interface ILSPluginUser extends EventEmitter<LSPluginUserEvents> {
  138. /**
  139. * Indicate connected with host
  140. */
  141. connected: boolean
  142. /**
  143. * Duplex message caller
  144. */
  145. caller: LSPluginCaller
  146. /**
  147. * Most from packages
  148. */
  149. baseInfo: LSPluginBaseInfo
  150. /**
  151. * Plugin user settings
  152. */
  153. settings?: LSPluginBaseInfo['settings']
  154. /**
  155. * Ready for host connected
  156. */
  157. ready (model?: Record<string, any>): Promise<any>
  158. ready (callback?: (e: any) => void | {}): Promise<any>
  159. ready (model?: Record<string, any>, callback?: (e: any) => void | {}): Promise<any>
  160. /**
  161. * @param callback
  162. */
  163. beforeunload: (callback: () => Promise<void>) => void
  164. /**
  165. * @param model
  166. */
  167. provideModel (model: Record<string, any>): this
  168. /**
  169. * @param theme options
  170. */
  171. provideTheme (theme: ThemeOptions): this
  172. /**
  173. * @param style
  174. */
  175. provideStyle (style: StyleString | StyleOptions): this
  176. /**
  177. * @param ui options
  178. */
  179. provideUI (ui: UIOptions): this
  180. /**
  181. * @param attrs
  182. */
  183. updateSettings (attrs: Record<string, any>): void
  184. /**
  185. * MainUI for index.html
  186. * @param attrs
  187. */
  188. setMainUIAttrs (attrs: Record<string, any>): void
  189. setMainUIInlineStyle (style: CSS.Properties): void
  190. showMainUI (): void
  191. hideMainUI (): void
  192. toggleMainUI (): void
  193. isMainUIVisible: boolean
  194. App: IAppProxy
  195. Editor: IEditorProxy
  196. DB: IDBProxy
  197. }