plugins: event 'deleting'
Massimo Melina committed
May 29, 2024 at 16:57 UTC
136cf0fa6bda0e84561e5ab0d696bf85f6fb06b5
4 files changed
+14
-5
dev-plugins.md
+1
-1
@@ -438,7 +438,7 @@ If you want to override a text regardless of the language, use the special langu
438
439
## API version history
440
441
-- 8.81 (v0.53.0)
441
+- 8.82 (v0.53.0)
442
- api.openDb
443
- frontend event: menuZip
444
- config.type:username
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.81
10
+export const API_VERSION = 8.82
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
+10
-3
@@ -38,16 +38,23 @@ export class BetterEventEmitter {
38
emit(event: string, ...args: any[]) {
39
let cbs = this.listeners.get(event)
40
if (!cbs?.size) return
41
- const ret = []
41
+ const ret: any[] = []
42
for (const cb of cbs) {
43
const res = cb(...args)
44
if (res !== undefined)
45
ret.push(res)
46
}
47
- return ret
47
+ return Object.assign(ret, {
48
+ preventDefault: () => ret.some(r => r === false)
49
+ })
50
}
51
emitAsync(event: string, ...args: any[]) {
50
- return Promise.all(this.emit(event, ...args) || [])
52
+ const ret = Promise.all(this.emit(event, ...args) || [])
53
+ return Object.assign(ret, {
54
+ async preventDefault() {
55
+ return (await ret).some((x: any) => x === false)
56
+ }
57
+ })
58
}
59
}
60
src/frontEndApis.ts
+2
@@ -78,6 +78,8 @@ export const frontEndApis: ApiHandlers = {
78
if (!hasPermission(node, 'can_delete', ctx))
79
throw new ApiError(HTTP_UNAUTHORIZED)
80
try {
81
+ if (await events.emitAsync('deleting', { node, ctx }).preventDefault())
82
+ return null // stop
83
await rm(node.source, { recursive: true })
84
void setCommentFor(node.source, '')
85
return {}