LSPlugin.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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 = any> = (e: IHookEvent & E) => 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 label - displayed name of command
  425. * @param action - can be a single callback function to run when the command is called
  426. */
  427. registerBlockContextMenuItem: (
  428. label: string,
  429. action: BlockCommandCallback
  430. ) => unknown
  431. /**
  432. * Current it's only available for pdf viewer
  433. * @param label - displayed name of command
  434. * @param action - callback for the clickable item
  435. * @param opts - clearSelection: clear highlight selection when callback invoked
  436. */
  437. registerHighlightContextMenuItem: (
  438. label: string,
  439. action: SimpleCommandCallback,
  440. opts?: {
  441. clearSelection: boolean
  442. }
  443. ) => unknown
  444. // block related APIs
  445. checkEditing: () => Promise<BlockUUID | boolean>
  446. insertAtEditingCursor: (content: string) => Promise<void>
  447. restoreEditingCursor: () => Promise<void>
  448. exitEditingMode: (selectBlock?: boolean) => Promise<void>
  449. getEditingCursorPosition: () => Promise<BlockCursorPosition | null>
  450. getEditingBlockContent: () => Promise<string>
  451. getCurrentPage: () => Promise<PageEntity | BlockEntity | null>
  452. getCurrentBlock: () => Promise<BlockEntity | null>
  453. getSelectedBlocks: () => Promise<Array<BlockEntity> | null>
  454. /**
  455. * get all blocks of the current page as a tree structure
  456. *
  457. * @example
  458. * ```ts
  459. * const blocks = await logseq.Editor.getCurrentPageBlocksTree()
  460. * initMindMap(blocks)
  461. * ```
  462. */
  463. getCurrentPageBlocksTree: () => Promise<Array<BlockEntity>>
  464. /**
  465. * get all blocks for the specified page
  466. *
  467. * @param srcPage - the page name or uuid
  468. */
  469. getPageBlocksTree: (srcPage: PageIdentity) => Promise<Array<BlockEntity>>
  470. /**
  471. * get all page/block linked references
  472. * @param srcPage
  473. */
  474. getPageLinkedReferences: (
  475. srcPage: PageIdentity
  476. ) => Promise<Array<[page: PageEntity, blocks: Array<BlockEntity>]> | null>
  477. /**
  478. * get flatten pages from top namespace
  479. * @param namespace
  480. */
  481. getPagesFromNamespace: (
  482. namespace: BlockPageName
  483. ) => Promise<Array<PageEntity> | null>
  484. /**
  485. * construct pages tree from namespace pages
  486. * @param namespace
  487. */
  488. getPagesTreeFromNamespace: (
  489. namespace: BlockPageName
  490. ) => Promise<Array<PageEntity> | null>
  491. /**
  492. * Create a unique UUID string which can then be assigned to a block.
  493. * @added 0.0.8
  494. */
  495. newBlockUUID: () => Promise<string>
  496. /**
  497. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-reddit-hot-news
  498. *
  499. * @param srcBlock
  500. * @param content
  501. * @param opts
  502. */
  503. insertBlock: (
  504. srcBlock: BlockIdentity,
  505. content: string,
  506. opts?: Partial<{
  507. before: boolean
  508. sibling: boolean
  509. isPageBlock: boolean
  510. focus: boolean
  511. customUUID: string
  512. properties: {}
  513. }>
  514. ) => Promise<BlockEntity | null>
  515. insertBatchBlock: (
  516. srcBlock: BlockIdentity,
  517. batch: IBatchBlock | Array<IBatchBlock>,
  518. opts?: Partial<{ before: boolean; sibling: boolean }>
  519. ) => Promise<Array<BlockEntity> | null>
  520. updateBlock: (
  521. srcBlock: BlockIdentity,
  522. content: string,
  523. opts?: Partial<{ properties: {} }>
  524. ) => Promise<void>
  525. removeBlock: (srcBlock: BlockIdentity) => Promise<void>
  526. getBlock: (
  527. srcBlock: BlockIdentity | EntityID,
  528. opts?: Partial<{ includeChildren: boolean }>
  529. ) => Promise<BlockEntity | null>
  530. /**
  531. * @example
  532. *
  533. * ```ts
  534. * logseq.Editor.setBlockCollapsed('uuid', true)
  535. * logseq.Editor.setBlockCollapsed('uuid', 'toggle')
  536. * ```
  537. * @param uuid
  538. * @param opts
  539. */
  540. setBlockCollapsed: (
  541. uuid: BlockUUID,
  542. opts: { flag: boolean | 'toggle' } | boolean | 'toggle'
  543. ) => Promise<void>
  544. getPage: (
  545. srcPage: PageIdentity | EntityID,
  546. opts?: Partial<{ includeChildren: boolean }>
  547. ) => Promise<PageEntity | null>
  548. createPage: (
  549. pageName: BlockPageName,
  550. properties?: {},
  551. opts?: Partial<{
  552. redirect: boolean
  553. createFirstBlock: boolean
  554. format: BlockEntity['format']
  555. journal: boolean
  556. }>
  557. ) => Promise<PageEntity | null>
  558. deletePage: (pageName: BlockPageName) => Promise<void>
  559. renamePage: (oldName: string, newName: string) => Promise<void>
  560. getAllPages: (repo?: string) => Promise<PageEntity[] | null>
  561. prependBlockInPage: (
  562. page: PageIdentity,
  563. content: string,
  564. opts?: Partial<{ properties: {} }>
  565. ) => Promise<BlockEntity | null>
  566. appendBlockInPage: (
  567. page: PageIdentity,
  568. content: string,
  569. opts?: Partial<{ properties: {} }>
  570. ) => Promise<BlockEntity | null>
  571. getPreviousSiblingBlock: (
  572. srcBlock: BlockIdentity
  573. ) => Promise<BlockEntity | null>
  574. getNextSiblingBlock: (srcBlock: BlockIdentity) => Promise<BlockEntity | null>
  575. moveBlock: (
  576. srcBlock: BlockIdentity,
  577. targetBlock: BlockIdentity,
  578. opts?: Partial<{ before: boolean; children: boolean }>
  579. ) => Promise<void>
  580. editBlock: (srcBlock: BlockIdentity, opts?: { pos: number }) => Promise<void>
  581. selectBlock: (srcBlock: BlockIdentity) => Promise<void>
  582. upsertBlockProperty: (
  583. block: BlockIdentity,
  584. key: string,
  585. value: any
  586. ) => Promise<void>
  587. removeBlockProperty: (block: BlockIdentity, key: string) => Promise<void>
  588. getBlockProperty: (block: BlockIdentity, key: string) => Promise<any>
  589. getBlockProperties: (block: BlockIdentity) => Promise<any>
  590. scrollToBlockInPage: (
  591. pageName: BlockPageName,
  592. blockId: BlockIdentity,
  593. opts?: { replaceState: boolean }
  594. ) => void
  595. openInRightSidebar: (uuid: BlockUUID) => void
  596. /**
  597. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-a-translator
  598. */
  599. onInputSelectionEnd: IUserHook<{
  600. caret: any
  601. point: { x: number; y: number }
  602. start: number
  603. end: number
  604. text: string
  605. }>
  606. }
  607. /**
  608. * Datascript related APIs
  609. */
  610. export interface IDBProxy {
  611. /**
  612. * Run a DSL query
  613. * @link https://docs.logseq.com/#/page/queries
  614. * @param dsl
  615. */
  616. q: <T = any>(dsl: string) => Promise<Array<T> | null>
  617. /**
  618. * Run a datascript query
  619. */
  620. datascriptQuery: <T = any>(query: string, ...inputs: Array<any>) => Promise<T>
  621. /**
  622. * Hook all transaction data of DB
  623. *
  624. * @added 0.0.2
  625. */
  626. onChanged: IUserHook<{
  627. blocks: Array<BlockEntity>
  628. txData: Array<IDatom>
  629. txMeta?: { outlinerOp: string; [key: string]: any }
  630. }>
  631. /**
  632. * Subscribe a specific block changed event
  633. *
  634. * @added 0.0.2
  635. */
  636. onBlockChanged(
  637. uuid: BlockUUID,
  638. callback: (
  639. block: BlockEntity,
  640. txData: Array<IDatom>,
  641. txMeta?: { outlinerOp: string; [key: string]: any }
  642. ) => void
  643. ): IUserOffHook
  644. }
  645. /**
  646. * Git related APIS
  647. */
  648. export interface IGitProxy {
  649. /**
  650. * @added 0.0.2
  651. * @link https://github.com/desktop/dugite/blob/master/docs/api/exec.md
  652. * @param args
  653. */
  654. execCommand: (args: string[]) => Promise<IGitResult>
  655. loadIgnoreFile: () => Promise<string>
  656. saveIgnoreFile: (content: string) => Promise<void>
  657. }
  658. /**
  659. * UI related APIs
  660. */
  661. export type UIMsgOptions = {
  662. key: string
  663. timeout: number // milliseconds. `0` indicate that keep showing
  664. }
  665. export type UIMsgKey = UIMsgOptions['key']
  666. export interface IUIProxy {
  667. /**
  668. * @added 0.0.2
  669. *
  670. * @param content
  671. * @param status
  672. * @param opts
  673. */
  674. showMsg: (
  675. content: string,
  676. status?: 'success' | 'warning' | 'error' | string,
  677. opts?: Partial<UIMsgOptions>
  678. ) => Promise<UIMsgKey>
  679. closeMsg: (key: UIMsgKey) => void
  680. }
  681. /**
  682. * Assets related APIs
  683. */
  684. export interface IAssetsProxy {
  685. /**
  686. * @added 0.0.2
  687. * @param exts
  688. */
  689. listFilesOfCurrentGraph(exts?: string | string[]): Promise<{
  690. path: string
  691. size: number
  692. accessTime: number
  693. modifiedTime: number
  694. changeTime: number
  695. birthTime: number
  696. }>
  697. /**
  698. * @example https://github.com/logseq/logseq/pull/6488
  699. * @added 0.0.10
  700. */
  701. makeSandboxStorage(): IAsyncStorage
  702. }
  703. export interface ILSPluginThemeManager {
  704. get themes(): Map<PluginLocalIdentity, Theme[]>
  705. registerTheme(id: PluginLocalIdentity, opt: Theme): Promise<void>
  706. unregisterTheme(id: PluginLocalIdentity, effect?: boolean): Promise<void>
  707. selectTheme(
  708. opt: Theme | LegacyTheme,
  709. options: { effect?: boolean; emit?: boolean }
  710. ): Promise<void>
  711. }
  712. export type LSPluginUserEvents = 'ui:visible:changed' | 'settings:changed'
  713. export interface ILSPluginUser extends EventEmitter<LSPluginUserEvents> {
  714. /**
  715. * Connection status with the main app
  716. */
  717. connected: boolean
  718. /**
  719. * Duplex message caller
  720. */
  721. caller: LSPluginCaller
  722. /**
  723. * The plugin configurations from package.json
  724. */
  725. baseInfo: LSPluginBaseInfo
  726. /**
  727. * The plugin user settings
  728. */
  729. settings?: LSPluginBaseInfo['settings']
  730. /**
  731. * The main Logseq app is ready to run the plugin
  732. *
  733. * @param model - same as the model in `provideModel`
  734. */
  735. ready(model?: Record<string, any>): Promise<any>
  736. /**
  737. * @param callback - a function to run when the main Logseq app is ready
  738. */
  739. ready(callback?: (e: any) => void | {}): Promise<any>
  740. ready(
  741. model?: Record<string, any>,
  742. callback?: (e: any) => void | {}
  743. ): Promise<any>
  744. beforeunload: (callback: () => Promise<void>) => void
  745. /**
  746. * Create a object to hold the methods referenced in `provideUI`
  747. *
  748. * @example
  749. * ```ts
  750. * logseq.provideModel({
  751. * openCalendar () {
  752. * console.log('Open the calendar!')
  753. * }
  754. * })
  755. * ```
  756. */
  757. provideModel(model: Record<string, any>): this
  758. /**
  759. * Set the theme for the main Logseq app
  760. */
  761. provideTheme(theme: Theme): this
  762. /**
  763. * Inject custom css for the main Logseq app
  764. *
  765. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  766. * @example
  767. * ```ts
  768. * logseq.provideStyle(`
  769. * @import url("https://at.alicdn.com/t/font_2409735_r7em724douf.css");
  770. * )
  771. * ```
  772. */
  773. provideStyle(style: StyleString | StyleOptions): this
  774. /**
  775. * Inject custom UI at specific DOM node.
  776. * Event handlers can not be passed by string, so you need to create them in `provideModel`
  777. *
  778. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-a-translator
  779. * @example
  780. * ```ts
  781. * logseq.provideUI({
  782. * key: 'open-calendar',
  783. * path: '#search',
  784. * template: `
  785. * <a data-on-click="openCalendar" onclick="alert('abc')' style="opacity: .6; display: inline-flex; padding-left: 3px;'>
  786. * <i class="iconfont icon-Calendaralt2"></i>
  787. * </a>
  788. * `
  789. * })
  790. * ```
  791. */
  792. provideUI(ui: UIOptions): this
  793. /**
  794. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  795. *
  796. * @param schemas
  797. */
  798. useSettingsSchema(schemas: Array<SettingSchemaDesc>): this
  799. /**
  800. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  801. *
  802. * @param attrs
  803. */
  804. updateSettings(attrs: Record<string, any>): void
  805. onSettingsChanged<T = any>(cb: (a: T, b: T) => void): IUserOffHook
  806. showSettingsUI(): void
  807. hideSettingsUI(): void
  808. setMainUIAttrs(attrs: Record<string, any>): void
  809. /**
  810. * Set the style for the plugin's UI
  811. *
  812. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  813. * @example
  814. * ```ts
  815. * logseq.setMainUIInlineStyle({
  816. * position: 'fixed',
  817. * zIndex: 11,
  818. * })
  819. * ```
  820. */
  821. setMainUIInlineStyle(style: CSS.Properties): void
  822. /**
  823. * show the plugin's UI
  824. */
  825. showMainUI(opts?: { autoFocus: boolean }): void
  826. /**
  827. * hide the plugin's UI
  828. */
  829. hideMainUI(opts?: { restoreEditingCursor: boolean }): void
  830. /**
  831. * toggle the plugin's UI
  832. */
  833. toggleMainUI(): void
  834. isMainUIVisible: boolean
  835. resolveResourceFullUrl(filePath: string): string
  836. App: IAppProxy & Record<string, any>
  837. Editor: IEditorProxy & Record<string, any>
  838. DB: IDBProxy
  839. Git: IGitProxy
  840. UI: IUIProxy
  841. Assets: IAssetsProxy
  842. Request: LSPluginRequest
  843. FileStorage: LSPluginFileStorage
  844. Experiments: LSPluginExperiments
  845. }