Skip to content

Commit

Permalink
Add support for importing from a clipboard #450
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Dec 4, 2024
1 parent 639a755 commit 4e98d36
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/js/component/item/actions/add-by-identifier.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ const AddByIdentifier = props => {
}
}, [toggleOpen]);

const handlePaste = useCallback(ev => {
const clipboardData = ev.clipboardData || window.clipboardData;
const pastedData = clipboardData.getData('Text');
const isMultiLineData = pastedData.split('\n').filter(line => line.trim().length > 0).length > 1;

if (!isMultiLineData) {
return;
}

ev.preventDefault();
setIdentifier(pastedData);
dispatch(searchIdentifier(pastedData, { shouldImport: true }));
}, [dispatch]);

useEffect(() => {
if(isOpen && item && prevItem === null) {
addItem({ ...item });
Expand Down Expand Up @@ -194,6 +208,7 @@ const AddByIdentifier = props => {
onChange={ handleInputChange }
onCommit={ handleInputCommit }
onKeyDown={ handleInputKeyDown }
onPaste={ handlePaste }
ref={ inputEl }
tabIndex={ 0 }
value={ identifier }
Expand Down
15 changes: 15 additions & 0 deletions src/js/component/modal/add-by-identifier.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ const AddByIdentifierModal = () => {
dispatch(searchIdentifier(identifier));
}, [dispatch, identifier]);

const handlePaste = useCallback(ev => {
const clipboardData = ev.clipboardData || window.clipboardData;
const pastedData = clipboardData.getData('Text');
const isMultiLineData = pastedData.split('\n').filter(line => line.trim().length > 0).length > 1;

if (!isMultiLineData) {
return;
}

ev.preventDefault();
setIdentifier(pastedData);
dispatch(searchIdentifier(pastedData, { shouldImport: true }));
}, [dispatch]);

useEffect(() => {
if(isOpen && item && prevItem === null) {
addItem({ ...item });
Expand Down Expand Up @@ -134,6 +148,7 @@ const AddByIdentifierModal = () => {
onBlur={ handleInputBlur }
onChange={ handleInputChange }
onCommit={ handleInputCommit }
onPaste={ handlePaste }
placeholder="URL, ISBNs, DOIs, PMIDs, or arXiv IDs"
ref={ inputEl }
tabIndex={ 0 }
Expand Down

0 comments on commit 4e98d36

Please sign in to comment.