Skip to content

Commit b6ccfd9

Browse files
committed
fix: prevent accidental deletion of relative headings nested under other headings.
1 parent 5d0db98 commit b6ccfd9

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

packages/webapp/components/TipTap/extentions/ContentHeading.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,12 @@ const HeadingsTitle = Node.create({
403403
const { selection, doc, schema } = state
404404
const { $from, $to } = selection
405405

406-
// Check if we're in a content heading
406+
// Check if the caret in a content heading
407407
if ($from.parent.type.name !== ENUMS.NODES.CONTENT_HEADING_TYPE) {
408408
return false
409409
}
410410

411-
// Check if the cursor is in the middle of the node
411+
// Check if the caret is in the middle of the node
412412
if ($from.parentOffset === 0 || $from.parentOffset === $from.parent.content.size) {
413413
return false
414414
}

packages/webapp/components/TipTap/extentions/helper.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export const getHeadingsBlocksMap = (doc, start, end) => {
201201
depth,
202202
startBlockPos: pos,
203203
endBlockPos: pos + node.nodeSize,
204+
endContentPos: pos + node.content.size,
204205
index
205206
})
206207
}
@@ -380,7 +381,7 @@ export const insertRemainingHeadings = ({
380381
}) => {
381382
if (!headings.length) {
382383
console.info('[Heading][insertRemainingHeadings] no headings to insert')
383-
return false
384+
return true
384385
}
385386

386387
let mapHPost = getHeadingsBlocksMap(tr.doc, titleStartPos, titleEndPos)

packages/webapp/components/TipTap/extentions/wrapContenWithHeading.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const wrapContentWithHeading = (arrg, attributes, newSelection = null) => {
7676
selection
7777
)
7878

79-
tr.delete(start, titleEndPos)
79+
tr.delete(start + 1, titleEndPos)
8080

8181
let titleHMap = getHeadingsBlocksMap(tr.doc, titleStartPos, tr.mapping.map(titleEndPos))
8282
const { prevHStartPos } = getPrevHeadingPos(tr.doc, titleStartPos, start)
@@ -88,7 +88,12 @@ const wrapContentWithHeading = (arrg, attributes, newSelection = null) => {
8888
const insertFirstNodes = prevBlock.endBlockPos - (shouldNested ? 2 : 0)
8989
tr.insert(insertFirstNodes, headingNode)
9090

91-
const updatedSelection = putTextSelectionEndNode(tr, insertFirstNodes, headingNode)
91+
const targetPos = Math.min(
92+
insertFirstNodes + headingNode[0].firstChild.nodeSize,
93+
tr.doc.content.size - 1
94+
)
95+
96+
const updatedSelection = new TextSelection(tr.doc.resolve(targetPos))
9297
tr.setSelection(updatedSelection)
9398

9499
return insertRemainingHeadings({
@@ -140,14 +145,14 @@ const wrapContentWithHeading = (arrg, attributes, newSelection = null) => {
140145

141146
const insertFirstNodesPos = tr.mapping.map(newStartPos)
142147

143-
tr.delete(insertFirstNodesPos, block.parent.end)
148+
tr.delete(insertFirstNodesPos, $to.end(1))
144149
tr.insert(insertFirstNodesPos, newHeadingNode)
145150

146151
const updatedSelection = putTextSelectionEndNode(tr, insertFirstNodesPos, newHeadingNode)
147152
tr.setSelection(updatedSelection)
148153

149154
titleStartPos = newSelection ? $from.start(1) : $from.start(1) - 1
150-
titleEndPos = newSelection ? tr.curSelection.$to.end(1) : $to.end(1)
155+
titleEndPos = newSelection ? tr.curSelection.$to.end(1) : tr.mapping.map($to.end(1))
151156

152157
try {
153158
insertRemainingHeadings({
@@ -160,7 +165,7 @@ const wrapContentWithHeading = (arrg, attributes, newSelection = null) => {
160165
})
161166
return true
162167
} catch (error) {
163-
console.error('[Heading][wrapContentWithHeading]: error insertRemainingHeadings')
168+
console.error('[Heading][wrapContentWithHeading]: error insertRemainingHeadings', error)
164169
return false
165170
}
166171
}

0 commit comments

Comments
 (0)