LSPlugin.ts 22 KB

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