-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(web): add support for layer name editing in beta (#665)
Co-authored-by: KaWaite <[email protected]>
- Loading branch information
Showing
13 changed files
with
213 additions
and
104 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
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
99 changes: 99 additions & 0 deletions
99
web/src/beta/features/Editor/tabs/map/LeftPanel/Layers/LayerItem.tsx
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,99 @@ | ||
import { MouseEvent, useCallback, useState } from "react"; | ||
|
||
import TextInput from "@reearth/beta/components/fields/common/TextInput"; | ||
import ListItem from "@reearth/beta/components/ListItem"; | ||
import PopoverMenuContent from "@reearth/beta/components/PopoverMenuContent"; | ||
import Text from "@reearth/beta/components/Text"; | ||
import type { LayerNameUpdateProps } from "@reearth/beta/features/Editor/useLayers"; | ||
|
||
type LayerItemProps = { | ||
id: string; | ||
layerTitle: string; | ||
isSelected: boolean; | ||
onDelete: () => void; | ||
onSelect: () => void; | ||
onLayerNameUpdate: (inp: LayerNameUpdateProps) => void; | ||
}; | ||
|
||
const LayerItem = ({ | ||
id, | ||
layerTitle, | ||
isSelected, | ||
onDelete, | ||
onSelect, | ||
onLayerNameUpdate, | ||
}: LayerItemProps) => { | ||
const [isActionOpen, setActionOpen] = useState(false); | ||
const [isEditing, setIsEditing] = useState(false); | ||
const [newValue, setNewValue] = useState(layerTitle); | ||
|
||
const handleActionMenuToggle = useCallback(() => setActionOpen(prev => !prev), []); | ||
|
||
const handleClick = useCallback( | ||
(e?: MouseEvent<Element>) => { | ||
if (e?.shiftKey) { | ||
e?.stopPropagation(); | ||
setIsEditing(true); | ||
return; | ||
} | ||
onSelect(); | ||
}, | ||
[onSelect], | ||
); | ||
|
||
const handleChange = useCallback((newTitle: string) => setNewValue(newTitle), []); | ||
|
||
const handleTitleSubmit = useCallback(() => { | ||
setIsEditing(false); | ||
onLayerNameUpdate({ layerId: id, name: newValue }); | ||
}, [id, newValue, onLayerNameUpdate]); | ||
|
||
const handleEditExit = useCallback( | ||
(e?: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (layerTitle !== newValue && e?.key !== "Escape") { | ||
handleTitleSubmit(); | ||
} else { | ||
setNewValue(layerTitle); | ||
} | ||
setIsEditing(false); | ||
}, | ||
[layerTitle, newValue, handleTitleSubmit], | ||
); | ||
|
||
return ( | ||
<ListItem | ||
isSelected={isSelected} | ||
isOpenAction={isActionOpen} | ||
actionPlacement="bottom-start" | ||
onItemClick={handleClick} | ||
onActionClick={handleActionMenuToggle} | ||
onOpenChange={isOpen => setActionOpen(!!isOpen)} | ||
actionContent={ | ||
<PopoverMenuContent | ||
size="sm" | ||
items={[ | ||
{ | ||
name: "Delete", | ||
icon: "bin", | ||
onClick: onDelete, | ||
}, | ||
]} | ||
/> | ||
}> | ||
{isEditing ? ( | ||
<TextInput | ||
value={newValue} | ||
timeout={0} | ||
autoFocus | ||
onChange={handleChange} | ||
onExit={handleEditExit} | ||
onBlur={handleEditExit} | ||
/> | ||
) : ( | ||
<Text size="body">{layerTitle}</Text> | ||
)} | ||
</ListItem> | ||
); | ||
}; | ||
|
||
export default LayerItem; |
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
Oops, something went wrong.