LSPlugin.ts 27 KB

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