scaling-swarms / frontend /src /editor /embeds /embed-drafts.ts
tfrere's picture
tfrere HF Staff
Cursor
feat(embed-studio): drafts with Apply to article, compare, data drawer
f7733a1
Raw History Blame Contribute Delete
4.69 kB
import * as Y from "yjs";
import type { EmbedStore } from "./embed-store";
import type { EmbedMeta, EmbedWorkspace } from "./embed-tool-exec";
/**
* Work in progress of the Embed Studio, shared through `Y.Map("embed-drafts")`
* (src -> draft). The article keeps rendering the applied HTML from
* `Y.Map("embeds")` until someone presses "Apply to article", so co-authors
* never see half-finished iterations, and a new chart only gets a node in
* the document once it exists.
*/
export interface EmbedDraft {
html: string;
/** Applied HTML when the draft started ("" for a new chart). */
baseHtml: string;
/** True until the chart has been applied once (no node, no embeds entry). */
isNew: boolean;
title?: string;
source?: string;
author?: string;
updatedAt: number;
}
export function createEmbedDraftStore(ydoc: Y.Doc) {
const ymap = ydoc.getMap<EmbedDraft>("embed-drafts");
function get(src: string): EmbedDraft | undefined {
return ymap.get(src);
}
function has(src: string): boolean {
return ymap.has(src);
}
/** Create the draft if it does not exist yet. Returns the draft. */
function ensure(src: string, init: { baseHtml: string; isNew: boolean; author?: string }): EmbedDraft {
const existing = ymap.get(src);
if (existing) return existing;
const draft: EmbedDraft = {
html: init.baseHtml,
baseHtml: init.baseHtml,
isNew: init.isNew,
...(init.author ? { author: init.author } : {}),
updatedAt: Date.now(),
};
ymap.set(src, draft);
return draft;
}
function update(src: string, patch: Partial<EmbedDraft>) {
const current = ymap.get(src);
if (!current) return;
ymap.set(src, { ...current, ...patch, updatedAt: Date.now() });
}
function write(src: string, html: string, author?: string) {
const current = ymap.get(src);
if (!current) {
ymap.set(src, { html, baseHtml: "", isNew: true, ...(author ? { author } : {}), updatedAt: Date.now() });
return;
}
update(src, { html, ...(author ? { author } : {}) });
}
function setMeta(src: string, meta: EmbedMeta) {
const patch: Partial<EmbedDraft> = {};
if (meta.title) patch.title = meta.title;
if (meta.source) patch.source = meta.source;
update(src, patch);
}
function rename(oldSrc: string, newSrc: string) {
const current = ymap.get(oldSrc);
if (!current || oldSrc === newSrc) return;
ydoc.transact(() => {
ymap.set(newSrc, current);
ymap.delete(oldSrc);
});
}
function discard(src: string) {
ymap.delete(src);
}
function keys(): string[] {
return Array.from(ymap.keys());
}
function observe(callback: () => void) {
ymap.observe(callback);
return () => ymap.unobserve(callback);
}
return { get, has, ensure, update, write, setMeta, rename, discard, keys, observe };
}
export type EmbedDraftStore = ReturnType<typeof createEmbedDraftStore>;
/** The draft holds changes that are not in the article yet. */
export function isDraftDirty(draft: EmbedDraft | undefined): boolean {
return !!draft && draft.html.trim() !== "" && draft.html !== draft.baseHtml;
}
/**
* Studio workspace backed by drafts: reads the draft (falling back to the
* applied HTML), writes only to the draft. File names count as taken when
* either a draft or an applied embed uses them.
*/
export function draftWorkspace(
drafts: EmbedDraftStore,
embeds: EmbedStore,
author?: () => string | undefined,
): EmbedWorkspace {
return {
read: (src) => drafts.get(src)?.html ?? embeds.get(src),
write: (src, html) => {
if (!drafts.has(src)) drafts.ensure(src, { baseHtml: embeds.get(src), isNew: !embeds.has(src) });
drafts.write(src, html, author?.());
},
has: (src) => drafts.has(src) || embeds.has(src),
rename: (oldSrc, newSrc) => drafts.rename(oldSrc, newSrc),
};
}
export interface ApplyResult {
src: string;
html: string;
title?: string;
source?: string;
/** The chart had no node yet: the caller must insert one. */
needsNode: boolean;
}
/**
* Publish a draft into the article's embeds and drop it. Returns what the
* caller needs to update or insert the node, or null when there is nothing
* to apply.
*/
export function applyDraft(drafts: EmbedDraftStore, embeds: EmbedStore, src: string): ApplyResult | null {
const draft = drafts.get(src);
if (!draft || !draft.html.trim()) return null;
const needsNode = draft.isNew;
embeds.set(src, draft.html);
drafts.discard(src);
return {
src,
html: draft.html,
...(draft.title ? { title: draft.title } : {}),
...(draft.source ? { source: draft.source } : {}),
needsNode,
};
}