-
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.
Direct paste from clipboard flow works.
- Loading branch information
erikh2000
authored and
erikh2000
committed
Dec 27, 2024
1 parent
98287d8
commit 81170f6
Showing
6 changed files
with
168 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useState, useEffect, useMemo } from 'react'; | ||
|
||
import ModalDialog from '@/components/modalDialogs/ModalDialog'; | ||
import DialogFooter from '@/components/modalDialogs/DialogFooter'; | ||
import DialogButton from '@/components/modalDialogs/DialogButton'; | ||
import SheetView from '../SheetView'; | ||
import HoneSheet from '@/sheets/types/HoneSheet'; | ||
import { doesSheetHaveWritableColumns } from '@/sheets/sheetUtil'; | ||
|
||
type Props = { | ||
pastedSheet:HoneSheet|null, | ||
existingSheet:HoneSheet|null, | ||
isOpen:boolean, | ||
onConfirm(sheet:HoneSheet):void, | ||
onCancel():void | ||
} | ||
|
||
function _getUiTextAffectedByExistingSheet(existingSheet:HoneSheet|null):{confirmButtonText:string, description:string} { | ||
if (!existingSheet) return {confirmButtonText:'Import', description:'Import this sheet into Hone?'}; | ||
let description = `Replace "${existingSheet.name}" sheet with this new pasted sheet?`; | ||
if (doesSheetHaveWritableColumns(existingSheet)) description += ' You will discard any added columns.'; | ||
return {confirmButtonText:'Replace', description}; | ||
} | ||
|
||
function ConfirmSheetPasteDialog({pastedSheet, existingSheet, isOpen, onConfirm, onCancel}:Props) { | ||
if (!isOpen || !pastedSheet) return null; | ||
|
||
const {confirmButtonText, description} = _getUiTextAffectedByExistingSheet(existingSheet); | ||
|
||
return ( | ||
<ModalDialog isOpen={isOpen} onCancel={onCancel} title="Confirm Sheet Paste"> | ||
<p>{description}</p> | ||
<SheetView sheet={pastedSheet} maxRows={5} padToMax={true}/> | ||
<DialogFooter> | ||
<DialogButton text="Cancel" onClick={onCancel} /> | ||
<DialogButton text={confirmButtonText} onClick={() => { if (pastedSheet) onConfirm(pastedSheet)}} isPrimary/> | ||
</DialogFooter> | ||
</ModalDialog> | ||
); | ||
} | ||
|
||
export default ConfirmSheetPasteDialog; |
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 |
---|---|---|
@@ -1,6 +1,36 @@ | ||
import { setSystemMessage } from "@/llm/llmUtil"; | ||
import { SYSTEM_MESSAGE } from "./prompt"; | ||
import { importFromPasteEvent } from "./import"; | ||
|
||
export async function init() { | ||
type PasteHandlerFunction = (event:ClipboardEvent) => void; | ||
|
||
let pasteHandler:PasteHandlerFunction|null = null; | ||
|
||
function _handlePaste(clipboardEvent:ClipboardEvent, setAvailableSheets:Function, setModalDialog:Function) { | ||
const target = clipboardEvent.target as HTMLElement|null; | ||
|
||
if (target) { | ||
// If an editable DOM element is focused, I just want the browser's default handling of the paste. | ||
const targetName = target.tagName; | ||
if (targetName === 'INPUT' || targetName === 'TEXTAREA' || target.isContentEditable) return; | ||
} | ||
|
||
// Try to import a sheet from the clipboard. | ||
clipboardEvent.preventDefault(); | ||
importFromPasteEvent(clipboardEvent, setAvailableSheets, setModalDialog); | ||
} | ||
|
||
export async function init(setAvailableSheets:Function, setModalDialog:Function) { | ||
setSystemMessage(SYSTEM_MESSAGE); | ||
|
||
if (pasteHandler) document.removeEventListener('paste', pasteHandler); | ||
pasteHandler = clipboardEvent => _handlePaste(clipboardEvent, setAvailableSheets, setModalDialog); | ||
document.addEventListener('paste', pasteHandler); | ||
} | ||
|
||
export function deinit() { | ||
if (pasteHandler) { | ||
document.removeEventListener('paste', pasteHandler); | ||
pasteHandler = null; | ||
} | ||
} |
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