LSPlugin.core.ts 39 KB

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