aboutsummaryrefslogtreecommitdiffstats
path: root/packages/excalidraw/tests/helpers/polyfills.ts
diff options
context:
space:
mode:
authorkj_sh6042026-03-15 16:19:35 -0400
committerkj_sh6042026-03-15 16:19:35 -0400
commit6ec259a0e71174651bae95d4628138bf6fd68742 (patch)
tree5e33c6a5ec091ecabfcb257fdc7b6a88ed8754ac /packages/excalidraw/tests/helpers/polyfills.ts
parent16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff)
refactor: packages/
Diffstat (limited to 'packages/excalidraw/tests/helpers/polyfills.ts')
-rw-r--r--packages/excalidraw/tests/helpers/polyfills.ts95
1 files changed, 95 insertions, 0 deletions
diff --git a/packages/excalidraw/tests/helpers/polyfills.ts b/packages/excalidraw/tests/helpers/polyfills.ts
new file mode 100644
index 0000000..967e8d4
--- /dev/null
+++ b/packages/excalidraw/tests/helpers/polyfills.ts
@@ -0,0 +1,95 @@
+import { URL } from "node:url";
+
+class ClipboardEvent {
+ constructor(
+ type: "paste" | "copy",
+ eventInitDict: {
+ clipboardData: DataTransfer;
+ },
+ ) {
+ return Object.assign(
+ new Event("paste", {
+ bubbles: true,
+ cancelable: true,
+ composed: true,
+ }),
+ {
+ clipboardData: eventInitDict.clipboardData,
+ },
+ ) as any as ClipboardEvent;
+ }
+}
+
+type DataKind = "string" | "file";
+
+class DataTransferItem {
+ kind: DataKind;
+ type: string;
+ data: string | Blob;
+
+ constructor(kind: DataKind, type: string, data: string | Blob) {
+ this.kind = kind;
+ this.type = type;
+ this.data = data;
+ }
+
+ getAsString(callback: (data: string) => void): void {
+ if (this.kind === "string") {
+ callback(this.data as string);
+ }
+ }
+
+ getAsFile(): File | null {
+ if (this.kind === "file" && this.data instanceof File) {
+ return this.data;
+ }
+ return null;
+ }
+}
+
+class DataTransferList {
+ items: DataTransferItem[] = [];
+
+ add(data: string | File, type: string = ""): void {
+ if (typeof data === "string") {
+ this.items.push(new DataTransferItem("string", type, data));
+ } else if (data instanceof File) {
+ this.items.push(new DataTransferItem("file", type, data));
+ }
+ }
+
+ clear(): void {
+ this.items = [];
+ }
+}
+
+class DataTransfer {
+ public items: DataTransferList = new DataTransferList();
+ private _types: Record<string, string> = {};
+
+ get files() {
+ return this.items.items
+ .filter((item) => item.kind === "file")
+ .map((item) => item.getAsFile()!);
+ }
+
+ add(data: string | File, type: string = ""): void {
+ this.items.add(data, type);
+ }
+
+ setData(type: string, value: string) {
+ this._types[type] = value;
+ }
+
+ getData(type: string) {
+ return this._types[type] || "";
+ }
+}
+
+export const testPolyfills = {
+ ClipboardEvent,
+ DataTransfer,
+ DataTransferItem,
+ // https://github.com/vitest-dev/vitest/pull/4164#issuecomment-2172729965
+ URL,
+};