LSPlugin.ts 27 KB

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