LSPlugin.core.ts 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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. if (this.installedFromUserWebUrl) {
  437. return `${this.installedFromUserWebUrl}/${filePath}`
  438. }
  439. return `https://pub-80f42b85b62c40219354a834fcf2bbfa.r2.dev/${path.join(localRoot, filePath)}`
  440. }
  441. const reg = /^(http|file)/
  442. if (!reg.test(filePath)) {
  443. const url = path.join(localRoot, filePath)
  444. filePath = reg.test(url) ? url : PROTOCOL_FILE + url
  445. }
  446. return !this.options.effect && this.isInstalledInLocalDotRoot
  447. ? convertToLSPResource(filePath, this.dotPluginsRoot)
  448. : filePath
  449. }
  450. async _preparePackageConfigs() {
  451. const { url, webPkg } = this._options
  452. let pkg: any = webPkg
  453. if (!pkg) {
  454. try {
  455. if (!url) {
  456. throw new Error('Can not resolve package config location')
  457. }
  458. debug('prepare package root', url)
  459. pkg = await invokeHostExportedApi('load_plugin_config', url)
  460. if (!pkg || ((pkg = JSON.parse(pkg)), !pkg)) {
  461. throw new Error(`Parse package config error #${url}/package.json`)
  462. }
  463. } catch (e) {
  464. throw new IllegalPluginPackageError(e.message)
  465. }
  466. }
  467. // Pick legal attrs
  468. ;[
  469. 'name',
  470. 'author',
  471. 'repository',
  472. 'version',
  473. 'description',
  474. 'repo',
  475. 'title',
  476. 'effect',
  477. 'sponsors',
  478. ]
  479. .concat(!this.isInstalledInLocalDotRoot ? ['devEntry'] : [])
  480. .forEach((k) => {
  481. this._options[k] = pkg[k]
  482. })
  483. const { repo, version } = this._options
  484. const localRoot = (this._localRoot = this.isWebPlugin ? `${repo || url}/${version}` : safetyPathNormalize(url))
  485. const logseq: Partial<LSPluginPkgConfig> = pkg.logseq || {}
  486. const validateEntry = (main) => main && /\.(js|html)$/.test(main)
  487. // Entry from main
  488. const entry = logseq.entry || logseq.main || pkg.main
  489. if (validateEntry(entry)) {
  490. // Theme has no main
  491. this._options.entry = this._resolveResourceFullUrl(entry, localRoot)
  492. // development mode entry
  493. this._options.devEntry = logseq.devEntry
  494. if (logseq.mode) {
  495. this._options.mode = logseq.mode
  496. }
  497. }
  498. const title = logseq.title || pkg.title
  499. const icon = logseq.icon || pkg.icon
  500. this._options.title = title
  501. this._options.icon = icon && this._resolveResourceFullUrl(icon)
  502. this._options.theme = Boolean(logseq.theme || !!logseq.themes)
  503. if (this.isInstalledInLocalDotRoot) {
  504. this._id = path.basename(localRoot)
  505. } else if (!this.isWebPlugin) {
  506. // development mode
  507. if (logseq.id) {
  508. this._id = logseq.id
  509. } else {
  510. logseq.id = this.id
  511. try {
  512. await invokeHostExportedApi('save_plugin_package_json', url, {
  513. ...pkg,
  514. logseq,
  515. })
  516. } catch (e) {
  517. debug('[save plugin ID Error] ', e)
  518. }
  519. }
  520. }
  521. // Validate id
  522. const { registeredPlugins, isRegistering } = this._ctx
  523. if (isRegistering && registeredPlugins.has(this.id)) {
  524. throw new ExistedImportedPluginPackageError(this.id)
  525. }
  526. return async () => {
  527. try {
  528. // 0. Install Themes
  529. const themes = logseq.themes
  530. if (themes) {
  531. await this._loadConfigThemes(
  532. Array.isArray(themes) ? themes : [themes]
  533. )
  534. }
  535. } catch (e) {
  536. debug('[prepare package effect Error]', e)
  537. }
  538. }
  539. }
  540. async _tryToNormalizeEntry() {
  541. let { entry, settings, devEntry } = this.options
  542. devEntry = devEntry || settings?.get('_devEntry')
  543. if (devEntry) {
  544. this._options.entry = devEntry
  545. return
  546. }
  547. if (!entry.endsWith('.js')) return
  548. let dirPathInstalled = null
  549. let tmp_file_method = 'write_user_tmp_file'
  550. if (this.isInstalledInLocalDotRoot) {
  551. tmp_file_method = 'write_dotdir_file'
  552. dirPathInstalled = this._localRoot.replace(this.dotPluginsRoot, '')
  553. dirPathInstalled = path.join(DIR_PLUGINS, dirPathInstalled)
  554. }
  555. const tag = new Date().getDay()
  556. const sdkPathRoot = await getSDKPathRoot()
  557. const entryPath = await invokeHostExportedApi(
  558. tmp_file_method,
  559. `${this._id}_index.html`,
  560. `<!doctype html>
  561. <html lang="en">
  562. <head>
  563. <meta charset="UTF-8">
  564. <title>logseq plugin entry</title>
  565. ${
  566. IS_DEV
  567. ? `<script src="${sdkPathRoot}/lsplugin.user.js?v=${tag}"></script>`
  568. : `<script src="https://cdn.jsdelivr.net/npm/@logseq/libs/dist/lsplugin.user.min.js?v=${tag}"></script>`
  569. }
  570. </head>
  571. <body>
  572. <div id="app"></div>
  573. <script src="${entry}"></script>
  574. </body>
  575. </html>`,
  576. dirPathInstalled
  577. )
  578. entry = convertToLSPResource(
  579. withFileProtocol(path.normalize(entryPath)),
  580. this.dotPluginsRoot
  581. )
  582. this._options.entry = entry
  583. }
  584. async _loadConfigThemes(themes: Theme[]) {
  585. themes.forEach((options) => {
  586. if (!options.url) return
  587. if (!options.url.startsWith('http') && this._localRoot) {
  588. options.url = this._resolveResourceFullUrl(options.url, this._localRoot)
  589. // file:// for native
  590. if (!this.isWebPlugin &&
  591. !options.url.startsWith('file:') &&
  592. !options.url.startsWith('lsp:')) {
  593. options.url = 'assets://' + options.url
  594. }
  595. }
  596. this.emit('provider:theme', options)
  597. })
  598. }
  599. async _loadLayoutsData(): Promise<Record<string, any>> {
  600. const key = this.id + '_layouts'
  601. const [, layouts] = await invokeHostExportedApi(
  602. 'load_plugin_user_settings',
  603. key
  604. )
  605. return layouts || {}
  606. }
  607. async _saveLayoutsData(data) {
  608. const key = this.id + '_layouts'
  609. await invokeHostExportedApi('save_plugin_user_settings', key, data)
  610. }
  611. async _persistMainUILayoutData(e: {
  612. width: number
  613. height: number
  614. left: number
  615. top: number
  616. }) {
  617. const layouts = await this._loadLayoutsData()
  618. layouts.$$0 = e
  619. await this._saveLayoutsData(layouts)
  620. }
  621. _setupDraggableContainer(
  622. el: HTMLElement,
  623. opts: Partial<{ key: string; title: string; close: () => void }> = {}
  624. ): () => void {
  625. const ds = el.dataset
  626. if (ds.inited_draggable) return
  627. if (!ds.identity) {
  628. ds.identity = 'dd-' + genID()
  629. }
  630. const isInjectedUI = !!opts.key
  631. const handle = document.createElement('div')
  632. handle.classList.add('draggable-handle')
  633. handle.innerHTML = `
  634. <div class="th">
  635. <div class="l"><h3>${opts.title || ''}</h3></div>
  636. <div class="r">
  637. <a class="button x"><i class="ti ti-x"></i></a>
  638. </div>
  639. </div>
  640. `
  641. handle.querySelector('.x').addEventListener(
  642. 'click',
  643. (e) => {
  644. opts?.close?.()
  645. e.stopPropagation()
  646. },
  647. false
  648. )
  649. handle.addEventListener(
  650. 'mousedown',
  651. (e) => {
  652. const target = e.target as HTMLElement
  653. if (target?.closest('.r')) {
  654. e.stopPropagation()
  655. e.preventDefault()
  656. }
  657. },
  658. false
  659. )
  660. el.prepend(handle)
  661. // move to top
  662. el.addEventListener(
  663. 'mousedown',
  664. (e) => {
  665. this.layoutCore.move_container_to_top(ds.identity)
  666. },
  667. true
  668. )
  669. const setTitle = (title) => {
  670. handle.querySelector('h3').textContent = title
  671. }
  672. const dispose = this.layoutCore.setup_draggable_container_BANG_(
  673. el,
  674. !isInjectedUI ? this._persistMainUILayoutData.bind(this) : () => {}
  675. )
  676. ds.inited_draggable = 'true'
  677. if (opts.title) {
  678. setTitle(opts.title)
  679. }
  680. // click outside
  681. let removeOutsideListener = null
  682. if (ds.close === 'outside') {
  683. const handler = (e) => {
  684. const target = e.target
  685. if (!el.contains(target)) {
  686. opts.close()
  687. }
  688. }
  689. document.addEventListener('click', handler, false)
  690. removeOutsideListener = () => {
  691. document.removeEventListener('click', handler)
  692. }
  693. }
  694. return () => {
  695. dispose()
  696. removeOutsideListener?.()
  697. }
  698. }
  699. _setupResizableContainer(el: HTMLElement, key?: string): () => void {
  700. const ds = el.dataset
  701. if (ds.inited_resizable) return
  702. if (!ds.identity) {
  703. ds.identity = 'dd-' + genID()
  704. }
  705. const handle = document.createElement('div')
  706. handle.classList.add('resizable-handle')
  707. el.prepend(handle)
  708. // @ts-expect-error
  709. const layoutCore = window.frontend.modules.layout.core
  710. const dispose = layoutCore.setup_resizable_container_BANG_(
  711. el,
  712. !key ? this._persistMainUILayoutData.bind(this) : () => {}
  713. )
  714. ds.inited_resizable = 'true'
  715. return dispose
  716. }
  717. async load(
  718. opts?: Partial<{
  719. indicator: DeferredActor
  720. reload: boolean
  721. }>
  722. ) {
  723. if (this.pending) {
  724. return
  725. }
  726. this._status = PluginLocalLoadStatus.LOADING
  727. this._loadErr = undefined
  728. try {
  729. // if (!this.options.entry) { // Themes package no entry field
  730. // }
  731. const installPackageThemes = await this._preparePackageConfigs()
  732. this._dispose(await this._setupUserSettings(opts?.reload))
  733. if (!this.disabled) {
  734. await installPackageThemes.call(null)
  735. }
  736. if (this.disabled || !this.options.entry) {
  737. return
  738. }
  739. this._ctx.emit('beforeload', this)
  740. await this._tryToNormalizeEntry()
  741. this._caller = new LSPluginCaller(this)
  742. await this._caller.connectToChild()
  743. const readyFn = () => {
  744. this._caller?.callUserModel(LSPMSG_READY, { pid: this.id })
  745. }
  746. if (opts?.indicator) {
  747. opts.indicator.promise.then(readyFn)
  748. } else {
  749. readyFn()
  750. }
  751. this._dispose(async () => {
  752. await this._caller?.destroy()
  753. })
  754. this._dispose(cleanInjectedScripts.bind(this))
  755. this._ctx.emit('loadeded', this)
  756. } catch (e) {
  757. this.logger.error('load', e, true)
  758. this.dispose().catch(null)
  759. this._status = PluginLocalLoadStatus.ERROR
  760. this._loadErr = e
  761. } finally {
  762. if (!this._loadErr) {
  763. if (this.disabled) {
  764. this._status = PluginLocalLoadStatus.UNLOADED
  765. } else {
  766. this._status = PluginLocalLoadStatus.LOADED
  767. }
  768. }
  769. }
  770. }
  771. async reload() {
  772. if (this.pending) {
  773. return
  774. }
  775. this._ctx.emit('beforereload', this)
  776. await this.unload()
  777. await this.load({ reload: true })
  778. this._ctx.emit('reloaded', this)
  779. }
  780. /**
  781. * @param unregister If true delete plugin files
  782. */
  783. async unload(unregister: boolean = false) {
  784. if (this.pending) {
  785. return
  786. }
  787. if (unregister) {
  788. await this.unload()
  789. if (this.isWebPlugin || this.isInstalledInLocalDotRoot) {
  790. this._ctx.emit('unlink-plugin', this.id)
  791. }
  792. return
  793. }
  794. try {
  795. const eventBeforeUnload = { unregister }
  796. if (this.loaded) {
  797. this._status = PluginLocalLoadStatus.UNLOADING
  798. try {
  799. await this._caller?.callUserModel(
  800. AWAIT_LSPMSGFn(LSPMSG_BEFORE_UNLOAD),
  801. eventBeforeUnload
  802. )
  803. this.emit('beforeunload', eventBeforeUnload)
  804. } catch (e) {
  805. this.logger.error('beforeunload', e)
  806. }
  807. await this.dispose()
  808. }
  809. this.emit('unloaded')
  810. } catch (e) {
  811. this.logger.error('unload', e)
  812. } finally {
  813. this._status = PluginLocalLoadStatus.UNLOADED
  814. }
  815. }
  816. private async dispose() {
  817. for (const fn of this._disposes) {
  818. try {
  819. fn && (await fn())
  820. } catch (e) {
  821. console.error(this.debugTag, 'dispose Error', e)
  822. }
  823. }
  824. // clear
  825. this._disposes = []
  826. }
  827. _dispose(fn: any) {
  828. if (!fn) return
  829. this._disposes.push(fn)
  830. }
  831. _onHostMounted(callback: () => void) {
  832. const actor = this._ctx.hostMountedActor
  833. if (!actor || actor.settled) {
  834. callback()
  835. } else {
  836. actor?.promise.then(callback)
  837. }
  838. }
  839. get isWebPlugin() {
  840. return this._ctx.isWebPlatform || !!this.options.webPkg
  841. }
  842. get installedFromUserWebUrl() {
  843. return this.isWebPlugin && this.options.webPkg?.installedFromUserWebUrl
  844. }
  845. get layoutCore(): any {
  846. // @ts-expect-error
  847. return window.frontend.modules.layout.core
  848. }
  849. get isInstalledInLocalDotRoot() {
  850. if (this.isWebPlugin) return false
  851. const dotRoot = this.dotConfigRoot
  852. const plgRoot = this.localRoot
  853. return dotRoot && plgRoot && plgRoot.startsWith(dotRoot)
  854. }
  855. get loaded() {
  856. return this._status === PluginLocalLoadStatus.LOADED
  857. }
  858. get pending() {
  859. return [
  860. PluginLocalLoadStatus.LOADING,
  861. PluginLocalLoadStatus.UNLOADING,
  862. ].includes(this._status)
  863. }
  864. get status(): PluginLocalLoadStatus {
  865. return this._status
  866. }
  867. get settings() {
  868. return this.options.settings
  869. }
  870. set settingsSchema(schema: SettingSchemaDesc[]) {
  871. this._options.settingsSchema = schema
  872. }
  873. get settingsSchema() {
  874. return this.options.settingsSchema
  875. }
  876. get logger() {
  877. return this._logger
  878. }
  879. get disabled() {
  880. return this.settings?.get('disabled')
  881. }
  882. get theme() {
  883. return this.options.theme
  884. }
  885. get caller() {
  886. return this._caller
  887. }
  888. get id(): string {
  889. return this._id
  890. }
  891. get shadow(): boolean {
  892. return this.options.mode === 'shadow'
  893. }
  894. get options(): PluginLocalOptions {
  895. return this._options
  896. }
  897. get themeMgr(): ILSPluginThemeManager {
  898. return this._themeMgr
  899. }
  900. get debugTag() {
  901. const name = this._options?.name
  902. return `#${this._id} - ${name ?? ''}`
  903. }
  904. get localRoot(): string {
  905. return this._localRoot || this._options.url
  906. }
  907. get loadErr(): Error | undefined {
  908. return this._loadErr
  909. }
  910. get dotConfigRoot() {
  911. return path.normalize(this._ctx.options.dotConfigRoot)
  912. }
  913. get dotSettingsFile(): string | undefined {
  914. return this._dotSettingsFile
  915. }
  916. get dotPluginsRoot() {
  917. return path.join(this.dotConfigRoot, DIR_PLUGINS)
  918. }
  919. get sdk(): Partial<PluginLocalSDKMetadata> {
  920. return this._sdk
  921. }
  922. set sdk(value: Partial<PluginLocalSDKMetadata>) {
  923. this._sdk = value
  924. }
  925. toJSON(settings = true) {
  926. const json = { ...this.options } as any
  927. json.id = this.id
  928. json.err = this.loadErr
  929. json.usf = this.dotSettingsFile
  930. json.iir = this.isInstalledInLocalDotRoot
  931. json.webMode = this.isWebPlugin ? (this.installedFromUserWebUrl ? 'user' : 'github') : false
  932. json.lsr = this._resolveResourceFullUrl('/')
  933. if (settings === false) {
  934. delete json.settings
  935. } else {
  936. json.settings = json.settings?.toJSON()
  937. }
  938. return json
  939. }
  940. }
  941. /**
  942. * Host plugin core
  943. */
  944. class LSPluginCore
  945. extends EventEmitter<
  946. | 'beforeenable'
  947. | 'enabled'
  948. | 'beforedisable'
  949. | 'disabled'
  950. | 'registered'
  951. | 'error'
  952. | 'unregistered'
  953. | 'ready'
  954. | 'themes-changed'
  955. | 'theme-selected'
  956. | 'reset-custom-theme'
  957. | 'settings-changed'
  958. | 'unlink-plugin'
  959. | 'beforeload'
  960. | 'loadeded'
  961. | 'beforereload'
  962. | 'reloaded'
  963. >
  964. implements ILSPluginThemeManager {
  965. private _isRegistering = false
  966. private _readyIndicator?: DeferredActor
  967. private readonly _hostMountedActor: DeferredActor = deferred()
  968. private readonly _userPreferences: UserPreferences = {
  969. theme: null,
  970. themes: {
  971. mode: 'light',
  972. light: null,
  973. dark: null,
  974. },
  975. externals: [],
  976. }
  977. private readonly _registeredThemes = new Map<PluginLocalIdentity, Theme[]>()
  978. private readonly _registeredPlugins = new Map<
  979. PluginLocalIdentity,
  980. PluginLocal
  981. >()
  982. private _currentTheme: {
  983. pid: PluginLocalIdentity
  984. opt: Theme | LegacyTheme
  985. eject: () => void
  986. }
  987. /**
  988. * @param _options
  989. */
  990. constructor(private readonly _options: Partial<LSPluginCoreOptions>) {
  991. super()
  992. }
  993. async loadUserPreferences() {
  994. try {
  995. const settings = await invokeHostExportedApi('load_user_preferences')
  996. if (settings) {
  997. Object.assign(this._userPreferences, settings)
  998. }
  999. } catch (e) {
  1000. debug('[load user preferences Error]', e)
  1001. }
  1002. }
  1003. async saveUserPreferences(settings: Partial<UserPreferences>) {
  1004. try {
  1005. if (settings) {
  1006. Object.assign(this._userPreferences, settings)
  1007. }
  1008. await invokeHostExportedApi(
  1009. 'save_user_preferences',
  1010. this._userPreferences
  1011. )
  1012. } catch (e) {
  1013. debug('[save user preferences Error]', e)
  1014. }
  1015. }
  1016. /**
  1017. * Activate the user preferences.
  1018. *
  1019. * Steps:
  1020. *
  1021. * 1. Load the custom theme.
  1022. *
  1023. * @memberof LSPluginCore
  1024. */
  1025. async activateUserPreferences() {
  1026. const { theme: legacyTheme, themes } = this._userPreferences
  1027. const currentTheme = themes[themes.mode]
  1028. // If there is currently a theme that has been set
  1029. if (currentTheme) {
  1030. await this.selectTheme(currentTheme, { effect: false, emit: false })
  1031. } else if (legacyTheme) {
  1032. // Otherwise compatible with older versions
  1033. await this.selectTheme(legacyTheme, { effect: false, emit: false })
  1034. }
  1035. }
  1036. /**
  1037. * @param plugins
  1038. * @param initial
  1039. */
  1040. async register(
  1041. plugins: RegisterPluginOpts[] | RegisterPluginOpts,
  1042. initial = false
  1043. ) {
  1044. if (!Array.isArray(plugins)) {
  1045. await this.register([plugins])
  1046. return
  1047. }
  1048. const perfTable = new Map<
  1049. string,
  1050. { o: PluginLocal; s: number; e: number }
  1051. >()
  1052. const debugPerfInfo = () => {
  1053. const data: any = Array.from(perfTable.values()).reduce((ac, it) => {
  1054. const { id, options, status, disabled } = it.o
  1055. if (
  1056. disabled !== true &&
  1057. (options.entry || (!options.name && !options.entry))
  1058. ) {
  1059. ac[id] = {
  1060. name: options.name,
  1061. entry: options.entry,
  1062. status: status,
  1063. enabled:
  1064. typeof disabled === 'boolean' ? (!disabled ? '🟢' : '⚫️') : '🔴',
  1065. perf: !it.e ? it.o.loadErr : `${(it.e - it.s).toFixed(2)}ms`,
  1066. }
  1067. }
  1068. return ac
  1069. }, {})
  1070. console.table(data)
  1071. }
  1072. // @ts-expect-error
  1073. window.__debugPluginsPerfInfo = debugPerfInfo
  1074. try {
  1075. this._isRegistering = true
  1076. const _userConfigRoot = this._options.dotConfigRoot
  1077. const readyIndicator = (this._readyIndicator = deferred())
  1078. await this.loadUserPreferences()
  1079. let externals = new Set(this._userPreferences.externals)
  1080. // valid externals
  1081. if (externals?.size) {
  1082. try {
  1083. const validatedExternals: Record<string, boolean> =
  1084. await invokeHostExportedApi('validate_external_plugins', [
  1085. ...externals,
  1086. ])
  1087. externals = new Set(
  1088. [...Object.entries(validatedExternals)].reduce((a, [k, v]) => {
  1089. if (v) {
  1090. a.push(k)
  1091. }
  1092. return a
  1093. }, [])
  1094. )
  1095. } catch (e) {
  1096. console.error('[validatedExternals Error]', e)
  1097. }
  1098. }
  1099. if (initial) {
  1100. plugins = plugins.concat(
  1101. [...externals]
  1102. .filter((url) => {
  1103. return (
  1104. !plugins.length ||
  1105. (plugins as RegisterPluginOpts[]).every(
  1106. (p) => !p.entry && p.url !== url
  1107. )
  1108. )
  1109. })
  1110. .map((url) => ({ url }))
  1111. )
  1112. }
  1113. for (const pluginOptions of plugins) {
  1114. const { url } = pluginOptions as PluginLocalOptions
  1115. const pluginLocal = new PluginLocal(
  1116. pluginOptions as PluginLocalOptions,
  1117. this,
  1118. this
  1119. )
  1120. const perfInfo = { o: pluginLocal, s: performance.now(), e: 0 }
  1121. perfTable.set(url, perfInfo)
  1122. await pluginLocal.load({ indicator: readyIndicator })
  1123. perfInfo.e = performance.now()
  1124. const { loadErr } = pluginLocal
  1125. if (loadErr) {
  1126. debug('[Failed LOAD Plugin] #', pluginOptions)
  1127. this.emit('error', loadErr)
  1128. if (
  1129. loadErr instanceof IllegalPluginPackageError ||
  1130. loadErr instanceof ExistedImportedPluginPackageError
  1131. ) {
  1132. // TODO: notify global log system?
  1133. continue
  1134. }
  1135. }
  1136. pluginLocal.settings?.on('change', (a) => {
  1137. this.emit('settings-changed', pluginLocal.id, a)
  1138. pluginLocal.caller?.callUserModel(LSPMSG_SETTINGS, { payload: a })
  1139. })
  1140. this._registeredPlugins.set(pluginLocal.id, pluginLocal)
  1141. this.emit('registered', pluginLocal)
  1142. // external plugins
  1143. if (!pluginLocal.isWebPlugin && !pluginLocal.isInstalledInLocalDotRoot) {
  1144. externals.add(url)
  1145. }
  1146. }
  1147. await this.saveUserPreferences({ externals: Array.from(externals) })
  1148. await this.activateUserPreferences()
  1149. readyIndicator.resolve('ready')
  1150. } catch (e) {
  1151. console.error(e)
  1152. } finally {
  1153. this._isRegistering = false
  1154. this.emit('ready', perfTable)
  1155. debugPerfInfo()
  1156. }
  1157. }
  1158. async reload(plugins: PluginLocalIdentity[] | PluginLocalIdentity) {
  1159. if (!Array.isArray(plugins)) {
  1160. await this.reload([plugins])
  1161. return
  1162. }
  1163. for (const identity of plugins) {
  1164. try {
  1165. const p = this.ensurePlugin(identity)
  1166. await p.reload()
  1167. } catch (e) {
  1168. debug(e)
  1169. }
  1170. }
  1171. }
  1172. async unregister(plugins: PluginLocalIdentity[] | PluginLocalIdentity) {
  1173. if (!Array.isArray(plugins)) {
  1174. await this.unregister([plugins])
  1175. return
  1176. }
  1177. const unregisteredExternals: string[] = []
  1178. for (const identity of plugins) {
  1179. const p = this.ensurePlugin(identity)
  1180. if (!p.isWebPlugin && !p.isInstalledInLocalDotRoot) {
  1181. unregisteredExternals.push(p.options.url)
  1182. }
  1183. await p.unload(true)
  1184. this._registeredPlugins.delete(identity)
  1185. this.emit('unregistered', identity)
  1186. }
  1187. const externals = this._userPreferences.externals
  1188. if (externals.length && unregisteredExternals.length) {
  1189. await this.saveUserPreferences({
  1190. externals: externals.filter((it) => {
  1191. return !unregisteredExternals.includes(it)
  1192. }),
  1193. })
  1194. }
  1195. }
  1196. async enable(plugin: PluginLocalIdentity) {
  1197. const p = this.ensurePlugin(plugin)
  1198. if (p.pending) return
  1199. this.emit('beforeenable')
  1200. p.settings?.set('disabled', false)
  1201. this.emit('enabled', p.id)
  1202. }
  1203. async disable(plugin: PluginLocalIdentity) {
  1204. const p = this.ensurePlugin(plugin)
  1205. if (p.pending) return
  1206. this.emit('beforedisable')
  1207. p.settings?.set('disabled', true)
  1208. this.emit('disabled', p.id)
  1209. }
  1210. async _hook(ns: string, type: string, payload?: any, pid?: string) {
  1211. const hook = `${ns}:${safeSnakeCase(type)}`
  1212. const isDbChangedHook = hook === 'hook:db:changed'
  1213. const isDbBlockChangeHook = hook.startsWith('hook:db:block')
  1214. const act = (p: PluginLocal) => {
  1215. debug(`[call hook][#${p.id}]`, ns, type)
  1216. p.caller?.callUserModel(LSPMSG, {
  1217. ns,
  1218. type: safeSnakeCase(type),
  1219. payload,
  1220. })
  1221. }
  1222. const p = pid && this._registeredPlugins.get(pid)
  1223. if (p && !p.disabled && p.options.entry) {
  1224. act(p)
  1225. return
  1226. }
  1227. for (const [_, p] of this._registeredPlugins) {
  1228. if (!p.options.entry || p.disabled) {
  1229. continue
  1230. }
  1231. if (!pid) {
  1232. // compatible for old SDK < 0.0.2
  1233. const sdkVersion = p.sdk?.version
  1234. // TODO: remove optimization after few releases
  1235. if (!sdkVersion) {
  1236. if (isDbChangedHook || isDbBlockChangeHook) {
  1237. continue
  1238. } else {
  1239. act(p)
  1240. }
  1241. }
  1242. if (
  1243. sdkVersion &&
  1244. invokeHostExportedApi('should_exec_plugin_hook', p.id, hook)
  1245. ) {
  1246. act(p)
  1247. }
  1248. } else if (pid === p.id) {
  1249. act(p)
  1250. break
  1251. }
  1252. }
  1253. }
  1254. async hookApp(type: string, payload?: any, pid?: string) {
  1255. return await this._hook('hook:app', type, payload, pid)
  1256. }
  1257. async hookEditor(type: string, payload?: any, pid?: string) {
  1258. return await this._hook('hook:editor', type, payload, pid)
  1259. }
  1260. async hookDb(type: string, payload?: any, pid?: string) {
  1261. return await this._hook('hook:db', type, payload, pid)
  1262. }
  1263. ensurePlugin(plugin: PluginLocalIdentity | PluginLocal) {
  1264. if (plugin instanceof PluginLocal) {
  1265. return plugin
  1266. }
  1267. const p = this._registeredPlugins.get(plugin)
  1268. if (!p) {
  1269. throw new Error(`plugin #${plugin} not existed.`)
  1270. }
  1271. return p
  1272. }
  1273. hostMounted() {
  1274. this._hostMountedActor.resolve()
  1275. }
  1276. _forceCleanInjectedUI(id: string) {
  1277. if (!id) return
  1278. return cleanInjectedUI(id)
  1279. }
  1280. get isWebPlatform() {
  1281. return this.options.dotConfigRoot?.startsWith('LSPUserDotRoot')
  1282. }
  1283. get registeredPlugins(): Map<PluginLocalIdentity, PluginLocal> {
  1284. return this._registeredPlugins
  1285. }
  1286. get options() {
  1287. return this._options
  1288. }
  1289. get readyIndicator(): DeferredActor | undefined {
  1290. return this._readyIndicator
  1291. }
  1292. get hostMountedActor(): DeferredActor {
  1293. return this._hostMountedActor
  1294. }
  1295. get isRegistering(): boolean {
  1296. return this._isRegistering
  1297. }
  1298. get themes() {
  1299. return this._registeredThemes
  1300. }
  1301. get enabledPlugins() {
  1302. return [...this.registeredPlugins.entries()].reduce((a, b) => {
  1303. let p = b?.[1]
  1304. if (p?.disabled !== true) {
  1305. a.set(b?.[0], p)
  1306. }
  1307. return a
  1308. }, new Map())
  1309. }
  1310. async registerTheme(id: PluginLocalIdentity, opt: Theme): Promise<void> {
  1311. debug('Register theme #', id, opt)
  1312. if (!id) return
  1313. let themes: Theme[] = this._registeredThemes.get(id)!
  1314. if (!themes) {
  1315. this._registeredThemes.set(id, (themes = []))
  1316. }
  1317. themes.push(opt)
  1318. this.emit('themes-changed', this.themes, { id, ...opt })
  1319. }
  1320. async selectTheme(
  1321. theme: Theme | LegacyTheme,
  1322. options: {
  1323. effect?: boolean
  1324. emit?: boolean
  1325. } = {}
  1326. ) {
  1327. const { effect, emit } = Object.assign(
  1328. { effect: true, emit: true }, options)
  1329. // Clear current theme before injecting.
  1330. if (this._currentTheme) {
  1331. this._currentTheme.eject()
  1332. }
  1333. // Detect if it is the default theme (no url).
  1334. if (!theme.url) {
  1335. this._currentTheme = null
  1336. } else {
  1337. const ejectTheme = injectTheme(theme.url)
  1338. this._currentTheme = {
  1339. pid: theme.pid,
  1340. opt: theme,
  1341. eject: ejectTheme,
  1342. }
  1343. }
  1344. if (effect) {
  1345. await this.saveUserPreferences(
  1346. theme.mode
  1347. ? {
  1348. themes: {
  1349. ...this._userPreferences.themes,
  1350. mode: theme.mode,
  1351. [theme.mode]: theme,
  1352. },
  1353. }
  1354. : { theme: theme }
  1355. )
  1356. }
  1357. if (emit) {
  1358. this.emit('theme-selected', theme, options)
  1359. }
  1360. }
  1361. async unregisterTheme(id: PluginLocalIdentity, effect = true) {
  1362. debug('Unregister theme #', id)
  1363. if (!this._registeredThemes.has(id)) {
  1364. return
  1365. }
  1366. this._registeredThemes.delete(id)
  1367. this.emit('themes-changed', this.themes, { id })
  1368. if (effect && this._currentTheme?.pid === id) {
  1369. this._currentTheme.eject()
  1370. this._currentTheme = null
  1371. const { theme, themes } = this._userPreferences
  1372. await this.saveUserPreferences({
  1373. theme: theme?.pid === id ? null : theme,
  1374. themes: {
  1375. ...themes,
  1376. light: themes.light?.pid === id ? null : themes.light,
  1377. dark: themes.dark?.pid === id ? null : themes.dark,
  1378. },
  1379. })
  1380. // Reset current theme if it is unregistered
  1381. this.emit('reset-custom-theme', this._userPreferences.themes)
  1382. }
  1383. }
  1384. }
  1385. function setupPluginCore(options: any) {
  1386. const pluginCore = new LSPluginCore(options)
  1387. debug('=== 🔗 Setup Logseq Plugin System 🔗 ===')
  1388. window.LSPluginCore = pluginCore
  1389. window.DOMPurify = DOMPurify
  1390. }
  1391. export { PluginLocal, pluginHelpers, setupPluginCore }