plugins: exports.customHtml
Massimo Melina committed
Jun 5, 2024 at 17:04 UTC
8b10c06a864fd2ef804ffbff09f9d11bd8b9d832
4 files changed
+15
-6
dev-plugins.md
+3
-1
@@ -115,7 +115,8 @@ used must be strictly JSON (thus, no single quotes, only double quotes for strin
115
When necessary your plugin will read its value using `api.getConfig('message')`.
116
117
- `configDialog: DialogOptions` object to override dialog options. Please refer to sources for details.
118
-- `onFrontendConfig: (config: object) => void | object` manipulate config values exposed to front-end
118
+- `onFrontendConfig: (config: object) => void | object` manipulate config values exposed to front-end.
119
+- `customHtml: object | () => object` return custom-html sections programmatically.
120
121
### FieldDescriptor
122
@@ -591,6 +592,7 @@ If you want to override a text regardless of the language, use the special langu
592
- HFS.debounceAsync
593
- middleware: ctx.stop()
594
- the old way of returning true is now deprecated
595
+ - exports.customHtml
596
- 8.72 (v0.52.0)
597
- HFS.toast
598
- HFS.misc functions
src/cross.ts
+4
@@ -456,6 +456,10 @@ export function mapFilter<T=unknown, R=T>(arr: T[], map: (x:T, idx: number) => R
456
}, [] as R[])
457
}
458
459
+export function callable<T>(x: T | ((...args: unknown[]) => T), ...args: unknown[]) {
460
+ return _.isFunction(x) ? x(...args) : x
461
+}
462
+
463
export function shortenAgent(agent: string) {
464
return _.findKey(BROWSERS, re => re.test(agent))
465
|| /^[^/(]+ ?/.exec(agent)?.[0]
src/customHtml.ts
+2
-2
@@ -36,11 +36,11 @@ export function watchLoadCustomHtml(folder='') {
36
37
export function getSection(name: string) {
38
return (customHtmlState.sections.get(name) || '')
39
- + mapPlugins(pl => pl.getData().customHtml?.()[name]).join('\n')
39
+ + mapPlugins(pl => pl.getData().getCustomHtml()[name]).join('\n')
40
}
41
42
export function getAllSections() {
43
- const keys = mapPlugins(pl => Object.keys(pl.getData().customHtml?.()))
43
+ const keys = mapPlugins(pl => Object.keys(pl.getData().getCustomHtml()))
44
keys.push(Array.from(customHtmlState.sections.keys()))
45
const all = _.uniq(keys.flat())
46
return Object.fromEntries(all.map(x => [x, getSection(x)]))
src/plugins.ts
+6
-3
@@ -8,7 +8,7 @@ import { API_VERSION, APP_PATH, COMPATIBLE_API_VERSION, HTTP_NOT_FOUND, IS_WINDO
8
import * as Const from './const'
9
import Koa from 'koa'
10
import {
11
- adjustStaticPathForGlob, Callback, debounceAsync, Dict, getOrSet, objSameKeys, onlyTruthy,
11
+ adjustStaticPathForGlob, callable, Callback, debounceAsync, Dict, getOrSet, objSameKeys, onlyTruthy,
12
PendingPromise, pendingPromise, Promisable, same, tryJson, wait, waitFor, wantArray, watchDir
13
} from './misc'
14
import { defineConfig, getConfig } from './config'
@@ -258,7 +258,9 @@ const serverCode = defineConfig('server_code', '', async (script, { k }) => {
258
const res: any = {}
259
try {
260
new Function('exports', script)(res) // parse
261
- return new Plugin(SERVER_CODE_ID, '', await initPlugin(res), _.noop)
261
+ await initPlugin(res)
262
+ res.getCustomHtml = () => callable(res.customHtml) || {}
263
+ return new Plugin(SERVER_CODE_ID, '', res, _.noop)
264
}
265
catch (e: any) {
266
return console.error(k + ':', e.message || String(e))
@@ -451,7 +453,8 @@ function watchPlugin(id: string, path: string) {
453
})
454
const folder = dirname(module)
455
const { state, unwatch } = watchLoadCustomHtml(folder)
454
- pluginData.customHtml ??= () => Object.fromEntries(state)
456
+ pluginData.getCustomHtml = () =>
457
+ Object.assign(Object.fromEntries(state), callable(pluginData.customHtml) || {})
458
459
const plugin = new Plugin(id, folder, pluginData, async () => {
460
unwatch()