HFS.getPluginConfig
Massimo Melina committed
Apr 8, 2023 at 14:37 UTC
af0b6e7473ff025b5ea111307e21aeeda9259b61
5 files changed
+26
-16
dev-plugins.md
+2
-1
@@ -78,7 +78,7 @@ Currently, these properties are supported:
78
- `defaultValue: any` value to be used when nothing is set.
79
- `helperText: string` extra text printed next to the field.
80
- `frontend: boolean` expose this setting on the frontend, so that javascript can access it as
81
- `HFS.plugins[PLUGIN_NAME][CONFIG_KEY]` but also css can access it as `var(--PLUGIN_NAME-CONFIG_KEY)`
81
+ `HFS.getPluginConfig()[CONFIG_KEY]` but also css can access it as `var(--PLUGIN_NAME-CONFIG_KEY)`
82
83
Based on `type`, other properties are supported:
84
- `string`
@@ -228,6 +228,7 @@ HFS will scan through them in inverted alphabetical order searching for a compat
228
- 8 (v0.43.0)
229
- entry.name & .uri
230
- tools.dialogLib
231
+ - HFS.getPluginConfig()
232
- 7 (v0.42.0)
233
- event.fileMenu
234
- HFS.SPECIAL_URI, PLUGINS_PUB_URI, FRONTEND_URI,
plugins/download-counter/plugin.js
+1
-1
@@ -1,6 +1,6 @@
1
exports.description = "Counts downloads for each file, and displays the total in the list or file menu"
2
exports.version = 4 // config.where
3
-exports.apiRequired = 6
3
+exports.apiRequired = 8
4
5
exports.config = {
6
where: { frontend: true, type: 'select', options: ['list', 'menu'] }
plugins/download-counter/public/main.js
+11
-7
@@ -1,8 +1,12 @@
1
-const inMenu = HFS.plugins['download-counter'].where === 'menu'
2
-HFS.onEvent('additionalEntryProps', ({ entry: { hits } }, { t }) =>
3
- hits && !inMenu && `<span class="download-counter" title="${t`download counter`}">${hits}</span>`)
1
+(() => { // this wrapper avoids name clashing of outer variables and functions
2
+ const config = HFS.getPluginConfig()
3
5
-HFS.onEvent('fileMenu', ({ entry, props }) => {
6
- if (inMenu && !entry.isFolder)
7
- props.push(["Downloads", entry.hits || 0])
8
-})
\ No newline at end of file
4
+ const inMenu = config.where === 'menu'
5
+ HFS.onEvent('additionalEntryProps', ({ entry: { hits } }, { t }) =>
6
+ hits && !inMenu && `<span class="download-counter" title="${t`download counter`}">${hits}</span>`)
7
+
8
+ HFS.onEvent('fileMenu', ({ entry, props }) => {
9
+ if (inMenu && !entry.isFolder)
10
+ props.push(["Downloads", entry.hits || 0])
11
+ })
12
+})()
src/const.ts
+1
-1
@@ -17,7 +17,7 @@ export const VERSION = pkg.version
17
export const DAY = 86_400_000
18
export const SESSION_DURATION = DAY
19
20
-export const API_VERSION = 8 // entry.uri
20
+export const API_VERSION = 8 // entry.uri + script.plugin
21
export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise is made equal to API_VERSION
22
23
export const SPECIAL_URI = '/~/'
src/serveGuiFiles.ts
+11
-6
@@ -75,10 +75,6 @@ async function treatIndex(ctx: Koa.Context, filesUri: string, body: string) {
75
const isFrontend = filesUri === FRONTEND_URI
76
77
const pub = ctx.state.revProxyPath + PLUGINS_PUB_URI
78
- const css = mapPlugins((plug,k) =>
79
- (isFrontend ? plug.frontend_css : null)?.map(f => pub + k + '/' + f)).flat().filter(Boolean)
80
- const js = mapPlugins((plug,k) =>
81
- (isFrontend ? plug.frontend_js : null)?.map(f => pub + k + '/' + f)).flat().filter(Boolean)
78
79
// expose plugins' configs that are declared with 'frontend' attribute
80
const plugins = Object.fromEntries(onlyTruthy(mapPlugins((pl,name) => {
@@ -112,6 +108,9 @@ async function treatIndex(ctx: Koa.Context, filesUri: string, body: string) {
108
}, null, 4)
109
.replace(/<(\/script)/g, '<"+"$1') /*avoid breaking our script container*/}
110
document.documentElement.setAttribute('ver', '${VERSION.split('-')[0] /*for style selectors*/}')
111
+ HFS.getPluginKey = () => document.currentScript?.getAttribute('plugin')
112
+ || console.error("this function must be called at the very top of your file")
113
+ HFS.getPluginConfig = () => HFS.plugins[HFS.getPluginKey()]
114
</script>
115
<style>
116
:root {
@@ -119,8 +118,14 @@ async function treatIndex(ctx: Koa.Context, filesUri: string, body: string) {
118
_.map(configs, (v,k) => `--${pluginName}-${k}: ${serializeCss(v)};`).join('\n')).join('')}
119
}
120
</style>
122
- ${css.map(uri => `<link rel='stylesheet' type='text/css' href='${uri}'/>`).join('\n')}
123
- ${js.map(uri => `<script defer src='${uri}'></script>`).join('\n')}
121
+ ${!isFrontend ? '' : mapPlugins((plug,k) =>
122
+ plug.frontend_css?.map(f =>
123
+ `<link rel='stylesheet' type='text/css' href='${pub + k + '/' + f}' plugin=${JSON.stringify(k)}/>`))
124
+ .flat().filter(Boolean).join('\n')}
125
+ ${!isFrontend ? '' : mapPlugins((plug,k) =>
126
+ plug.frontend_js?.map(f =>
127
+ `<script defer plugin=${JSON.stringify(k)} src='${pub + k + '/' + f}'></script>`))
128
+ .flat().filter(Boolean).join('\n')}
129
</head>`)
130
if (isFrontend)
131
ret = ret