diff --git a/src/js/component/item/actions/add-by-identifier.jsx b/src/js/component/item/actions/add-by-identifier.jsx index 0989029b..42353207 100644 --- a/src/js/component/item/actions/add-by-identifier.jsx +++ b/src/js/component/item/actions/add-by-identifier.jsx @@ -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 }); @@ -194,6 +208,7 @@ const AddByIdentifier = props => { onChange={ handleInputChange } onCommit={ handleInputCommit } onKeyDown={ handleInputKeyDown } + onPaste={ handlePaste } ref={ inputEl } tabIndex={ 0 } value={ identifier } diff --git a/src/js/component/modal/add-by-identifier.jsx b/src/js/component/modal/add-by-identifier.jsx index 8142cd0d..1201c63a 100644 --- a/src/js/component/modal/add-by-identifier.jsx +++ b/src/js/component/modal/add-by-identifier.jsx @@ -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 }); @@ -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 }