-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
262 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* JPEG Sawmill - A viewer for JPEG progressive scans | ||
* Copyright (C) 2024 Rob Cowsill | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { html } from "../external/preact-htm-3.1.1.js"; | ||
|
||
|
||
const mainClass = "file-drop-target"; | ||
const activeClass = "active"; | ||
const dragoverClass = "dragover"; | ||
|
||
(function initModule() { | ||
const root = document; | ||
root.addEventListener("dragenter", handleRootDragEnter); | ||
root.addEventListener("dragleave", handleRootDragClear); | ||
root.addEventListener("dragover", handleRootDragOver); | ||
root.addEventListener("drop", handleRootDragClear); | ||
})(); | ||
|
||
function handleRootDragEnter(e) { | ||
const root = e.currentTarget; | ||
for (const el of root.querySelectorAll(`.${mainClass}`)) { | ||
el.classList.add(activeClass); | ||
} | ||
|
||
handleRootDragOver(e); | ||
} | ||
|
||
function handleRootDragClear(e) { | ||
// Deactivate drop targets when drag leaves without entering another element | ||
if (e.relatedTarget === null) { | ||
const root = e.currentTarget; | ||
for (const el of root.querySelectorAll(`.${mainClass}`)) { | ||
el.classList.remove(activeClass); | ||
} | ||
} | ||
} | ||
|
||
function handleRootDragOver(e) { | ||
const items = e.dataTransfer.items; | ||
if (items.length > 0) { | ||
// Chrome workaround: preventDefault makes file input's button non-dropzone | ||
const targetIsInput = e.target instanceof HTMLInputElement; | ||
const targetIsFileInput = (targetIsInput && e.target.type === "file"); | ||
if (!targetIsFileInput || items[0].kind !== "file") { | ||
e.preventDefault(); | ||
} | ||
} | ||
} | ||
|
||
function handleTargetDragOver(e) { | ||
const items = e.dataTransfer.items; | ||
if (items.length === 1 && items[0].kind === "file") { | ||
e.currentTarget.classList.add(dragoverClass); | ||
} else { | ||
e.dataTransfer.dropEffect = "none"; | ||
} | ||
|
||
e.preventDefault(); | ||
} | ||
|
||
function handleTargetDragLeave(e) { | ||
e.currentTarget.classList.remove(dragoverClass); | ||
} | ||
|
||
|
||
function FileDropTarget({ children, onFileDrop }) { | ||
function handleTargetDrop(e) { | ||
handleTargetDragLeave(e); | ||
|
||
const items = e.dataTransfer.items; | ||
if (items.length === 1 && items[0].kind === "file") { | ||
onFileDrop(e); | ||
} | ||
|
||
e.preventDefault(); | ||
} | ||
|
||
const targetEvents = { | ||
onDragOver: handleTargetDragOver, | ||
onDragLeave: handleTargetDragLeave, | ||
onDrop: handleTargetDrop | ||
}; | ||
|
||
return html` | ||
<div class=${mainClass} ...${targetEvents}> | ||
${children} | ||
</div> | ||
`; | ||
} | ||
|
||
export default FileDropTarget; |
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,40 @@ | ||
/* | ||
* JPEG Sawmill - A viewer for JPEG progressive scans | ||
* Copyright (C) 2024 Rob Cowsill | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { html } from "../external/preact-htm-3.1.1.js"; | ||
import FileDropTarget from "./file_drop_target.js"; | ||
|
||
|
||
function SawmillFileDropTarget({ onFileDrop }) { | ||
return html` | ||
<div class="sawmill-file-drop-target"> | ||
<${FileDropTarget} onFileDrop=${onFileDrop}> | ||
<div class="sawmill-file-drop-overlay"> | ||
<div class=drop-filter /> | ||
<div class=drop-background /> | ||
<div class=drop-vignette /> | ||
<div class=drop-outline /> | ||
<h1 class=drop-active-label>Drop a JPEG file here to open it</h1> | ||
<h1 class=drop-dragover-label>Open file...</h1> | ||
</div> | ||
<//> | ||
</div> | ||
`; | ||
} | ||
|
||
export default SawmillFileDropTarget; |
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