|
| 1 | +import { setCaretPosition } from "@src/utils/caretUtils"; |
| 2 | +import { KeyHandlerContext } from "../types"; |
| 3 | +import { updateEditorState, decreaseIndent, findBlockByIndex } from "../utils"; |
| 4 | + |
| 5 | +export const handleBackspaceKey = ( |
| 6 | + e: React.KeyboardEvent<HTMLDivElement>, |
| 7 | + ctx: KeyHandlerContext, |
| 8 | +) => { |
| 9 | + const { |
| 10 | + editorCRDT, |
| 11 | + pageId, |
| 12 | + clientId, |
| 13 | + setEditorState, |
| 14 | + sendBlockDeleteOperation, |
| 15 | + sendBlockUpdateOperation, |
| 16 | + sendCharInsertOperation, |
| 17 | + sendCharDeleteOperation, |
| 18 | + } = ctx; |
| 19 | + |
| 20 | + const { currentBlock } = editorCRDT; |
| 21 | + if (!currentBlock) return; |
| 22 | + |
| 23 | + const currentContent = currentBlock.crdt.read(); |
| 24 | + const currentCharNodes = currentBlock.crdt.spread(); |
| 25 | + const currentIndex = editorCRDT.LinkedList.spread().findIndex((block) => |
| 26 | + block.id.equals(currentBlock.id), |
| 27 | + ); |
| 28 | + |
| 29 | + const update = () => updateEditorState(editorCRDT, setEditorState); |
| 30 | + |
| 31 | + if (currentContent === "") { |
| 32 | + e.preventDefault(); |
| 33 | + |
| 34 | + if (currentBlock.indent > 0) { |
| 35 | + decreaseIndent(editorCRDT, currentBlock, pageId, setEditorState, sendBlockUpdateOperation); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if (currentBlock.type === "p") { |
| 40 | + const prevBlock = currentBlock.prev ? editorCRDT.LinkedList.getNode(currentBlock.prev) : null; |
| 41 | + const nextBlock = currentBlock.next ? editorCRDT.LinkedList.getNode(currentBlock.next) : null; |
| 42 | + |
| 43 | + if (prevBlock?.type === "ol" && nextBlock?.type === "ol") { |
| 44 | + sendBlockDeleteOperation(editorCRDT.localDelete(currentIndex, undefined, pageId)); |
| 45 | + editorCRDT.LinkedList.updateAllOrderedListIndices(); |
| 46 | + |
| 47 | + editorCRDT.currentBlock = prevBlock; |
| 48 | + prevBlock.crdt.currentCaret = prevBlock.crdt.read().length; |
| 49 | + update(); |
| 50 | + return; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (currentBlock.type !== "p") { |
| 55 | + const wasOrderedList = currentBlock.type === "ol"; |
| 56 | + currentBlock.type = "p"; |
| 57 | + sendBlockUpdateOperation(editorCRDT.localUpdate(currentBlock, pageId)); |
| 58 | + editorCRDT.currentBlock = currentBlock; |
| 59 | + |
| 60 | + if (wasOrderedList) { |
| 61 | + editorCRDT.LinkedList.updateAllOrderedListIndices(); |
| 62 | + } |
| 63 | + update(); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const prevBlock = currentIndex > 0 ? editorCRDT.LinkedList.findByIndex(currentIndex - 1) : null; |
| 68 | + |
| 69 | + if (prevBlock) { |
| 70 | + sendBlockDeleteOperation(editorCRDT.localDelete(currentIndex, undefined, pageId)); |
| 71 | + |
| 72 | + let targetIndex = currentIndex - 1; |
| 73 | + let targetBlock = findBlockByIndex(editorCRDT, targetIndex); |
| 74 | + |
| 75 | + while (targetBlock && targetBlock.type === "hr") { |
| 76 | + targetIndex -= 1; |
| 77 | + targetBlock = findBlockByIndex(editorCRDT, targetIndex); |
| 78 | + } |
| 79 | + |
| 80 | + if (targetBlock && targetBlock.type !== "hr") { |
| 81 | + targetBlock.crdt.currentCaret = targetBlock.crdt.read().length; |
| 82 | + editorCRDT.currentBlock = targetBlock; |
| 83 | + setCaretPosition({ |
| 84 | + blockId: targetBlock.id, |
| 85 | + position: targetBlock.crdt.read().length, |
| 86 | + pageId, |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + update(); |
| 91 | + } |
| 92 | + |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + const { currentCaret } = currentBlock.crdt; |
| 97 | + if (currentCaret === 0) { |
| 98 | + if (currentBlock.indent > 0) { |
| 99 | + decreaseIndent(editorCRDT, currentBlock, pageId, setEditorState, sendBlockUpdateOperation); |
| 100 | + update(); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (currentBlock.type !== "p") { |
| 105 | + const wasOrderedList = currentBlock.type === "ol"; |
| 106 | + currentBlock.type = "p"; |
| 107 | + sendBlockUpdateOperation(editorCRDT.localUpdate(currentBlock, pageId)); |
| 108 | + editorCRDT.currentBlock = currentBlock; |
| 109 | + if (wasOrderedList) { |
| 110 | + editorCRDT.LinkedList.updateAllOrderedListIndices(); |
| 111 | + } |
| 112 | + update(); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const prevBlock = currentIndex > 0 ? editorCRDT.LinkedList.findByIndex(currentIndex - 1) : null; |
| 117 | + const nextBlock = |
| 118 | + currentIndex < editorCRDT.LinkedList.spread().length - 1 |
| 119 | + ? editorCRDT.LinkedList.findByIndex(currentIndex + 1) |
| 120 | + : null; |
| 121 | + |
| 122 | + if (prevBlock) { |
| 123 | + let targetIndex = currentIndex - 1; |
| 124 | + let targetBlock = findBlockByIndex(editorCRDT, targetIndex); |
| 125 | + |
| 126 | + while (targetBlock && targetBlock.type === "hr") { |
| 127 | + targetIndex -= 1; |
| 128 | + targetBlock = findBlockByIndex(editorCRDT, targetIndex); |
| 129 | + } |
| 130 | + |
| 131 | + if (targetBlock && prevBlock.type === "hr") { |
| 132 | + editorCRDT.currentBlock = targetBlock; |
| 133 | + editorCRDT.currentBlock.crdt.currentCaret = targetBlock.crdt.read().length; |
| 134 | + update(); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const prevBlockEndCaret = prevBlock.crdt.read().length; |
| 139 | + |
| 140 | + for (let i = 0; i < currentContent.length; i++) { |
| 141 | + const currentCharNode = currentCharNodes[i]; |
| 142 | + sendCharInsertOperation( |
| 143 | + prevBlock.crdt.localInsert( |
| 144 | + prevBlockEndCaret + i, |
| 145 | + currentContent[i], |
| 146 | + prevBlock.id, |
| 147 | + pageId, |
| 148 | + clientId, |
| 149 | + currentCharNode.style, |
| 150 | + currentCharNode.color, |
| 151 | + currentCharNode.backgroundColor, |
| 152 | + ), |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + currentContent.split("").forEach(() => { |
| 157 | + sendCharDeleteOperation(currentBlock.crdt.localDelete(0, currentBlock.id, pageId)); |
| 158 | + }); |
| 159 | + |
| 160 | + editorCRDT.currentBlock = prevBlock; |
| 161 | + prevBlock.crdt.currentCaret = prevBlockEndCaret; |
| 162 | + sendBlockDeleteOperation(editorCRDT.localDelete(currentIndex, undefined, pageId)); |
| 163 | + update(); |
| 164 | + |
| 165 | + if (prevBlock.type === "ol" && nextBlock?.type === "ol") { |
| 166 | + editorCRDT.LinkedList.updateAllOrderedListIndices(); |
| 167 | + } |
| 168 | + e.preventDefault(); |
| 169 | + } |
| 170 | + } |
| 171 | +}; |
0 commit comments