Skip to content

Commit

Permalink
Simplify clipboard handling
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchas116 committed Apr 30, 2023
1 parent 1820548 commit 0499110
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 275 deletions.
7 changes: 0 additions & 7 deletions packages/dashboard/src/components/editor/Editor.tsx
Expand Up @@ -15,7 +15,6 @@ import { DocumentTitleEdit } from "../DocumentTitleEdit";
import { toastController } from "@uimix/foundation/src/components/toast/Toast";
import { LoadingErrorOverlay } from "./LoadingErrorOverlay";
import { assertNonNull } from "../../utils/assertNonNull";
import { DefaultClipboardHandler } from "@uimix/editor/src/state/DefaultClipboardHandler";

class Connection extends TypedEmitter<{
readyToShow(): void;
Expand Down Expand Up @@ -73,12 +72,6 @@ class Connection extends TypedEmitter<{
thumbnail: url,
});
},
getClipboard: async (type) => {
return await new DefaultClipboardHandler().get(type);
},
setClipboard: async (type, text) => {
await new DefaultClipboardHandler().set(type, text);
},
getCodeAssets: async () => {
return undefined;
},
Expand Down
8 changes: 0 additions & 8 deletions packages/dashboard/src/components/editor/LocalEditor.tsx
Expand Up @@ -11,7 +11,6 @@ import { ProjectData } from "@uimix/model/src/collaborative";
import { LoadingErrorOverlay } from "./LoadingErrorOverlay";
import { DocumentMetadata, getDesktopAPI } from "../../types/DesktopAPI";
import { assertNonNull } from "../../utils/assertNonNull";
import { DefaultClipboardHandler } from "@uimix/editor/src/state/DefaultClipboardHandler";

// TODO: test

Expand Down Expand Up @@ -49,13 +48,6 @@ class Connection extends TypedEmitter<{
// TODO: set thumbnail for file
},

getClipboard: async (type) => {
return await new DefaultClipboardHandler().get(type);
},
setClipboard: async (type, text) => {
await new DefaultClipboardHandler().set(type, text);
},

getCodeAssets: async () => {
// TODO
return undefined;
Expand Down
151 changes: 0 additions & 151 deletions packages/dashboard/src/components/editor/VSCodeEditor.tsx

This file was deleted.

29 changes: 0 additions & 29 deletions packages/dashboard/src/pages/vscode-editor/index.tsx

This file was deleted.

15 changes: 0 additions & 15 deletions packages/dashboard/src/types/VSCodeEditorRPC.ts

This file was deleted.

46 changes: 44 additions & 2 deletions packages/editor/src/state/Clipboard.ts
@@ -1,11 +1,53 @@
import * as Data from "@uimix/model/src/data/v1";
import { projectState } from "./ProjectState";
import { DefaultClipboardHandler } from "./DefaultClipboardHandler";
import { ClipboardHandler } from "../types/ClipboardHandler";
import isSvg from "is-svg";

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

interface ClipboardHandler {
get(type: "text" | "image"): Promise<string | undefined>;
set(type: "text" | "image", textOrDataURL: string): Promise<void>;
}

class DefaultClipboardHandler {
async get(type: "text" | "image") {
switch (type) {
case "text":
return await navigator.clipboard.readText();
case "image": {
const items = await navigator.clipboard.read();
const item = items.find((item) => item.types.includes(`image/png`));
if (!item) {
return;
}
const blob = await item.getType(`image/png`);

return await new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
}
}

async set(type: "text" | "image", textOrDataURL: string) {
switch (type) {
case "text":
await navigator.clipboard.writeText(textOrDataURL);
case "image": {
const blob = await fetch(textOrDataURL).then((r) => r.blob());
await navigator.clipboard.write([
new ClipboardItem({
"image/png": blob,
}),
]);
}
}
}
}

export class Clipboard {
static handler: ClipboardHandler = new DefaultClipboardHandler();

Expand Down
40 changes: 0 additions & 40 deletions packages/editor/src/state/DefaultClipboardHandler.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/editor/src/state/IFrameDataConnector.ts
Expand Up @@ -9,7 +9,6 @@ import {
} from "../types/IFrameRPC";
import { throttle } from "lodash-es";
import { ThumbnailTakerHost } from "./ThumbnailTakerHost";
import { Clipboard } from "./Clipboard";
import { viewOptions } from "./ViewOptions";

export function vscodeParentTarget(): Target {
Expand Down Expand Up @@ -77,15 +76,6 @@ export class IFrameDataConnector {

void this.rpc.remote.ready();

// Clipboard.handler = {
// get: async (type) => {
// return this.rpc.remote.getClipboard(type);
// },
// set: async (type, text) => {
// void this.rpc.remote.setClipboard(type, text);
// },
// };

void this.rpc.remote.getCodeAssets().then((assets) => {
if (assets) {
this.state.project.localCodeAssets = assets;
Expand Down
4 changes: 0 additions & 4 deletions packages/editor/src/types/ClipboardHandler.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/editor/src/types/IFrameRPC.ts
Expand Up @@ -16,7 +16,4 @@ export interface IEditorToRootRPCHandler {
): Promise<string>;
updateThumbnail(pngData: Uint8Array): Promise<void>;
getCodeAssets(): Promise<CodeAssets | undefined>;

setClipboard(type: "text" | "image", textOrDataURL: string): Promise<void>;
getClipboard(type: "text" | "image"): Promise<string | undefined>;
}
6 changes: 0 additions & 6 deletions packages/vscode/src/EditorSession.ts
Expand Up @@ -109,12 +109,6 @@ export class EditorSession {
updateThumbnail: async () => {
// no op
},
getClipboard: async () => {
throw new Error("should be intercepted in webview.");
},
setClipboard: async () => {
throw new Error("should be intercepted in webview.");
},
getCodeAssets: async () => this.loadCodeAssets(),
}
);
Expand Down

0 comments on commit 0499110

Please sign in to comment.