LSPlugin.core.ts 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. import EventEmitter from 'eventemitter3'
  2. import {
  3. deepMerge,
  4. setupInjectedStyle,
  5. genID,
  6. setupInjectedUI,
  7. deferred,
  8. invokeHostExportedApi,
  9. isObject,
  10. withFileProtocol,
  11. getSDKPathRoot,
  12. PROTOCOL_FILE,
  13. URL_LSP,
  14. safetyPathJoin,
  15. path,
  16. safetyPathNormalize,
  17. mergeSettingsWithSchema,
  18. IS_DEV,
  19. cleanInjectedScripts,
  20. safeSnakeCase,
  21. injectTheme,
  22. cleanInjectedUI,
  23. PluginLogger,
  24. } from './helpers'
  25. import * as pluginHelpers from './helpers'
  26. import DOMPurify from 'dompurify'
  27. import Debug from 'debug'
  28. import {
  29. LSPluginCaller,
  30. LSPMSG_READY,
  31. LSPMSG_SYNC,
  32. LSPMSG,
  33. LSPMSG_SETTINGS,
  34. LSPMSG_ERROR_TAG,
  35. LSPMSG_BEFORE_UNLOAD,
  36. AWAIT_LSPMSGFn,
  37. } from './LSPlugin.caller'
  38. import {
  39. ILSPluginThemeManager,
  40. LegacyTheme,
  41. LSPluginPkgConfig,
  42. SettingSchemaDesc,
  43. StyleOptions,
  44. StyleString,
  45. Theme,
  46. ThemeMode,
  47. UIContainerAttrs,
  48. UIOptions,
  49. } from './LSPlugin'
  50. const debug = Debug('LSPlugin:core')
  51. const DIR_PLUGINS = 'plugins'
  52. declare global {
  53. interface Window {
  54. LSPluginCore: LSPluginCore
  55. DOMPurify: typeof DOMPurify
  56. $$callerPluginID: string | undefined
  57. }
  58. }
  59. type DeferredActor = ReturnType<typeof deferred>
  60. interface LSPluginCoreOptions {
  61. dotConfigRoot: string
  62. }
  63. /**
  64. * User settings
  65. */
  66. class PluginSettings extends EventEmitter<'change' | 'reset'> {
  67. private _settings: Record<string, any> = {
  68. disabled: false,
  69. }
  70. constructor(
  71. private readonly _userPluginSettings: any,
  72. private _schema?: SettingSchemaDesc[]
  73. ) {
  74. super()
  75. Object.assign(this._settings, _userPluginSettings)
  76. }
  77. get<T = any>(k: string): T {
  78. return this._settings[k]
  79. }
  80. set(k: string | Record<string, any>, v?: any) {
  81. const o = deepMerge({}, this._settings)
  82. if (typeof k === 'string') {
  83. if (this._settings[k] == v) return
  84. this._settings[k] = v
  85. } else if (isObject(k)) {
  86. this._settings = deepMerge(this._settings, k)
  87. } else {
  88. return
  89. }
  90. this.emit('change', { ...this._settings }, o)
  91. }
  92. set settings(value: Record<string, any>) {
  93. const o = deepMerge({}, this._settings)
  94. this._settings = value || {}
  95. this.emit('change', { ...this._settings }, o)
  96. }
  97. get settings(): Record<string, any> {
  98. return this._settings
  99. }
  100. setSchema(schema: SettingSchemaDesc[], syncSettings?: boolean) {
  101. this._schema = schema
  102. if (syncSettings) {
  103. const _settings = this._settings
  104. this._settings = mergeSettingsWithSchema(_settings, schema)
  105. this.emit('change', this._settings, _settings)
  106. }
  107. }
  108. reset() {
  109. const o = this.settings
  110. const val = {}
  111. if (this._schema) {
  112. // TODO: generated by schema
  113. }
  114. this.settings = val
  115. this.emit('reset', val, o)
  116. }
  117. toJSON() {
  118. return this._settings
  119. }
  120. }
  121. interface UserPreferences {
  122. theme: LegacyTheme
  123. themes: {
  124. mode: ThemeMode
  125. light: Theme
  126. dark: Theme
  127. }
  128. externals: string[] // external plugin locations
  129. }
  130. interface PluginLocalOptions {
  131. key?: string // Unique from Logseq Plugin Store
  132. entry: string // Plugin main file
  133. url: string // Plugin package absolute fs location
  134. name: string
  135. version: string
  136. mode: 'shadow' | 'iframe'
  137. webPkg?: any // web plugin package.json data
  138. settingsSchema?: SettingSchemaDesc[]
  139. settings?: PluginSettings
  140. effect?: boolean
  141. theme?: boolean
  142. [key: string]: any
  143. }
  144. interface PluginLocalSDKMetadata {
  145. version: string
  146. [key: string]: any
  147. }
  148. type PluginLocalUrl = Pick<PluginLocalOptions, 'url'> & { [key: string]: any }
  149. type RegisterPluginOpts = PluginLocalOptions | PluginLocalUrl
  150. type PluginLocalIdentity = string
  151. enum PluginLocalLoadStatus {
  152. LOADING = 'loading',
  153. UNLOADING = 'unloading',
  154. LOADED = 'loaded',
  155. UNLOADED = 'unload',
  156. ERROR = 'error',
  157. }
  158. function initUserSettingsHandlers(pluginLocal: PluginLocal) {
  159. const _ = (label: string): any => `settings:${label}`
  160. // settings:schema
  161. pluginLocal.on(
  162. _('schema'),
  163. ({ schema, isSync }: { schema: SettingSchemaDesc[]; isSync?: boolean }) => {
  164. pluginLocal.settingsSchema = schema
  165. pluginLocal.settings?.setSchema(schema, isSync)
  166. }
  167. )
  168. // settings:update
  169. pluginLocal.on(_('update'), (attrs) => {
  170. if (!attrs) return
  171. pluginLocal.settings?.set(attrs)
  172. })
  173. // settings:visible:changed
  174. pluginLocal.on(_('visible:changed'), (payload) => {
  175. const visible = payload?.visible
  176. invokeHostExportedApi(
  177. 'set_focused_settings',
  178. visible ? pluginLocal.id : null
  179. )
  180. })
  181. }
  182. function initMainUIHandlers(pluginLocal: PluginLocal) {
  183. const _ = (label: string): any => `main-ui:${label}`
  184. // main-ui:visible
  185. pluginLocal.on(_('visible'), ({ visible, toggle, cursor, autoFocus }) => {
  186. const el = pluginLocal.getMainUIContainer()
  187. el?.classList[toggle ? 'toggle' : visible ? 'add' : 'remove']('visible')
  188. // pluginLocal.caller!.callUserModel(LSPMSG, { type: _('visible'), payload: visible })
  189. // auto focus frame
  190. if (visible) {
  191. if (!pluginLocal.shadow && el && autoFocus !== false) {
  192. el.querySelector('iframe')?.contentWindow?.focus()
  193. }
  194. } else {
  195. // @ts-expect-error set activeElement back to `body`
  196. el.ownerDocument.activeElement.blur()
  197. }
  198. if (cursor) {
  199. invokeHostExportedApi('restore_editing_cursor')
  200. }
  201. })
  202. // main-ui:attrs
  203. pluginLocal.on(_('attrs'), (attrs: Partial<UIContainerAttrs>) => {
  204. const el = pluginLocal.getMainUIContainer()
  205. Object.entries(attrs).forEach(([k, v]) => {
  206. el?.setAttribute(k, String(v))
  207. if (k === 'draggable' && v) {
  208. pluginLocal._dispose(
  209. pluginLocal._setupDraggableContainer(el, {
  210. title: pluginLocal.options.name,
  211. close: () => {
  212. pluginLocal.caller.call('sys:ui:visible', { toggle: true })
  213. },
  214. })
  215. )
  216. }
  217. if (k === 'resizable' && v) {
  218. pluginLocal._dispose(pluginLocal._setupResizableContainer(el))
  219. }
  220. })
  221. })
  222. // main-ui:style
  223. pluginLocal.on(_('style'), (style: Record<string, any>) => {
  224. const el = pluginLocal.getMainUIContainer()
  225. const isInitedLayout = !!el.dataset.inited_layout
  226. Object.entries(style).forEach(([k, v]) => {
  227. if (
  228. isInitedLayout &&
  229. ['left', 'top', 'bottom', 'right', 'width', 'height'].includes(k)
  230. ) {
  231. return
  232. }
  233. el.style[k] = v
  234. })
  235. })
  236. }
  237. function initProviderHandlers(pluginLocal: PluginLocal) {
  238. const _ = (label: string): any => `provider:${label}`
  239. let themed = false
  240. // provider:theme
  241. pluginLocal.on(_('theme'), (theme: Theme) => {
  242. pluginLocal.themeMgr.registerTheme(pluginLocal.id, theme)
  243. if (!themed) {
  244. pluginLocal._dispose(() => {
  245. pluginLocal.themeMgr.unregisterTheme(pluginLocal.id)
  246. })
  247. themed = true
  248. }
  249. })
  250. // provider:style
  251. pluginLocal.on(_('style'), (style: StyleString | StyleOptions) => {
  252. let key: string | undefined
  253. if (typeof style !== 'string') {
  254. key = style.key
  255. style = style.style
  256. }
  257. if (!style || !style.trim()) return
  258. pluginLocal._dispose(
  259. setupInjectedStyle(style, {
  260. 'data-injected-style': key ? `${key}-${pluginLocal.id}` : '',
  261. 'data-ref': pluginLocal.id,
  262. })
  263. )
  264. })
  265. // provider:ui
  266. pluginLocal.on(_('ui'), (ui: UIOptions) => {
  267. pluginLocal._onHostMounted(() => {
  268. const ret = setupInjectedUI.call(
  269. pluginLocal,
  270. ui,
  271. Object.assign(
  272. {
  273. 'data-ref': pluginLocal.id,
  274. },
  275. ui.attrs || {}
  276. ),
  277. ({ el, float }) => {
  278. if (!float) return
  279. const identity = el.dataset.identity
  280. pluginLocal.layoutCore.move_container_to_top(identity)
  281. }
  282. )
  283. if (typeof ret === 'function') {
  284. pluginLocal._dispose(ret)
  285. }
  286. })
  287. })
  288. }
  289. function initApiProxyHandlers(pluginLocal: PluginLocal) {
  290. const _ = (label: string): any => `api:${label}`
  291. pluginLocal.on(_('call'), async (payload) => {
  292. let ret: any
  293. try {
  294. window.$$callerPluginID = pluginLocal.id
  295. ret = await invokeHostExportedApi.apply(pluginLocal, [
  296. payload.method,
  297. ...payload.args,
  298. ])
  299. } catch (e) {
  300. ret = {
  301. [LSPMSG_ERROR_TAG]: e,
  302. }
  303. } finally {
  304. window.$$callerPluginID = undefined
  305. }
  306. if (pluginLocal.shadow) {
  307. if (payload.actor) {
  308. payload.actor.resolve(ret)
  309. }
  310. return
  311. }
  312. const { _sync } = payload
  313. if (_sync != null) {
  314. const reply = (result: any) => {
  315. pluginLocal.caller?.callUserModel(LSPMSG_SYNC, {
  316. result,
  317. _sync,
  318. })
  319. }
  320. Promise.resolve(ret).then(reply, reply)
  321. }
  322. })
  323. }
  324. function convertToLSPResource(fullUrl: string, dotPluginRoot: string) {
  325. if (dotPluginRoot && fullUrl.startsWith(PROTOCOL_FILE + dotPluginRoot)) {
  326. fullUrl = safetyPathJoin(
  327. URL_LSP,
  328. fullUrl.substr(PROTOCOL_FILE.length + dotPluginRoot.length)
  329. )
  330. }
  331. return fullUrl
  332. }
  333. class IllegalPluginPackageError extends Error {
  334. constructor(message: string) {
  335. super(message)
  336. this.name = 'IllegalPluginPackageError'
  337. }
  338. }
  339. class ExistedImportedPluginPackageError extends Error {
  340. constructor(message: string) {
  341. super(message)
  342. this.name = 'ExistedImportedPluginPackageError'
  343. }
  344. }
  345. /**
  346. * Host plugin for local
  347. */
  348. class PluginLocal extends EventEmitter<
  349. 'loaded' | 'unloaded' | 'beforeunload' | 'error' | string
  350. > {
  351. private _sdk: Partial<PluginLocalSDKMetadata> = {}
  352. private _disposes: Array<() => Promise<any>> = []
  353. private _id: PluginLocalIdentity
  354. private _status: PluginLocalLoadStatus = PluginLocalLoadStatus.UNLOADED
  355. private _loadErr?: Error
  356. private _localRoot?: string
  357. private _dotSettingsFile?: string
  358. private _caller?: LSPluginCaller
  359. private _logger?: PluginLogger = new PluginLogger('PluginLocal')
  360. /**
  361. * @param _options
  362. * @param _themeMgr
  363. * @param _ctx
  364. */
  365. constructor(
  366. private _options: PluginLocalOptions,
  367. private readonly _themeMgr: ILSPluginThemeManager,
  368. private readonly _ctx: LSPluginCore
  369. ) {
  370. super()
  371. this._id = _options.key || genID()
  372. initUserSettingsHandlers(this)
  373. initMainUIHandlers(this)
  374. initProviderHandlers(this)
  375. initApiProxyHandlers(this)
  376. }
  377. async _setupUserSettings(reload?: boolean) {
  378. const { _options } = this
  379. const logger = (this._logger = new PluginLogger(`Loader:${this.debugTag}`))
  380. if (_options.settings && !reload) {
  381. return
  382. }
  383. try {
  384. const loadFreshSettings = () =>
  385. invokeHostExportedApi('load_plugin_user_settings', this.id)
  386. const [userSettingsFilePath, userSettings] = await loadFreshSettings()
  387. this._dotSettingsFile = userSettingsFilePath
  388. let settings = _options.settings
  389. if (!settings) {
  390. settings = _options.settings = new PluginSettings(userSettings)
  391. }
  392. if (reload) {
  393. settings.settings = userSettings
  394. return
  395. }
  396. const handler = async (a, b) => {
  397. debug('Settings changed', this.debugTag, a)
  398. if (!a.disabled && b.disabled) {
  399. // Enable plugin
  400. const [, freshSettings] = await loadFreshSettings()
  401. freshSettings.disabled = false
  402. a = Object.assign(a, freshSettings)
  403. settings.settings = a
  404. await this.load()
  405. }
  406. if (a.disabled && !b.disabled) {
  407. // Disable plugin
  408. const [, freshSettings] = await loadFreshSettings()
  409. freshSettings.disabled = true
  410. a = Object.assign(a, freshSettings)
  411. await this.unload()
  412. }
  413. if (a) {
  414. invokeHostExportedApi('save_plugin_user_settings', this.id, a)
  415. }
  416. }
  417. // observe settings
  418. settings.on('change', handler)
  419. return () => {}
  420. } catch (e) {
  421. debug('[load plugin user settings Error]', e)
  422. logger?.error(e)
  423. }
  424. }
  425. getMainUIContainer(): HTMLElement | undefined {
  426. if (this.shadow) {
  427. return this.caller?._getSandboxShadowContainer()
  428. }
  429. return this.caller?._getSandboxIframeContainer()
  430. }
  431. _resolveResourceFullUrl(filePath: string, localRoot?: string) {
  432. if (!filePath?.trim()) return
  433. localRoot = localRoot || this._localRoot
  434. if (this.isWebPlugin) {
  435. // TODO: strategy for Logseq plugins center
  436. if (this.installedFromUserWebUrl) {
  437. return `${this.installedFromUserWebUrl}/${filePath}`
  438. }
  439. return `https://pub-80f42b85b62c40219354a834fcf2bbfa.r2.dev/${path.join(localRoot, filePath)}`
  440. }
  441. const reg = /^(http|file)/
  442. if (!reg.test(filePath)) {
  443. const url = path.join(localRoot, filePath)
  444. filePath = reg.test(url) ? url : PROTOCOL_FILE + url
  445. }
  446. return !this.options.effect && this.isInstalledInLocalDotRoot
  447. ? convertToLSPResource(filePath, this.dotPluginsRoot)
  448. : filePath
  449. }
  450. async _preparePackageConfigs() {
  451. const { url, webPkg } = this._options
  452. let pkg: any = webPkg
  453. if (!pkg) {
  454. try {
  455. if (!url) {
  456. throw new Error('Can not resolve package config location')
  457. }
  458. debug('prepare package root', url)
  459. pkg = await invokeHostExportedApi('load_plugin_config', url)
  460. if (!pkg || ((pkg = JSON.parse(pkg)), !pkg)) {
  461. throw new Error(`Parse package config error #${url}/package.json`)
  462. }
  463. } catch (e) {
  464. throw new IllegalPluginPackageError(e.message)
  465. }
  466. }
  467. // Pick legal attrs
  468. ;[
  469. 'name',
  470. 'author',
  471. 'repository',
  472. 'version',
  473. 'description',
  474. 'repo',
  475. 'title',
  476. 'effect',
  477. 'sponsors',
  478. ]
  479. .concat(!this.isInstalledInLocalDotRoot ? ['devEntry'] : [])
  480. .forEach((k) => {
  481. this._options[k] = pkg[k]
  482. })
  483. const { repo, version } = this._options
  484. const localRoot = (this._localRoot = this.isWebPlugin ? `${repo || url}/${version}` : safetyPathNormalize(url))
  485. const logseq: Partial<LSPluginPkgConfig> = pkg.logseq || {}
  486. const validateEntry = (main) => main && /\.(js|html)$/.test(main)
  487. // Entry from main
  488. const entry = logseq.entry || logseq.main || pkg.main
  489. if (validateEntry(entry)) {
  490. // Theme has no main
  491. this._options.entry = this._resolveResourceFullUrl(entry, localRoot)
  492. // development mode entry
  493. this._options.devEntry = logseq.devEntry
  494. if (logseq.mode) {
  495. this._options.mode = logseq.mode
  496. }
  497. }
  498. const title = logseq.title || pkg.title
  499. const icon = logseq.icon || pkg.icon
  500. this._options.title = title
  501. this._options.icon = icon && this._resolveResourceFullUrl(icon)
  502. this._options.theme = Boolean(logseq.theme || !!logseq.themes)
  503. if (this.isInstalledInLocalDotRoot) {
  504. this._id = path.basename(localRoot)
  505. } else if (!this.isWebPlugin) {
  506. // development mode
  507. if (logseq.id) {
  508. this._id = logseq.id
  509. } else {
  510. logseq.id = this.id
  511. try {
  512. await invokeHostExportedApi('save_plugin_package_json', url, {
  513. ...pkg,
  514. logseq,
  515. })
  516. } catch (e) {
  517. debug('[save plugin ID Error] ', e)
  518. }
  519. }
  520. }
  521. // Validate id
  522. const { registeredPlugins, isRegistering } = this._ctx
  523. if (isRegistering && registeredPlugins.has(this.id)) {
  524. throw new ExistedImportedPluginPackageError(this.id)
  525. }
  526. return async () => {
  527. try {
  528. // 0. Install Themes
  529. const themes = logseq.themes
  530. if (themes) {
  531. await this._loadConfigThemes(
  532. Array.isArray(themes) ? themes : [themes]
  533. )
  534. }
  535. } catch (e) {
  536. debug('[prepare package effect Error]', e)
  537. }
  538. }
  539. }
  540. async _tryToNormalizeEntry() {
  541. let { entry, settings, devEntry } = this.options
  542. devEntry = devEntry || settings?.get('_devEntry')
  543. if (devEntry) {
  544. this._options.entry = devEntry
  545. return
  546. }
  547. if (!entry.endsWith('.js')) return
  548. let dirPathInstalled = null
  549. let tmp_file_method = 'write_user_tmp_file'
  550. if (this.isInstalledInLocalDotRoot) {
  551. tmp_file_method = 'write_dotdir_file'
  552. dirPathInstalled = this._localRoot.replace(this.dotPluginsRoot, '')
  553. dirPathInstalled = path.join(DIR_PLUGINS, dirPathInstalled)
  554. }
  555. const tag = new Date().getDay()
  556. const sdkPathRoot = await getSDKPathRoot()
  557. const entryPath = await invokeHostExportedApi(
  558. tmp_file_method,
  559. `${this._id}_index.html`,
  560. `<!doctype html>
  561. <html lang="en">
  562. <head>
  563. <meta charset="UTF-8">
  564. <title>logseq plugin entry</title>
  565. ${
  566. IS_DEV
  567. ? `<script src="${sdkPathRoot}/lsplugin.user.js?v=${tag}"></script>`
  568. : `<script src="https://cdn.jsdelivr.net/npm/@logseq/libs/dist/lsplugin.user.min.js?v=${tag}"></script>`
  569. }
  570. </head>
  571. <body>
  572. <div id="app"></div>
  573. <script src="${entry}"></script>
  574. </body>
  575. </html>`,
  576. dirPathInstalled
  577. )
  578. entry = convertToLSPResource(
  579. withFileProtocol(path.normalize(entryPath)),
  580. this.dotPluginsRoot
  581. )
  582. this._options.entry = entry
  583. }
  584. async _loadConfigThemes(themes: Theme[]) {
  585. themes.forEach((options) => {
  586. if (!options.url) return
  587. if (!options.url.startsWith('http') && this._localRoot) {
  588. options.url = this._resolveResourceFullUrl(options.url, this._localRoot)
  589. // file:// for native
  590. if (!this.isWebPlugin && !options.url.startsWith('file:')) {
  591. options.url = 'assets://' + options.url
  592. }
  593. }
  594. this.emit('provider:theme', options)
  595. })
  596. }
  597. async _loadLayoutsData(): Promise<Record<string, any>> {
  598. const key = this.id + '_layouts'
  599. const [, layouts] = await invokeHostExportedApi(
  600. 'load_plugin_user_settings',
  601. key
  602. )
  603. return layouts || {}
  604. }
  605. async _saveLayoutsData(data) {
  606. const key = this.id + '_layouts'
  607. await invokeHostExportedApi('save_plugin_user_settings', key, data)
  608. }
  609. async _persistMainUILayoutData(e: {
  610. width: number
  611. height: number
  612. left: number
  613. top: number
  614. }) {
  615. const layouts = await this._loadLayoutsData()
  616. layouts.$$0 = e
  617. await this._saveLayoutsData(layouts)
  618. }
  619. _setupDraggableContainer(
  620. el: HTMLElement,
  621. opts: Partial<{ key: string; title: string; close: () => void }> = {}
  622. ): () => void {
  623. const ds = el.dataset
  624. if (ds.inited_draggable) return
  625. if (!ds.identity) {
  626. ds.identity = 'dd-' + genID()
  627. }
  628. const isInjectedUI = !!opts.key
  629. const handle = document.createElement('div')
  630. handle.classList.add('draggable-handle')
  631. handle.innerHTML = `
  632. <div class="th">
  633. <div class="l"><h3>${opts.title || ''}</h3></div>
  634. <div class="r">
  635. <a class="button x"><i class="ti ti-x"></i></a>
  636. </div>
  637. </div>
  638. `
  639. handle.querySelector('.x').addEventListener(
  640. 'click',
  641. (e) => {
  642. opts?.close?.()
  643. e.stopPropagation()
  644. },
  645. false
  646. )
  647. handle.addEventListener(
  648. 'mousedown',
  649. (e) => {
  650. const target = e.target as HTMLElement
  651. if (target?.closest('.r')) {
  652. e.stopPropagation()
  653. e.preventDefault()
  654. }
  655. },
  656. false
  657. )
  658. el.prepend(handle)
  659. // move to top
  660. el.addEventListener(
  661. 'mousedown',
  662. (e) => {
  663. this.layoutCore.move_container_to_top(ds.identity)
  664. },
  665. true
  666. )
  667. const setTitle = (title) => {
  668. handle.querySelector('h3').textContent = title
  669. }
  670. const dispose = this.layoutCore.setup_draggable_container_BANG_(
  671. el,
  672. !isInjectedUI ? this._persistMainUILayoutData.bind(this) : () => {}
  673. )
  674. ds.inited_draggable = 'true'
  675. if (opts.title) {
  676. setTitle(opts.title)
  677. }
  678. // click outside
  679. let removeOutsideListener = null
  680. if (ds.close === 'outside') {
  681. const handler = (e) => {
  682. const target = e.target
  683. if (!el.contains(target)) {
  684. opts.close()
  685. }
  686. }
  687. document.addEventListener('click', handler, false)
  688. removeOutsideListener = () => {
  689. document.removeEventListener('click', handler)
  690. }
  691. }
  692. return () => {
  693. dispose()
  694. removeOutsideListener?.()
  695. }
  696. }
  697. _setupResizableContainer(el: HTMLElement, key?: string): () => void {
  698. const ds = el.dataset
  699. if (ds.inited_resizable) return
  700. if (!ds.identity) {
  701. ds.identity = 'dd-' + genID()
  702. }
  703. const handle = document.createElement('div')
  704. handle.classList.add('resizable-handle')
  705. el.prepend(handle)
  706. // @ts-expect-error
  707. const layoutCore = window.frontend.modules.layout.core
  708. const dispose = layoutCore.setup_resizable_container_BANG_(
  709. el,
  710. !key ? this._persistMainUILayoutData.bind(this) : () => {}
  711. )
  712. ds.inited_resizable = 'true'
  713. return dispose
  714. }
  715. async load(
  716. opts?: Partial<{
  717. indicator: DeferredActor
  718. reload: boolean
  719. }>
  720. ) {
  721. if (this.pending) {
  722. return
  723. }
  724. this._status = PluginLocalLoadStatus.LOADING
  725. this._loadErr = undefined
  726. try {
  727. // if (!this.options.entry) { // Themes package no entry field
  728. // }
  729. const installPackageThemes = await this._preparePackageConfigs()
  730. this._dispose(await this._setupUserSettings(opts?.reload))
  731. if (!this.disabled) {
  732. await installPackageThemes.call(null)
  733. }
  734. if (this.disabled || !this.options.entry) {
  735. return
  736. }
  737. this._ctx.emit('beforeload', this)
  738. await this._tryToNormalizeEntry()
  739. this._caller = new LSPluginCaller(this)
  740. await this._caller.connectToChild()
  741. const readyFn = () => {
  742. this._caller?.callUserModel(LSPMSG_READY, { pid: this.id })
  743. }
  744. if (opts?.indicator) {
  745. opts.indicator.promise.then(readyFn)
  746. } else {
  747. readyFn()
  748. }
  749. this._dispose(async () => {
  750. await this._caller?.destroy()
  751. })
  752. this._dispose(cleanInjectedScripts.bind(this))
  753. this._ctx.emit('loadeded', this)
  754. } catch (e) {
  755. this.logger.error('load', e, true)
  756. this.dispose().catch(null)
  757. this._status = PluginLocalLoadStatus.ERROR
  758. this._loadErr = e
  759. } finally {
  760. if (!this._loadErr) {
  761. if (this.disabled) {
  762. this._status = PluginLocalLoadStatus.UNLOADED
  763. } else {
  764. this._status = PluginLocalLoadStatus.LOADED
  765. }
  766. }
  767. }
  768. }
  769. async reload() {
  770. if (this.pending) {
  771. return
  772. }
  773. this._ctx.emit('beforereload', this)
  774. await this.unload()
  775. await this.load({ reload: true })
  776. this._ctx.emit('reloaded', this)
  777. }
  778. /**
  779. * @param unregister If true delete plugin files
  780. */
  781. async unload(unregister: boolean = false) {
  782. if (this.pending) {
  783. return
  784. }
  785. if (unregister) {
  786. await this.unload()
  787. if (this.isWebPlugin || this.isInstalledInLocalDotRoot) {
  788. this._ctx.emit('unlink-plugin', this.id)
  789. }
  790. return
  791. }
  792. try {
  793. const eventBeforeUnload = { unregister }
  794. if (this.loaded) {
  795. this._status = PluginLocalLoadStatus.UNLOADING
  796. try {
  797. await this._caller?.callUserModel(
  798. AWAIT_LSPMSGFn(LSPMSG_BEFORE_UNLOAD),
  799. eventBeforeUnload
  800. )
  801. this.emit('beforeunload', eventBeforeUnload)
  802. } catch (e) {
  803. this.logger.error('beforeunload', e)
  804. }
  805. await this.dispose()
  806. }
  807. this.emit('unloaded')
  808. } catch (e) {
  809. this.logger.error('unload', e)
  810. } finally {
  811. this._status = PluginLocalLoadStatus.UNLOADED
  812. }
  813. }
  814. private async dispose() {
  815. for (const fn of this._disposes) {
  816. try {
  817. fn && (await fn())
  818. } catch (e) {
  819. console.error(this.debugTag, 'dispose Error', e)
  820. }
  821. }
  822. // clear
  823. this._disposes = []
  824. }
  825. _dispose(fn: any) {
  826. if (!fn) return
  827. this._disposes.push(fn)
  828. }
  829. _onHostMounted(callback: () => void) {
  830. const actor = this._ctx.hostMountedActor
  831. if (!actor || actor.settled) {
  832. callback()
  833. } else {
  834. actor?.promise.then(callback)
  835. }
  836. }
  837. get isWebPlugin() {
  838. return this._ctx.isWebPlatform || !!this.options.webPkg
  839. }
  840. get installedFromUserWebUrl() {
  841. return this.isWebPlugin && this.options.webPkg?.installedFromUserWebUrl
  842. }
  843. get layoutCore(): any {
  844. // @ts-expect-error
  845. return window.frontend.modules.layout.core
  846. }
  847. get isInstalledInLocalDotRoot() {
  848. if (this.isWebPlugin) return false
  849. const dotRoot = this.dotConfigRoot
  850. const plgRoot = this.localRoot
  851. return dotRoot && plgRoot && plgRoot.startsWith(dotRoot)
  852. }
  853. get loaded() {
  854. return this._status === PluginLocalLoadStatus.LOADED
  855. }
  856. get pending() {
  857. return [
  858. PluginLocalLoadStatus.LOADING,
  859. PluginLocalLoadStatus.UNLOADING,
  860. ].includes(this._status)
  861. }
  862. get status(): PluginLocalLoadStatus {
  863. return this._status
  864. }
  865. get settings() {
  866. return this.options.settings
  867. }
  868. set settingsSchema(schema: SettingSchemaDesc[]) {
  869. this._options.settingsSchema = schema
  870. }
  871. get settingsSchema() {
  872. return this.options.settingsSchema
  873. }
  874. get logger() {
  875. return this._logger
  876. }
  877. get disabled() {
  878. return this.settings?.get('disabled')
  879. }
  880. get theme() {
  881. return this.options.theme
  882. }
  883. get caller() {
  884. return this._caller
  885. }
  886. get id(): string {
  887. return this._id
  888. }
  889. get shadow(): boolean {
  890. return this.options.mode === 'shadow'
  891. }
  892. get options(): PluginLocalOptions {
  893. return this._options
  894. }
  895. get themeMgr(): ILSPluginThemeManager {
  896. return this._themeMgr
  897. }
  898. get debugTag() {
  899. const name = this._options?.name
  900. return `#${this._id} - ${name ?? ''}`
  901. }
  902. get localRoot(): string {
  903. return this._localRoot || this._options.url
  904. }
  905. get loadErr(): Error | undefined {
  906. return this._loadErr
  907. }
  908. get dotConfigRoot() {
  909. return path.normalize(this._ctx.options.dotConfigRoot)
  910. }
  911. get dotSettingsFile(): string | undefined {
  912. return this._dotSettingsFile
  913. }
  914. get dotPluginsRoot() {
  915. return path.join(this.dotConfigRoot, DIR_PLUGINS)
  916. }
  917. get sdk(): Partial<PluginLocalSDKMetadata> {
  918. return this._sdk
  919. }
  920. set sdk(value: Partial<PluginLocalSDKMetadata>) {
  921. this._sdk = value
  922. }
  923. toJSON(settings = true) {
  924. const json = { ...this.options } as any
  925. json.id = this.id
  926. json.err = this.loadErr
  927. json.usf = this.dotSettingsFile
  928. json.iir = this.isInstalledInLocalDotRoot
  929. json.webMode = this.isWebPlugin ? (this.installedFromUserWebUrl ? 'user' : 'github') : false
  930. json.lsr = this._resolveResourceFullUrl('/')
  931. if (settings === false) {
  932. delete json.settings
  933. } else {
  934. json.settings = json.settings?.toJSON()
  935. }
  936. return json
  937. }
  938. }
  939. /**
  940. * Host plugin core
  941. */
  942. class LSPluginCore
  943. extends EventEmitter<
  944. | 'beforeenable'
  945. | 'enabled'
  946. | 'beforedisable'
  947. | 'disabled'
  948. | 'registered'
  949. | 'error'
  950. | 'unregistered'
  951. | 'ready'
  952. | 'themes-changed'
  953. | 'theme-selected'
  954. | 'reset-custom-theme'
  955. | 'settings-changed'
  956. | 'unlink-plugin'
  957. | 'beforeload'
  958. | 'loadeded'
  959. | 'beforereload'
  960. | 'reloaded'
  961. >
  962. implements ILSPluginThemeManager {
  963. private _isRegistering = false
  964. private _readyIndicator?: DeferredActor
  965. private readonly _hostMountedActor: DeferredActor = deferred()
  966. private readonly _userPreferences: UserPreferences = {
  967. theme: null,
  968. themes: {
  969. mode: 'light',
  970. light: null,
  971. dark: null,
  972. },
  973. externals: [],
  974. }
  975. private readonly _registeredThemes = new Map<PluginLocalIdentity, Theme[]>()
  976. private readonly _registeredPlugins = new Map<
  977. PluginLocalIdentity,
  978. PluginLocal
  979. >()
  980. private _currentTheme: {
  981. pid: PluginLocalIdentity
  982. opt: Theme | LegacyTheme
  983. eject: () => void
  984. }
  985. /**
  986. * @param _options
  987. */
  988. constructor(private readonly _options: Partial<LSPluginCoreOptions>) {
  989. super()
  990. }
  991. async loadUserPreferences() {
  992. try {
  993. const settings = await invokeHostExportedApi('load_user_preferences')
  994. if (settings) {
  995. Object.assign(this._userPreferences, settings)
  996. }
  997. } catch (e) {
  998. debug('[load user preferences Error]', e)
  999. }
  1000. }
  1001. async saveUserPreferences(settings: Partial<UserPreferences>) {
  1002. try {
  1003. if (settings) {
  1004. Object.assign(this._userPreferences, settings)
  1005. }
  1006. await invokeHostExportedApi(
  1007. 'save_user_preferences',
  1008. this._userPreferences
  1009. )
  1010. } catch (e) {
  1011. debug('[save user preferences Error]', e)
  1012. }
  1013. }
  1014. /**
  1015. * Activate the user preferences.
  1016. *
  1017. * Steps:
  1018. *
  1019. * 1. Load the custom theme.
  1020. *
  1021. * @memberof LSPluginCore
  1022. */
  1023. async activateUserPreferences() {
  1024. const { theme: legacyTheme, themes } = this._userPreferences
  1025. const currentTheme = themes[themes.mode]
  1026. // If there is currently a theme that has been set
  1027. if (currentTheme) {
  1028. await this.selectTheme(currentTheme, { effect: false, emit: false })
  1029. } else if (legacyTheme) {
  1030. // Otherwise compatible with older versions
  1031. await this.selectTheme(legacyTheme, { effect: false, emit: false })
  1032. }
  1033. }
  1034. /**
  1035. * @param plugins
  1036. * @param initial
  1037. */
  1038. async register(
  1039. plugins: RegisterPluginOpts[] | RegisterPluginOpts,
  1040. initial = false
  1041. ) {
  1042. if (!Array.isArray(plugins)) {
  1043. await this.register([plugins])
  1044. return
  1045. }
  1046. const perfTable = new Map<
  1047. string,
  1048. { o: PluginLocal; s: number; e: number }
  1049. >()
  1050. const debugPerfInfo = () => {
  1051. const data: any = Array.from(perfTable.values()).reduce((ac, it) => {
  1052. const { id, options, status, disabled } = it.o
  1053. if (
  1054. disabled !== true &&
  1055. (options.entry || (!options.name && !options.entry))
  1056. ) {
  1057. ac[id] = {
  1058. name: options.name,
  1059. entry: options.entry,
  1060. status: status,
  1061. enabled:
  1062. typeof disabled === 'boolean' ? (!disabled ? '🟢' : '⚫️') : '🔴',
  1063. perf: !it.e ? it.o.loadErr : `${(it.e - it.s).toFixed(2)}ms`,
  1064. }
  1065. }
  1066. return ac
  1067. }, {})
  1068. console.table(data)
  1069. }
  1070. // @ts-expect-error
  1071. window.__debugPluginsPerfInfo = debugPerfInfo
  1072. try {
  1073. this._isRegistering = true
  1074. const _userConfigRoot = this._options.dotConfigRoot
  1075. const readyIndicator = (this._readyIndicator = deferred())
  1076. await this.loadUserPreferences()
  1077. let externals = new Set(this._userPreferences.externals)
  1078. // valid externals
  1079. if (externals?.size) {
  1080. try {
  1081. const validatedExternals: Record<string, boolean> =
  1082. await invokeHostExportedApi('validate_external_plugins', [
  1083. ...externals,
  1084. ])
  1085. externals = new Set(
  1086. [...Object.entries(validatedExternals)].reduce((a, [k, v]) => {
  1087. if (v) {
  1088. a.push(k)
  1089. }
  1090. return a
  1091. }, [])
  1092. )
  1093. } catch (e) {
  1094. console.error('[validatedExternals Error]', e)
  1095. }
  1096. }
  1097. if (initial) {
  1098. plugins = plugins.concat(
  1099. [...externals]
  1100. .filter((url) => {
  1101. return (
  1102. !plugins.length ||
  1103. (plugins as RegisterPluginOpts[]).every(
  1104. (p) => !p.entry && p.url !== url
  1105. )
  1106. )
  1107. })
  1108. .map((url) => ({ url }))
  1109. )
  1110. }
  1111. for (const pluginOptions of plugins) {
  1112. const { url } = pluginOptions as PluginLocalOptions
  1113. const pluginLocal = new PluginLocal(
  1114. pluginOptions as PluginLocalOptions,
  1115. this,
  1116. this
  1117. )
  1118. const perfInfo = { o: pluginLocal, s: performance.now(), e: 0 }
  1119. perfTable.set(url, perfInfo)
  1120. await pluginLocal.load({ indicator: readyIndicator })
  1121. perfInfo.e = performance.now()
  1122. const { loadErr } = pluginLocal
  1123. if (loadErr) {
  1124. debug('[Failed LOAD Plugin] #', pluginOptions)
  1125. this.emit('error', loadErr)
  1126. if (
  1127. loadErr instanceof IllegalPluginPackageError ||
  1128. loadErr instanceof ExistedImportedPluginPackageError
  1129. ) {
  1130. // TODO: notify global log system?
  1131. continue
  1132. }
  1133. }
  1134. pluginLocal.settings?.on('change', (a) => {
  1135. this.emit('settings-changed', pluginLocal.id, a)
  1136. pluginLocal.caller?.callUserModel(LSPMSG_SETTINGS, { payload: a })
  1137. })
  1138. this._registeredPlugins.set(pluginLocal.id, pluginLocal)
  1139. this.emit('registered', pluginLocal)
  1140. // external plugins
  1141. if (!pluginLocal.isWebPlugin && !pluginLocal.isInstalledInLocalDotRoot) {
  1142. externals.add(url)
  1143. }
  1144. }
  1145. await this.saveUserPreferences({ externals: Array.from(externals) })
  1146. await this.activateUserPreferences()
  1147. readyIndicator.resolve('ready')
  1148. } catch (e) {
  1149. console.error(e)
  1150. } finally {
  1151. this._isRegistering = false
  1152. this.emit('ready', perfTable)
  1153. debugPerfInfo()
  1154. }
  1155. }
  1156. async reload(plugins: PluginLocalIdentity[] | PluginLocalIdentity) {
  1157. if (!Array.isArray(plugins)) {
  1158. await this.reload([plugins])
  1159. return
  1160. }
  1161. for (const identity of plugins) {
  1162. try {
  1163. const p = this.ensurePlugin(identity)
  1164. await p.reload()
  1165. } catch (e) {
  1166. debug(e)
  1167. }
  1168. }
  1169. }
  1170. async unregister(plugins: PluginLocalIdentity[] | PluginLocalIdentity) {
  1171. if (!Array.isArray(plugins)) {
  1172. await this.unregister([plugins])
  1173. return
  1174. }
  1175. const unregisteredExternals: string[] = []
  1176. for (const identity of plugins) {
  1177. const p = this.ensurePlugin(identity)
  1178. if (!p.isWebPlugin && !p.isInstalledInLocalDotRoot) {
  1179. unregisteredExternals.push(p.options.url)
  1180. }
  1181. await p.unload(true)
  1182. this._registeredPlugins.delete(identity)
  1183. this.emit('unregistered', identity)
  1184. }
  1185. const externals = this._userPreferences.externals
  1186. if (externals.length && unregisteredExternals.length) {
  1187. await this.saveUserPreferences({
  1188. externals: externals.filter((it) => {
  1189. return !unregisteredExternals.includes(it)
  1190. }),
  1191. })
  1192. }
  1193. }
  1194. async enable(plugin: PluginLocalIdentity) {
  1195. const p = this.ensurePlugin(plugin)
  1196. if (p.pending) return
  1197. this.emit('beforeenable')
  1198. p.settings?.set('disabled', false)
  1199. this.emit('enabled', p.id)
  1200. }
  1201. async disable(plugin: PluginLocalIdentity) {
  1202. const p = this.ensurePlugin(plugin)
  1203. if (p.pending) return
  1204. this.emit('beforedisable')
  1205. p.settings?.set('disabled', true)
  1206. this.emit('disabled', p.id)
  1207. }
  1208. async _hook(ns: string, type: string, payload?: any, pid?: string) {
  1209. const hook = `${ns}:${safeSnakeCase(type)}`
  1210. const isDbChangedHook = hook === 'hook:db:changed'
  1211. const isDbBlockChangeHook = hook.startsWith('hook:db:block')
  1212. const act = (p: PluginLocal) => {
  1213. debug(`[call hook][#${p.id}]`, ns, type)
  1214. p.caller?.callUserModel(LSPMSG, {
  1215. ns,
  1216. type: safeSnakeCase(type),
  1217. payload,
  1218. })
  1219. }
  1220. const p = pid && this._registeredPlugins.get(pid)
  1221. if (p && !p.disabled && p.options.entry) {
  1222. act(p)
  1223. return
  1224. }
  1225. for (const [_, p] of this._registeredPlugins) {
  1226. if (!p.options.entry || p.disabled) {
  1227. continue
  1228. }
  1229. if (!pid) {
  1230. // compatible for old SDK < 0.0.2
  1231. const sdkVersion = p.sdk?.version
  1232. // TODO: remove optimization after few releases
  1233. if (!sdkVersion) {
  1234. if (isDbChangedHook || isDbBlockChangeHook) {
  1235. continue
  1236. } else {
  1237. act(p)
  1238. }
  1239. }
  1240. if (
  1241. sdkVersion &&
  1242. invokeHostExportedApi('should_exec_plugin_hook', p.id, hook)
  1243. ) {
  1244. act(p)
  1245. }
  1246. } else if (pid === p.id) {
  1247. act(p)
  1248. break
  1249. }
  1250. }
  1251. }
  1252. async hookApp(type: string, payload?: any, pid?: string) {
  1253. return await this._hook('hook:app', type, payload, pid)
  1254. }
  1255. async hookEditor(type: string, payload?: any, pid?: string) {
  1256. return await this._hook('hook:editor', type, payload, pid)
  1257. }
  1258. async hookDb(type: string, payload?: any, pid?: string) {
  1259. return await this._hook('hook:db', type, payload, pid)
  1260. }
  1261. ensurePlugin(plugin: PluginLocalIdentity | PluginLocal) {
  1262. if (plugin instanceof PluginLocal) {
  1263. return plugin
  1264. }
  1265. const p = this._registeredPlugins.get(plugin)
  1266. if (!p) {
  1267. throw new Error(`plugin #${plugin} not existed.`)
  1268. }
  1269. return p
  1270. }
  1271. hostMounted() {
  1272. this._hostMountedActor.resolve()
  1273. }
  1274. _forceCleanInjectedUI(id: string) {
  1275. if (!id) return
  1276. return cleanInjectedUI(id)
  1277. }
  1278. get isWebPlatform() {
  1279. return this.options.dotConfigRoot?.startsWith('LSPUserDotRoot')
  1280. }
  1281. get registeredPlugins(): Map<PluginLocalIdentity, PluginLocal> {
  1282. return this._registeredPlugins
  1283. }
  1284. get options() {
  1285. return this._options
  1286. }
  1287. get readyIndicator(): DeferredActor | undefined {
  1288. return this._readyIndicator
  1289. }
  1290. get hostMountedActor(): DeferredActor {
  1291. return this._hostMountedActor
  1292. }
  1293. get isRegistering(): boolean {
  1294. return this._isRegistering
  1295. }
  1296. get themes() {
  1297. return this._registeredThemes
  1298. }
  1299. get enabledPlugins() {
  1300. return [...this.registeredPlugins.entries()].reduce((a, b) => {
  1301. let p = b?.[1]
  1302. if (p?.disabled !== true) {
  1303. a.set(b?.[0], p)
  1304. }
  1305. return a
  1306. }, new Map())
  1307. }
  1308. async registerTheme(id: PluginLocalIdentity, opt: Theme): Promise<void> {
  1309. debug('Register theme #', id, opt)
  1310. if (!id) return
  1311. let themes: Theme[] = this._registeredThemes.get(id)!
  1312. if (!themes) {
  1313. this._registeredThemes.set(id, (themes = []))
  1314. }
  1315. themes.push(opt)
  1316. this.emit('themes-changed', this.themes, { id, ...opt })
  1317. }
  1318. async selectTheme(
  1319. theme: Theme | LegacyTheme,
  1320. options: {
  1321. effect?: boolean
  1322. emit?: boolean
  1323. } = {}
  1324. ) {
  1325. const { effect, emit } = Object.assign(
  1326. { effect: true, emit: true }, options)
  1327. // Clear current theme before injecting.
  1328. if (this._currentTheme) {
  1329. this._currentTheme.eject()
  1330. }
  1331. // Detect if it is the default theme (no url).
  1332. if (!theme.url) {
  1333. this._currentTheme = null
  1334. } else {
  1335. const ejectTheme = injectTheme(theme.url)
  1336. this._currentTheme = {
  1337. pid: theme.pid,
  1338. opt: theme,
  1339. eject: ejectTheme,
  1340. }
  1341. }
  1342. if (effect) {
  1343. await this.saveUserPreferences(
  1344. theme.mode
  1345. ? {
  1346. themes: {
  1347. ...this._userPreferences.themes,
  1348. mode: theme.mode,
  1349. [theme.mode]: theme,
  1350. },
  1351. }
  1352. : { theme: theme }
  1353. )
  1354. }
  1355. if (emit) {
  1356. this.emit('theme-selected', theme, options)
  1357. }
  1358. }
  1359. async unregisterTheme(id: PluginLocalIdentity, effect = true) {
  1360. debug('Unregister theme #', id)
  1361. if (!this._registeredThemes.has(id)) {
  1362. return
  1363. }
  1364. this._registeredThemes.delete(id)
  1365. this.emit('themes-changed', this.themes, { id })
  1366. if (effect && this._currentTheme?.pid === id) {
  1367. this._currentTheme.eject()
  1368. this._currentTheme = null
  1369. const { theme, themes } = this._userPreferences
  1370. await this.saveUserPreferences({
  1371. theme: theme?.pid === id ? null : theme,
  1372. themes: {
  1373. ...themes,
  1374. light: themes.light?.pid === id ? null : themes.light,
  1375. dark: themes.dark?.pid === id ? null : themes.dark,
  1376. },
  1377. })
  1378. // Reset current theme if it is unregistered
  1379. this.emit('reset-custom-theme', this._userPreferences.themes)
  1380. }
  1381. }
  1382. }
  1383. function setupPluginCore(options: any) {
  1384. const pluginCore = new LSPluginCore(options)
  1385. debug('=== 🔗 Setup Logseq Plugin System 🔗 ===')
  1386. window.LSPluginCore = pluginCore
  1387. window.DOMPurify = DOMPurify
  1388. }
  1389. export { PluginLocal, pluginHelpers, setupPluginCore }