| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658 | 
							- import EventEmitter from 'eventemitter3'
 
- import {
 
-   deepMerge,
 
-   setupInjectedStyle,
 
-   genID,
 
-   setupInjectedUI,
 
-   deferred,
 
-   invokeHostExportedApi,
 
-   isObject,
 
-   withFileProtocol,
 
-   getSDKPathRoot,
 
-   PROTOCOL_FILE,
 
-   URL_LSP,
 
-   safetyPathJoin,
 
-   path,
 
-   safetyPathNormalize,
 
-   mergeSettingsWithSchema,
 
-   IS_DEV,
 
-   cleanInjectedScripts,
 
-   safeSnakeCase,
 
-   injectTheme,
 
-   cleanInjectedUI,
 
-   PluginLogger,
 
- } from './helpers'
 
- import * as pluginHelpers from './helpers'
 
- import DOMPurify from 'dompurify'
 
- import Debug from 'debug'
 
- import {
 
-   LSPluginCaller,
 
-   LSPMSG_READY,
 
-   LSPMSG_SYNC,
 
-   LSPMSG,
 
-   LSPMSG_SETTINGS,
 
-   LSPMSG_ERROR_TAG,
 
-   LSPMSG_BEFORE_UNLOAD,
 
-   AWAIT_LSPMSGFn,
 
- } from './LSPlugin.caller'
 
- import {
 
-   ILSPluginThemeManager,
 
-   LegacyTheme,
 
-   LSPluginPkgConfig,
 
-   SettingSchemaDesc,
 
-   StyleOptions,
 
-   StyleString,
 
-   Theme,
 
-   ThemeMode,
 
-   UIContainerAttrs,
 
-   UIOptions,
 
- } from './LSPlugin'
 
- const debug = Debug('LSPlugin:core')
 
- const DIR_PLUGINS = 'plugins'
 
- declare global {
 
-   interface Window {
 
-     LSPluginCore: LSPluginCore
 
-     DOMPurify: typeof DOMPurify
 
-     $$callerPluginID: string | undefined
 
-   }
 
- }
 
- type DeferredActor = ReturnType<typeof deferred>
 
- interface LSPluginCoreOptions {
 
-   dotConfigRoot: string
 
- }
 
- /**
 
-  * User settings
 
-  */
 
- class PluginSettings extends EventEmitter<'change' | 'reset'> {
 
-   private _settings: Record<string, any> = {
 
-     disabled: false,
 
-   }
 
-   constructor(
 
-     private readonly _userPluginSettings: any,
 
-     private _schema?: SettingSchemaDesc[]
 
-   ) {
 
-     super()
 
-     Object.assign(this._settings, _userPluginSettings)
 
-   }
 
-   get<T = any>(k: string): T {
 
-     return this._settings[k]
 
-   }
 
-   set(k: string | Record<string, any>, v?: any) {
 
-     const o = deepMerge({}, this._settings)
 
-     if (typeof k === 'string') {
 
-       if (this._settings[k] == v) return
 
-       this._settings[k] = v
 
-     } else if (isObject(k)) {
 
-       this._settings = deepMerge(this._settings, k)
 
-     } else {
 
-       return
 
-     }
 
-     this.emit('change', { ...this._settings }, o)
 
-   }
 
-   set settings(value: Record<string, any>) {
 
-     const o = deepMerge({}, this._settings)
 
-     this._settings = value || {}
 
-     this.emit('change', { ...this._settings }, o)
 
-   }
 
-   get settings(): Record<string, any> {
 
-     return this._settings
 
-   }
 
-   setSchema(schema: SettingSchemaDesc[], syncSettings?: boolean) {
 
-     this._schema = schema
 
-     if (syncSettings) {
 
-       const _settings = this._settings
 
-       this._settings = mergeSettingsWithSchema(_settings, schema)
 
-       this.emit('change', this._settings, _settings)
 
-     }
 
-   }
 
-   reset() {
 
-     const o = this.settings
 
-     const val = {}
 
-     if (this._schema) {
 
-       // TODO: generated by schema
 
-     }
 
-     this.settings = val
 
-     this.emit('reset', val, o)
 
-   }
 
-   toJSON() {
 
-     return this._settings
 
-   }
 
- }
 
- interface UserPreferences {
 
-   theme: LegacyTheme
 
-   themes: {
 
-     mode: ThemeMode
 
-     light: Theme
 
-     dark: Theme
 
-   }
 
-   externals: string[] // external plugin locations
 
- }
 
- interface PluginLocalOptions {
 
-   key?: string // Unique from Logseq Plugin Store
 
-   entry: string // Plugin main file
 
-   url: string // Plugin package absolute fs location
 
-   name: string
 
-   version: string
 
-   mode: 'shadow' | 'iframe'
 
-   webPkg?: any // web plugin package.json data
 
-   settingsSchema?: SettingSchemaDesc[]
 
-   settings?: PluginSettings
 
-   effect?: boolean
 
-   theme?: boolean
 
-   [key: string]: any
 
- }
 
- interface PluginLocalSDKMetadata {
 
-   version: string
 
-   [key: string]: any
 
- }
 
- type PluginLocalUrl = Pick<PluginLocalOptions, 'url'> & { [key: string]: any }
 
- type RegisterPluginOpts = PluginLocalOptions | PluginLocalUrl
 
- type PluginLocalIdentity = string
 
- enum PluginLocalLoadStatus {
 
-   LOADING = 'loading',
 
-   UNLOADING = 'unloading',
 
-   LOADED = 'loaded',
 
-   UNLOADED = 'unload',
 
-   ERROR = 'error',
 
- }
 
- function initUserSettingsHandlers(pluginLocal: PluginLocal) {
 
-   const _ = (label: string): any => `settings:${label}`
 
-   // settings:schema
 
-   pluginLocal.on(
 
-     _('schema'),
 
-     ({ schema, isSync }: { schema: SettingSchemaDesc[]; isSync?: boolean }) => {
 
-       pluginLocal.settingsSchema = schema
 
-       pluginLocal.settings?.setSchema(schema, isSync)
 
-     }
 
-   )
 
-   // settings:update
 
-   pluginLocal.on(_('update'), (attrs) => {
 
-     if (!attrs) return
 
-     pluginLocal.settings?.set(attrs)
 
-   })
 
-   // settings:visible:changed
 
-   pluginLocal.on(_('visible:changed'), (payload) => {
 
-     const visible = payload?.visible
 
-     invokeHostExportedApi(
 
-       'set_focused_settings',
 
-       visible ? pluginLocal.id : null
 
-     )
 
-   })
 
- }
 
- function initMainUIHandlers(pluginLocal: PluginLocal) {
 
-   const _ = (label: string): any => `main-ui:${label}`
 
-   // main-ui:visible
 
-   pluginLocal.on(_('visible'), ({ visible, toggle, cursor, autoFocus }) => {
 
-     const el = pluginLocal.getMainUIContainer()
 
-     el?.classList[toggle ? 'toggle' : visible ? 'add' : 'remove']('visible')
 
-     // pluginLocal.caller!.callUserModel(LSPMSG, { type: _('visible'), payload: visible })
 
-     // auto focus frame
 
-     if (visible) {
 
-       if (!pluginLocal.shadow && el && autoFocus !== false) {
 
-         el.querySelector('iframe')?.contentWindow?.focus()
 
-       }
 
-     } else {
 
-       // @ts-expect-error set activeElement back to `body`
 
-       el.ownerDocument.activeElement.blur()
 
-     }
 
-     if (cursor) {
 
-       invokeHostExportedApi('restore_editing_cursor')
 
-     }
 
-   })
 
-   // main-ui:attrs
 
-   pluginLocal.on(_('attrs'), (attrs: Partial<UIContainerAttrs>) => {
 
-     const el = pluginLocal.getMainUIContainer()
 
-     Object.entries(attrs).forEach(([k, v]) => {
 
-       el?.setAttribute(k, String(v))
 
-       if (k === 'draggable' && v) {
 
-         pluginLocal._dispose(
 
-           pluginLocal._setupDraggableContainer(el, {
 
-             title: pluginLocal.options.name,
 
-             close: () => {
 
-               pluginLocal.caller.call('sys:ui:visible', { toggle: true })
 
-             },
 
-           })
 
-         )
 
-       }
 
-       if (k === 'resizable' && v) {
 
-         pluginLocal._dispose(pluginLocal._setupResizableContainer(el))
 
-       }
 
-     })
 
-   })
 
-   // main-ui:style
 
-   pluginLocal.on(_('style'), (style: Record<string, any>) => {
 
-     const el = pluginLocal.getMainUIContainer()
 
-     const isInitedLayout = !!el.dataset.inited_layout
 
-     Object.entries(style).forEach(([k, v]) => {
 
-       if (
 
-         isInitedLayout &&
 
-         ['left', 'top', 'bottom', 'right', 'width', 'height'].includes(k)
 
-       ) {
 
-         return
 
-       }
 
-       el.style[k] = v
 
-     })
 
-   })
 
- }
 
- function initProviderHandlers(pluginLocal: PluginLocal) {
 
-   const _ = (label: string): any => `provider:${label}`
 
-   let themed = false
 
-   // provider:theme
 
-   pluginLocal.on(_('theme'), (theme: Theme) => {
 
-     pluginLocal.themeMgr.registerTheme(pluginLocal.id, theme)
 
-     if (!themed) {
 
-       pluginLocal._dispose(() => {
 
-         pluginLocal.themeMgr.unregisterTheme(pluginLocal.id)
 
-       })
 
-       themed = true
 
-     }
 
-   })
 
-   // provider:style
 
-   pluginLocal.on(_('style'), (style: StyleString | StyleOptions) => {
 
-     let key: string | undefined
 
-     if (typeof style !== 'string') {
 
-       key = style.key
 
-       style = style.style
 
-     }
 
-     if (!style || !style.trim()) return
 
-     pluginLocal._dispose(
 
-       setupInjectedStyle(style, {
 
-         'data-injected-style': key ? `${key}-${pluginLocal.id}` : '',
 
-         'data-ref': pluginLocal.id,
 
-       })
 
-     )
 
-   })
 
-   // provider:ui
 
-   pluginLocal.on(_('ui'), (ui: UIOptions) => {
 
-     pluginLocal._onHostMounted(() => {
 
-       const ret = setupInjectedUI.call(
 
-         pluginLocal,
 
-         ui,
 
-         Object.assign(
 
-           {
 
-             'data-ref': pluginLocal.id,
 
-           },
 
-           ui.attrs || {}
 
-         ),
 
-         ({ el, float }) => {
 
-           if (!float) return
 
-           const identity = el.dataset.identity
 
-           pluginLocal.layoutCore.move_container_to_top(identity)
 
-         }
 
-       )
 
-       if (typeof ret === 'function') {
 
-         pluginLocal._dispose(ret)
 
-       }
 
-     })
 
-   })
 
- }
 
- function initApiProxyHandlers(pluginLocal: PluginLocal) {
 
-   const _ = (label: string): any => `api:${label}`
 
-   pluginLocal.on(_('call'), async (payload) => {
 
-     let ret: any
 
-     try {
 
-       window.$$callerPluginID = pluginLocal.id
 
-       ret = await invokeHostExportedApi.apply(pluginLocal, [
 
-         payload.method,
 
-         ...payload.args,
 
-       ])
 
-     } catch (e) {
 
-       ret = {
 
-         [LSPMSG_ERROR_TAG]: e,
 
-       }
 
-     } finally {
 
-       window.$$callerPluginID = undefined
 
-     }
 
-     if (pluginLocal.shadow) {
 
-       if (payload.actor) {
 
-         payload.actor.resolve(ret)
 
-       }
 
-       return
 
-     }
 
-     const { _sync } = payload
 
-     if (_sync != null) {
 
-       const reply = (result: any) => {
 
-         pluginLocal.caller?.callUserModel(LSPMSG_SYNC, {
 
-           result,
 
-           _sync,
 
-         })
 
-       }
 
-       Promise.resolve(ret).then(reply, reply)
 
-     }
 
-   })
 
- }
 
- function convertToLSPResource(fullUrl: string, dotPluginRoot: string) {
 
-   if (dotPluginRoot && fullUrl.startsWith(PROTOCOL_FILE + dotPluginRoot)) {
 
-     fullUrl = safetyPathJoin(
 
-       URL_LSP,
 
-       fullUrl.substr(PROTOCOL_FILE.length + dotPluginRoot.length)
 
-     )
 
-   }
 
-   return fullUrl
 
- }
 
- class IllegalPluginPackageError extends Error {
 
-   constructor(message: string) {
 
-     super(message)
 
-     this.name = 'IllegalPluginPackageError'
 
-   }
 
- }
 
- class ExistedImportedPluginPackageError extends Error {
 
-   constructor(message: string) {
 
-     super(message)
 
-     this.name = 'ExistedImportedPluginPackageError'
 
-   }
 
- }
 
- /**
 
-  * Host plugin for local
 
-  */
 
- class PluginLocal extends EventEmitter<
 
-   'loaded' | 'unloaded' | 'beforeunload' | 'error' | string
 
- > {
 
-   private _sdk: Partial<PluginLocalSDKMetadata> = {}
 
-   private _disposes: Array<() => Promise<any>> = []
 
-   private _id: PluginLocalIdentity
 
-   private _status: PluginLocalLoadStatus = PluginLocalLoadStatus.UNLOADED
 
-   private _loadErr?: Error
 
-   private _localRoot?: string
 
-   private _dotSettingsFile?: string
 
-   private _caller?: LSPluginCaller
 
-   private _logger?: PluginLogger = new PluginLogger('PluginLocal')
 
-   /**
 
-    * @param _options
 
-    * @param _themeMgr
 
-    * @param _ctx
 
-    */
 
-   constructor(
 
-     private _options: PluginLocalOptions,
 
-     private readonly _themeMgr: ILSPluginThemeManager,
 
-     private readonly _ctx: LSPluginCore
 
-   ) {
 
-     super()
 
-     this._id = _options.key || genID()
 
-     initUserSettingsHandlers(this)
 
-     initMainUIHandlers(this)
 
-     initProviderHandlers(this)
 
-     initApiProxyHandlers(this)
 
-   }
 
-   async _setupUserSettings(reload?: boolean) {
 
-     const { _options } = this
 
-     const logger = (this._logger = new PluginLogger(`Loader:${this.debugTag}`))
 
-     if (_options.settings && !reload) {
 
-       return
 
-     }
 
-     try {
 
-       const loadFreshSettings = () =>
 
-         invokeHostExportedApi('load_plugin_user_settings', this.id)
 
-       const [userSettingsFilePath, userSettings] = await loadFreshSettings()
 
-       this._dotSettingsFile = userSettingsFilePath
 
-       let settings = _options.settings
 
-       if (!settings) {
 
-         settings = _options.settings = new PluginSettings(userSettings)
 
-       }
 
-       if (reload) {
 
-         settings.settings = userSettings
 
-         return
 
-       }
 
-       const handler = async (a, b) => {
 
-         debug('Settings changed', this.debugTag, a)
 
-         if (!a.disabled && b.disabled) {
 
-           // Enable plugin
 
-           const [, freshSettings] = await loadFreshSettings()
 
-           freshSettings.disabled = false
 
-           a = Object.assign(a, freshSettings)
 
-           settings.settings = a
 
-           await this.load()
 
-         }
 
-         if (a.disabled && !b.disabled) {
 
-           // Disable plugin
 
-           const [, freshSettings] = await loadFreshSettings()
 
-           freshSettings.disabled = true
 
-           a = Object.assign(a, freshSettings)
 
-           await this.unload()
 
-         }
 
-         if (a) {
 
-           invokeHostExportedApi('save_plugin_user_settings', this.id, a)
 
-         }
 
-       }
 
-       // observe settings
 
-       settings.on('change', handler)
 
-       return () => {}
 
-     } catch (e) {
 
-       debug('[load plugin user settings Error]', e)
 
-       logger?.error(e)
 
-     }
 
-   }
 
-   getMainUIContainer(): HTMLElement | undefined {
 
-     if (this.shadow) {
 
-       return this.caller?._getSandboxShadowContainer()
 
-     }
 
-     return this.caller?._getSandboxIframeContainer()
 
-   }
 
-   _resolveResourceFullUrl(filePath: string, localRoot?: string) {
 
-     if (!filePath?.trim()) return
 
-     localRoot = localRoot || this._localRoot
 
-     if (this.isWebPlugin) {
 
-       // TODO: strategy for Logseq plugins center
 
-       return `https://pub-80f42b85b62c40219354a834fcf2bbfa.r2.dev/${path.join(localRoot, filePath)}`
 
-     }
 
-     const reg = /^(http|file)/
 
-     if (!reg.test(filePath)) {
 
-       const url = path.join(localRoot, filePath)
 
-       filePath = reg.test(url) ? url : PROTOCOL_FILE + url
 
-     }
 
-     return !this.options.effect && this.isInstalledInLocalDotRoot
 
-       ? convertToLSPResource(filePath, this.dotPluginsRoot)
 
-       : filePath
 
-   }
 
-   async _preparePackageConfigs() {
 
-     const { url, webPkg } = this._options
 
-     let pkg: any = webPkg
 
-     if (!pkg) {
 
-       try {
 
-         if (!url) {
 
-           throw new Error('Can not resolve package config location')
 
-         }
 
-         debug('prepare package root', url)
 
-         pkg = await invokeHostExportedApi('load_plugin_config', url)
 
-         if (!pkg || ((pkg = JSON.parse(pkg)), !pkg)) {
 
-           throw new Error(`Parse package config error #${url}/package.json`)
 
-         }
 
-       } catch (e) {
 
-         throw new IllegalPluginPackageError(e.message)
 
-       }
 
-     }
 
-     // Pick legal attrs
 
-     ;[
 
-       'name',
 
-       'author',
 
-       'repository',
 
-       'version',
 
-       'description',
 
-       'repo',
 
-       'title',
 
-       'effect',
 
-       'sponsors',
 
-     ]
 
-       .concat(!this.isInstalledInLocalDotRoot ? ['devEntry'] : [])
 
-       .forEach((k) => {
 
-         this._options[k] = pkg[k]
 
-       })
 
-     const { repo, version } = this._options
 
-     const localRoot = (this._localRoot = this.isWebPlugin ? `${repo}/${version}` : safetyPathNormalize(url))
 
-     const logseq: Partial<LSPluginPkgConfig> = pkg.logseq || {}
 
-     const validateEntry = (main) => main && /\.(js|html)$/.test(main)
 
-     // Entry from main
 
-     const entry = logseq.entry || logseq.main || pkg.main
 
-     if (validateEntry(entry)) {
 
-       // Theme has no main
 
-       this._options.entry = this._resolveResourceFullUrl(entry, localRoot)
 
-       // development mode entry
 
-       this._options.devEntry = logseq.devEntry
 
-       if (logseq.mode) {
 
-         this._options.mode = logseq.mode
 
-       }
 
-     }
 
-     const title = logseq.title || pkg.title
 
-     const icon = logseq.icon || pkg.icon
 
-     this._options.title = title
 
-     this._options.icon = icon && this._resolveResourceFullUrl(icon)
 
-     this._options.theme = Boolean(logseq.theme || !!logseq.themes)
 
-     if (this.isInstalledInLocalDotRoot) {
 
-       this._id = path.basename(localRoot)
 
-     } else if (!this.isWebPlugin) {
 
-       // development mode
 
-       if (logseq.id) {
 
-         this._id = logseq.id
 
-       } else {
 
-         logseq.id = this.id
 
-         try {
 
-           await invokeHostExportedApi('save_plugin_package_json', url, {
 
-             ...pkg,
 
-             logseq,
 
-           })
 
-         } catch (e) {
 
-           debug('[save plugin ID Error] ', e)
 
-         }
 
-       }
 
-     }
 
-     // Validate id
 
-     const { registeredPlugins, isRegistering } = this._ctx
 
-     if (isRegistering && registeredPlugins.has(this.id)) {
 
-       throw new ExistedImportedPluginPackageError(this.id)
 
-     }
 
-     return async () => {
 
-       try {
 
-         // 0. Install Themes
 
-         const themes = logseq.themes
 
-         if (themes) {
 
-           await this._loadConfigThemes(
 
-             Array.isArray(themes) ? themes : [themes]
 
-           )
 
-         }
 
-       } catch (e) {
 
-         debug('[prepare package effect Error]', e)
 
-       }
 
-     }
 
-   }
 
-   async _tryToNormalizeEntry() {
 
-     let { entry, settings, devEntry } = this.options
 
-     devEntry = devEntry || settings?.get('_devEntry')
 
-     if (devEntry) {
 
-       this._options.entry = devEntry
 
-       return
 
-     }
 
-     if (!entry.endsWith('.js')) return
 
-     let dirPathInstalled = null
 
-     let tmp_file_method = 'write_user_tmp_file'
 
-     if (this.isInstalledInLocalDotRoot) {
 
-       tmp_file_method = 'write_dotdir_file'
 
-       dirPathInstalled = this._localRoot.replace(this.dotPluginsRoot, '')
 
-       dirPathInstalled = path.join(DIR_PLUGINS, dirPathInstalled)
 
-     }
 
-     const tag = new Date().getDay()
 
-     const sdkPathRoot = await getSDKPathRoot()
 
-     const entryPath = await invokeHostExportedApi(
 
-       tmp_file_method,
 
-       `${this._id}_index.html`,
 
-       `<!doctype html>
 
- <html lang="en">
 
-   <head>
 
-     <meta charset="UTF-8">
 
-     <title>logseq plugin entry</title>
 
-     ${
 
-         IS_DEV
 
-           ? `<script src="${sdkPathRoot}/lsplugin.user.js?v=${tag}"></script>`
 
-           : `<script src="https://cdn.jsdelivr.net/npm/@logseq/libs/dist/lsplugin.user.min.js?v=${tag}"></script>`
 
-       }
 
-     
 
-   </head>
 
-   <body>
 
-   <div id="app"></div>
 
-   <script src="${entry}"></script>
 
-   </body>
 
- </html>`,
 
-       dirPathInstalled
 
-     )
 
-     entry = convertToLSPResource(
 
-       withFileProtocol(path.normalize(entryPath)),
 
-       this.dotPluginsRoot
 
-     )
 
-     this._options.entry = entry
 
-   }
 
-   async _loadConfigThemes(themes: Theme[]) {
 
-     themes.forEach((options) => {
 
-       if (!options.url) return
 
-       if (!options.url.startsWith('http') && this._localRoot) {
 
-         options.url = this._resolveResourceFullUrl(options.url, this._localRoot)
 
-         // file:// for native
 
-         if (!this.isWebPlugin && !options.url.startsWith('file:')) {
 
-           options.url = 'assets://' + options.url
 
-         }
 
-       }
 
-       this.emit('provider:theme', options)
 
-     })
 
-   }
 
-   async _loadLayoutsData(): Promise<Record<string, any>> {
 
-     const key = this.id + '_layouts'
 
-     const [, layouts] = await invokeHostExportedApi(
 
-       'load_plugin_user_settings',
 
-       key
 
-     )
 
-     return layouts || {}
 
-   }
 
-   async _saveLayoutsData(data) {
 
-     const key = this.id + '_layouts'
 
-     await invokeHostExportedApi('save_plugin_user_settings', key, data)
 
-   }
 
-   async _persistMainUILayoutData(e: {
 
-     width: number
 
-     height: number
 
-     left: number
 
-     top: number
 
-   }) {
 
-     const layouts = await this._loadLayoutsData()
 
-     layouts.$$0 = e
 
-     await this._saveLayoutsData(layouts)
 
-   }
 
-   _setupDraggableContainer(
 
-     el: HTMLElement,
 
-     opts: Partial<{ key: string; title: string; close: () => void }> = {}
 
-   ): () => void {
 
-     const ds = el.dataset
 
-     if (ds.inited_draggable) return
 
-     if (!ds.identity) {
 
-       ds.identity = 'dd-' + genID()
 
-     }
 
-     const isInjectedUI = !!opts.key
 
-     const handle = document.createElement('div')
 
-     handle.classList.add('draggable-handle')
 
-     handle.innerHTML = `
 
-       <div class="th">
 
-         <div class="l"><h3>${opts.title || ''}</h3></div>
 
-         <div class="r">
 
-           <a class="button x"><i class="ti ti-x"></i></a>
 
-         </div>
 
-       </div>
 
-     `
 
-     handle.querySelector('.x').addEventListener(
 
-       'click',
 
-       (e) => {
 
-         opts?.close?.()
 
-         e.stopPropagation()
 
-       },
 
-       false
 
-     )
 
-     handle.addEventListener(
 
-       'mousedown',
 
-       (e) => {
 
-         const target = e.target as HTMLElement
 
-         if (target?.closest('.r')) {
 
-           e.stopPropagation()
 
-           e.preventDefault()
 
-         }
 
-       },
 
-       false
 
-     )
 
-     el.prepend(handle)
 
-     // move to top
 
-     el.addEventListener(
 
-       'mousedown',
 
-       (e) => {
 
-         this.layoutCore.move_container_to_top(ds.identity)
 
-       },
 
-       true
 
-     )
 
-     const setTitle = (title) => {
 
-       handle.querySelector('h3').textContent = title
 
-     }
 
-     const dispose = this.layoutCore.setup_draggable_container_BANG_(
 
-       el,
 
-       !isInjectedUI ? this._persistMainUILayoutData.bind(this) : () => {}
 
-     )
 
-     ds.inited_draggable = 'true'
 
-     if (opts.title) {
 
-       setTitle(opts.title)
 
-     }
 
-     // click outside
 
-     let removeOutsideListener = null
 
-     if (ds.close === 'outside') {
 
-       const handler = (e) => {
 
-         const target = e.target
 
-         if (!el.contains(target)) {
 
-           opts.close()
 
-         }
 
-       }
 
-       document.addEventListener('click', handler, false)
 
-       removeOutsideListener = () => {
 
-         document.removeEventListener('click', handler)
 
-       }
 
-     }
 
-     return () => {
 
-       dispose()
 
-       removeOutsideListener?.()
 
-     }
 
-   }
 
-   _setupResizableContainer(el: HTMLElement, key?: string): () => void {
 
-     const ds = el.dataset
 
-     if (ds.inited_resizable) return
 
-     if (!ds.identity) {
 
-       ds.identity = 'dd-' + genID()
 
-     }
 
-     const handle = document.createElement('div')
 
-     handle.classList.add('resizable-handle')
 
-     el.prepend(handle)
 
-     // @ts-expect-error
 
-     const layoutCore = window.frontend.modules.layout.core
 
-     const dispose = layoutCore.setup_resizable_container_BANG_(
 
-       el,
 
-       !key ? this._persistMainUILayoutData.bind(this) : () => {}
 
-     )
 
-     ds.inited_resizable = 'true'
 
-     return dispose
 
-   }
 
-   async load(
 
-     opts?: Partial<{
 
-       indicator: DeferredActor
 
-       reload: boolean
 
-     }>
 
-   ) {
 
-     if (this.pending) {
 
-       return
 
-     }
 
-     this._status = PluginLocalLoadStatus.LOADING
 
-     this._loadErr = undefined
 
-     try {
 
-       // if (!this.options.entry) { // Themes package no entry field
 
-       // }
 
-       const installPackageThemes = await this._preparePackageConfigs()
 
-       this._dispose(await this._setupUserSettings(opts?.reload))
 
-       if (!this.disabled) {
 
-         await installPackageThemes.call(null)
 
-       }
 
-       if (this.disabled || !this.options.entry) {
 
-         return
 
-       }
 
-       this._ctx.emit('beforeload', this)
 
-       await this._tryToNormalizeEntry()
 
-       this._caller = new LSPluginCaller(this)
 
-       await this._caller.connectToChild()
 
-       const readyFn = () => {
 
-         this._caller?.callUserModel(LSPMSG_READY, { pid: this.id })
 
-       }
 
-       if (opts?.indicator) {
 
-         opts.indicator.promise.then(readyFn)
 
-       } else {
 
-         readyFn()
 
-       }
 
-       this._dispose(async () => {
 
-         await this._caller?.destroy()
 
-       })
 
-       this._dispose(cleanInjectedScripts.bind(this))
 
-       this._ctx.emit('loadeded', this)
 
-     } catch (e) {
 
-       this.logger.error('load', e, true)
 
-       this.dispose().catch(null)
 
-       this._status = PluginLocalLoadStatus.ERROR
 
-       this._loadErr = e
 
-     } finally {
 
-       if (!this._loadErr) {
 
-         if (this.disabled) {
 
-           this._status = PluginLocalLoadStatus.UNLOADED
 
-         } else {
 
-           this._status = PluginLocalLoadStatus.LOADED
 
-         }
 
-       }
 
-     }
 
-   }
 
-   async reload() {
 
-     if (this.pending) {
 
-       return
 
-     }
 
-     this._ctx.emit('beforereload', this)
 
-     await this.unload()
 
-     await this.load({ reload: true })
 
-     this._ctx.emit('reloaded', this)
 
-   }
 
-   /**
 
-    * @param unregister If true delete plugin files
 
-    */
 
-   async unload(unregister: boolean = false) {
 
-     if (this.pending) {
 
-       return
 
-     }
 
-     if (unregister) {
 
-       await this.unload()
 
-       if (this.isWebPlugin || this.isInstalledInLocalDotRoot) {
 
-         this._ctx.emit('unlink-plugin', this.id)
 
-       }
 
-       return
 
-     }
 
-     try {
 
-       const eventBeforeUnload = { unregister }
 
-       if (this.loaded) {
 
-         this._status = PluginLocalLoadStatus.UNLOADING
 
-         try {
 
-           await this._caller?.callUserModel(
 
-             AWAIT_LSPMSGFn(LSPMSG_BEFORE_UNLOAD),
 
-             eventBeforeUnload
 
-           )
 
-           this.emit('beforeunload', eventBeforeUnload)
 
-         } catch (e) {
 
-           this.logger.error('beforeunload', e)
 
-         }
 
-         await this.dispose()
 
-       }
 
-       this.emit('unloaded')
 
-     } catch (e) {
 
-       this.logger.error('unload', e)
 
-     } finally {
 
-       this._status = PluginLocalLoadStatus.UNLOADED
 
-     }
 
-   }
 
-   private async dispose() {
 
-     for (const fn of this._disposes) {
 
-       try {
 
-         fn && (await fn())
 
-       } catch (e) {
 
-         console.error(this.debugTag, 'dispose Error', e)
 
-       }
 
-     }
 
-     // clear
 
-     this._disposes = []
 
-   }
 
-   _dispose(fn: any) {
 
-     if (!fn) return
 
-     this._disposes.push(fn)
 
-   }
 
-   _onHostMounted(callback: () => void) {
 
-     const actor = this._ctx.hostMountedActor
 
-     if (!actor || actor.settled) {
 
-       callback()
 
-     } else {
 
-       actor?.promise.then(callback)
 
-     }
 
-   }
 
-   get isWebPlugin() {
 
-     return this._ctx.isWebPlatform || !!this.options.webPkg
 
-   }
 
-   get layoutCore(): any {
 
-     // @ts-expect-error
 
-     return window.frontend.modules.layout.core
 
-   }
 
-   get isInstalledInLocalDotRoot() {
 
-     if (this.isWebPlugin) return false
 
-     const dotRoot = this.dotConfigRoot
 
-     const plgRoot = this.localRoot
 
-     return dotRoot && plgRoot && plgRoot.startsWith(dotRoot)
 
-   }
 
-   get loaded() {
 
-     return this._status === PluginLocalLoadStatus.LOADED
 
-   }
 
-   get pending() {
 
-     return [
 
-       PluginLocalLoadStatus.LOADING,
 
-       PluginLocalLoadStatus.UNLOADING,
 
-     ].includes(this._status)
 
-   }
 
-   get status(): PluginLocalLoadStatus {
 
-     return this._status
 
-   }
 
-   get settings() {
 
-     return this.options.settings
 
-   }
 
-   set settingsSchema(schema: SettingSchemaDesc[]) {
 
-     this._options.settingsSchema = schema
 
-   }
 
-   get settingsSchema() {
 
-     return this.options.settingsSchema
 
-   }
 
-   get logger() {
 
-     return this._logger
 
-   }
 
-   get disabled() {
 
-     return this.settings?.get('disabled')
 
-   }
 
-   get theme() {
 
-     return this.options.theme
 
-   }
 
-   get caller() {
 
-     return this._caller
 
-   }
 
-   get id(): string {
 
-     return this._id
 
-   }
 
-   get shadow(): boolean {
 
-     return this.options.mode === 'shadow'
 
-   }
 
-   get options(): PluginLocalOptions {
 
-     return this._options
 
-   }
 
-   get themeMgr(): ILSPluginThemeManager {
 
-     return this._themeMgr
 
-   }
 
-   get debugTag() {
 
-     const name = this._options?.name
 
-     return `#${this._id} - ${name ?? ''}`
 
-   }
 
-   get localRoot(): string {
 
-     return this._localRoot || this._options.url
 
-   }
 
-   get loadErr(): Error | undefined {
 
-     return this._loadErr
 
-   }
 
-   get dotConfigRoot() {
 
-     return path.normalize(this._ctx.options.dotConfigRoot)
 
-   }
 
-   get dotSettingsFile(): string | undefined {
 
-     return this._dotSettingsFile
 
-   }
 
-   get dotPluginsRoot() {
 
-     return path.join(this.dotConfigRoot, DIR_PLUGINS)
 
-   }
 
-   get sdk(): Partial<PluginLocalSDKMetadata> {
 
-     return this._sdk
 
-   }
 
-   set sdk(value: Partial<PluginLocalSDKMetadata>) {
 
-     this._sdk = value
 
-   }
 
-   toJSON(settings = true) {
 
-     const json = { ...this.options } as any
 
-     json.id = this.id
 
-     json.err = this.loadErr
 
-     json.usf = this.dotSettingsFile
 
-     json.iir = this.isInstalledInLocalDotRoot
 
-     json.lsr = this._resolveResourceFullUrl('/')
 
-     if (settings === false) {
 
-       delete json.settings
 
-     } else {
 
-       json.settings = json.settings?.toJSON()
 
-     }
 
-     return json
 
-   }
 
- }
 
- /**
 
-  * Host plugin core
 
-  */
 
- class LSPluginCore
 
-   extends EventEmitter<
 
-     | 'beforeenable'
 
-     | 'enabled'
 
-     | 'beforedisable'
 
-     | 'disabled'
 
-     | 'registered'
 
-     | 'error'
 
-     | 'unregistered'
 
-     | 'ready'
 
-     | 'themes-changed'
 
-     | 'theme-selected'
 
-     | 'reset-custom-theme'
 
-     | 'settings-changed'
 
-     | 'unlink-plugin'
 
-     | 'beforeload'
 
-     | 'loadeded'
 
-     | 'beforereload'
 
-     | 'reloaded'
 
-   >
 
-   implements ILSPluginThemeManager {
 
-   private _isRegistering = false
 
-   private _readyIndicator?: DeferredActor
 
-   private readonly _hostMountedActor: DeferredActor = deferred()
 
-   private readonly _userPreferences: UserPreferences = {
 
-     theme: null,
 
-     themes: {
 
-       mode: 'light',
 
-       light: null,
 
-       dark: null,
 
-     },
 
-     externals: [],
 
-   }
 
-   private readonly _registeredThemes = new Map<PluginLocalIdentity, Theme[]>()
 
-   private readonly _registeredPlugins = new Map<
 
-     PluginLocalIdentity,
 
-     PluginLocal
 
-   >()
 
-   private _currentTheme: {
 
-     pid: PluginLocalIdentity
 
-     opt: Theme | LegacyTheme
 
-     eject: () => void
 
-   }
 
-   /**
 
-    * @param _options
 
-    */
 
-   constructor(private readonly _options: Partial<LSPluginCoreOptions>) {
 
-     super()
 
-   }
 
-   async loadUserPreferences() {
 
-     try {
 
-       const settings = await invokeHostExportedApi('load_user_preferences')
 
-       if (settings) {
 
-         Object.assign(this._userPreferences, settings)
 
-       }
 
-     } catch (e) {
 
-       debug('[load user preferences Error]', e)
 
-     }
 
-   }
 
-   async saveUserPreferences(settings: Partial<UserPreferences>) {
 
-     try {
 
-       if (settings) {
 
-         Object.assign(this._userPreferences, settings)
 
-       }
 
-       await invokeHostExportedApi(
 
-         'save_user_preferences',
 
-         this._userPreferences
 
-       )
 
-     } catch (e) {
 
-       debug('[save user preferences Error]', e)
 
-     }
 
-   }
 
-   /**
 
-    * Activate the user preferences.
 
-    *
 
-    * Steps:
 
-    *
 
-    * 1. Load the custom theme.
 
-    *
 
-    * @memberof LSPluginCore
 
-    */
 
-   async activateUserPreferences() {
 
-     const { theme: legacyTheme, themes } = this._userPreferences
 
-     const currentTheme = themes[themes.mode]
 
-     // If there is currently a theme that has been set
 
-     if (currentTheme) {
 
-       await this.selectTheme(currentTheme, { effect: false, emit: false })
 
-     } else if (legacyTheme) {
 
-       // Otherwise compatible with older versions
 
-       await this.selectTheme(legacyTheme, { effect: false, emit: false })
 
-     }
 
-   }
 
-   /**
 
-    * @param plugins
 
-    * @param initial
 
-    */
 
-   async register(
 
-     plugins: RegisterPluginOpts[] | RegisterPluginOpts,
 
-     initial = false
 
-   ) {
 
-     if (!Array.isArray(plugins)) {
 
-       await this.register([plugins])
 
-       return
 
-     }
 
-     const perfTable = new Map<
 
-       string,
 
-       { o: PluginLocal; s: number; e: number }
 
-     >()
 
-     const debugPerfInfo = () => {
 
-       const data: any = Array.from(perfTable.values()).reduce((ac, it) => {
 
-         const { id, options, status, disabled } = it.o
 
-         if (
 
-           disabled !== true &&
 
-           (options.entry || (!options.name && !options.entry))
 
-         ) {
 
-           ac[id] = {
 
-             name: options.name,
 
-             entry: options.entry,
 
-             status: status,
 
-             enabled:
 
-               typeof disabled === 'boolean' ? (!disabled ? '🟢' : '⚫️') : '🔴',
 
-             perf: !it.e ? it.o.loadErr : `${(it.e - it.s).toFixed(2)}ms`,
 
-           }
 
-         }
 
-         return ac
 
-       }, {})
 
-       console.table(data)
 
-     }
 
-     // @ts-expect-error
 
-     window.__debugPluginsPerfInfo = debugPerfInfo
 
-     try {
 
-       this._isRegistering = true
 
-       const _userConfigRoot = this._options.dotConfigRoot
 
-       const readyIndicator = (this._readyIndicator = deferred())
 
-       await this.loadUserPreferences()
 
-       let externals = new Set(this._userPreferences.externals)
 
-       // valid externals
 
-       if (externals?.size) {
 
-         try {
 
-           const validatedExternals: Record<string, boolean> =
 
-             await invokeHostExportedApi('validate_external_plugins', [
 
-               ...externals,
 
-             ])
 
-           externals = new Set(
 
-             [...Object.entries(validatedExternals)].reduce((a, [k, v]) => {
 
-               if (v) {
 
-                 a.push(k)
 
-               }
 
-               return a
 
-             }, [])
 
-           )
 
-         } catch (e) {
 
-           console.error('[validatedExternals Error]', e)
 
-         }
 
-       }
 
-       if (initial) {
 
-         plugins = plugins.concat(
 
-           [...externals]
 
-             .filter((url) => {
 
-               return (
 
-                 !plugins.length ||
 
-                 (plugins as RegisterPluginOpts[]).every(
 
-                   (p) => !p.entry && p.url !== url
 
-                 )
 
-               )
 
-             })
 
-             .map((url) => ({ url }))
 
-         )
 
-       }
 
-       for (const pluginOptions of plugins) {
 
-         const { url } = pluginOptions as PluginLocalOptions
 
-         const pluginLocal = new PluginLocal(
 
-           pluginOptions as PluginLocalOptions,
 
-           this,
 
-           this
 
-         )
 
-         const perfInfo = { o: pluginLocal, s: performance.now(), e: 0 }
 
-         perfTable.set(url, perfInfo)
 
-         await pluginLocal.load({ indicator: readyIndicator })
 
-         perfInfo.e = performance.now()
 
-         const { loadErr } = pluginLocal
 
-         if (loadErr) {
 
-           debug('[Failed LOAD Plugin] #', pluginOptions)
 
-           this.emit('error', loadErr)
 
-           if (
 
-             loadErr instanceof IllegalPluginPackageError ||
 
-             loadErr instanceof ExistedImportedPluginPackageError
 
-           ) {
 
-             // TODO: notify global log system?
 
-             continue
 
-           }
 
-         }
 
-         pluginLocal.settings?.on('change', (a) => {
 
-           this.emit('settings-changed', pluginLocal.id, a)
 
-           pluginLocal.caller?.callUserModel(LSPMSG_SETTINGS, { payload: a })
 
-         })
 
-         this._registeredPlugins.set(pluginLocal.id, pluginLocal)
 
-         this.emit('registered', pluginLocal)
 
-         // external plugins
 
-         if (!pluginLocal.isWebPlugin && !pluginLocal.isInstalledInLocalDotRoot) {
 
-           externals.add(url)
 
-         }
 
-       }
 
-       await this.saveUserPreferences({ externals: Array.from(externals) })
 
-       await this.activateUserPreferences()
 
-       readyIndicator.resolve('ready')
 
-     } catch (e) {
 
-       console.error(e)
 
-     } finally {
 
-       this._isRegistering = false
 
-       this.emit('ready', perfTable)
 
-       debugPerfInfo()
 
-     }
 
-   }
 
-   async reload(plugins: PluginLocalIdentity[] | PluginLocalIdentity) {
 
-     if (!Array.isArray(plugins)) {
 
-       await this.reload([plugins])
 
-       return
 
-     }
 
-     for (const identity of plugins) {
 
-       try {
 
-         const p = this.ensurePlugin(identity)
 
-         await p.reload()
 
-       } catch (e) {
 
-         debug(e)
 
-       }
 
-     }
 
-   }
 
-   async unregister(plugins: PluginLocalIdentity[] | PluginLocalIdentity) {
 
-     if (!Array.isArray(plugins)) {
 
-       await this.unregister([plugins])
 
-       return
 
-     }
 
-     const unregisteredExternals: string[] = []
 
-     for (const identity of plugins) {
 
-       const p = this.ensurePlugin(identity)
 
-       if (!p.isWebPlugin && !p.isInstalledInLocalDotRoot) {
 
-         unregisteredExternals.push(p.options.url)
 
-       }
 
-       await p.unload(true)
 
-       this._registeredPlugins.delete(identity)
 
-       this.emit('unregistered', identity)
 
-     }
 
-     const externals = this._userPreferences.externals
 
-     if (externals.length && unregisteredExternals.length) {
 
-       await this.saveUserPreferences({
 
-         externals: externals.filter((it) => {
 
-           return !unregisteredExternals.includes(it)
 
-         }),
 
-       })
 
-     }
 
-   }
 
-   async enable(plugin: PluginLocalIdentity) {
 
-     const p = this.ensurePlugin(plugin)
 
-     if (p.pending) return
 
-     this.emit('beforeenable')
 
-     p.settings?.set('disabled', false)
 
-     this.emit('enabled', p.id)
 
-   }
 
-   async disable(plugin: PluginLocalIdentity) {
 
-     const p = this.ensurePlugin(plugin)
 
-     if (p.pending) return
 
-     this.emit('beforedisable')
 
-     p.settings?.set('disabled', true)
 
-     this.emit('disabled', p.id)
 
-   }
 
-   async _hook(ns: string, type: string, payload?: any, pid?: string) {
 
-     const hook = `${ns}:${safeSnakeCase(type)}`
 
-     const isDbChangedHook = hook === 'hook:db:changed'
 
-     const isDbBlockChangeHook = hook.startsWith('hook:db:block')
 
-     const act = (p: PluginLocal) => {
 
-       debug(`[call hook][#${p.id}]`, ns, type)
 
-       p.caller?.callUserModel(LSPMSG, {
 
-         ns,
 
-         type: safeSnakeCase(type),
 
-         payload,
 
-       })
 
-     }
 
-     const p = pid && this._registeredPlugins.get(pid)
 
-     if (p && !p.disabled && p.options.entry) {
 
-       act(p)
 
-       return
 
-     }
 
-     for (const [_, p] of this._registeredPlugins) {
 
-       if (!p.options.entry || p.disabled) {
 
-         continue
 
-       }
 
-       if (!pid) {
 
-         // compatible for old SDK < 0.0.2
 
-         const sdkVersion = p.sdk?.version
 
-         // TODO: remove optimization after few releases
 
-         if (!sdkVersion) {
 
-           if (isDbChangedHook || isDbBlockChangeHook) {
 
-             continue
 
-           } else {
 
-             act(p)
 
-           }
 
-         }
 
-         if (
 
-           sdkVersion &&
 
-           invokeHostExportedApi('should_exec_plugin_hook', p.id, hook)
 
-         ) {
 
-           act(p)
 
-         }
 
-       } else if (pid === p.id) {
 
-         act(p)
 
-         break
 
-       }
 
-     }
 
-   }
 
-   async hookApp(type: string, payload?: any, pid?: string) {
 
-     return await this._hook('hook:app', type, payload, pid)
 
-   }
 
-   async hookEditor(type: string, payload?: any, pid?: string) {
 
-     return await this._hook('hook:editor', type, payload, pid)
 
-   }
 
-   async hookDb(type: string, payload?: any, pid?: string) {
 
-     return await this._hook('hook:db', type, payload, pid)
 
-   }
 
-   ensurePlugin(plugin: PluginLocalIdentity | PluginLocal) {
 
-     if (plugin instanceof PluginLocal) {
 
-       return plugin
 
-     }
 
-     const p = this._registeredPlugins.get(plugin)
 
-     if (!p) {
 
-       throw new Error(`plugin #${plugin} not existed.`)
 
-     }
 
-     return p
 
-   }
 
-   hostMounted() {
 
-     this._hostMountedActor.resolve()
 
-   }
 
-   _forceCleanInjectedUI(id: string) {
 
-     if (!id) return
 
-     return cleanInjectedUI(id)
 
-   }
 
-   get isWebPlatform() {
 
-     return this.options.dotConfigRoot?.startsWith('LSPUserDotRoot')
 
-   }
 
-   get registeredPlugins(): Map<PluginLocalIdentity, PluginLocal> {
 
-     return this._registeredPlugins
 
-   }
 
-   get options() {
 
-     return this._options
 
-   }
 
-   get readyIndicator(): DeferredActor | undefined {
 
-     return this._readyIndicator
 
-   }
 
-   get hostMountedActor(): DeferredActor {
 
-     return this._hostMountedActor
 
-   }
 
-   get isRegistering(): boolean {
 
-     return this._isRegistering
 
-   }
 
-   get themes() {
 
-     return this._registeredThemes
 
-   }
 
-   get enabledPlugins() {
 
-     return [...this.registeredPlugins.entries()].reduce((a, b) => {
 
-       let p = b?.[1]
 
-       if (p?.disabled !== true) {
 
-         a.set(b?.[0], p)
 
-       }
 
-       return a
 
-     }, new Map())
 
-   }
 
-   async registerTheme(id: PluginLocalIdentity, opt: Theme): Promise<void> {
 
-     debug('Register theme #', id, opt)
 
-     if (!id) return
 
-     let themes: Theme[] = this._registeredThemes.get(id)!
 
-     if (!themes) {
 
-       this._registeredThemes.set(id, (themes = []))
 
-     }
 
-     themes.push(opt)
 
-     this.emit('themes-changed', this.themes, { id, ...opt })
 
-   }
 
-   async selectTheme(
 
-     theme: Theme | LegacyTheme,
 
-     options: {
 
-       effect?: boolean
 
-       emit?: boolean
 
-     } = {}
 
-   ) {
 
-     const { effect, emit } = Object.assign(
 
-       { effect: true, emit: true }, options)
 
-     // Clear current theme before injecting.
 
-     if (this._currentTheme) {
 
-       this._currentTheme.eject()
 
-     }
 
-     // Detect if it is the default theme (no url).
 
-     if (!theme.url) {
 
-       this._currentTheme = null
 
-     } else {
 
-       const ejectTheme = injectTheme(theme.url)
 
-       this._currentTheme = {
 
-         pid: theme.pid,
 
-         opt: theme,
 
-         eject: ejectTheme,
 
-       }
 
-     }
 
-     if (effect) {
 
-       await this.saveUserPreferences(
 
-         theme.mode
 
-           ? {
 
-             themes: {
 
-               ...this._userPreferences.themes,
 
-               mode: theme.mode,
 
-               [theme.mode]: theme,
 
-             },
 
-           }
 
-           : { theme: theme }
 
-       )
 
-     }
 
-     if (emit) {
 
-       this.emit('theme-selected', theme, options)
 
-     }
 
-   }
 
-   async unregisterTheme(id: PluginLocalIdentity, effect = true) {
 
-     debug('Unregister theme #', id)
 
-     if (!this._registeredThemes.has(id)) {
 
-       return
 
-     }
 
-     this._registeredThemes.delete(id)
 
-     this.emit('themes-changed', this.themes, { id })
 
-     if (effect && this._currentTheme?.pid === id) {
 
-       this._currentTheme.eject()
 
-       this._currentTheme = null
 
-       const { theme, themes } = this._userPreferences
 
-       await this.saveUserPreferences({
 
-         theme: theme?.pid === id ? null : theme,
 
-         themes: {
 
-           ...themes,
 
-           light: themes.light?.pid === id ? null : themes.light,
 
-           dark: themes.dark?.pid === id ? null : themes.dark,
 
-         },
 
-       })
 
-       // Reset current theme if it is unregistered
 
-       this.emit('reset-custom-theme', this._userPreferences.themes)
 
-     }
 
-   }
 
- }
 
- function setupPluginCore(options: any) {
 
-   const pluginCore = new LSPluginCore(options)
 
-   debug('=== 🔗 Setup Logseq Plugin System 🔗 ===')
 
-   window.LSPluginCore = pluginCore
 
-   window.DOMPurify = DOMPurify
 
- }
 
- export { PluginLocal, pluginHelpers, setupPluginCore }
 
 
  |