LSPlugin.core.ts 37 KB

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