@@ -9,16 +9,12 @@ import {
99import clsx from "clsx" ;
1010import produce from "immer" ;
1111import { 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" ;
1414import tinykeys from "tinykeys" ;
1515import { useNanoid } from "../../../hooks/useNanoid" ;
1616import { useAdapter } from "../adapters/AdapterContext" ;
17- import {
18- anchorOffsetAtom ,
19- editingBlockIdAtom ,
20- LINE_HEIGHT_ATOM ,
21- } from "../store" ;
17+ import { anchorOffsetAtom , editingBlockIdAtom } from "../store" ;
2218
2319const leftBrackets = [ "[" , "{" ] ;
2420const 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