mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-05 11:38:36 +09:00
## Description - webflow css preset ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 5de6) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import { z } from "zod";
|
|
import {
|
|
WsEmbedTemplate,
|
|
generateDataFromEmbedTemplate,
|
|
} from "@webstudio-is/react-sdk";
|
|
import {
|
|
$selectedInstanceSelector,
|
|
$instances,
|
|
$selectedPage,
|
|
$registeredComponentMetas,
|
|
$breakpoints,
|
|
} from "../nano-states";
|
|
import {
|
|
computeInstancesConstraints,
|
|
findClosestDroppableTarget,
|
|
insertTemplateData,
|
|
} from "../instance-utils";
|
|
import { isBaseBreakpoint } from "../breakpoints";
|
|
|
|
const version = "@webstudio/template";
|
|
|
|
const ClipboardData = z.object({ [version]: WsEmbedTemplate });
|
|
|
|
const parse = (clipboardData: string): WsEmbedTemplate | undefined => {
|
|
try {
|
|
const data = ClipboardData.parse(JSON.parse(clipboardData));
|
|
return data[version];
|
|
} catch {
|
|
return;
|
|
}
|
|
};
|
|
|
|
export const mimeType = "text/plain";
|
|
|
|
export const onPaste = (clipboardData: string) => {
|
|
const template = parse(clipboardData);
|
|
|
|
const selectedPage = $selectedPage.get();
|
|
|
|
if (template === undefined || selectedPage === undefined) {
|
|
return false;
|
|
}
|
|
|
|
// paste to the root if nothing is selected
|
|
const instanceSelector = $selectedInstanceSelector.get() ?? [
|
|
selectedPage.rootInstanceId,
|
|
];
|
|
const breakpoints = $breakpoints.get();
|
|
const breakpointValues = Array.from(breakpoints.values());
|
|
const baseBreakpoint = breakpointValues.find(isBaseBreakpoint);
|
|
if (baseBreakpoint === undefined) {
|
|
return false;
|
|
}
|
|
const metas = $registeredComponentMetas.get();
|
|
const templateData = generateDataFromEmbedTemplate(
|
|
template,
|
|
metas,
|
|
baseBreakpoint.id
|
|
);
|
|
const newInstances = new Map(
|
|
templateData.instances.map((instance) => [instance.id, instance])
|
|
);
|
|
const rootInstanceIds = templateData.children
|
|
.filter((child) => child.type === "id")
|
|
.map((child) => child.value);
|
|
const dropTarget = findClosestDroppableTarget(
|
|
metas,
|
|
$instances.get(),
|
|
instanceSelector,
|
|
computeInstancesConstraints(metas, newInstances, rootInstanceIds)
|
|
);
|
|
if (dropTarget === undefined) {
|
|
return false;
|
|
}
|
|
insertTemplateData(templateData, dropTarget);
|
|
return true;
|
|
};
|