Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

feat: Paste copied variables with keyboard (#6549)

Oleg Isonenfad586889acc
diff --git a/apps/builder/app/builder/features/settings-panel/variable-menu.tsx b/apps/builder/app/builder/features/settings-panel/variable-menu.tsx
index 292266a93a..e63a25e3c9 100644
--- a/apps/builder/app/builder/features/settings-panel/variable-menu.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variable-menu.tsx
@@ -29,12 +29,15 @@ import {
} from "~/shared/sync/data-stores";
import { serverSyncStore } from "~/shared/sync/sync-stores";
import { readClipboardText } from "~/shared/clipboard";
+import {
+ serializeVariable,
+ deserializeVariable,
+} from "~/shared/copy-paste/variable-clipboard";
import {
DeleteDataVariableDialog,
deleteDataVariable,
validateDataVariableName,
} from "~/builder/shared/data-variable-utils";
-import { serializeVariable, deserializeVariable } from "./variable-clipboard";
type VariableToDelete = { id: string; name: string; usages: number };
diff --git a/apps/builder/app/shared/copy-paste/copy-paste.ts b/apps/builder/app/shared/copy-paste/copy-paste.ts
index 1cf8975e16..d4e52b18fb 100644
--- a/apps/builder/app/shared/copy-paste/copy-paste.ts
+++ b/apps/builder/app/shared/copy-paste/copy-paste.ts
@@ -18,6 +18,15 @@ import { webflow } from "./plugin-webflow/plugin-webflow";
import { designTokens } from "./plugin-design-tokens";
import { builderApi } from "../builder-api";
import { readClipboardText } from "../clipboard";
+import { $selectedInstance } from "../nano-states";
+import { $dataSources, $instances, $resources } from "../sync/data-stores";
+import { serverSyncStore } from "../sync/sync-stores";
+import { findAvailableVariables } from "@webstudio-is/project-build/runtime";
+import { validateDataVariableName } from "~/builder/shared/data-variable-utils";
+import {
+ deserializeVariable,
+ isVariableClipboardData,
+} from "./variable-clipboard";
const isTextEditing = (event: ClipboardEvent) => {
// Text is edited on the Canvas using the default Canvas text editor settings.
@@ -114,6 +123,70 @@ const reportPasteResult = (result: PasteResult) => {
const textMimeType = "text/plain";
+const variableClipboard: Plugin = {
+ name: "variable",
+ mimeType: textMimeType,
+ onPaste: (text) => {
+ if (isVariableClipboardData(text) === false) {
+ return pasteIgnored;
+ }
+ const instance = $selectedInstance.get();
+ if (!instance) {
+ return pasteHandled;
+ }
+ try {
+ const copied = deserializeVariable(
+ text,
+ instance.id,
+ new Map(
+ findAvailableVariables({
+ startingInstanceId: instance.id,
+ instances: $instances.get(),
+ dataSources: $dataSources.get(),
+ }).map((variable) => [variable.id, variable])
+ )
+ );
+ const originalName = copied.variable.name;
+ let suffix = 2;
+ let error = validateDataVariableName(
+ copied.variable.name,
+ undefined,
+ instance.id
+ );
+ while (error?.type === "duplicate") {
+ copied.variable.name = `${originalName} ${suffix++}`;
+ error = validateDataVariableName(
+ copied.variable.name,
+ undefined,
+ instance.id
+ );
+ }
+ if (error) {
+ return { success: false, error: error.message };
+ }
+ if (copied.resource) {
+ copied.resource.name = copied.variable.name;
+ }
+ serverSyncStore.createTransaction(
+ [$dataSources, $resources],
+ (dataSources, resources) => {
+ dataSources.set(copied.variable.id, copied.variable);
+ if (copied.resource) {
+ resources.set(copied.resource.id, copied.resource);
+ }
+ }
+ );
+ return pasteHandled;
+ } catch (error) {
+ return {
+ success: false,
+ error:
+ error instanceof Error ? error.message : "Unable to paste variable",
+ };
+ }
+ },
+};
+
const readClipboardData = (
mimeTypes: Array<string>,
clipboardData: null | DataTransfer
@@ -255,6 +328,7 @@ const initPlugins = ({
export const initCopyPaste = ({ signal }: { signal: AbortSignal }) => {
initPlugins({
plugins: [
+ variableClipboard,
pageText,
instanceJson,
instanceText,
diff --git a/apps/builder/app/builder/features/settings-panel/variable-clipboard.test.ts b/apps/builder/app/shared/copy-paste/variable-clipboard.test.ts
similarity index 100%
rename from apps/builder/app/builder/features/settings-panel/variable-clipboard.test.ts
rename to apps/builder/app/shared/copy-paste/variable-clipboard.test.ts
diff --git a/apps/builder/app/builder/features/settings-panel/variable-clipboard.ts b/apps/builder/app/shared/copy-paste/variable-clipboard.ts
similarity index 97%
rename from apps/builder/app/builder/features/settings-panel/variable-clipboard.ts
rename to apps/builder/app/shared/copy-paste/variable-clipboard.ts
index 03aff15a9c..23f0b53136 100644
--- a/apps/builder/app/builder/features/settings-panel/variable-clipboard.ts
+++ b/apps/builder/app/shared/copy-paste/variable-clipboard.ts
@@ -22,6 +22,14 @@ const payload = z.object({
references: z.array(z.object({ id: z.string(), name: z.string() })),
});
+export const isVariableClipboardData = (text: string) => {
+ try {
+ return Object.hasOwn(JSON.parse(text), namespace);
+ } catch {
+ return false;
+ }
+};
+
export const serializeVariable = (
variable: DataSource,
resources: ReadonlyMap<string, Resource>,