a tool for shared writing and social publishing
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix selection out of range error when switching block type

+18 -17
+18 -17
components/Toolbar/TextBlockTypeToolbar.tsx
··· 28 28 let setLevel = useCallback( 29 29 async (level: number) => { 30 30 if (!focusedBlock) return; 31 + let entityID = focusedBlock.entityID; 31 32 if ( 32 33 blockType?.data.value !== "text" && 33 34 blockType?.data.value !== "heading" 34 35 ) { 35 36 return; 36 37 } 38 + await rep?.mutate.assertFact({ 39 + entity: entityID, 40 + attribute: "block/heading-level", 41 + data: { type: "number", value: level }, 42 + }); 37 43 if (blockType.data.value === "text") { 38 - let existingEditor = 39 - useEditorStates.getState().editorStates[focusedBlock.entityID]; 44 + let existingEditor = useEditorStates.getState().editorStates[entityID]; 40 45 let selection = existingEditor?.editor.selection; 41 46 await rep?.mutate.assertFact({ 42 - entity: focusedBlock.entityID, 47 + entity: entityID, 43 48 attribute: "block/type", 44 49 data: { type: "block-type-union", value: "heading" }, 45 50 }); 46 51 47 - let newEditor = 48 - useEditorStates.getState().editorStates[focusedBlock.entityID]; 49 - if (!newEditor || !selection) return; 50 - newEditor.view?.dispatch( 51 - newEditor.editor.tr.setSelection( 52 - TextSelection.create(newEditor.editor.doc, selection.anchor), 53 - ), 54 - ); 52 + setTimeout(() => { 53 + let newEditor = useEditorStates.getState().editorStates[entityID]; 54 + if (!newEditor || !selection) return; 55 + newEditor.view?.dispatch( 56 + newEditor.editor.tr.setSelection( 57 + TextSelection.create(newEditor.editor.doc, selection.anchor), 58 + ), 59 + ); 55 60 56 - newEditor.view?.focus(); 61 + newEditor.view?.focus(); 62 + }, 20); 57 63 } 58 - rep?.mutate.assertFact({ 59 - entity: focusedBlock.entityID, 60 - attribute: "block/heading-level", 61 - data: { type: "number", value: level }, 62 - }); 63 64 }, 64 65 [rep, focusedBlock, blockType], 65 66 );