Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

925 B
Raw Blame
import type { Command } from "@tiptap/core";
import { Fragment } from "@tiptap/pm/model";
import {
  groupConsecutiveImagesIntoGalleries,
  type TiptapNode,
} from "@edgeever/shared";
import { mergeUploadedImagesIntoAdjacentGalleries } from "@edgeever/shared/image-gallery-editing";
import type { ResourceInsertionTarget } from "./resource-insertion-target";

/** Insert a batch and fold it into the adjacent images or gallery. */
export const insertUploadedResources = (
  target: ResourceInsertionTarget,
  content: TiptapNode[],
  updateSelection: boolean,
): Command => ({ tr, commands, dispatch }) => {
  const nodes = groupConsecutiveImagesIntoGalleries(content).map((node) => tr.doc.type.schema.nodeFromJSON(node));
  if (!commands.insertContentAt(target, Fragment.from(nodes), { updateSelection })) return false;
  if (!dispatch) return true;

  mergeUploadedImagesIntoAdjacentGalleries(tr, nodes);
  return true;
};