LSPlugin.ts 23 KB

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