MCPcopy Create free account
hub / github.com/PatoSala/react-native-blocks / HeaderBlock

Function HeaderBlock

packages/blocks/src/HeaderBlock.tsx:16–186  ·  view source on GitHub ↗
({ blockId } : Props)

Source from the content-addressed store, hash-verified

14}
15
16export function HeaderBlock({ blockId } : Props) {
17 const { getTextInputProps, isFocused, getValue, getSelection } = useTextInput(blockId); // Maybe in the future we can pass a ref to use useImperativeHandle
18 const {
19 blocks,
20 turnBlockInto,
21 insertBlock,
22 updateBlock,
23 updateBlockV2,
24 removeBlock
25 } = useBlocksContext();
26 const { inputRefs, textBasedBlocks } = useTextBlocksContext();
27 const placeholder = "Header 1";
28
29 const handleSubmitEditing = () => {
30 const value = getValue();
31 const selection = getSelection();
32
33 if (value.length === 0) {
34 inputRefs.current["ghostInput"]?.current.focus();
35 // This timeout prevents the keyboard flicker
36 setTimeout(() => {
37 turnBlockInto(blockId, "text"); // Maybe this type should be thee defaultBlockType (?)
38
39 requestAnimationFrame(() => {
40 inputRefs.current[blockId]?.current.focus(); // Maybe the "ghostTextInput" hack should be done inside this function.
41 });
42 }, 0);
43 return;
44 }
45
46 if (selection.start === 0 && selection.end === 0) {
47 const newBlock = createBlock({
48 type: "text",
49 properties: {
50 title: ""
51 },
52 parent: blocks[blockId].parent,
53 content: []
54 });
55
56 insertBlock(newBlock, {
57 nextBlockId: blockId
58 });
59 return;
60 }
61
62 if (selection.start === value.length && selection.end === value.length) {
63 const newBlock = createBlock({
64 type: "text",
65 properties: {
66 title: ""
67 },
68 parent: blocks[blockId].parent,
69 content: []
70 });
71
72 insertBlock(newBlock, {
73 prevBlockId: blockId

Callers

nothing calls this directly

Calls 4

useTextInputFunction · 0.90
useBlocksContextFunction · 0.90
useTextBlocksContextFunction · 0.90
getTextInputPropsFunction · 0.85

Tested by

no test coverage detected