-
Notifications
You must be signed in to change notification settings - Fork 43
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: KaWaite <[email protected]> Co-authored-by: pyshx <[email protected]>
- Loading branch information
1 parent
4a62abc
commit 8fd9d35
Showing
17 changed files
with
282 additions
and
33 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
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
69 changes: 69 additions & 0 deletions
69
web/src/beta/lib/core/StoryPanel/Block/builtin/Markdown/Editor.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,69 @@ | ||
import { debounce } from "lodash-es"; | ||
import { useContext, useCallback, useLayoutEffect, useRef, useMemo, useState } from "react"; | ||
|
||
import Markdown from "@reearth/beta/components/Markdown"; | ||
import { useT } from "@reearth/services/i18n"; | ||
import { styled } from "@reearth/services/theme"; | ||
|
||
import { BlockContext } from "../common/Wrapper"; | ||
|
||
export type Props = { | ||
text: string; | ||
onUpdate: (text: string) => void; | ||
}; | ||
|
||
const MdBlockEditor: React.FC<Props> = ({ text, onUpdate }) => { | ||
const textareaRef = useRef<HTMLTextAreaElement>(null); | ||
const t = useT(); | ||
const context = useContext(BlockContext); | ||
|
||
const [value, setValue] = useState(text); | ||
|
||
const debouncedHandleTextUpdate = useMemo(() => debounce(onUpdate, 1000), [onUpdate]); | ||
|
||
const onChange = useCallback( | ||
(e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { | ||
setValue(e.currentTarget.value); | ||
debouncedHandleTextUpdate(e.currentTarget.value); | ||
}, | ||
[debouncedHandleTextUpdate], | ||
); | ||
|
||
useLayoutEffect(() => { | ||
if (!textareaRef?.current) return; | ||
// Reset height - important to shrink on delete | ||
textareaRef.current.style.height = "inherit"; | ||
// Set height | ||
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`; | ||
}, [value, context?.editMode]); | ||
|
||
return context?.editMode ? ( | ||
<StyledTextArea | ||
ref={textareaRef} | ||
placeholder={t("Add markdown text here")} | ||
value={value} | ||
onChange={onChange} | ||
/> | ||
) : ( | ||
<StyledMarkdown empty={!value}>{value || t("Add markdown text here")}</StyledMarkdown> | ||
); | ||
}; | ||
|
||
const StyledTextArea = styled.textarea` | ||
width: 100%; | ||
resize: none; | ||
overflow: hidden; | ||
${({ value }) => !value && "min-height: 115px;"} | ||
border: none; | ||
font-size: 14px; | ||
padding: 0px; | ||
outline: none; | ||
`; | ||
|
||
const StyledMarkdown = styled(Markdown)<{ empty: boolean }>` | ||
${({ empty }) => empty && "min-height: 115px;"} | ||
font-size: 14px; | ||
opacity: ${({ empty }) => (!empty ? 1 : 0.6)}; | ||
`; | ||
|
||
export default MdBlockEditor; |
Oops, something went wrong.