Skip to content

Commit

Permalink
feat: enable pasting via the context menu in tree and table mode
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Feb 13, 2025
1 parent bb13594 commit 6c12e3b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/modals/CopyPasteModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Header title="Copying and pasting" {onClose} />

<div class="jse-modal-contents">
<div>These actions are unavailable via the menu. Please use:</div>
<div>Clipboard permission is disabled by your browser. You can use:</div>

<div class="jse-shortcuts">
<div class="jse-shortcut">
Expand Down
21 changes: 17 additions & 4 deletions src/lib/components/modes/tablemode/TableMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -1108,8 +1108,16 @@
setTimeout(focus)
}
function handlePasteFromMenu() {
copyPasteModalOpen = true
async function handlePasteFromMenu() {
try {
const clipboardText = await navigator.clipboard.readText()
_paste(clipboardText)
} catch (err) {
console.error(err)
copyPasteModalOpen = true
}
}
function handleClearPastedJson() {
Expand Down Expand Up @@ -1353,15 +1361,20 @@
function handlePaste(event: ClipboardEvent) {
event.preventDefault()
const clipboardText = event.clipboardData?.getData('text/plain') as string | undefined
const clipboardText = event.clipboardData?.getData('text/plain')
_paste(clipboardText)
}
function _paste(clipboardText: string | undefined) {
if (clipboardText === undefined) {
return
}
onPaste({
clipboardText,
json,
selection: selection,
selection,
readOnly,
parser,
onPatch: handlePatch,
Expand Down
23 changes: 18 additions & 5 deletions src/lib/components/modes/treemode/TreeMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,24 @@
function handlePaste(event: ClipboardEvent) {
event.preventDefault()
const clipboardText = event.clipboardData?.getData('text/plain') as string | undefined
const clipboardText = event.clipboardData?.getData('text/plain')
_paste(clipboardText)
}
async function handlePasteFromMenu() {
try {
const clipboardText = await navigator.clipboard.readText()
_paste(clipboardText)
} catch (err) {
console.error(err)
copyPasteModalOpen = true
}
}
function _paste(clipboardText: string | undefined) {
if (clipboardText === undefined) {
return
}
Expand All @@ -767,10 +784,6 @@
})
}
function handlePasteFromMenu() {
copyPasteModalOpen = true
}
function openRepairModal(text: string, onApply: (repairedText: string) => void) {
jsonRepairModalProps = {
text,
Expand Down

0 comments on commit 6c12e3b

Please sign in to comment.