-
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Support for file drag & drop * Interoperability with Drop Ruler * Save file and get path in the front-end * Working POC * Add 'raw' before path string literals * Move code templates to Julia * Change Image sample to use Images * Use TableIOInterface for Files -> Table conversions * Move drop handler out of Cell.js * Update the code for images & text to use let block * Make file transfer !!!25%!!! FASTER dropping base64 usage * Use correct is_extension_supported * Handle non-empty cells * Rename hook, var for debounce ms * Prevent error message while saving * Make JavaScripty names more Plutonic * update the handling of Juia files * Move assets folder along with notebook * Don't run file event when moving cells * Save files with the same name with incremental numbers * make warning message less aggressive * 🦆 Fix expression equality again * Revert "🦆 Fix expression equality again" This reverts commit 81d81be. * Fix generated path code by lungben * Add statistics * Benjamin's compat suggestion * Use the latest dralbase Also, we now support file-drops to cells with code (adds cell after it) * fix 1 bug, handle drops on body * fix typo * Fix frontend tests thanks) * don't change whitespace! Co-authored-by: Fons van der Plas <[email protected]>
- Loading branch information
Showing
11 changed files
with
266 additions
and
11 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
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,90 @@ | ||
import { PlutoContext } from "../common/PlutoContext.js" | ||
import { useState, useMemo, useContext } from "../imports/Preact.js" | ||
|
||
const MAGIC_TIMEOUT = 500 | ||
const DEBOUNCE_MAGIC_MS = 250 | ||
|
||
const prepareFile = (file) => | ||
new Promise((resolve, reject) => { | ||
const { name, type } = file | ||
const fr = new FileReader() | ||
fr.onerror = () => reject("Failed to read file!") | ||
fr.onloadstart = () => {} | ||
fr.onprogress = ({ loaded, total }) => {} | ||
fr.onload = () => {} | ||
fr.onloadend = ({ target: { result } }) => resolve({ file: result, name, type }) | ||
fr.readAsArrayBuffer(file) | ||
}) | ||
|
||
export const useDropHandler = () => { | ||
let pluto_actions = useContext(PlutoContext) | ||
const [saving_file, set_saving_file] = useState(false) | ||
const [drag_active, set_drag_active_fast] = useState(false) | ||
const set_drag_active = useMemo(() => _.debounce(set_drag_active_fast, DEBOUNCE_MAGIC_MS), [set_drag_active_fast]) | ||
|
||
const handler = useMemo(() => { | ||
const uploadAndCreateCodeTemplate = async (file, drop_cell_id) => { | ||
if (!(file instanceof File)) return " # File can't be read" | ||
set_saving_file(true) | ||
const { | ||
message: { success, code }, | ||
} = await prepareFile(file).then( | ||
(preparedObj) => { | ||
return pluto_actions.write_file(drop_cell_id, preparedObj) | ||
}, | ||
() => alert("Pluto can't save this file 😥") | ||
) | ||
set_saving_file(false) | ||
set_drag_active_fast(false) | ||
if (!success) { | ||
alert("Pluto can't save this file 😥") | ||
return "# File save failed" | ||
} | ||
if (code) return code | ||
alert("Pluto doesn't know what to do with this file 😥. Feel that's wrong? Open an issue!") | ||
return "" | ||
} | ||
return (ev) => { | ||
ev.stopPropagation() | ||
// dataTransfer is in Protected Mode here. see type, let Pluto DropRuler handle it. | ||
if (ev.dataTransfer.types[0] === "text/pluto-cell") return | ||
switch (ev.type) { | ||
case "cmdrop": | ||
case "drop": | ||
ev.preventDefault() // don't file open | ||
const cell_element = ev.path.find((el) => el.tagName === "PLUTO-CELL") | ||
const drop_cell_id = cell_element?.id || document.querySelector("pluto-cell:last-child")?.id | ||
const drop_cell_value = cell_element?.querySelector(".CodeMirror")?.CodeMirror?.getValue() | ||
const is_empty = drop_cell_value?.length === 0 && !cell_element?.classList?.contains("code_folded") | ||
set_drag_active(false) | ||
if (!ev.dataTransfer.files.length) { | ||
return | ||
} | ||
uploadAndCreateCodeTemplate(ev.dataTransfer.files[0], drop_cell_id).then((code) => { | ||
if (code) { | ||
if (!is_empty) { | ||
pluto_actions.add_remote_cell(drop_cell_id, "after", code) | ||
} else { | ||
pluto_actions.set_local_cell(drop_cell_id, code, () => pluto_actions.set_and_run_multiple([drop_cell_id])) | ||
} | ||
} | ||
}) | ||
break | ||
case "dragover": | ||
ev.preventDefault() | ||
ev.dataTransfer.dropEffect = "copy" | ||
set_drag_active(true) | ||
setTimeout(() => set_drag_active(false), MAGIC_TIMEOUT) | ||
break | ||
case "dragenter": | ||
set_drag_active_fast(true) | ||
break | ||
case "dragleave": | ||
set_drag_active(false) | ||
break | ||
default: | ||
} | ||
} | ||
}, [set_drag_active, set_drag_active_fast, set_saving_file, pluto_actions]) | ||
return { saving_file, drag_active, handler } | ||
} |
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
Oops, something went wrong.