diff --git a/src/common/components/ContentEditable.tsx b/src/common/components/ContentEditable.tsx new file mode 100644 index 0000000..5af070e --- /dev/null +++ b/src/common/components/ContentEditable.tsx @@ -0,0 +1,26 @@ +import { useEffect, useRef } from "preact/hooks"; +import type { JSX } from "preact"; + +type Props = Omit, 'value' | 'onInput'> & { + value: string; + onInput?: JSX.EventHandler>; +}; + +export const ContentEditable = ({ value, onInput, ...props }: Props) => { + const ref = useRef(null); + + useEffect(() => { + if (ref.current && ref.current.innerHTML !== value) { + ref.current.innerHTML = value; + } + }, [value]); + + return ( +
+ ); +}; diff --git a/src/common/hooks/useInputState.ts b/src/common/hooks/useInputState.ts index 5e0b121..41b10c3 100644 --- a/src/common/hooks/useInputState.ts +++ b/src/common/hooks/useInputState.ts @@ -10,6 +10,8 @@ export const useInputState = (defaultValue = ''): [string, (e: Event | string) = const { target } = e; if (target && 'value' in target && typeof target.value === 'string') { setValue(target.value); + } else if (target instanceof HTMLElement) { + setValue(target.innerHTML); } } }, []); diff --git a/src/games/storywriter/assets/editor.module.css b/src/games/storywriter/assets/editor.module.css index 8081538..7aa02f1 100644 --- a/src/games/storywriter/assets/editor.module.css +++ b/src/games/storywriter/assets/editor.module.css @@ -7,7 +7,7 @@ background: var(--bg); } -.textarea { +.editable { flex: 1; width: 100%; height: 100%; diff --git a/src/games/storywriter/assets/sidebar.module.css b/src/games/storywriter/assets/sidebar.module.css index b987f83..2cb6920 100644 --- a/src/games/storywriter/assets/sidebar.module.css +++ b/src/games/storywriter/assets/sidebar.module.css @@ -12,6 +12,10 @@ .sidebar:last-child { border-right: none; border-left: 1px solid var(--border); + + & .toggle { + align-self: flex-start; + } } .open { diff --git a/src/games/storywriter/components/app.tsx b/src/games/storywriter/components/app.tsx index 60933d5..35d705e 100644 --- a/src/games/storywriter/components/app.tsx +++ b/src/games/storywriter/components/app.tsx @@ -1,5 +1,5 @@ -import { Sidebar } from "./sidebar/sidebar"; -import { Editor } from "./editor/editor"; +import { Sidebar } from "./sidebar"; +import { Editor } from "./editor"; import styles from '../assets/app.module.css'; export const App = () => { diff --git a/src/games/storywriter/components/editor.tsx b/src/games/storywriter/components/editor.tsx new file mode 100644 index 0000000..1d1b3aa --- /dev/null +++ b/src/games/storywriter/components/editor.tsx @@ -0,0 +1,17 @@ +import { ContentEditable } from "@common/components/ContentEditable"; +import { useInputState } from "@common/hooks/useInputState"; +import styles from '../assets/editor.module.css'; + +export const Editor = () => { + const [content, handleInput] = useInputState(''); + + return ( +
+ +
+ ); +}; diff --git a/src/games/storywriter/components/editor/editor.tsx b/src/games/storywriter/components/editor/editor.tsx deleted file mode 100644 index b5934a6..0000000 --- a/src/games/storywriter/components/editor/editor.tsx +++ /dev/null @@ -1,9 +0,0 @@ -import styles from '../../assets/editor.module.css'; - -export const Editor = () => { - return ( -
-