From 76b6ff78cd8b436979c98e58e3b4b491816cd541 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Tue, 17 Oct 2023 21:37:42 +0000 Subject: [PATCH] File drag and drop, sort of. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4527 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/client.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/core/client.js b/core/client.js index 78dfd4de..602bd3b7 100644 --- a/core/client.js +++ b/core/client.js @@ -149,12 +149,14 @@ class TfFilesElement extends LitElement { return { current: {type: String}, files: {type: Object}, + dropping: {type: Number}, }; } constructor() { super(); this.files = {}; + this.dropping = 0; } file_click(file) { @@ -178,6 +180,36 @@ class TfFilesElement extends LitElement { return html`
this.file_click(file)}>${file}
`; } + async drop(event) { + event.preventDefault(); + event.stopPropagation(); + this.dropping = 0; + for (let file of event.dataTransfer.files) { + let text = ''; + try { + text = await file.text(); + } catch { + text = new TextDecoder('latin1').decode(await file.arrayBuffer()); + } + gFiles[file.name] = { + doc: new CodeMirror.Doc(text, guessMode(file.name)), + generation: -1, + }; + gCurrentFile = file.name; + } + openFile(gCurrentFile); + updateFiles(); + } + + drag_enter(event) { + this.dropping++; + event.preventDefault(); + } + + drag_leave(event) { + this.dropping--; + } + render() { let self = this; return html` @@ -202,9 +234,15 @@ class TfFilesElement extends LitElement { content: '*'; } -
+
${Object.keys(this.files).sort().map(x => self.render_file(x))}
+
+ Drop File(s) +
`; } } @@ -992,7 +1030,6 @@ function updateFiles() { }])); files.current = gCurrentFile; } - gEditor.focus(); }