mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-05 06:41:50 +09:00
- **Restore points for agent work**: Create named restore points before major edits, review changes with a dry run, and safely revert project content if needed. - **Shared Slot authoring**: Agents can turn existing sections into shared Slots and insert linked copies across pages, including valid nested Slots. - **DTCG and Figma token imports**: Import design tokens and CSS variables through CLI, MCP, or direct paste, with aliases, modes, composite values, and mappings supported. - **Consistent token conflict handling**: Every paste workflow now offers the same preserve, rename, or merge choices instead of silently overwriting existing design tokens. - **Draft-page previews**: Agents can generate and inspect previews of draft pages without publishing them or exposing drafts in production builds. - **Safer MCP mutations**: Destructive operations require explicit confirmation, invalid raw patches and invented record IDs are rejected, and restore operations commit atomically. - **More dependable agent contracts**: New tools have complete runtime schemas, clearer validation errors, and consistent behavior across CLI, MCP, Builder, and server APIs.
101 lines
3.0 KiB
TypeScript
101 lines
3.0 KiB
TypeScript
import {
|
|
detectDesignTokenSource,
|
|
planDesignTokenImport,
|
|
} from "@webstudio-is/project-build/runtime";
|
|
import {
|
|
executeRuntimeMutation,
|
|
getWebstudioData,
|
|
} from "../instance-utils/data";
|
|
import { builderApi } from "../builder-api";
|
|
import { resolveTokenConflicts } from "../resolve-token-conflicts";
|
|
import {
|
|
pasteHandled,
|
|
pasteIgnored,
|
|
type PasteResult,
|
|
type Plugin,
|
|
} from "./copy-paste";
|
|
|
|
const parseJson = (value: string) => {
|
|
try {
|
|
return JSON.parse(value) as unknown;
|
|
} catch {
|
|
return;
|
|
}
|
|
};
|
|
|
|
const collisionByResolution = {
|
|
ours: "skip",
|
|
theirs: "rename",
|
|
merge: "overwrite",
|
|
} as const;
|
|
|
|
const designTokenMapping = {
|
|
color: { target: "design-token", property: "color" },
|
|
fontFamily: { target: "design-token", property: "fontFamily" },
|
|
fontWeight: { target: "design-token", property: "fontWeight" },
|
|
strokeStyle: { target: "design-token", property: "borderStyle" },
|
|
border: { target: "design-token", property: "border" },
|
|
transition: { target: "design-token", property: "transition" },
|
|
shadow: { target: "design-token", property: "boxShadow" },
|
|
gradient: { target: "design-token", property: "backgroundImage" },
|
|
typography: { target: "design-token", property: "font" },
|
|
} as const;
|
|
|
|
const handlePasteDesignTokens = async (value: string): Promise<PasteResult> => {
|
|
const source = detectDesignTokenSource(parseJson(value));
|
|
if (source === undefined) {
|
|
return pasteIgnored;
|
|
}
|
|
try {
|
|
const target = await builderApi.showDesignTokenImportDialog();
|
|
if (target === "cancel") {
|
|
return pasteHandled;
|
|
}
|
|
const importInput = {
|
|
source,
|
|
mapping: target === "design-token" ? designTokenMapping : undefined,
|
|
defaultTarget: { target: "css-variable" as const },
|
|
collision: "skip" as const,
|
|
};
|
|
const plan = planDesignTokenImport(getWebstudioData(), importInput);
|
|
const conflicts = plan
|
|
.filter((entry) => entry.conflict)
|
|
.map((entry) => ({ tokenName: entry.outputName }));
|
|
const resolution = await resolveTokenConflicts(conflicts);
|
|
if (resolution === "cancel") {
|
|
return pasteHandled;
|
|
}
|
|
const collision = collisionByResolution[resolution];
|
|
const mutation = executeRuntimeMutation({
|
|
id: "designTokens.import",
|
|
input: { ...importInput, collision },
|
|
});
|
|
if (mutation === undefined) {
|
|
return {
|
|
success: false,
|
|
error: "Design tokens cannot be pasted in the current context.",
|
|
};
|
|
}
|
|
const { create, overwrite, skip } = mutation.result.counts;
|
|
const imported = create + overwrite;
|
|
builderApi.toast.success(
|
|
`Imported ${imported} ${imported === 1 ? "token" : "tokens"}${skip === 0 ? "" : `; skipped ${skip} existing`}.`
|
|
);
|
|
return pasteHandled;
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error:
|
|
error instanceof Error
|
|
? error.message
|
|
: "Could not import design tokens.",
|
|
};
|
|
}
|
|
};
|
|
|
|
export const designTokens: Plugin = {
|
|
name: "design-tokens",
|
|
mimeType: "text/plain",
|
|
onPaste: handlePasteDesignTokens,
|
|
};
|