LSPlugin.ts 23 KB

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