LSPlugin.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. import EventEmitter from 'eventemitter3'
  2. import * as CSS from 'csstype'
  3. import { LSPluginCaller } from './LSPlugin.caller'
  4. import { LSPluginFileStorage } from './modules/LSPlugin.Storage'
  5. import { LSPluginExperiments } from './modules/LSPlugin.Experiments'
  6. export type PluginLocalIdentity = string
  7. export type ThemeOptions = {
  8. name: string
  9. url: string
  10. description?: string
  11. mode?: 'dark' | 'light'
  12. [key: string]: any
  13. }
  14. export type StyleString = string
  15. export type StyleOptions = {
  16. key?: string
  17. style: StyleString
  18. }
  19. export type UIContainerAttrs = {
  20. draggable: boolean
  21. resizable: boolean
  22. [key: string]: any
  23. }
  24. export type UIBaseOptions = {
  25. key?: string
  26. replace?: boolean
  27. template: string | null
  28. style?: CSS.Properties
  29. attrs?: Record<string, string>
  30. close?: 'outside' | string
  31. reset?: boolean // reset slot content or not
  32. }
  33. export type UIPathIdentity = {
  34. /**
  35. * DOM selector
  36. */
  37. path: string
  38. }
  39. export type UISlotIdentity = {
  40. /**
  41. * Slot key
  42. */
  43. slot: string
  44. }
  45. export type UISlotOptions = UIBaseOptions & UISlotIdentity
  46. export type UIPathOptions = UIBaseOptions & UIPathIdentity
  47. export type UIOptions = UIBaseOptions | UIPathOptions | UISlotOptions
  48. export interface LSPluginPkgConfig {
  49. id: PluginLocalIdentity
  50. main: string
  51. entry: string // alias of main
  52. title: string
  53. mode: 'shadow' | 'iframe'
  54. themes: Array<ThemeOptions>
  55. icon: string
  56. [key: string]: any
  57. }
  58. export interface LSPluginBaseInfo {
  59. id: string // should be unique
  60. mode: 'shadow' | 'iframe'
  61. settings: {
  62. disabled: boolean
  63. [key: string]: any
  64. }
  65. [key: string]: any
  66. }
  67. export type IHookEvent = {
  68. [key: string]: any
  69. }
  70. export type IUserOffHook = () => void
  71. export type IUserHook<E = any, R = IUserOffHook> = (
  72. callback: (e: IHookEvent & E) => void
  73. ) => IUserOffHook
  74. export type IUserSlotHook<E = any> = (
  75. callback: (e: IHookEvent & UISlotIdentity & E) => void
  76. ) => void
  77. export type EntityID = number
  78. export type BlockUUID = string
  79. export type BlockUUIDTuple = ['uuid', BlockUUID]
  80. export type IEntityID = { id: EntityID; [key: string]: any }
  81. export type IBatchBlock = {
  82. content: string
  83. properties?: Record<string, any>
  84. children?: Array<IBatchBlock>
  85. }
  86. export type IDatom = [e: number, a: string, v: any, t: number, added: boolean]
  87. export type IGitResult = { stdout: string; stderr: string; exitCode: number }
  88. export interface AppUserInfo {
  89. [key: string]: any
  90. }
  91. export interface AppInfo {
  92. version: string
  93. [key: string]: any
  94. }
  95. /**
  96. * User's app configurations
  97. */
  98. export interface AppUserConfigs {
  99. preferredThemeMode: 'dark' | 'light'
  100. preferredFormat: 'markdown' | 'org'
  101. preferredDateFormat: string
  102. preferredStartOfWeek: string
  103. preferredLanguage: string
  104. preferredWorkflow: string
  105. currentGraph: string
  106. showBracket: boolean
  107. [key: string]: any
  108. }
  109. /**
  110. * In Logseq, a graph represents a repository of connected pages and blocks
  111. */
  112. export interface AppGraphInfo {
  113. name: string
  114. url: string
  115. path: string
  116. [key: string]: any
  117. }
  118. /**
  119. * Block - Logseq's fundamental data structure.
  120. */
  121. export interface BlockEntity {
  122. id: EntityID // db id
  123. uuid: BlockUUID
  124. left: IEntityID
  125. format: 'markdown' | 'org'
  126. parent: IEntityID
  127. unordered: boolean
  128. content: string
  129. page: IEntityID
  130. // optional fields in dummy page
  131. anchor?: string
  132. body?: any
  133. children?: Array<BlockEntity | BlockUUIDTuple>
  134. container?: string
  135. file?: IEntityID
  136. level?: number
  137. meta?: { timestamps: any; properties: any; startPos: number; endPos: number }
  138. title?: Array<any>
  139. [key: string]: any
  140. }
  141. /**
  142. * Page is just a block with some specific properties.
  143. */
  144. export interface PageEntity {
  145. id: EntityID
  146. uuid: BlockUUID
  147. name: string
  148. originalName: string
  149. 'journal?': boolean
  150. file?: IEntityID
  151. namespace?: IEntityID
  152. children?: Array<PageEntity>
  153. format?: 'markdown' | 'org'
  154. journalDay?: number
  155. }
  156. export type BlockIdentity = BlockUUID | Pick<BlockEntity, 'uuid'>
  157. export type BlockPageName = string
  158. export type PageIdentity = BlockPageName | BlockIdentity
  159. export type SlashCommandActionCmd =
  160. | 'editor/input'
  161. | 'editor/hook'
  162. | 'editor/clear-current-slash'
  163. | 'editor/restore-saved-cursor'
  164. export type SlashCommandAction = [cmd: SlashCommandActionCmd, ...args: any]
  165. export type SimpleCommandCallback = (e: IHookEvent) => void
  166. export type BlockCommandCallback = (
  167. e: IHookEvent & { uuid: BlockUUID }
  168. ) => Promise<void>
  169. export type BlockCursorPosition = {
  170. left: number
  171. top: number
  172. height: number
  173. pos: number
  174. rect: DOMRect
  175. }
  176. export type SimpleCommandKeybinding = {
  177. mode?: 'global' | 'non-editing' | 'editing'
  178. binding: string
  179. mac?: string // special for Mac OS
  180. }
  181. export type SettingSchemaDesc = {
  182. key: string
  183. type: 'string' | 'number' | 'boolean' | 'enum' | 'object'
  184. default: string | number | boolean | Array<any> | object | null
  185. title: string
  186. description: string // support markdown
  187. inputAs?: 'color' | 'date' | 'datetime-local' | 'range'
  188. enumChoices?: Array<string>
  189. enumPicker?: 'select' | 'radio' | 'checkbox' // default: select
  190. }
  191. export type ExternalCommandType =
  192. | 'logseq.command/run'
  193. | 'logseq.editor/cycle-todo'
  194. | 'logseq.editor/down'
  195. | 'logseq.editor/up'
  196. | 'logseq.editor/expand-block-children'
  197. | 'logseq.editor/collapse-block-children'
  198. | 'logseq.editor/open-file-in-default-app'
  199. | 'logseq.editor/open-file-in-directory'
  200. | 'logseq.editor/select-all-blocks'
  201. | 'logseq.editor/toggle-open-blocks'
  202. | 'logseq.editor/zoom-in'
  203. | 'logseq.editor/zoom-out'
  204. | 'logseq.editor/indent'
  205. | 'logseq.editor/outdent'
  206. | 'logseq.editor/copy'
  207. | 'logseq.editor/cut'
  208. | 'logseq.go/home'
  209. | 'logseq.go/journals'
  210. | 'logseq.go/keyboard-shortcuts'
  211. | 'logseq.go/next-journal'
  212. | 'logseq.go/prev-journal'
  213. | 'logseq.go/search'
  214. | 'logseq.go/search-in-page'
  215. | 'logseq.go/tomorrow'
  216. | 'logseq.go/backward'
  217. | 'logseq.go/forward'
  218. | 'logseq.search/re-index'
  219. | 'logseq.sidebar/clear'
  220. | 'logseq.sidebar/open-today-page'
  221. | 'logseq.ui/goto-plugins'
  222. | 'logseq.ui/select-theme-color'
  223. | 'logseq.ui/toggle-brackets'
  224. | 'logseq.ui/toggle-cards'
  225. | 'logseq.ui/toggle-contents'
  226. | 'logseq.ui/toggle-document-mode'
  227. | 'logseq.ui/toggle-help'
  228. | 'logseq.ui/toggle-left-sidebar'
  229. | 'logseq.ui/toggle-right-sidebar'
  230. | 'logseq.ui/toggle-settings'
  231. | 'logseq.ui/toggle-theme'
  232. | 'logseq.ui/toggle-wide-mode'
  233. | 'logseq.command-palette/toggle'
  234. export type UserProxyTags = 'app' | 'editor' | 'db' | 'git' | 'ui' | 'assets'
  235. /**
  236. * App level APIs
  237. */
  238. export interface IAppProxy {
  239. /**
  240. * @added 0.0.4
  241. * @param key
  242. */
  243. getInfo: (key?: keyof AppInfo) => Promise<AppInfo | any>
  244. getUserInfo: () => Promise<AppUserInfo | null>
  245. getUserConfigs: () => Promise<AppUserConfigs>
  246. // commands
  247. registerCommand: (
  248. type: string,
  249. opts: {
  250. key: string
  251. label: string
  252. desc?: string
  253. palette?: boolean
  254. keybinding?: SimpleCommandKeybinding
  255. },
  256. action: SimpleCommandCallback
  257. ) => void
  258. registerCommandPalette: (
  259. opts: {
  260. key: string
  261. label: string
  262. keybinding?: SimpleCommandKeybinding
  263. },
  264. action: SimpleCommandCallback
  265. ) => void
  266. registerCommandShortcut: (
  267. keybinding: SimpleCommandKeybinding,
  268. action: SimpleCommandCallback
  269. ) => void
  270. invokeExternalCommand: (
  271. type: ExternalCommandType,
  272. ...args: Array<any>
  273. ) => Promise<void>
  274. /**
  275. * Get state from app store
  276. * valid state is here
  277. * https://github.com/logseq/logseq/blob/master/src/main/frontend/state.cljs#L27
  278. *
  279. * @example
  280. * ```ts
  281. * const isDocMode = await logseq.App.getStateFromStore('document/mode?')
  282. * ```
  283. * @param path
  284. */
  285. getStateFromStore: <T = any>(path: string | Array<string>) => Promise<T>
  286. // native
  287. relaunch: () => Promise<void>
  288. quit: () => Promise<void>
  289. openExternalLink: (url: string) => Promise<void>
  290. /**
  291. * @deprecated Using `logseq.Git.execCommand`
  292. * @link https://github.com/desktop/dugite/blob/master/docs/api/exec.md
  293. * @param args
  294. */
  295. execGitCommand: (args: string[]) => Promise<string>
  296. // graph
  297. getCurrentGraph: () => Promise<AppGraphInfo | null>
  298. // router
  299. pushState: (
  300. k: string,
  301. params?: Record<string, any>,
  302. query?: Record<string, any>
  303. ) => void
  304. replaceState: (
  305. k: string,
  306. params?: Record<string, any>,
  307. query?: Record<string, any>
  308. ) => void
  309. // ui
  310. queryElementById: (id: string) => Promise<string | boolean>
  311. /**
  312. * @added 0.0.5
  313. * @param selector
  314. */
  315. queryElementRect: (selector: string) => Promise<DOMRectReadOnly | null>
  316. /**
  317. * @deprecated
  318. * @param content
  319. * @param status
  320. */
  321. showMsg: (
  322. content: string,
  323. status?: 'success' | 'warning' | 'error' | string
  324. ) => void
  325. setZoomFactor: (factor: number) => void
  326. setFullScreen: (flag: boolean | 'toggle') => void
  327. setLeftSidebarVisible: (flag: boolean | 'toggle') => void
  328. setRightSidebarVisible: (flag: boolean | 'toggle') => void
  329. registerUIItem: (
  330. type: 'toolbar' | 'pagebar',
  331. opts: { key: string; template: string }
  332. ) => void
  333. registerPageMenuItem: (
  334. tag: string,
  335. action: (e: IHookEvent & { page: string }) => void
  336. ) => void
  337. // hook events
  338. onCurrentGraphChanged: IUserHook
  339. onThemeModeChanged: IUserHook<{ mode: 'dark' | 'light' }>
  340. onBlockRendererSlotted: IUserSlotHook<{ uuid: BlockUUID }>
  341. /**
  342. * provide ui slot to block `renderer` macro for `{{renderer arg1, arg2}}`
  343. *
  344. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-pomodoro-timer
  345. * @example
  346. * ```ts
  347. * // e.g. {{renderer :h1, hello world, green}}
  348. *
  349. * logseq.App.onMacroRendererSlotted(({ slot, payload: { arguments } }) => {
  350. * let [type, text, color] = arguments
  351. * if (type !== ':h1') return
  352. * logseq.provideUI({
  353. * key: 'h1-playground',
  354. * slot, template: `
  355. * <h2 style="color: ${color || 'red'}">${text}</h2>
  356. * `,
  357. * })
  358. * })
  359. * ```
  360. */
  361. onMacroRendererSlotted: IUserSlotHook<{
  362. payload: { arguments: Array<string>; uuid: string; [key: string]: any }
  363. }>
  364. onPageHeadActionsSlotted: IUserSlotHook
  365. onRouteChanged: IUserHook<{ path: string; template: string }>
  366. onSidebarVisibleChanged: IUserHook<{ visible: boolean }>
  367. // internal
  368. _installPluginHook: (pid: string, hook: string) => void
  369. _uninstallPluginHook: (pid: string, hookOrAll: string | boolean) => void
  370. }
  371. /**
  372. * Editor related APIs
  373. */
  374. export interface IEditorProxy extends Record<string, any> {
  375. /**
  376. * register a custom command which will be added to the Logseq slash command list
  377. * @param tag - displayed name of command
  378. * @param action - can be a single callback function to run when the command is called, or an array of fixed commands with arguments
  379. *
  380. *
  381. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-slash-commands
  382. *
  383. * @example
  384. * ```ts
  385. * logseq.Editor.registerSlashCommand("Say Hi", () => {
  386. * console.log('Hi!')
  387. * })
  388. * ```
  389. *
  390. * @example
  391. * ```ts
  392. * logseq.Editor.registerSlashCommand("💥 Big Bang", [
  393. * ["editor/hook", "customCallback"],
  394. * ["editor/clear-current-slash"],
  395. * ]);
  396. * ```
  397. */
  398. registerSlashCommand: (
  399. tag: string,
  400. action: BlockCommandCallback | Array<SlashCommandAction>
  401. ) => unknown
  402. /**
  403. * register a custom command in the block context menu (triggered by right clicking the block dot)
  404. * @param tag - displayed name of command
  405. * @param action - can be a single callback function to run when the command is called
  406. */
  407. registerBlockContextMenuItem: (
  408. tag: string,
  409. action: BlockCommandCallback
  410. ) => unknown
  411. // block related APIs
  412. checkEditing: () => Promise<BlockUUID | boolean>
  413. insertAtEditingCursor: (content: string) => Promise<void>
  414. restoreEditingCursor: () => Promise<void>
  415. exitEditingMode: (selectBlock?: boolean) => Promise<void>
  416. getEditingCursorPosition: () => Promise<BlockCursorPosition | null>
  417. getEditingBlockContent: () => Promise<string>
  418. getCurrentPage: () => Promise<PageEntity | BlockEntity | null>
  419. getCurrentBlock: () => Promise<BlockEntity | null>
  420. getSelectedBlocks: () => Promise<Array<BlockEntity> | null>
  421. /**
  422. * get all blocks of the current page as a tree structure
  423. *
  424. * @example
  425. * ```ts
  426. * const blocks = await logseq.Editor.getCurrentPageBlocksTree()
  427. * initMindMap(blocks)
  428. * ```
  429. */
  430. getCurrentPageBlocksTree: () => Promise<Array<BlockEntity>>
  431. /**
  432. * get all blocks for the specified page
  433. *
  434. * @param srcPage - the page name or uuid
  435. */
  436. getPageBlocksTree: (srcPage: PageIdentity) => Promise<Array<BlockEntity>>
  437. /**
  438. * get all page/block linked references
  439. * @param srcPage
  440. */
  441. getPageLinkedReferences: (
  442. srcPage: PageIdentity
  443. ) => Promise<Array<[page: PageEntity, blocks: Array<BlockEntity>]> | null>
  444. /**
  445. * get flatten pages from top namespace
  446. * @param namespace
  447. */
  448. getPagesFromNamespace: (
  449. namespace: BlockPageName
  450. ) => Promise<Array<PageEntity> | null>
  451. /**
  452. * construct pages tree from namespace pages
  453. * @param namespace
  454. */
  455. getPagesTreeFromNamespace: (
  456. namespace: BlockPageName
  457. ) => Promise<Array<PageEntity> | null>
  458. /**
  459. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-reddit-hot-news
  460. *
  461. * @param srcBlock
  462. * @param content
  463. * @param opts
  464. */
  465. insertBlock: (
  466. srcBlock: BlockIdentity,
  467. content: string,
  468. opts?: Partial<{
  469. before: boolean
  470. sibling: boolean
  471. isPageBlock: boolean
  472. properties: {}
  473. }>
  474. ) => Promise<BlockEntity | null>
  475. insertBatchBlock: (
  476. srcBlock: BlockIdentity,
  477. batch: IBatchBlock | Array<IBatchBlock>,
  478. opts?: Partial<{ before: boolean; sibling: boolean }>
  479. ) => Promise<Array<BlockEntity> | null>
  480. updateBlock: (
  481. srcBlock: BlockIdentity,
  482. content: string,
  483. opts?: Partial<{ properties: {} }>
  484. ) => Promise<void>
  485. removeBlock: (srcBlock: BlockIdentity) => Promise<void>
  486. getBlock: (
  487. srcBlock: BlockIdentity | EntityID,
  488. opts?: Partial<{ includeChildren: boolean }>
  489. ) => Promise<BlockEntity | null>
  490. /**
  491. * @example
  492. *
  493. * ```ts
  494. * logseq.Editor.setBlockCollapsed('uuid', true)
  495. * logseq.Editor.setBlockCollapsed('uuid', 'toggle')
  496. * ```
  497. * @param uuid
  498. * @param opts
  499. */
  500. setBlockCollapsed: (
  501. uuid: BlockUUID,
  502. opts: { flag: boolean | 'toggle' } | boolean | 'toggle'
  503. ) => Promise<void>
  504. getPage: (
  505. srcPage: PageIdentity | EntityID,
  506. opts?: Partial<{ includeChildren: boolean }>
  507. ) => Promise<PageEntity | null>
  508. createPage: (
  509. pageName: BlockPageName,
  510. properties?: {},
  511. opts?: Partial<{
  512. redirect: boolean
  513. createFirstBlock: boolean
  514. format: BlockEntity['format']
  515. journal: boolean
  516. }>
  517. ) => Promise<PageEntity | null>
  518. deletePage: (pageName: BlockPageName) => Promise<void>
  519. renamePage: (oldName: string, newName: string) => Promise<void>
  520. getAllPages: (repo?: string) => Promise<any>
  521. prependBlockInPage: (page: PageIdentity, content: string, opts?: Partial<{ properties: {} }>) => Promise<BlockEntity | null>
  522. appendBlockInPage: (page: PageIdentity, content: string, opts?: Partial<{ properties: {} }>) => Promise<BlockEntity | null>
  523. getPreviousSiblingBlock: (
  524. srcBlock: BlockIdentity
  525. ) => Promise<BlockEntity | null>
  526. getNextSiblingBlock: (srcBlock: BlockIdentity) => Promise<BlockEntity | null>
  527. moveBlock: (
  528. srcBlock: BlockIdentity,
  529. targetBlock: BlockIdentity,
  530. opts?: Partial<{ before: boolean; children: boolean }>
  531. ) => Promise<void>
  532. editBlock: (srcBlock: BlockIdentity, opts?: { pos: number }) => Promise<void>
  533. upsertBlockProperty: (
  534. block: BlockIdentity,
  535. key: string,
  536. value: any
  537. ) => Promise<void>
  538. removeBlockProperty: (block: BlockIdentity, key: string) => Promise<void>
  539. getBlockProperty: (block: BlockIdentity, key: string) => Promise<any>
  540. getBlockProperties: (block: BlockIdentity) => Promise<any>
  541. scrollToBlockInPage: (
  542. pageName: BlockPageName,
  543. blockId: BlockIdentity,
  544. opts?: { replaceState: boolean }
  545. ) => void
  546. openInRightSidebar: (uuid: BlockUUID) => void
  547. /**
  548. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-a-translator
  549. */
  550. onInputSelectionEnd: IUserHook<{
  551. caret: any
  552. point: { x: number; y: number }
  553. start: number
  554. end: number
  555. text: string
  556. }>
  557. }
  558. /**
  559. * Datascript related APIs
  560. */
  561. export interface IDBProxy {
  562. /**
  563. * Run a DSL query
  564. * @link https://docs.logseq.com/#/page/queries
  565. * @param dsl
  566. */
  567. q: <T = any>(dsl: string) => Promise<Array<T> | null>
  568. /**
  569. * Run a datascript query
  570. */
  571. datascriptQuery: <T = any>(query: string, ...inputs: Array<any>) => Promise<T>
  572. /**
  573. * Hook all transaction data of DB
  574. *
  575. * @added 0.0.2
  576. */
  577. onChanged: IUserHook<{
  578. blocks: Array<BlockEntity>
  579. txData: Array<IDatom>
  580. txMeta?: { outlinerOp: string; [key: string]: any }
  581. }>
  582. /**
  583. * Subscribe a specific block changed event
  584. *
  585. * @added 0.0.2
  586. */
  587. onBlockChanged(
  588. uuid: BlockUUID,
  589. callback: (
  590. block: BlockEntity,
  591. txData: Array<IDatom>,
  592. txMeta?: { outlinerOp: string; [key: string]: any }
  593. ) => void
  594. ): IUserOffHook
  595. }
  596. /**
  597. * Git related APIS
  598. */
  599. export interface IGitProxy {
  600. /**
  601. * @added 0.0.2
  602. * @link https://github.com/desktop/dugite/blob/master/docs/api/exec.md
  603. * @param args
  604. */
  605. execCommand: (args: string[]) => Promise<IGitResult>
  606. loadIgnoreFile: () => Promise<string>
  607. saveIgnoreFile: (content: string) => Promise<void>
  608. }
  609. /**
  610. * UI related APIs
  611. */
  612. export type UIMsgOptions = {
  613. key: string
  614. timeout: number // milliseconds. `0` indicate that keep showing
  615. }
  616. export type UIMsgKey = UIMsgOptions['key']
  617. export interface IUIProxy {
  618. /**
  619. * @added 0.0.2
  620. *
  621. * @param content
  622. * @param status
  623. * @param opts
  624. */
  625. showMsg: (
  626. content: string,
  627. status?: 'success' | 'warning' | 'error' | string,
  628. opts?: Partial<UIMsgOptions>
  629. ) => Promise<UIMsgKey>
  630. closeMsg: (key: UIMsgKey) => void
  631. }
  632. /**
  633. * Assets related APIs
  634. */
  635. export interface IAssetsProxy {
  636. /**
  637. * @added 0.0.2
  638. * @param exts
  639. */
  640. listFilesOfCurrentGraph(
  641. exts: string | string[]
  642. ): Promise<{
  643. path: string
  644. size: number
  645. accessTime: number
  646. modifiedTime: number
  647. changeTime: number
  648. birthTime: number
  649. }>
  650. }
  651. export interface ILSPluginThemeManager extends EventEmitter {
  652. themes: Map<PluginLocalIdentity, Array<ThemeOptions>>
  653. registerTheme(id: PluginLocalIdentity, opt: ThemeOptions): Promise<void>
  654. unregisterTheme(id: PluginLocalIdentity): Promise<void>
  655. selectTheme(opt?: ThemeOptions): Promise<void>
  656. }
  657. export type LSPluginUserEvents = 'ui:visible:changed' | 'settings:changed'
  658. export interface ILSPluginUser extends EventEmitter<LSPluginUserEvents> {
  659. /**
  660. * Connection status with the main app
  661. */
  662. connected: boolean
  663. /**
  664. * Duplex message caller
  665. */
  666. caller: LSPluginCaller
  667. /**
  668. * The plugin configurations from package.json
  669. */
  670. baseInfo: LSPluginBaseInfo
  671. /**
  672. * The plugin user settings
  673. */
  674. settings?: LSPluginBaseInfo['settings']
  675. /**
  676. * The main Logseq app is ready to run the plugin
  677. *
  678. * @param model - same as the model in `provideModel`
  679. */
  680. ready(model?: Record<string, any>): Promise<any>
  681. /**
  682. * @param callback - a function to run when the main Logseq app is ready
  683. */
  684. ready(callback?: (e: any) => void | {}): Promise<any>
  685. ready(
  686. model?: Record<string, any>,
  687. callback?: (e: any) => void | {}
  688. ): Promise<any>
  689. beforeunload: (callback: () => Promise<void>) => void
  690. /**
  691. * Create a object to hold the methods referenced in `provideUI`
  692. *
  693. * @example
  694. * ```ts
  695. * logseq.provideModel({
  696. * openCalendar () {
  697. * console.log('Open the calendar!')
  698. * }
  699. * })
  700. * ```
  701. */
  702. provideModel(model: Record<string, any>): this
  703. /**
  704. * Set the theme for the main Logseq app
  705. */
  706. provideTheme(theme: ThemeOptions): this
  707. /**
  708. * Inject custom css for the main Logseq app
  709. *
  710. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  711. * @example
  712. * ```ts
  713. * logseq.provideStyle(`
  714. * @import url("https://at.alicdn.com/t/font_2409735_r7em724douf.css");
  715. * )
  716. * ```
  717. */
  718. provideStyle(style: StyleString | StyleOptions): this
  719. /**
  720. * Inject custom UI at specific DOM node.
  721. * Event handlers can not be passed by string, so you need to create them in `provideModel`
  722. *
  723. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-a-translator
  724. * @example
  725. * ```ts
  726. * logseq.provideUI({
  727. * key: 'open-calendar',
  728. * path: '#search',
  729. * template: `
  730. * <a data-on-click="openCalendar" onclick="alert('abc')' style="opacity: .6; display: inline-flex; padding-left: 3px;'>
  731. * <i class="iconfont icon-Calendaralt2"></i>
  732. * </a>
  733. * `
  734. * })
  735. * ```
  736. */
  737. provideUI(ui: UIOptions): this
  738. /**
  739. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  740. *
  741. * @param schemas
  742. */
  743. useSettingsSchema(schemas: Array<SettingSchemaDesc>): this
  744. /**
  745. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  746. *
  747. * @param attrs
  748. */
  749. updateSettings(attrs: Record<string, any>): void
  750. onSettingsChanged<T = any>(cb: (a: T, b: T) => void): IUserOffHook
  751. showSettingsUI(): void
  752. hideSettingsUI(): void
  753. setMainUIAttrs(attrs: Record<string, any>): void
  754. /**
  755. * Set the style for the plugin's UI
  756. *
  757. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  758. * @example
  759. * ```ts
  760. * logseq.setMainUIInlineStyle({
  761. * position: 'fixed',
  762. * zIndex: 11,
  763. * })
  764. * ```
  765. */
  766. setMainUIInlineStyle(style: CSS.Properties): void
  767. /**
  768. * show the plugin's UI
  769. */
  770. showMainUI(opts?: { autoFocus: boolean }): void
  771. /**
  772. * hide the plugin's UI
  773. */
  774. hideMainUI(opts?: { restoreEditingCursor: boolean }): void
  775. /**
  776. * toggle the plugin's UI
  777. */
  778. toggleMainUI(): void
  779. isMainUIVisible: boolean
  780. resolveResourceFullUrl(filePath: string): string
  781. App: IAppProxy & Record<string, any>
  782. Editor: IEditorProxy & Record<string, any>
  783. DB: IDBProxy
  784. Git: IGitProxy
  785. UI: IUIProxy
  786. FileStorage: LSPluginFileStorage
  787. Experiments: LSPluginExperiments
  788. }