Skip to content

Commit b389301

Browse files
authored
Do not use nested iframes in VSCode custom editor (#197)
1 parent 9a4cd42 commit b389301

File tree

15 files changed

+225
-284
lines changed

15 files changed

+225
-284
lines changed

packages/dashboard/src/components/editor/Editor.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { DocumentTitleEdit } from "../DocumentTitleEdit";
1515
import { toastController } from "@uimix/foundation/src/components/toast/Toast";
1616
import { LoadingErrorOverlay } from "./LoadingErrorOverlay";
1717
import { assertNonNull } from "../../utils/assertNonNull";
18-
import { DefaultClipboardHandler } from "@uimix/editor/src/state/DefaultClipboardHandler";
1918

2019
class Connection extends TypedEmitter<{
2120
readyToShow(): void;
@@ -73,12 +72,6 @@ class Connection extends TypedEmitter<{
7372
thumbnail: url,
7473
});
7574
},
76-
getClipboard: async (type) => {
77-
return await new DefaultClipboardHandler().get(type);
78-
},
79-
setClipboard: async (type, text) => {
80-
await new DefaultClipboardHandler().set(type, text);
81-
},
8275
getCodeAssets: async () => {
8376
return undefined;
8477
},

packages/dashboard/src/components/editor/LocalEditor.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { ProjectData } from "@uimix/model/src/collaborative";
1111
import { LoadingErrorOverlay } from "./LoadingErrorOverlay";
1212
import { DocumentMetadata, getDesktopAPI } from "../../types/DesktopAPI";
1313
import { assertNonNull } from "../../utils/assertNonNull";
14-
import { DefaultClipboardHandler } from "@uimix/editor/src/state/DefaultClipboardHandler";
1514

1615
// TODO: test
1716

@@ -49,13 +48,6 @@ class Connection extends TypedEmitter<{
4948
// TODO: set thumbnail for file
5049
},
5150

52-
getClipboard: async (type) => {
53-
return await new DefaultClipboardHandler().get(type);
54-
},
55-
setClipboard: async (type, text) => {
56-
await new DefaultClipboardHandler().set(type, text);
57-
},
58-
5951
getCodeAssets: async () => {
6052
// TODO
6153
return undefined;

packages/dashboard/src/components/editor/VSCodeEditor.tsx

Lines changed: 0 additions & 151 deletions
This file was deleted.

packages/dashboard/src/pages/vscode-editor/index.tsx

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/dashboard/src/types/VSCodeEditorRPC.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/editor/src/state/Clipboard.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
11
import * as Data from "@uimix/model/src/data/v1";
22
import { projectState } from "./ProjectState";
3-
import { DefaultClipboardHandler } from "./DefaultClipboardHandler";
4-
import { ClipboardHandler } from "../types/ClipboardHandler";
53
import isSvg from "is-svg";
64

75
// const mimeType = "application/x-macaron-nodes";
86

7+
interface ClipboardHandler {
8+
get(type: "text" | "image"): Promise<string | undefined>;
9+
set(type: "text" | "image", textOrDataURL: string): Promise<void>;
10+
}
11+
12+
class DefaultClipboardHandler {
13+
async get(type: "text" | "image") {
14+
switch (type) {
15+
case "text":
16+
return await navigator.clipboard.readText();
17+
case "image": {
18+
const items = await navigator.clipboard.read();
19+
const item = items.find((item) => item.types.includes(`image/png`));
20+
if (!item) {
21+
return;
22+
}
23+
const blob = await item.getType(`image/png`);
24+
25+
return await new Promise<string>((resolve, reject) => {
26+
const reader = new FileReader();
27+
reader.onload = () => resolve(reader.result as string);
28+
reader.onerror = reject;
29+
reader.readAsDataURL(blob);
30+
});
31+
}
32+
}
33+
}
34+
35+
async set(type: "text" | "image", textOrDataURL: string) {
36+
switch (type) {
37+
case "text":
38+
await navigator.clipboard.writeText(textOrDataURL);
39+
case "image": {
40+
const blob = await fetch(textOrDataURL).then((r) => r.blob());
41+
await navigator.clipboard.write([
42+
new ClipboardItem({
43+
"image/png": blob,
44+
}),
45+
]);
46+
}
47+
}
48+
}
49+
}
50+
951
export class Clipboard {
1052
static handler: ClipboardHandler = new DefaultClipboardHandler();
1153

packages/editor/src/state/DefaultClipboardHandler.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)