refactor(tiptap): UploadCoordinator emit callback replaces window global

This commit is contained in:
xfy 2026-06-23 10:52:20 +08:00
parent 9c2b0b1a66
commit b09216ef29

View File

@ -7,13 +7,21 @@ interface UploadEntry {
fileName: string
}
/** coordinator 推给 Rust 的事件(通过 window.__tiptap_uploads.events。 */
/** counts 快照:随事件一起传给宿主,替代 window 全局。 */
export interface UploadCounts {
uploading: number
error: number
}
/** coordinator 推给宿主的事件(通过 emit 回调,替代 window 全局轮询)。 */
export interface UploadEvent {
kind: 'error' | 'success' | 'removed'
uploadId: string
fileName: string
errorMsg?: string
ts: number
/** 当前编辑器内 uploading/error 占位符计数emit 时由 JS 遍历文档算出)。 */
counts: UploadCounts
}
/** 生成 uploadId兼容非安全上下文无 crypto.randomUUID 时)。 */
@ -28,7 +36,7 @@ function genUploadId(): string {
* - uploadId blob URL
* - src error
* - upload-id /
* - window.__tiptap_uploads Rust
* - emit 宿 window
*
* pending Map File
*/
@ -38,6 +46,7 @@ export class UploadCoordinator {
constructor(
private editor: Editor,
private onImageUpload: (file: File) => Promise<string>,
private emit: (event: UploadEvent) => void,
) {}
/**
@ -157,17 +166,10 @@ export class UploadCoordinator {
}
/**
* window.__tiptap_uploads.events counts
* Rust 500ms events counts
* emit counts 宿
* window.__tiptap_uploads Rust
*/
private notifyRust(event: Omit<UploadEvent, 'ts'>): void {
const w = window as unknown as { __tiptap_uploads?: { events: UploadEvent[]; counts: { uploading: number; error: number } } }
if (!w.__tiptap_uploads) {
w.__tiptap_uploads = { events: [], counts: { uploading: 0, error: 0 } }
}
w.__tiptap_uploads.events.push({ ...event, ts: Date.now() })
// 重算 counts遍历文档统计当前 uploading/error 占位符
private notifyRust(event: Omit<UploadEvent, 'ts' | 'counts'>): void {
let uploading = 0
let error = 0
this.editor.state.doc.descendants((node) => {
@ -176,7 +178,7 @@ export class UploadCoordinator {
else if (state === 'error') error++
return true
})
w.__tiptap_uploads.counts = { uploading, error }
this.emit({ ...event, ts: Date.now(), counts: { uploading, error } })
}
/** pending Map 查询(供内部/测试)。 */