From 52ce5b971542232bc6a4339083e27948c4088fc4 Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Tue, 19 Aug 2025 16:34:46 -0400 Subject: [PATCH] put block creation on enter in async function --- components/Blocks/TextBlock/index.tsx | 10 ++-- components/Blocks/TextBlock/keymap.ts | 74 ++++++++++++++------------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/components/Blocks/TextBlock/index.tsx b/components/Blocks/TextBlock/index.tsx index 3e26cb04..2899b754 100644 --- a/components/Blocks/TextBlock/index.tsx +++ b/components/Blocks/TextBlock/index.tsx @@ -189,10 +189,12 @@ export function BaseTextBlock(props: BlockProps & { className?: string }) { let repRef = useRef>(null); let headingLevel = useEntity(props.entityID, "block/heading-level"); let entity_set = useEntitySetContext(); - let propsRef = useRef({ ...props, entity_set }); + let alignment = + useEntity(props.entityID, "block/text-alignment")?.data.value || "left"; + let propsRef = useRef({ ...props, entity_set, alignment }); useEffect(() => { - propsRef.current = { ...props, entity_set }; - }, [props, entity_set]); + propsRef.current = { ...props, entity_set, alignment }; + }, [props, entity_set, alignment]); let rep = useReplicache(); useEffect(() => { repRef.current = rep.rep; @@ -202,8 +204,6 @@ export function BaseTextBlock(props: BlockProps & { className?: string }) { (s) => !!s.selectedBlocks.find((b) => b.value === props.entityID), ); let focused = useUIState((s) => s.focusedEntity?.entityID === props.entityID); - let alignment = - useEntity(props.entityID, "block/text-alignment")?.data.value || "left"; let alignmentClass = { left: "text-left", right: "text-right", diff --git a/components/Blocks/TextBlock/keymap.ts b/components/Blocks/TextBlock/keymap.ts index 8e123f83..0bd952f4 100644 --- a/components/Blocks/TextBlock/keymap.ts +++ b/components/Blocks/TextBlock/keymap.ts @@ -12,7 +12,7 @@ import { } from "prosemirror-state"; import { RefObject } from "react"; import { Replicache } from "replicache"; -import type { ReplicacheMutators } from "src/replicache"; +import type { Fact, ReplicacheMutators } from "src/replicache"; import { elementId } from "src/utils/elementId"; import { schema } from "./schema"; import { useUIState } from "src/useUIState"; @@ -25,7 +25,12 @@ import { getBlocksWithType } from "src/hooks/queries/useBlocks"; import { isTextBlock } from "src/utils/isTextBlock"; import { UndoManager } from "src/undoManager"; -type PropsRef = RefObject; +type PropsRef = RefObject< + BlockProps & { + entity_set: { set: string }; + alignment: Fact<"block/text-alignment">["data"]["value"]; + } +>; export const TextBlockKeymap = ( propsRef: PropsRef, repRef: RefObject | null>, @@ -377,7 +382,7 @@ const shifttab = const enter = ( - propsRef: RefObject, + propsRef: PropsRef, repRef: RefObject | null>, ) => ( @@ -393,10 +398,6 @@ const enter = let newEntityID = v7(); let position: string; - useUIState.getState().setSelectedBlock({ - value: newEntityID, - parent: propsRef.current.parent, - }); let asyncRun = async () => { let blockType = propsRef.current.type === "heading" && state.selection.anchor <= 2 @@ -547,45 +548,48 @@ const enter = data: { type: "number", value: headingLevel.data.value || 0 }, }); } - let alignment = await repRef.current?.query((tx) => - scanIndex(tx).eav(propsRef.current.entityID, "block/text-alignment"), - ); - if (alignment?.[0] && alignment?.[0].data.value !== "left") { + if (propsRef.current.alignment !== "left") { await repRef.current?.mutate.assertFact({ entity: newEntityID, attribute: "block/text-alignment", data: { type: "text-alignment-type-union", - value: alignment?.[0].data.value, + value: propsRef.current.alignment, }, }); } }; - asyncRun(); + asyncRun().then(() => { + useUIState.getState().setSelectedBlock({ + value: newEntityID, + parent: propsRef.current.parent, + }); - // if you are in the middle of a text block, split the block - setTimeout(() => { - let block = useEditorStates.getState().editorStates[newEntityID]; - if (block) { - let tr = block.editor.tr; - if (newContent.content.size > 2) { - tr.replaceWith(0, tr.doc.content.size, newContent.content); - tr.setSelection(TextSelection.create(tr.doc, 0)); - let newState = block.editor.apply(tr); - setEditorState(newEntityID, { - editor: newState, - }); + setTimeout(() => { + let block = useEditorStates.getState().editorStates[newEntityID]; + if (block) { + let tr = block.editor.tr; + if (newContent.content.size > 2) { + tr.replaceWith(0, tr.doc.content.size, newContent.content); + tr.setSelection(TextSelection.create(tr.doc, 0)); + let newState = block.editor.apply(tr); + setEditorState(newEntityID, { + editor: newState, + }); + } + focusBlock( + { + value: newEntityID, + parent: propsRef.current.parent, + type: "text", + }, + { type: "start" }, + ); } - focusBlock( - { - value: newEntityID, - parent: propsRef.current.parent, - type: "text", - }, - { type: "start" }, - ); - } - }, 10); + }, 10); + }); + + // if you are in the middle of a text block, split the block return true; }; -- 2.51.2