tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
put block creation on enter in async function
awarm.space
7 months ago
52ce5b97
30ad328d
+44
-40
2 changed files
expand all
collapse all
unified
split
components
Blocks
TextBlock
index.tsx
keymap.ts
+5
-5
components/Blocks/TextBlock/index.tsx
···
189
189
let repRef = useRef<null | Replicache<ReplicacheMutators>>(null);
190
190
let headingLevel = useEntity(props.entityID, "block/heading-level");
191
191
let entity_set = useEntitySetContext();
192
192
-
let propsRef = useRef({ ...props, entity_set });
192
192
+
let alignment =
193
193
+
useEntity(props.entityID, "block/text-alignment")?.data.value || "left";
194
194
+
let propsRef = useRef({ ...props, entity_set, alignment });
193
195
useEffect(() => {
194
194
-
propsRef.current = { ...props, entity_set };
195
195
-
}, [props, entity_set]);
196
196
+
propsRef.current = { ...props, entity_set, alignment };
197
197
+
}, [props, entity_set, alignment]);
196
198
let rep = useReplicache();
197
199
useEffect(() => {
198
200
repRef.current = rep.rep;
···
202
204
(s) => !!s.selectedBlocks.find((b) => b.value === props.entityID),
203
205
);
204
206
let focused = useUIState((s) => s.focusedEntity?.entityID === props.entityID);
205
205
-
let alignment =
206
206
-
useEntity(props.entityID, "block/text-alignment")?.data.value || "left";
207
207
let alignmentClass = {
208
208
left: "text-left",
209
209
right: "text-right",
+39
-35
components/Blocks/TextBlock/keymap.ts
···
12
12
} from "prosemirror-state";
13
13
import { RefObject } from "react";
14
14
import { Replicache } from "replicache";
15
15
-
import type { ReplicacheMutators } from "src/replicache";
15
15
+
import type { Fact, ReplicacheMutators } from "src/replicache";
16
16
import { elementId } from "src/utils/elementId";
17
17
import { schema } from "./schema";
18
18
import { useUIState } from "src/useUIState";
···
25
25
import { isTextBlock } from "src/utils/isTextBlock";
26
26
import { UndoManager } from "src/undoManager";
27
27
28
28
-
type PropsRef = RefObject<BlockProps & { entity_set: { set: string } }>;
28
28
+
type PropsRef = RefObject<
29
29
+
BlockProps & {
30
30
+
entity_set: { set: string };
31
31
+
alignment: Fact<"block/text-alignment">["data"]["value"];
32
32
+
}
33
33
+
>;
29
34
export const TextBlockKeymap = (
30
35
propsRef: PropsRef,
31
36
repRef: RefObject<Replicache<ReplicacheMutators> | null>,
···
377
382
378
383
const enter =
379
384
(
380
380
-
propsRef: RefObject<BlockProps & { entity_set: { set: string } }>,
385
385
+
propsRef: PropsRef,
381
386
repRef: RefObject<Replicache<ReplicacheMutators> | null>,
382
387
) =>
383
388
(
···
393
398
394
399
let newEntityID = v7();
395
400
let position: string;
396
396
-
useUIState.getState().setSelectedBlock({
397
397
-
value: newEntityID,
398
398
-
parent: propsRef.current.parent,
399
399
-
});
400
401
let asyncRun = async () => {
401
402
let blockType =
402
403
propsRef.current.type === "heading" && state.selection.anchor <= 2
···
547
548
data: { type: "number", value: headingLevel.data.value || 0 },
548
549
});
549
550
}
550
550
-
let alignment = await repRef.current?.query((tx) =>
551
551
-
scanIndex(tx).eav(propsRef.current.entityID, "block/text-alignment"),
552
552
-
);
553
553
-
if (alignment?.[0] && alignment?.[0].data.value !== "left") {
551
551
+
if (propsRef.current.alignment !== "left") {
554
552
await repRef.current?.mutate.assertFact({
555
553
entity: newEntityID,
556
554
attribute: "block/text-alignment",
557
555
data: {
558
556
type: "text-alignment-type-union",
559
559
-
value: alignment?.[0].data.value,
557
557
+
value: propsRef.current.alignment,
560
558
},
561
559
});
562
560
}
563
561
};
564
564
-
asyncRun();
562
562
+
asyncRun().then(() => {
563
563
+
useUIState.getState().setSelectedBlock({
564
564
+
value: newEntityID,
565
565
+
parent: propsRef.current.parent,
566
566
+
});
567
567
+
568
568
+
setTimeout(() => {
569
569
+
let block = useEditorStates.getState().editorStates[newEntityID];
570
570
+
if (block) {
571
571
+
let tr = block.editor.tr;
572
572
+
if (newContent.content.size > 2) {
573
573
+
tr.replaceWith(0, tr.doc.content.size, newContent.content);
574
574
+
tr.setSelection(TextSelection.create(tr.doc, 0));
575
575
+
let newState = block.editor.apply(tr);
576
576
+
setEditorState(newEntityID, {
577
577
+
editor: newState,
578
578
+
});
579
579
+
}
580
580
+
focusBlock(
581
581
+
{
582
582
+
value: newEntityID,
583
583
+
parent: propsRef.current.parent,
584
584
+
type: "text",
585
585
+
},
586
586
+
{ type: "start" },
587
587
+
);
588
588
+
}
589
589
+
}, 10);
590
590
+
});
565
591
566
592
// if you are in the middle of a text block, split the block
567
567
-
setTimeout(() => {
568
568
-
let block = useEditorStates.getState().editorStates[newEntityID];
569
569
-
if (block) {
570
570
-
let tr = block.editor.tr;
571
571
-
if (newContent.content.size > 2) {
572
572
-
tr.replaceWith(0, tr.doc.content.size, newContent.content);
573
573
-
tr.setSelection(TextSelection.create(tr.doc, 0));
574
574
-
let newState = block.editor.apply(tr);
575
575
-
setEditorState(newEntityID, {
576
576
-
editor: newState,
577
577
-
});
578
578
-
}
579
579
-
focusBlock(
580
580
-
{
581
581
-
value: newEntityID,
582
582
-
parent: propsRef.current.parent,
583
583
-
type: "text",
584
584
-
},
585
585
-
{ type: "start" },
586
586
-
);
587
587
-
}
588
588
-
}, 10);
589
593
return true;
590
594
};
591
595