LSPlugin.ts 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. getCurrentGraphConfigs: (...keys: string[]) => Promise<any>
  399. setCurrentGraphConfigs: (configs: {}) => Promise<void>
  400. getCurrentGraphFavorites: () => Promise<Array<string> | null>
  401. getCurrentGraphRecent: () => Promise<Array<string> | null>
  402. getCurrentGraphTemplates: () => Promise<Record<string, BlockEntity> | null>
  403. // router
  404. pushState: (
  405. k: string,
  406. params?: Record<string, any>,
  407. query?: Record<string, any>
  408. ) => void
  409. replaceState: (
  410. k: string,
  411. params?: Record<string, any>,
  412. query?: Record<string, any>
  413. ) => void
  414. // templates
  415. getTemplate: (name: string) => Promise<BlockEntity | null>
  416. existTemplate: (name: string) => Promise<Boolean>
  417. createTemplate: (
  418. target: BlockUUID,
  419. name: string,
  420. opts?: { overwrite: boolean }
  421. ) => Promise<any>
  422. removeTemplate: (name: string) => Promise<any>
  423. insertTemplate: (target: BlockUUID, name: string) => Promise<any>
  424. setZoomFactor: (factor: number) => void
  425. setFullScreen: (flag: boolean | 'toggle') => void
  426. setLeftSidebarVisible: (flag: boolean | 'toggle') => void
  427. setRightSidebarVisible: (flag: boolean | 'toggle') => void
  428. clearRightSidebarBlocks: (opts?: { close: boolean }) => void
  429. registerUIItem: (
  430. type: 'toolbar' | 'pagebar',
  431. opts: { key: string; template: string }
  432. ) => void
  433. registerPageMenuItem: (
  434. tag: string,
  435. action: (e: IHookEvent & { page: string }) => void
  436. ) => void
  437. // hook events
  438. onCurrentGraphChanged: IUserHook
  439. onGraphAfterIndexed: IUserHook<{ repo: string }>
  440. onThemeModeChanged: IUserHook<{ mode: 'dark' | 'light' }>
  441. onThemeChanged: IUserHook<
  442. Partial<{ name: string; mode: string; pid: string; url: string }>>
  443. onTodayJournalCreated: IUserHook<{ title: string }>
  444. onBeforeCommandInvoked: (condition: ExternalCommandType | string, callback: (e: IHookEvent) => void) => IUserOffHook
  445. onAfterCommandInvoked: (condition: ExternalCommandType | string, callback: (e: IHookEvent) => void) => IUserOffHook
  446. /**
  447. * provide ui slot to specific block with UUID
  448. *
  449. * @added 0.0.13
  450. */
  451. onBlockRendererSlotted: IUserConditionSlotHook<
  452. BlockUUID,
  453. Omit<BlockEntity, 'children' | 'page'>
  454. >
  455. /**
  456. * provide ui slot to block `renderer` macro for `{{renderer arg1, arg2}}`
  457. *
  458. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-pomodoro-timer
  459. * @example
  460. * ```ts
  461. * // e.g. {{renderer :h1, hello world, green}}
  462. *
  463. * logseq.App.onMacroRendererSlotted(({ slot, payload: { arguments } }) => {
  464. * let [type, text, color] = arguments
  465. * if (type !== ':h1') return
  466. * logseq.provideUI({
  467. * key: 'h1-playground',
  468. * slot, template: `
  469. * <h2 style="color: ${color || 'red'}">${text}</h2>
  470. * `,
  471. * })
  472. * })
  473. * ```
  474. */
  475. onMacroRendererSlotted: IUserSlotHook<{
  476. payload: { arguments: Array<string>; uuid: string; [key: string]: any }
  477. }>
  478. onPageHeadActionsSlotted: IUserSlotHook
  479. onRouteChanged: IUserHook<{ path: string; template: string }>
  480. onSidebarVisibleChanged: IUserHook<{ visible: boolean }>
  481. // internal
  482. _installPluginHook: (pid: string, hook: string, opts?: any) => void
  483. _uninstallPluginHook: (pid: string, hookOrAll: string | boolean) => void
  484. }
  485. /**
  486. * Editor related APIs
  487. */
  488. export interface IEditorProxy extends Record<string, any> {
  489. /**
  490. * register a custom command which will be added to the Logseq slash command list
  491. * @param tag - displayed name of command
  492. * @param action - can be a single callback function to run when the command is called, or an array of fixed commands with arguments
  493. *
  494. *
  495. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-slash-commands
  496. *
  497. * @example
  498. * ```ts
  499. * logseq.Editor.registerSlashCommand("Say Hi", () => {
  500. * console.log('Hi!')
  501. * })
  502. * ```
  503. *
  504. * @example
  505. * ```ts
  506. * logseq.Editor.registerSlashCommand("💥 Big Bang", [
  507. * ["editor/hook", "customCallback"],
  508. * ["editor/clear-current-slash"],
  509. * ]);
  510. * ```
  511. */
  512. registerSlashCommand: (
  513. tag: string,
  514. action: BlockCommandCallback | Array<SlashCommandAction>
  515. ) => unknown
  516. /**
  517. * register a custom command in the block context menu (triggered by right-clicking the block dot)
  518. * @param label - displayed name of command
  519. * @param action - can be a single callback function to run when the command is called
  520. */
  521. registerBlockContextMenuItem: (
  522. label: string,
  523. action: BlockCommandCallback
  524. ) => unknown
  525. /**
  526. * Current it's only available for pdf viewer
  527. * @param label - displayed name of command
  528. * @param action - callback for the clickable item
  529. * @param opts - clearSelection: clear highlight selection when callback invoked
  530. */
  531. registerHighlightContextMenuItem: (
  532. label: string,
  533. action: SimpleCommandCallback,
  534. opts?: {
  535. clearSelection: boolean
  536. }
  537. ) => unknown
  538. // block related APIs
  539. checkEditing: () => Promise<BlockUUID | boolean>
  540. insertAtEditingCursor: (content: string) => Promise<void>
  541. restoreEditingCursor: () => Promise<void>
  542. exitEditingMode: (selectBlock?: boolean) => Promise<void>
  543. getEditingCursorPosition: () => Promise<BlockCursorPosition | null>
  544. getEditingBlockContent: () => Promise<string>
  545. getCurrentPage: () => Promise<PageEntity | BlockEntity | null>
  546. getCurrentBlock: () => Promise<BlockEntity | null>
  547. getSelectedBlocks: () => Promise<Array<BlockEntity> | null>
  548. clearSelectedBlocks: () => Promise<void>
  549. /**
  550. * get all blocks of the current page as a tree structure
  551. *
  552. * @example
  553. * ```ts
  554. * const blocks = await logseq.Editor.getCurrentPageBlocksTree()
  555. * initMindMap(blocks)
  556. * ```
  557. */
  558. getCurrentPageBlocksTree: () => Promise<Array<BlockEntity>>
  559. /**
  560. * get all blocks for the specified page
  561. *
  562. * @param srcPage - the page name or uuid
  563. */
  564. getPageBlocksTree: (srcPage: PageIdentity) => Promise<Array<BlockEntity>>
  565. /**
  566. * get all page/block linked references
  567. * @param srcPage
  568. */
  569. getPageLinkedReferences: (
  570. srcPage: PageIdentity
  571. ) => Promise<Array<[page: PageEntity, blocks: Array<BlockEntity>]> | null>
  572. /**
  573. * get flatten pages from top namespace
  574. * @param namespace
  575. */
  576. getPagesFromNamespace: (
  577. namespace: BlockPageName
  578. ) => Promise<Array<PageEntity> | null>
  579. /**
  580. * construct pages tree from namespace pages
  581. * @param namespace
  582. */
  583. getPagesTreeFromNamespace: (
  584. namespace: BlockPageName
  585. ) => Promise<Array<PageEntity> | null>
  586. /**
  587. * Create a unique UUID string which can then be assigned to a block.
  588. * @added 0.0.8
  589. */
  590. newBlockUUID: () => Promise<string>
  591. /**
  592. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-reddit-hot-news
  593. *
  594. * @param srcBlock
  595. * @param content
  596. * @param opts
  597. */
  598. insertBlock: (
  599. srcBlock: BlockIdentity,
  600. content: string,
  601. opts?: Partial<{
  602. before: boolean
  603. sibling: boolean
  604. isPageBlock: boolean
  605. focus: boolean
  606. customUUID: string
  607. properties: {}
  608. }>
  609. ) => Promise<BlockEntity | null>
  610. /**
  611. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-reddit-hot-news
  612. *
  613. * `keepUUID` will allow you to set a custom UUID for blocks by setting their properties.id
  614. */
  615. insertBatchBlock: (
  616. srcBlock: BlockIdentity,
  617. batch: IBatchBlock | Array<IBatchBlock>,
  618. opts?: Partial<{ before: boolean; sibling: boolean; keepUUID: boolean }>
  619. ) => Promise<Array<BlockEntity> | null>
  620. updateBlock: (
  621. srcBlock: BlockIdentity,
  622. content: string,
  623. opts?: Partial<{ properties: {} }>
  624. ) => Promise<void>
  625. removeBlock: (srcBlock: BlockIdentity) => Promise<void>
  626. getBlock: (
  627. srcBlock: BlockIdentity | EntityID,
  628. opts?: Partial<{ includeChildren: boolean }>
  629. ) => Promise<BlockEntity | null>
  630. /**
  631. * @example
  632. *
  633. * ```ts
  634. * logseq.Editor.setBlockCollapsed('uuid', true)
  635. * logseq.Editor.setBlockCollapsed('uuid', 'toggle')
  636. * ```
  637. * @param uuid
  638. * @param opts
  639. */
  640. setBlockCollapsed: (
  641. uuid: BlockUUID,
  642. opts: { flag: boolean | 'toggle' } | boolean | 'toggle'
  643. ) => Promise<void>
  644. getPage: (
  645. srcPage: PageIdentity | EntityID,
  646. opts?: Partial<{ includeChildren: boolean }>
  647. ) => Promise<PageEntity | null>
  648. createPage: (
  649. pageName: BlockPageName,
  650. properties?: {},
  651. opts?: Partial<{
  652. redirect: boolean
  653. createFirstBlock: boolean
  654. format: BlockEntity['format']
  655. journal: boolean
  656. }>
  657. ) => Promise<PageEntity | null>
  658. deletePage: (pageName: BlockPageName) => Promise<void>
  659. renamePage: (oldName: string, newName: string) => Promise<void>
  660. getAllPages: (repo?: string) => Promise<PageEntity[] | null>
  661. prependBlockInPage: (
  662. page: PageIdentity,
  663. content: string,
  664. opts?: Partial<{ properties: {} }>
  665. ) => Promise<BlockEntity | null>
  666. appendBlockInPage: (
  667. page: PageIdentity,
  668. content: string,
  669. opts?: Partial<{ properties: {} }>
  670. ) => Promise<BlockEntity | null>
  671. getPreviousSiblingBlock: (
  672. srcBlock: BlockIdentity
  673. ) => Promise<BlockEntity | null>
  674. getNextSiblingBlock: (
  675. srcBlock: BlockIdentity
  676. ) => Promise<BlockEntity | null>
  677. moveBlock: (
  678. srcBlock: BlockIdentity,
  679. targetBlock: BlockIdentity,
  680. opts?: Partial<{ before: boolean; children: boolean }>
  681. ) => Promise<void>
  682. editBlock: (srcBlock: BlockIdentity, opts?: { pos: number }) => Promise<void>
  683. selectBlock: (srcBlock: BlockIdentity) => Promise<void>
  684. saveFocusedCodeEditorContent: () => Promise<void>
  685. upsertBlockProperty: (
  686. block: BlockIdentity,
  687. key: string,
  688. value: any
  689. ) => Promise<void>
  690. removeBlockProperty: (block: BlockIdentity, key: string) => Promise<void>
  691. getBlockProperty: (block: BlockIdentity, key: string) => Promise<BlockEntity | string| null>
  692. getBlockProperties: (block: BlockIdentity) => Promise<Record<string, any> | null>
  693. scrollToBlockInPage: (
  694. pageName: BlockPageName,
  695. blockId: BlockIdentity,
  696. opts?: { replaceState: boolean }
  697. ) => void
  698. openInRightSidebar: (id: BlockUUID | EntityID) => void
  699. /**
  700. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-a-translator
  701. */
  702. onInputSelectionEnd: IUserHook<{
  703. caret: any
  704. point: { x: number; y: number }
  705. start: number
  706. end: number
  707. text: string
  708. }>
  709. }
  710. /**
  711. * Datascript related APIs
  712. */
  713. export interface IDBProxy {
  714. /**
  715. * Run a DSL query
  716. * @link https://docs.logseq.com/#/page/queries
  717. * @param dsl
  718. */
  719. q: <T = any>(dsl: string) => Promise<Array<T> | null>
  720. /**
  721. * Run a datascript query
  722. */
  723. datascriptQuery: <T = any>(query: string, ...inputs: Array<any>) => Promise<T>
  724. /**
  725. * Hook all transaction data of DB
  726. *
  727. * @added 0.0.2
  728. */
  729. onChanged: IUserHook<{
  730. blocks: Array<BlockEntity>
  731. txData: Array<IDatom>
  732. txMeta?: { outlinerOp: string; [key: string]: any }
  733. }>
  734. /**
  735. * Subscribe a specific block changed event
  736. *
  737. * @added 0.0.2
  738. */
  739. onBlockChanged(
  740. uuid: BlockUUID,
  741. callback: (
  742. block: BlockEntity,
  743. txData: Array<IDatom>,
  744. txMeta?: { outlinerOp: string; [key: string]: any }
  745. ) => void
  746. ): IUserOffHook
  747. }
  748. /**
  749. * Git related APIS
  750. */
  751. export interface IGitProxy {
  752. /**
  753. * @added 0.0.2
  754. * @link https://github.com/desktop/dugite/blob/master/docs/api/exec.md
  755. * @param args
  756. */
  757. execCommand: (args: string[]) => Promise<IGitResult>
  758. loadIgnoreFile: () => Promise<string>
  759. saveIgnoreFile: (content: string) => Promise<void>
  760. }
  761. /**
  762. * UI related APIs
  763. */
  764. export type UIMsgOptions = {
  765. key: string
  766. timeout: number // milliseconds. `0` indicate that keep showing
  767. }
  768. export type UIMsgKey = UIMsgOptions['key']
  769. export interface IUIProxy {
  770. showMsg: (
  771. content: string,
  772. status?: 'success' | 'warning' | 'error' | string,
  773. opts?: Partial<UIMsgOptions>
  774. ) => Promise<UIMsgKey>
  775. closeMsg: (key: UIMsgKey) => void
  776. queryElementRect: (selector: string) => Promise<DOMRectReadOnly | null>
  777. queryElementById: (id: string) => Promise<string | boolean>
  778. checkSlotValid: (slot: UISlotIdentity['slot']) => Promise<boolean>
  779. resolveThemeCssPropsVals: (props: string | Array<string>) => Promise<Record<string, string | undefined> | null>
  780. }
  781. /**
  782. * Assets related APIs
  783. */
  784. export interface IAssetsProxy {
  785. /**
  786. * @added 0.0.2
  787. * @param exts
  788. */
  789. listFilesOfCurrentGraph(exts?: string | string[]): Promise<
  790. Array<{
  791. path: string
  792. size: number
  793. accessTime: number
  794. modifiedTime: number
  795. changeTime: number
  796. birthTime: number
  797. }>
  798. >
  799. /**
  800. * @example https://github.com/logseq/logseq/pull/6488
  801. * @added 0.0.10
  802. */
  803. makeSandboxStorage(): IAsyncStorage
  804. /**
  805. * make assets scheme url based on current graph
  806. * @added 0.0.15
  807. * @param path
  808. */
  809. makeUrl(path: string): Promise<string>
  810. /**
  811. * try to open asset type file in Logseq app
  812. * @added 0.0.16
  813. * @param path
  814. */
  815. builtInOpen(path: string): Promise<boolean | undefined>
  816. }
  817. export interface ILSPluginThemeManager {
  818. get themes(): Map<PluginLocalIdentity, Theme[]>
  819. registerTheme(id: PluginLocalIdentity, opt: Theme): Promise<void>
  820. unregisterTheme(id: PluginLocalIdentity, effect?: boolean): Promise<void>
  821. selectTheme(
  822. opt: Theme | LegacyTheme,
  823. options: { effect?: boolean; emit?: boolean }
  824. ): Promise<void>
  825. }
  826. export type LSPluginUserEvents = 'ui:visible:changed' | 'settings:changed'
  827. export interface ILSPluginUser extends EventEmitter<LSPluginUserEvents> {
  828. /**
  829. * Connection status with the main app
  830. */
  831. connected: boolean
  832. /**
  833. * Duplex message caller
  834. */
  835. caller: LSPluginCaller
  836. /**
  837. * The plugin configurations from package.json
  838. */
  839. baseInfo: LSPluginBaseInfo
  840. /**
  841. * The plugin user settings
  842. */
  843. settings?: LSPluginBaseInfo['settings']
  844. /**
  845. * The main Logseq app is ready to run the plugin
  846. *
  847. * @param model - same as the model in `provideModel`
  848. */
  849. ready(model?: Record<string, any>): Promise<any>
  850. /**
  851. * @param callback - a function to run when the main Logseq app is ready
  852. */
  853. ready(callback?: (e: any) => void | {}): Promise<any>
  854. ready(
  855. model?: Record<string, any>,
  856. callback?: (e: any) => void | {}
  857. ): Promise<any>
  858. beforeunload: (callback: () => Promise<void>) => void
  859. /**
  860. * Create a object to hold the methods referenced in `provideUI`
  861. *
  862. * @example
  863. * ```ts
  864. * logseq.provideModel({
  865. * openCalendar () {
  866. * console.log('Open the calendar!')
  867. * }
  868. * })
  869. * ```
  870. */
  871. provideModel(model: Record<string, any>): this
  872. /**
  873. * Set the theme for the main Logseq app
  874. */
  875. provideTheme(theme: Theme): this
  876. /**
  877. * Inject custom css for the main Logseq app
  878. *
  879. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  880. * @example
  881. * ```ts
  882. * logseq.provideStyle(`
  883. * @import url("https://at.alicdn.com/t/font_2409735_r7em724douf.css");
  884. * )
  885. * ```
  886. */
  887. provideStyle(style: StyleString | StyleOptions): this
  888. /**
  889. * Inject custom UI at specific DOM node.
  890. * Event handlers can not be passed by string, so you need to create them in `provideModel`
  891. *
  892. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-a-translator
  893. * @example
  894. * ```ts
  895. * logseq.provideUI({
  896. * key: 'open-calendar',
  897. * path: '#search',
  898. * template: `
  899. * <a data-on-click="openCalendar" onclick="alert('abc')' style="opacity: .6; display: inline-flex; padding-left: 3px;'>
  900. * <i class="iconfont icon-Calendaralt2"></i>
  901. * </a>
  902. * `
  903. * })
  904. * ```
  905. */
  906. provideUI(ui: UIOptions): this
  907. /**
  908. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  909. *
  910. * @param schemas
  911. */
  912. useSettingsSchema(schemas: Array<SettingSchemaDesc>): this
  913. /**
  914. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  915. *
  916. * @param attrs
  917. */
  918. updateSettings(attrs: Record<string, any>): void
  919. onSettingsChanged<T = any>(cb: (a: T, b: T) => void): IUserOffHook
  920. showSettingsUI(): void
  921. hideSettingsUI(): void
  922. setMainUIAttrs(attrs: Record<string, any>): void
  923. /**
  924. * Set the style for the plugin's UI
  925. *
  926. * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-awesome-fonts
  927. * @example
  928. * ```ts
  929. * logseq.setMainUIInlineStyle({
  930. * position: 'fixed',
  931. * zIndex: 11,
  932. * })
  933. * ```
  934. */
  935. setMainUIInlineStyle(style: CSS.Properties): void
  936. /**
  937. * show the plugin's UI
  938. */
  939. showMainUI(opts?: { autoFocus: boolean }): void
  940. /**
  941. * hide the plugin's UI
  942. */
  943. hideMainUI(opts?: { restoreEditingCursor: boolean }): void
  944. /**
  945. * toggle the plugin's UI
  946. */
  947. toggleMainUI(): void
  948. isMainUIVisible: boolean
  949. resolveResourceFullUrl(filePath: string): string
  950. App: IAppProxy
  951. Editor: IEditorProxy
  952. DB: IDBProxy
  953. Git: IGitProxy
  954. UI: IUIProxy
  955. Assets: IAssetsProxy
  956. Request: LSPluginRequest
  957. FileStorage: LSPluginFileStorage
  958. Experiments: LSPluginExperiments
  959. }