Skip to content

Commit 3d064f1

Browse files
authored
fix: paste markdown (#5235)
Async in html paste broke markdown because await was missing in onPaste.
1 parent 8b10634 commit 3d064f1

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

apps/builder/app/shared/copy-paste/init-copy-paste.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
} from "../nano-states";
66
import { instanceText, instanceJson } from "./plugin-instance";
77
import { html } from "./plugin-html";
8-
import * as markdown from "./plugin-markdown";
9-
import * as webflow from "./plugin-webflow/plugin-webflow";
8+
import { markdown } from "./plugin-markdown";
9+
import { webflow } from "./plugin-webflow/plugin-webflow";
1010
import { builderApi } from "../builder-api";
1111

1212
const isTextEditing = (event: ClipboardEvent) => {
@@ -64,6 +64,7 @@ const validateClipboardEvent = (event: ClipboardEvent) => {
6464
};
6565

6666
export type Plugin = {
67+
name: string;
6768
mimeType: string;
6869
onCopy?: () => undefined | string;
6970
onCut?: () => undefined | string;
@@ -109,7 +110,7 @@ const initPlugins = ({
109110
}
110111
};
111112

112-
const handlePaste = (event: ClipboardEvent) => {
113+
const handlePaste = async (event: ClipboardEvent) => {
113114
if (validateClipboardEvent(event) === false) {
114115
return;
115116
}
@@ -118,7 +119,7 @@ const initPlugins = ({
118119
// this shouldn't matter, but just in case
119120
event.preventDefault();
120121
const data = event.clipboardData?.getData(mimeType).trim();
121-
if (data && onPaste?.(data)) {
122+
if (data && (await onPaste?.(data))) {
122123
break;
123124
}
124125
}

apps/builder/app/shared/copy-paste/plugin-html.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { denormalizeSrcProps } from "./asset-upload";
44
import type { Plugin } from "./init-copy-paste";
55

66
export const html: Plugin = {
7+
name: "html",
78
mimeType: "text/plain",
89
onPaste: async (html: string) => {
910
let fragment = generateFragmentFromHtml(html);
1011
fragment = await denormalizeSrcProps(fragment);
11-
return insertWebstudioFragmentAt(fragment);
12+
const result = insertWebstudioFragmentAt(fragment);
13+
return result;
1214
},
1315
};

apps/builder/app/shared/copy-paste/plugin-instance.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,15 @@ const onCut = () => {
225225
};
226226

227227
export const instanceText: Plugin = {
228+
name: "instance-text",
228229
mimeType: "text/plain",
229230
onCopy,
230231
onCut,
231232
onPaste,
232233
};
233234

234235
export const instanceJson: Plugin = {
236+
name: "instance-json",
235237
mimeType: "application/json",
236238
onPaste,
237239
};

apps/builder/app/shared/copy-paste/plugin-markdown.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { micromark } from "micromark";
33
import { insertWebstudioFragmentAt } from "../instance-utils";
44
import { denormalizeSrcProps } from "./asset-upload";
55
import { generateFragmentFromHtml } from "../html";
6-
7-
export const mimeType = "text/plain";
6+
import type { Plugin } from "./init-copy-paste";
87

98
const parse = (clipboardData: string) => {
109
const html = micromark(clipboardData, "utf-8", {
@@ -23,13 +22,17 @@ const parse = (clipboardData: string) => {
2322
return fragment;
2423
};
2524

26-
export const onPaste = async (clipboardData: string) => {
27-
let fragment = parse(clipboardData);
28-
if (fragment === undefined) {
29-
return false;
30-
}
31-
fragment = await denormalizeSrcProps(fragment);
32-
return insertWebstudioFragmentAt(fragment);
25+
export const markdown: Plugin = {
26+
name: "markdown",
27+
mimeType: "text/plain",
28+
onPaste: async (clipboardData: string) => {
29+
let fragment = parse(clipboardData);
30+
if (fragment === undefined) {
31+
return false;
32+
}
33+
fragment = await denormalizeSrcProps(fragment);
34+
return insertWebstudioFragmentAt(fragment);
35+
},
3336
};
3437

3538
export const __testing__ = {

apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ import { builderApi } from "~/shared/builder-api";
1919
import { denormalizeSrcProps } from "../asset-upload";
2020
import { nanoHash } from "~/shared/nano-hash";
2121
import { findAvailableVariables } from "~/shared/data-variables";
22+
import type { Plugin } from "../init-copy-paste";
2223

2324
const { toast } = builderApi;
2425

25-
export const mimeType = "application/json";
26-
2726
const toWebstudioFragment = async (wfData: WfData) => {
2827
const fragment: WebstudioFragment = {
2928
children: [],
@@ -165,7 +164,7 @@ const parse = (clipboardData: string) => {
165164
console.error(result.error.message);
166165
};
167166

168-
export const onPaste = async (clipboardData: string) => {
167+
const onPaste = async (clipboardData: string) => {
169168
const project = $project.get();
170169
const wfData = parse(clipboardData);
171170
if (wfData === undefined || project === undefined) {
@@ -211,6 +210,12 @@ export const onPaste = async (clipboardData: string) => {
211210
return true;
212211
};
213212

213+
export const webflow: Plugin = {
214+
name: "webflow",
215+
mimeType: "application/json",
216+
onPaste,
217+
};
218+
214219
export const __testing__ = {
215220
toWebstudioFragment,
216221
};

0 commit comments

Comments
 (0)