plugin: HFS.getPluginPublic

Massimo Melina committed May 20, 2023 at 16:20 UTC 3f0848a5f1641855c91aab9468317dc26ffd6cab
2 files changed +11 -3
dev-plugins.md
+5 -1
@@ -157,6 +157,10 @@ The HFS objects contains many properties:
157 - `Icon` component for icons. Properties:
158 - `name: string` refer to file `icons.ts` for names, but you can also enter an emoji instead.
159
160 +The following properties are accessible only immediately at top-level; don't call it later in a callback.
161 +- `getPluginConfig()` returns object of all config keys that are declared frontend-accessible by this plugin.
162 +- `getPluginPublic()` returns plugin's public folder, with final slash. Useful to point to public files.
163 +
164 ### Front-end API events
165
166 API at this level is done with frontend-events, that you can handle by calling
@@ -256,7 +260,7 @@ HFS will scan through them in inverted alphabetical order searching for a compat
260 - full URL support for frontend_js and frontend_css
261 - custom.html
262 - entry.cantOpen
259 - - HFS.apiCall, reloadList, logout, h, React, state, t, _, dialogLib, Icon
263 + - HFS.apiCall, reloadList, logout, h, React, state, t, _, dialogLib, Icon, getPluginPublic
264 - second parameter of onEvent is now deprecated
265 - renamed: additionalEntryProps > additionalEntryDetails & entry-props > entry-details
266 - entryIcon event
src/serveGuiFiles.ts
+6 -2
@@ -108,9 +108,13 @@ 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")
111 + function getScriptAttr(k) {
112 + return document.currentScript?.getAttribute(k)
113 + || console.error("this function must be called at the very top of your file")
114 + }
115 + HFS.getPluginKey = () => getScriptAttr('plugin')
116 HFS.getPluginConfig = () => HFS.plugins[HFS.getPluginKey()]
117 + HFS.getPluginPublic = () => getScriptAttr('src')?.match(/^.*\\//)[0]
118 </script>
119 <style>
120 :root {