11"use client" ;
22
3+ import type { BlockIdentifier } from "@blocknote/core" ;
34import { BlockNoteView } from "@blocknote/mantine" ;
45import { useTheme } from "next-themes" ;
6+ import { useMemo , useRef } from "react" ;
7+ import { EditorTitle } from "~/components/editor/editor-title" ;
58import { useBlockEditor } from "./hooks/use-block-editor" ;
69import type { BlockNoteEditorProps } from "./types" ;
710
@@ -10,8 +13,11 @@ export function BlockEditor({
1013 parentId,
1114 parentType,
1215 isFullyLoaded,
16+ title,
17+ titlePlaceholder = "New page" ,
1318} : BlockNoteEditorProps ) {
1419 const { theme, systemTheme } = useTheme ( ) ;
20+ const titleRef = useRef < HTMLInputElement > ( null ) ;
1521
1622 // Use the main editor hook that contains all the logic
1723 const { editor, handleEditorChange } = useBlockEditor (
@@ -21,21 +27,55 @@ export function BlockEditor({
2127 isFullyLoaded ,
2228 ) ;
2329
30+ const firstBlock = useMemo ( ( ) => {
31+ return editor . document [ 0 ] ;
32+ } , [ editor . document ] ) ;
33+
2434 const resolvedTheme = theme === "system" ? systemTheme : theme ;
2535
36+ const handleTitleKeyDown = ( e : React . KeyboardEvent < HTMLInputElement > ) => {
37+ if ( e . key === "Enter" || e . key === "ArrowDown" ) {
38+ e . preventDefault ( ) ;
39+ if ( editor && firstBlock ) {
40+ editor . setTextCursorPosition ( firstBlock . id as BlockIdentifier , "end" ) ;
41+ editor . focus ( ) ;
42+ }
43+ }
44+ } ;
45+
46+ const handleEditorKeyDown = ( e : React . KeyboardEvent < HTMLDivElement > ) => {
47+ if ( e . key === "ArrowUp" && parentType === "page" ) {
48+ const cursorPos = editor . getTextCursorPosition ( ) ;
49+ if ( cursorPos . block . id === firstBlock ?. id ) {
50+ titleRef . current ?. focus ( ) ;
51+ setTimeout ( ( ) => {
52+ const length = titleRef . current ?. value . length || 0 ;
53+ titleRef . current ?. setSelectionRange ( length , length ) ;
54+ } , 0 ) ;
55+ }
56+ }
57+ } ;
58+
2659 return (
27- < div className = "blocknote-editor" data-color-scheme = { resolvedTheme } >
28- < BlockNoteView
29- editor = { editor }
30- onChange = { isFullyLoaded ? handleEditorChange : undefined }
31- editable = { isFullyLoaded }
32- theme = { resolvedTheme as "light" | "dark" }
60+ < >
61+ < EditorTitle
62+ ref = { titleRef }
63+ parentId = { parentId }
64+ parentType = { parentType }
65+ title = { title }
66+ placeholder = { titlePlaceholder }
67+ onKeyDown = { handleTitleKeyDown }
68+ className = "mb-4 pl-13"
3369 />
34- { ! isFullyLoaded && (
35- < div className = "mb-2 text-muted-foreground text-sm" >
36- Loading blocks...
37- </ div >
38- ) }
39- </ div >
70+ < div className = "blocknote-editor" data-color-scheme = { resolvedTheme } >
71+ < BlockNoteView
72+ editor = { editor }
73+ onChange = { isFullyLoaded ? handleEditorChange : undefined }
74+ editable = { isFullyLoaded }
75+ theme = { resolvedTheme as "light" | "dark" }
76+ onKeyDown = { handleEditorKeyDown }
77+ />
78+ </ div >
79+ </ >
4080 ) ;
4181}
0 commit comments