{"version":3,"file":"uploadRegistry.cjs","sources":["../../../../../../../src/components/ui/editor/ihub-editor/upload/uploadRegistry.ts"],"sourcesContent":["import type { Editor } from \"@tiptap/core\";\nimport type { MediaKind } from \"../types\";\nimport type { EditorUploader } from \"./uploadFile\";\n\nexport interface UploadStatus {\n  progress: number;\n  name: string;\n  preview: string | null;\n  error?: string;\n}\n\ninterface InflightUpload extends UploadStatus {\n  listeners: Set<(status: UploadStatus) => void>;\n}\n\n/**\n * Uploads live here, keyed by the block's pendingId, not inside a node view.\n * Dragging the block, undo/redo or wrapping it recreates the view; the upload\n * keeps going and the result is written to whichever node carries the id.\n */\nconst inflight = new Map<string, InflightUpload>();\n\nexport function newUploadId(): string {\n  return `media-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;\n}\n\nexport function getUpload(id?: string | null): UploadStatus | undefined {\n  return id ? inflight.get(id) : undefined;\n}\n\nexport function subscribeUpload(id: string, listener: (status: UploadStatus) => void): () => void {\n  const entry = inflight.get(id);\n  if (!entry) return () => undefined;\n  entry.listeners.add(listener);\n  return () => entry.listeners.delete(listener);\n}\n\nfunction notify(entry: InflightUpload): void {\n  const snapshot: UploadStatus = { progress: entry.progress, name: entry.name, preview: entry.preview, error: entry.error };\n  entry.listeners.forEach((listener) => listener(snapshot));\n}\n\n/** Merge attrs into the media node carrying this pendingId, wherever it now is. */\nexport function patchMediaNode(editor: Editor, id: string, attrs: Record<string, unknown>): boolean {\n  if (editor.isDestroyed) return false;\n  let target: { pos: number; attrs: Record<string, unknown> } | null = null;\n  editor.state.doc.descendants((node, pos) => {\n    if (target) return false;\n    if (node.type.name === \"mediaBlock\" && node.attrs.pendingId === id) {\n      target = { pos, attrs: node.attrs };\n      return false;\n    }\n    return true;\n  });\n  if (!target) return false;\n  const { pos, attrs: current } = target as { pos: number; attrs: Record<string, unknown> };\n  // Not an undo step: undoing should remove the block, not un-upload it.\n  editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, { ...current, ...attrs }).setMeta(\"addToHistory\", false));\n  return true;\n}\n\nexport function beginUpload(editor: Editor, id: string, file: File, kind: MediaKind, uploader: EditorUploader): void {\n  const canPreview = kind === \"image\" && typeof URL.createObjectURL === \"function\";\n  const preview = canPreview ? URL.createObjectURL(file) : null;\n  const entry: InflightUpload = { progress: 0, name: file.name, preview, listeners: new Set() };\n  inflight.set(id, entry);\n\n  uploader(file, kind, (percent) => {\n    entry.progress = percent;\n    notify(entry);\n  })\n    .then((result) => {\n      patchMediaNode(editor, id, {\n        kind,\n        src: result.url,\n        name: result.name || file.name,\n        size: result.size ?? file.size,\n        mime: result.mime || file.type,\n        pendingId: null,\n      });\n    })\n    .catch((err: unknown) => {\n      entry.error = err instanceof Error ? err.message : \"Upload failed. Please try again.\";\n      notify(entry);\n      patchMediaNode(editor, id, { pendingId: null, uploadError: entry.error });\n    })\n    .finally(() => {\n      inflight.delete(id);\n      if (preview) URL.revokeObjectURL(preview);\n    });\n}\n"],"names":["inflight","Map","notify","entry","snapshot","progress","name","preview","error","listeners","forEach","listener","patchMediaNode","editor","id","attrs","isDestroyed","target","state","doc","descendants","node","pos","type","pendingId","_target","current","view","dispatch","tr","setNodeMarkup","undefined","_objectSpread","setMeta","file","kind","uploader","URL","createObjectURL","Set","set","percent","then","result","_result$size","src","url","size","mime","err","Error","message","uploadError","revokeObjectURL","get","concat","Date","now","Math","random","toString","slice","add"],"mappings":"8IAoBMA,EAAW,IAAIC,IAiBrB,SAASC,EAAOC,GACd,IAAMC,EAAyB,CAAEC,SAAUF,EAAME,SAAUC,KAAMH,EAAMG,KAAMC,QAASJ,EAAMI,QAASC,MAAOL,EAAMK,OAClHL,EAAMM,UAAUC,QAAQ,SAACC,GAAQ,OAAKA,EAASP,IACjD,UAGgBQ,EAAeC,EAAgBC,EAAYC,GACzD,GAAIF,EAAOG,YAAa,OAAO,EAC/B,IAAIC,EAAiE,KASrE,GARAJ,EAAOK,MAAMC,IAAIC,YAAY,SAACC,EAAMC,GAClC,OAAIL,IACmB,eA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