@@ -2,7 +2,7 @@ import { Block } from "@blocknote/core";
22import {
33 blockTypeSelectItems ,
44 useBlockNoteEditor ,
5- useEditorContentOrSelectionChange ,
5+ useEditorState ,
66} from "@blocknote/react" ;
77import {
88 Done ,
@@ -108,17 +108,10 @@ function MUIToolbarSelect<Item extends { name: string; icon?: FC }>(props: {
108108function MUIBlockTypeSelect ( ) {
109109 const editor = useBlockNoteEditor < TextBlockSchema > ( ) ;
110110
111- // The block currently containing the text cursor.
112- const [ block , setBlock ] = useState < Block > (
113- editor . getTextCursorPosition ( ) . block ,
114- ) ;
115-
116- // Updates the block currently containing the text cursor whenever the editor
117- // content or selection changes.
118- useEditorContentOrSelectionChange (
119- ( ) => setBlock ( editor . getTextCursorPosition ( ) . block ) ,
111+ const block = useEditorState ( {
120112 editor,
121- ) ;
113+ selector : ( { editor } ) => editor . getTextCursorPosition ( ) . block ,
114+ } ) ;
122115
123116 // Gets the default items for the select.
124117 const defaultBlockTypeSelectItems = useMemo (
@@ -154,8 +147,6 @@ function MUIBlockTypeSelect() {
154147 props : newSelectedItem . props ,
155148 } ) ;
156149 editor . focus ( ) ;
157-
158- setBlock ( editor . getTextCursorPosition ( ) . block ) ;
159150 } ,
160151 [ block , defaultBlockTypeSelectItems , editor ] ,
161152 ) ;
@@ -220,16 +211,10 @@ function MUIBasicTextStyleButton(props: {
220211 const editor = useBlockNoteEditor < TextBlockSchema > ( ) ;
221212
222213 // Whether the text style is currently active.
223- const [ textStyleActive , setTextStyleActive ] = useState (
224- ! ! editor . getActiveStyles ( ) [ props . textStyle ] ,
225- ) ;
226-
227- // Updates whether the text style is active when the editor content or
228- // selection changes.
229- useEditorContentOrSelectionChange (
230- ( ) => setTextStyleActive ( props . textStyle in editor . getActiveStyles ( ) ) ,
214+ const textStyleActive = useEditorState ( {
231215 editor,
232- ) ;
216+ selector : ( { editor } ) => props . textStyle in editor . getActiveStyles ( ) ,
217+ } ) ;
233218
234219 // Tooltip for the button.
235220 const tooltip = useMemo (
@@ -273,24 +258,16 @@ function MUITextAlignButton(props: {
273258 const editor = useBlockNoteEditor < TextBlockSchema > ( ) ;
274259
275260 // The text alignment of the block currently containing the text cursor.
276- const [ activeTextAlignment , setActiveTextAlignment ] = useState ( ( ) => {
277- const props = editor . getTextCursorPosition ( ) . block . props ;
278- if ( "textAlignment" in props ) {
279- return props . textAlignment ;
280- }
281- return undefined ;
282- } ) ;
283-
284- // Updates the text alignment when the editor content or selection changes.
285- useEditorContentOrSelectionChange ( ( ) => {
286- setActiveTextAlignment ( ( ) => {
261+ const activeTextAlignment = useEditorState ( {
262+ editor,
263+ selector : ( { editor } ) => {
287264 const props = editor . getTextCursorPosition ( ) . block . props ;
288265 if ( "textAlignment" in props ) {
289266 return props . textAlignment ;
290267 }
291268 return undefined ;
292- } ) ;
293- } , editor ) ;
269+ } ,
270+ } ) ;
294271
295272 // Tooltip for the button.
296273 const tooltip = useMemo (
@@ -346,21 +323,15 @@ function MUIColorStyleButton() {
346323 const [ anchorEl , setAnchorEl ] = useState < null | HTMLElement > ( null ) ;
347324
348325 // The active text and background colors.
349- const [ activeTextColor , setActiveTextColor ] = useState (
350- ( ) => editor . getActiveStyles ( ) . textColor || "default" ,
351- ) ;
352- const [ activeBackgroundColor , setActiveBackgroundColor ] = useState (
353- ( ) => editor . getActiveStyles ( ) . backgroundColor || "default" ,
354- ) ;
355-
356- // Updates the active text and background colors when the editor content or
357- // selection changes.
358- useEditorContentOrSelectionChange ( ( ) => {
359- const activeStyles = editor . getActiveStyles ( ) ;
360-
361- setActiveTextColor ( activeStyles . textColor || "default" ) ;
362- setActiveBackgroundColor ( activeStyles . backgroundColor || "default" ) ;
363- } , editor ) ;
326+ const activeTextColor = useEditorState ( {
327+ editor,
328+ selector : ( { editor } ) => editor . getActiveStyles ( ) . textColor || "default" ,
329+ } ) ;
330+ const activeBackgroundColor = useEditorState ( {
331+ editor,
332+ selector : ( { editor } ) =>
333+ editor . getActiveStyles ( ) . backgroundColor || "default" ,
334+ } ) ;
364335
365336 // Handles opening and closing the color menu.
366337 const onClick = useCallback (
0 commit comments