LSPlugin.ts 22 KB

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