2
0

🚸 (buttons) Better multiple item parsing

Instead of using the clipboard, we directly detect pasting in the onChange handler
This commit is contained in:
Baptiste Arnaud
2024-02-16 07:41:40 +01:00
parent 5d0be9ecd9
commit 07240e6d4b

View File

@ -62,13 +62,13 @@ export const ButtonsItemNode = ({ item, indices, isMouseOver }: Props) => {
itemValue !== t('blocks.inputs.button.clickToEdit.label') itemValue !== t('blocks.inputs.button.clickToEdit.label')
) )
handlePlusClick() handlePlusClick()
if (e.key === 'v' && (e.ctrlKey || e.metaKey)) { }
const clipboardContents = await navigator.clipboard.read()
const item = clipboardContents[0] const handleEditableChange = (val: string) => {
if (!item.types.includes('text/plain')) return const isPastingMultipleItems =
const text = await (await item.getType('text/plain')).text() val.length - itemValue.length > 1 && val.includes(',')
if (!text || !text.includes(',')) return if (isPastingMultipleItems) {
const values = text.split(',') const values = val.split(',')
return values.forEach((v, i) => { return values.forEach((v, i) => {
if (i === 0) { if (i === 0) {
setItemValue(v) setItemValue(v)
@ -80,6 +80,7 @@ export const ButtonsItemNode = ({ item, indices, isMouseOver }: Props) => {
} }
}) })
} }
setItemValue(val)
} }
const handlePlusClick = () => { const handlePlusClick = () => {
@ -105,7 +106,7 @@ export const ButtonsItemNode = ({ item, indices, isMouseOver }: Props) => {
flex="1" flex="1"
startWithEditView={isNotDefined(item.content)} startWithEditView={isNotDefined(item.content)}
value={itemValue} value={itemValue}
onChange={setItemValue} onChange={handleEditableChange}
onSubmit={handleInputSubmit} onSubmit={handleInputSubmit}
onKeyDownCapture={handleKeyPress} onKeyDownCapture={handleKeyPress}
maxW="180px" maxW="180px"