LSPlugin.core.ts 39 KB

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