HFS.onEvent/callback/setOrder #766

Massimo Melina committed Oct 8, 2024 at 17:47 UTC 0520e499390eb00c1e45865c62546915c7c230b6
4 files changed +23 -11
dev-plugins.md
+2 -1
@@ -658,7 +658,7 @@ If you want to override a text regardless of the language, use the special langu
658
659 ## API version history
660
661 -- 9.3 (v0.54.0)
661 +- 9.4 (v0.54.0)
662 - frontend event: showPlay
663 - api.addBlock
664 - api.misc
@@ -670,6 +670,7 @@ If you want to override a text regardless of the language, use the special langu
670 - HFS.DirEntry
671 - frontend event: appendMenuBar
672 - config.helperText: basic md formatting
673 + - HFS.onEvent.setOrder
674 - 8.891 (v0.53.0)
675 - api.openDb
676 - frontend event: menuZip
frontend/src/misc.ts
+16 -6
@@ -54,9 +54,11 @@ export function working() {
54
55 export function hfsEvent(name: string, params?:Dict) {
56 const output: any[] = []
57 - const ev = new CustomEvent('hfs.'+name, { cancelable: true, detail: { params, output } })
57 + const order: number[] = []
58 + const ev = new CustomEvent('hfs.'+name, { cancelable: true, detail: { params, output, order } })
59 document.dispatchEvent(ev)
59 - return Object.assign(output, {
60 + const sortedOutput = order.length && _.sortBy(output.map((x, i) => [order[i] || 0, x]), '0').map(x => x[1])
61 + return Object.assign(sortedOutput || output, {
62 isDefaultPrevent: () => ev.defaultPrevented,
63 })
64 }
@@ -74,16 +76,24 @@ Object.assign(getHFS(), {
76 return apiCall(cross.PLUGIN_CUSTOM_REST_PREFIX + name, ...rest)
77 },
78 html: (html: string) => h(Html, {}, html),
77 - onEvent(name: string, cb: (params:any, extra: { output: any[], preventDefault: Callback }, output: any[]) => any) {
79 + onEvent(name: string, cb: (params:any, extra: { output: any[], setOrder: Callback<number>, preventDefault: Callback }, output: any[]) => any) {
80 const key = 'hfs.' + name
81 document.addEventListener(key, wrapper)
82 return () => document.removeEventListener(key, wrapper)
83
84 function wrapper(ev: Event) {
83 - const { params, output } = (ev as CustomEvent).detail
84 - const res = cb(params, { output, preventDefault: () => ev.preventDefault() }, output) // legacy pre-0.54, third parameter used by file-icons plugin
85 - if (res !== undefined && Array.isArray(output))
85 + const { params, output, order } = (ev as CustomEvent).detail
86 + let thisOrder
87 + const res = cb(params, {
88 + output,
89 + setOrder(x) { thisOrder = x },
90 + preventDefault: () => ev.preventDefault()
91 + }, output) // legacy pre-0.54, third parameter used by file-icons plugin
92 + if (res !== undefined && Array.isArray(output)) {
93 output.push(res)
94 + if (thisOrder)
95 + order[output.length - 1] = thisOrder
96 + }
97 }
98 }
99 })
src/const.ts
+1 -1
@@ -7,7 +7,7 @@ import _ from 'lodash'
7 import { basename, dirname, join } from 'path'
8 export * from './cross-const'
9
10 -export const API_VERSION = 9.3
10 +export const API_VERSION = 9.4
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
src/events.ts
+4 -3
@@ -50,9 +50,10 @@ export class BetterEventEmitter {
50 emit(event: string, ...args: any[]) {
51 let cbs = this.listeners.get(event)
52 if (!cbs?.size) return
53 - const ret: any[] = []
53 + const output: any[] = []
54 let prevented = false
55 const extra = {
56 + output,
57 preventDefault() { prevented = true }
58 }
59 for (const cb of cbs) {
@@ -60,9 +61,9 @@ export class BetterEventEmitter {
61 if (res === this.preventDefault)
62 extra.preventDefault()
63 else if (res !== undefined)
63 - ret.push(res)
64 + output.push(res)
65 }
65 - return Object.assign(ret, {
66 + return Object.assign(output, {
67 isDefaultPrevented: () => prevented,
68 })
69 }