feat(web): make it possible to add to object masks

This commit is contained in:
Paul Armstrong 2021-01-26 07:37:12 -08:00 committed by Blake Blackshear
parent d4e512c1fc
commit 75a01f657e

View File

@ -198,7 +198,7 @@ ${Object.keys(zonePoints)
const handleRemoveObjectMask = useCallback( const handleRemoveObjectMask = useCallback(
(key, subkey) => { (key, subkey) => {
const newObjectMaskPoints = { ...objectMaskPoints }; const newObjectMaskPoints = { ...objectMaskPoints };
delete newObjectMaskPoints[key]; delete newObjectMaskPoints[key][subkey];
setObjectMaskPoints(newObjectMaskPoints); setObjectMaskPoints(newObjectMaskPoints);
}, },
[objectMaskPoints, setObjectMaskPoints] [objectMaskPoints, setObjectMaskPoints]
@ -218,6 +218,20 @@ ${Object.keys(objectMaskPoints)
.join('\n')}`); .join('\n')}`);
}, [objectMaskPoints]); }, [objectMaskPoints]);
const handleAddToObjectMask = useCallback(
(key) => {
const newObjectMaskPoints = { ...objectMaskPoints, [key]: [...objectMaskPoints[key], []] };
setObjectMaskPoints(newObjectMaskPoints);
setEditing({
set: newObjectMaskPoints,
key,
subkey: newObjectMaskPoints[key].length - 1,
fn: setObjectMaskPoints,
});
},
[objectMaskPoints, setObjectMaskPoints, setEditing]
);
const handleChangeSnap = useCallback( const handleChangeSnap = useCallback(
(id, value) => { (id, value) => {
setSnap(value); setSnap(value);
@ -281,6 +295,7 @@ ${Object.keys(objectMaskPoints)
isMulti isMulti
editing={editing} editing={editing}
title="Object masks" title="Object masks"
onAdd={handleAddToObjectMask}
onCopy={handleCopyObjectMasks} onCopy={handleCopyObjectMasks}
onCreate={handleAddObjectMask} onCreate={handleAddObjectMask}
onEdit={handleEditObjectMask} onEdit={handleEditObjectMask}
@ -410,6 +425,7 @@ function MaskValues({
isMulti = false, isMulti = false,
editing, editing,
title, title,
onAdd,
onCopy, onCopy,
onCreate, onCreate,
onEdit, onEdit,
@ -451,6 +467,14 @@ function MaskValues({
[onRemove] [onRemove]
); );
const handleAdd = useCallback(
(event) => {
const { key } = event.target.dataset;
onAdd(key);
},
[onAdd]
);
return ( return (
<Box className="overflow-hidden" onmouseover={handleMousein} onmouseout={handleMouseout}> <Box className="overflow-hidden" onmouseover={handleMousein} onmouseout={handleMouseout}>
<div class="flex space-x-4"> <div class="flex space-x-4">
@ -467,15 +491,20 @@ function MaskValues({
return ( return (
<div> <div>
{` ${mainkey}:\n mask:\n`} {` ${mainkey}:\n mask:\n`}
{onAdd && showButtons ? (
<Button className="absolute -mt-12 right-0 font-sans" data-key={mainkey} onClick={handleAdd}>
{`Add to ${mainkey}`}
</Button>
) : null}
{points[mainkey].map((item, subkey) => ( {points[mainkey].map((item, subkey) => (
<Item <Item
mainkey={mainkey} mainkey={mainkey}
subkey={subkey} subkey={subkey}
editing={editing} editing={editing}
handleEdit={handleEdit} handleEdit={handleEdit}
handleRemove={handleRemove}
points={item} points={item}
showButtons={showButtons} showButtons={showButtons}
handleRemove={handleRemove}
yamlKeyPrefix={yamlKeyPrefix} yamlKeyPrefix={yamlKeyPrefix}
/> />
))} ))}
@ -486,10 +515,11 @@ function MaskValues({
<Item <Item
mainkey={mainkey} mainkey={mainkey}
editing={editing} editing={editing}
handleAdd={onAdd ? handleAdd : undefined}
handleEdit={handleEdit} handleEdit={handleEdit}
handleRemove={handleRemove}
points={points[mainkey]} points={points[mainkey]}
showButtons={showButtons} showButtons={showButtons}
handleRemove={handleRemove}
yamlKeyPrefix={yamlKeyPrefix} yamlKeyPrefix={yamlKeyPrefix}
/> />
); );
@ -500,7 +530,7 @@ function MaskValues({
); );
} }
function Item({ mainkey, subkey, editing, handleEdit, points, showButtons, handleRemove, yamlKeyPrefix }) { function Item({ mainkey, subkey, editing, handleEdit, points, showButtons, handleAdd, handleRemove, yamlKeyPrefix }) {
return ( return (
<span <span
data-key={mainkey} data-key={mainkey}