Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

899 B
Raw Blame
import type { Editor } from "@tiptap/core";
import type { uploadTaskImage } from "@/lib/upload-task-image";

export function insertUploadedAsset(
  editor: Editor,
  asset: Awaited<ReturnType<typeof uploadTaskImage>>,
  errorMessage: string,
  range?: { from: number; to: number },
) {
  const chain = editor.chain().focus();
  if (range) chain.deleteRange(range);
  else if (!editor.state.selection.empty)
    chain.setTextSelection(editor.state.selection.to);
  const ran =
    asset.kind === "image"
      ? chain.setImage({ src: asset.url, alt: asset.alt }).run()
      : chain
          .insertContent({
            type: "attachmentCard",
            attrs: {
              url: asset.url,
              filename: asset.filename,
              mimeType: asset.mimeType,
              size: asset.size,
            },
          })
          .run();
  if (!ran) throw new Error(errorMessage);
}