-
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.
Co-authored-by: airslice <[email protected]>
- Loading branch information
1 parent
ab9db9a
commit b46698f
Showing
13 changed files
with
781 additions
and
358 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
123 changes: 123 additions & 0 deletions
123
web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Link/Content.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,123 @@ | ||
import { FC, useCallback, useContext, useState } from "react"; | ||
|
||
import Button from "@reearth/beta/components/Button"; | ||
import { BlockContext } from "@reearth/beta/features/Visualizer/shared/components/BlockWrapper"; | ||
import { useT } from "@reearth/services/i18n"; | ||
import { styled } from "@reearth/services/theme"; | ||
|
||
import LinkEditor, { type LinkBlock as LinkBlockType } from "./Editor"; | ||
|
||
type Props = { | ||
propertyId?: string; | ||
linkButtons: LinkBlockType[]; | ||
isEditable?: boolean; | ||
onPropertyUpdate?: ( | ||
propertyId?: string, | ||
schemaItemId?: string, | ||
fieldId?: string, | ||
itemId?: string, | ||
vt?: any, | ||
v?: any, | ||
) => Promise<void>; | ||
onPropertyItemAdd?: (propertyId?: string, schemaGroupId?: string) => Promise<void>; | ||
onPropertyItemMove?: ( | ||
propertyId?: string, | ||
schemaGroupId?: string, | ||
itemId?: string, | ||
index?: number, | ||
) => Promise<void>; | ||
onPropertyItemDelete?: ( | ||
propertyId?: string, | ||
schemaGroupId?: string, | ||
itemId?: string, | ||
) => Promise<void>; | ||
}; | ||
|
||
const Content: FC<Props> = ({ | ||
propertyId, | ||
linkButtons, | ||
isEditable, | ||
onPropertyUpdate, | ||
onPropertyItemAdd, | ||
onPropertyItemDelete, | ||
onPropertyItemMove, | ||
}) => { | ||
const t = useT(); | ||
const blockContext = useContext(BlockContext); | ||
const [selected, setSelected] = useState<string>(linkButtons[0]?.id); | ||
|
||
const handleClick = useCallback( | ||
(itemId: string) => { | ||
if (isEditable) { | ||
setSelected(itemId); | ||
return; | ||
} | ||
const item = linkButtons.find(i => i.id === itemId); | ||
|
||
if (!item?.url?.value) return; | ||
window.open(item.url.value, "_blank"); | ||
}, | ||
[isEditable, linkButtons], | ||
); | ||
|
||
return ( | ||
<Wrapper> | ||
<ButtonWrapper> | ||
{linkButtons.map(({ title, color, bgColor, id }) => { | ||
return ( | ||
//The button will be updated in future | ||
<StyledButton | ||
key={id} | ||
color={color?.value} | ||
bgColor={bgColor?.value} | ||
icon="linkButtonStoryBlock" | ||
buttonType="primary" | ||
text={title?.value ?? t("New Link Button")} | ||
size="small" | ||
onClick={() => handleClick(id)} | ||
/> | ||
); | ||
})} | ||
</ButtonWrapper> | ||
{blockContext?.editMode && ( | ||
<LinkEditor | ||
items={linkButtons} | ||
propertyId={propertyId} | ||
selected={selected} | ||
setSelected={setSelected} | ||
onPropertyUpdate={onPropertyUpdate} | ||
onPropertyItemAdd={onPropertyItemAdd} | ||
onPropertyItemMove={onPropertyItemMove} | ||
onPropertyItemDelete={onPropertyItemDelete} | ||
/> | ||
)} | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
export default Content; | ||
|
||
const Wrapper = styled("div")(() => ({ | ||
width: "100%", | ||
})); | ||
|
||
const ButtonWrapper = styled("div")(({ theme }) => ({ | ||
width: "100%", | ||
display: "flex", | ||
flexWrap: "wrap", | ||
gap: theme.spacing.smallest, | ||
maxWidth: "400px", | ||
})); | ||
|
||
const StyledButton = styled(Button)<{ color?: string; bgColor?: string; userSelected?: boolean }>( | ||
({ color, bgColor, userSelected, theme }) => ({ | ||
color: userSelected ? bgColor ?? theme.content.strong : color, | ||
backgroundColor: userSelected ? color ?? theme.primary.main : bgColor, | ||
borderColor: color, | ||
|
||
":hover": { | ||
color: bgColor, | ||
backgroundColor: color ?? theme.primary.main, | ||
}, | ||
}), | ||
); |
116 changes: 116 additions & 0 deletions
116
web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Link/Editor/hooks.ts
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,116 @@ | ||
import { debounce } from "lodash-es"; | ||
import { useCallback, useMemo } from "react"; | ||
|
||
import { ValueTypes } from "@reearth/beta/utils/value"; | ||
import { useT } from "@reearth/services/i18n"; | ||
|
||
import type { Field } from "../../../../types"; | ||
|
||
export type LinkBlock = { | ||
id: string; | ||
title?: Field<string>; | ||
color?: Field<string>; | ||
bgColor?: Field<string>; | ||
url?: Field<URL>; | ||
}; | ||
|
||
export default ({ | ||
items, | ||
propertyId, | ||
selected, | ||
onPropertyUpdate, | ||
onPropertyItemAdd, | ||
onPropertyItemDelete, | ||
onPropertyItemMove, | ||
}: { | ||
items: LinkBlock[]; | ||
propertyId?: string; | ||
selected?: string; | ||
onPropertyUpdate?: ( | ||
propertyId?: string, | ||
schemaItemId?: string, | ||
fieldId?: string, | ||
itemId?: string, | ||
vt?: any, | ||
v?: any, | ||
) => Promise<void>; | ||
onPropertyItemAdd?: (propertyId?: string, schemaGroupId?: string) => Promise<void>; | ||
onPropertyItemMove?: ( | ||
propertyId?: string, | ||
schemaGroupId?: string, | ||
itemId?: string, | ||
index?: number, | ||
) => Promise<void>; | ||
onPropertyItemDelete?: ( | ||
propertyId?: string, | ||
schemaGroupId?: string, | ||
itemId?: string, | ||
) => Promise<void>; | ||
}) => { | ||
const t = useT(); | ||
|
||
const editorProperties = useMemo(() => items.find(i => i.id === selected), [items, selected]); | ||
|
||
const handlePropertyValueUpdate = useCallback( | ||
(schemaGroupId: string, propertyId: string, fieldId: string, vt: any, itemId?: string) => { | ||
return async (v?: any) => { | ||
await onPropertyUpdate?.(propertyId, schemaGroupId, fieldId, itemId, vt, v); | ||
}; | ||
}, | ||
[onPropertyUpdate], | ||
); | ||
|
||
const handleUpdate = useCallback( | ||
( | ||
itemId: string, | ||
fieldId: string, | ||
fieldType: keyof ValueTypes, | ||
updatedValue?: ValueTypes[keyof ValueTypes], | ||
) => { | ||
if (!propertyId || !itemId) return; | ||
|
||
handlePropertyValueUpdate("default", propertyId, fieldId, fieldType, itemId)(updatedValue); | ||
}, | ||
[propertyId, handlePropertyValueUpdate], | ||
); | ||
|
||
const debounceOnUpdate = useMemo(() => debounce(handleUpdate, 500), [handleUpdate]); | ||
|
||
const listItems = useMemo( | ||
() => items.map(({ id, title }) => ({ id, title: title?.value ?? t("New Link Button") })), | ||
[items, t], | ||
); | ||
|
||
const handleItemAdd = useCallback(() => { | ||
if (!propertyId) return; | ||
onPropertyItemAdd?.(propertyId, "default"); | ||
}, [propertyId, onPropertyItemAdd]); | ||
|
||
const handleItemRemove = useCallback( | ||
(itemId: string) => { | ||
if (!propertyId || !itemId) return; | ||
|
||
onPropertyItemDelete?.(propertyId, "default", itemId); | ||
}, | ||
[propertyId, onPropertyItemDelete], | ||
); | ||
|
||
const handleItemMove = useCallback( | ||
(id: string, index: number) => { | ||
if (!propertyId || !id) return; | ||
|
||
onPropertyItemMove?.(propertyId, "default", id, index); | ||
}, | ||
[propertyId, onPropertyItemMove], | ||
); | ||
|
||
return { | ||
editorProperties, | ||
debounceOnUpdate, | ||
listItems, | ||
handleUpdate, | ||
handleItemAdd, | ||
handleItemRemove, | ||
handleItemMove, | ||
}; | ||
}; |
Oops, something went wrong.