better code: future-proof arguments for frontend events, breaking for plugins using undocumented parameters (file-icons)
Massimo Melina committed
Aug 26, 2024 at 12:18 UTC
9a9c937486332d34e7927532d7a99fba59ca931d
3 files changed
+10
-7
dev-plugins.md
+1
-1
@@ -626,7 +626,7 @@ If you want to override a text regardless of the language, use the special langu
626
627
## API version history
628
629
-- 8.9 (v0.54.0)
629
+- 9 (v0.54.0)
630
- frontend event: showPlay
631
- api.addBlock
632
- api.misc
frontend/src/misc.ts
+8
-5
@@ -4,7 +4,7 @@ import React, { createElement as h } from 'react'
4
import { iconBtn, Spinner } from './components'
5
import { newDialog, toast } from './dialog'
6
import { Icon } from './icons'
7
-import { Dict, domOn, getHFS, Html, HTTP_MESSAGES, useBatch } from '@hfs/shared'
7
+import { Callback, Dict, domOn, getHFS, Html, HTTP_MESSAGES, useBatch } from '@hfs/shared'
8
import * as cross from '../../src/cross'
9
import * as shared from '@hfs/shared'
10
import { apiCall, getNotifications, useApi } from '@hfs/shared/api'
@@ -54,8 +54,11 @@ export function working() {
54
55
export function hfsEvent(name: string, params?:Dict) {
56
const output: any[] = []
57
- document.dispatchEvent(new CustomEvent('hfs.'+name, { detail: { params, output } }))
58
- return output
57
+ const ev = new CustomEvent('hfs.'+name, { cancelable: true, detail: { params, output } })
58
+ document.dispatchEvent(ev)
59
+ return Object.assign(output, {
60
+ isDefaultPrevent: () => ev.defaultPrevented,
61
+ })
62
}
63
64
const tools = {
@@ -74,14 +77,14 @@ Object.assign(getHFS(), {
77
debounceAsync,
78
useSnapState,
79
html: (html: string) => h(Html, {}, html),
77
- onEvent(name: string, cb: (params:any, tools: any, output:any) => any) {
80
+ onEvent(name: string, cb: (params:any, extra: { output: any[], preventDefault: Callback }, output: any[]) => any) {
81
const key = 'hfs.' + name
82
document.addEventListener(key, wrapper)
83
return () => document.removeEventListener(key, wrapper)
84
85
function wrapper(ev: Event) {
86
const { params, output } = (ev as CustomEvent).detail
84
- const res = cb(params, tools, output)
87
+ const res = cb(params, { output, preventDefault: () => ev.preventDefault() }, output) // legacy pre-0.54, third parameter used by file-icons plugin
88
if (res !== undefined && Array.isArray(output))
89
output.push(res)
90
}
src/const.ts
+1
-1
@@ -7,7 +7,7 @@ import { mkdirSync } from 'fs'
7
import { basename, dirname, join } from 'path'
8
export * from './cross-const'
9
10
-export const API_VERSION = 8.9
10
+export const API_VERSION = 9
11
export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise it is made equal to API_VERSION
12
export const HFS_REPO = 'rejetto/hfs'
13