-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: allow pasting full marimo applications in a cell (#3273)
- Loading branch information
Showing
12 changed files
with
613 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
frontend/src/core/codemirror/misc/__tests__/dnd.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* Copyright 2024 Marimo. All rights reserved. */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { EditorView } from "@codemirror/view"; | ||
import { dndBundle } from "../dnd"; | ||
import { describe, beforeEach, afterEach, it, expect } from "vitest"; | ||
|
||
describe("dnd", () => { | ||
let view: EditorView; | ||
|
||
beforeEach(() => { | ||
const el = document.createElement("div"); | ||
view = new EditorView({ | ||
parent: el, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
view.destroy(); | ||
}); | ||
|
||
it("handles text file drops", () => { | ||
const extension = dndBundle(); | ||
const handlers = extension[0] as any; | ||
const dropHandler = handlers.domEventHandlers.drop; | ||
|
||
const file = new File(["test content"], "test.txt", { type: "text/plain" }); | ||
const event = new DragEvent("drop", { | ||
dataTransfer: new DataTransfer(), | ||
}); | ||
event.dataTransfer?.items.add(file); | ||
|
||
const result = dropHandler(event, view); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("handles image file drops", () => { | ||
const extension = dndBundle(); | ||
const handlers = extension[0] as any; | ||
const dropHandler = handlers.domEventHandlers.drop; | ||
|
||
const file = new File([""], "test.png", { type: "image/png" }); | ||
const event = new DragEvent("drop", { | ||
dataTransfer: new DataTransfer(), | ||
}); | ||
event.dataTransfer?.items.add(file); | ||
|
||
const result = dropHandler(event, view); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("handles plain text drops", () => { | ||
const extension = dndBundle(); | ||
const handlers = extension[0] as any; | ||
const dropHandler = handlers.domEventHandlers.drop; | ||
|
||
const event = new DragEvent("drop", { | ||
dataTransfer: new DataTransfer(), | ||
clientX: 0, | ||
clientY: 0, | ||
}); | ||
event.dataTransfer?.setData("text/plain", "dropped text"); | ||
|
||
const result = dropHandler(event, view); | ||
expect(result).toBe(true); | ||
}); | ||
}); | ||
|
||
class DragEvent extends Event { | ||
dataTransfer: DataTransfer; | ||
clientX: number; | ||
clientY: number; | ||
|
||
constructor( | ||
type: string, | ||
{ | ||
dataTransfer, | ||
clientX, | ||
clientY, | ||
}: { dataTransfer?: DataTransfer; clientX?: number; clientY?: number } = {}, | ||
) { | ||
super(type); | ||
this.dataTransfer = dataTransfer || new DataTransfer(); | ||
this.clientX = clientX || 0; | ||
this.clientY = clientY || 0; | ||
} | ||
} | ||
|
||
class DataTransfer { | ||
data: Record<string, string> = {}; | ||
_items: File[] = []; | ||
|
||
setData(type: string, data: string) { | ||
this.data[type] = data; | ||
} | ||
|
||
get items() { | ||
return { | ||
add: (file: File) => { | ||
this._items.push(file); | ||
}, | ||
}; | ||
} | ||
|
||
get files() { | ||
return this._items; | ||
} | ||
|
||
getData(type: string) { | ||
return this.data[type]; | ||
} | ||
} |
Oops, something went wrong.