Skip to content

Commit e1434e2

Browse files
committed
fix: textarea line height and wrap
1 parent 8af1943 commit e1434e2

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

components/Block/Preview.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ const PreviewImpl: React.FC<PropsType> = ({ blockId, className }) => {
4444
}
4545
`}</style>
4646
<div
47-
className={clsx("preview", "flex-1", "cursor-text", className)}
47+
className={clsx(
48+
"preview flex-1 cursor-text whitespace-pre-wrap",
49+
className
50+
)}
4851
onClick={focusCallback}
4952
>
5053
{parsed.map((token) => {

components/Editor/blocks/Editable.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@ import {
99
import clsx from "clsx";
1010
import produce from "immer";
1111
import { useAtom } from "jotai";
12-
import { useAtomValue, useUpdateAtom } from "jotai/utils";
13-
import { useEffect, useRef } from "react";
12+
import { useUpdateAtom } from "jotai/utils";
13+
import { useCallback, useEffect, useRef } from "react";
1414
import tinykeys from "tinykeys";
1515
import { useNanoid } from "../../../hooks/useNanoid";
1616
import { useAdapter } from "../adapters/AdapterContext";
17-
import {
18-
anchorOffsetAtom,
19-
editingBlockIdAtom,
20-
LINE_HEIGHT_ATOM,
21-
} from "../store";
17+
import { anchorOffsetAtom, editingBlockIdAtom } from "../store";
2218

2319
const leftBrackets = ["[", "{"];
2420
const rightBracketsMap = { "[": "]", "{": "}" } as const;
@@ -58,7 +54,7 @@ export const EditableBlock: React.FC<EditablePropsType> = ({
5854
useKeyboardEvent(
5955
"Enter",
6056
(ev) => {
61-
if (!shouldCreateNewBlockRef.current) return;
57+
if (!shouldCreateNewBlockRef.current || ev.shiftKey) return;
6258
const textArea = textareaRef.current!;
6359
if (textArea.selectionStart === 0 && textArea.value.length > 0) {
6460
addNewBlock({
@@ -214,6 +210,18 @@ export const EditableBlock: React.FC<EditablePropsType> = ({
214210
undefined,
215211
{ target: textareaRef }
216212
);
213+
const resize = useCallback((target: HTMLTextAreaElement) => {
214+
target.style.height = "1px";
215+
target.style.height = +target.scrollHeight + "px";
216+
}, []);
217+
useEventListener(
218+
textareaRef,
219+
"input",
220+
(ev) => {
221+
resize(ev.target);
222+
},
223+
{ passive: true }
224+
);
217225
useMountEffect(() => {
218226
const textArea = textareaRef.current!;
219227
setTimeout(() => {
@@ -224,6 +232,7 @@ export const EditableBlock: React.FC<EditablePropsType> = ({
224232
);
225233
shouldDeleteBlockRef.current = textArea.value.length === 0;
226234
}, 0);
235+
resize(textArea);
227236
});
228237
useClickOutside(textareaRef, () => {
229238
setAnchorOffset(Infinity);
@@ -265,10 +274,6 @@ export const EditableBlock: React.FC<EditablePropsType> = ({
265274
ref={textareaRef}
266275
onChange={(ev) => {
267276
let { value } = ev.target;
268-
if (value.endsWith("\n")) {
269-
value = value.substring(0, value.length - 1);
270-
ev.target.value = value;
271-
}
272277
setAnchorOffset(value.length);
273278
setBlock(
274279
produce(block, (draft) => {
@@ -286,7 +291,6 @@ export const EditableBlock: React.FC<EditablePropsType> = ({
286291
"overflow-hidden",
287292
className
288293
)}
289-
style={{ height: useAtomValue(LINE_HEIGHT_ATOM) }}
290294
/>
291295
</>
292296
);

0 commit comments

Comments
 (0)