LSPlugin.core.ts 37 KB

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