-
-
Notifications
You must be signed in to change notification settings - Fork 629
fix: Deleting last block in column #2110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matthewlipski
wants to merge
7
commits into
main
Choose a base branch
from
multi-column-block-delete-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd6519b
Fixed deleting last block in column not collapsing column/column list
matthewlipski 226020d
Fixed and added tests
matthewlipski b96e9e5
Added one more test
matthewlipski aabf534
Added logic for collapsing `columns`/`columnList` nodes when entire c…
matthewlipski a521b8f
Implemented PR feedback
matthewlipski ff199d9
feat: Clean up invalid multi-column handling (#2113)
matthewlipski 4ff328f
Implemented PR feedback
matthewlipski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
packages/core/src/api/blockManipulation/commands/replaceBlocks/util/fixColumnList.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { Slice, type Node } from "prosemirror-model"; | ||
| import { type Transaction } from "prosemirror-state"; | ||
| import { ReplaceAroundStep } from "prosemirror-transform"; | ||
|
|
||
| /** | ||
| * Checks if a `column` node is empty, i.e. if it has only a single empty | ||
| * block. | ||
| * @param column The column to check. | ||
| * @returns Whether the column is empty. | ||
| */ | ||
| export function isEmptyColumn(column: Node) { | ||
| if (!column || column.type.name !== "column") { | ||
| throw new Error("Invalid columnPos: does not point to column node."); | ||
| } | ||
|
|
||
| const blockContainer = column.firstChild; | ||
| if (!blockContainer) { | ||
| throw new Error("Invalid column: does not have child node."); | ||
| } | ||
|
|
||
| const blockContent = blockContainer.firstChild; | ||
| if (!blockContent) { | ||
| throw new Error("Invalid blockContainer: does not have child node."); | ||
| } | ||
|
|
||
| return ( | ||
| column.childCount === 1 && | ||
| blockContainer.childCount === 1 && | ||
| blockContent.type.spec.content === "inline*" && | ||
| blockContent.content.content.length === 0 | ||
matthewlipski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Removes all empty `column` nodes in a `columnList`. A `column` node is empty | ||
| * if it has only a single empty block. If, however, removing the `column`s | ||
| * leaves the `columnList` that has fewer than two, ProseMirror will re-add | ||
| * empty columns. | ||
| * @param tr The `Transaction` to add the changes to. | ||
| * @param columnListPos The position just before the `columnList` node. | ||
| */ | ||
| export function removeEmptyColumns(tr: Transaction, columnListPos: number) { | ||
| const $columnListPos = tr.doc.resolve(columnListPos); | ||
| const columnList = $columnListPos.nodeAfter; | ||
| if (!columnList || columnList.type.name !== "columnList") { | ||
| throw new Error( | ||
| "Invalid columnListPos: does not point to columnList node.", | ||
| ); | ||
| } | ||
|
|
||
| for ( | ||
| let columnIndex = columnList.childCount - 1; | ||
| columnIndex >= 0; | ||
| columnIndex-- | ||
| ) { | ||
| const columnPos = tr.doc | ||
| .resolve($columnListPos.pos + 1) | ||
| .posAtIndex(columnIndex); | ||
| const $columnPos = tr.doc.resolve(columnPos); | ||
| const column = $columnPos.nodeAfter; | ||
| if (!column || column.type.name !== "column") { | ||
| throw new Error("Invalid columnPos: does not point to column node."); | ||
| } | ||
|
|
||
| if (isEmptyColumn(column)) { | ||
| tr.delete(columnPos, columnPos + column?.nodeSize); | ||
matthewlipski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Fixes potential issues in a `columnList` node after a | ||
| * `blockContainer`/`column` node is (re)moved from it: | ||
| * | ||
| * - Removes all empty `column` nodes. A `column` node is empty if it has only | ||
| * a single empty block. | ||
| * - If all but one `column` nodes are empty, replaces the `columnList` with | ||
| * the content of the non-empty `column`. | ||
| * - If all `column` nodes are empty, removes the `columnList` entirely. | ||
| * @param tr The `Transaction` to add the changes to. | ||
| * @param columnListPos | ||
| * @returns The position just before the `columnList` node. | ||
| */ | ||
| export function fixColumnList(tr: Transaction, columnListPos: number) { | ||
| removeEmptyColumns(tr, columnListPos); | ||
|
|
||
| const $columnListPos = tr.doc.resolve(columnListPos); | ||
| const columnList = $columnListPos.nodeAfter; | ||
| if (!columnList || columnList.type.name !== "columnList") { | ||
| throw new Error( | ||
| "Invalid columnListPos: does not point to columnList node.", | ||
| ); | ||
| } | ||
|
|
||
| if (columnList.childCount > 2) { | ||
| // Do nothing if the `columnList` has at least two non-empty `column`s. | ||
| return; | ||
| } | ||
matthewlipski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (columnList.childCount < 2) { | ||
| throw new Error("Invalid columnList: contains fewer than two children."); | ||
| } | ||
matthewlipski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const firstColumnBeforePos = columnListPos + 1; | ||
| const $firstColumnBeforePos = tr.doc.resolve(firstColumnBeforePos); | ||
| const firstColumn = $firstColumnBeforePos.nodeAfter; | ||
|
|
||
| const lastColumnAfterPos = columnListPos + columnList.nodeSize - 1; | ||
| const $lastColumnAfterPos = tr.doc.resolve(lastColumnAfterPos); | ||
| const lastColumn = $lastColumnAfterPos.nodeBefore; | ||
|
|
||
| if (!firstColumn || !lastColumn) { | ||
| throw new Error("Invalid columnList: does not contain children."); | ||
| } | ||
|
|
||
| const firstColumnEmpty = isEmptyColumn(firstColumn); | ||
| const lastColumnEmpty = isEmptyColumn(lastColumn); | ||
|
|
||
| if (firstColumnEmpty && lastColumnEmpty) { | ||
| // Removes `columnList` | ||
| tr.delete(columnListPos, columnListPos + columnList.nodeSize); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (firstColumnEmpty) { | ||
| tr.step( | ||
| new ReplaceAroundStep( | ||
| // Replaces `columnList`. | ||
| columnListPos, | ||
| columnListPos + columnList.nodeSize, | ||
| // Replaces with content of last `column`. | ||
| lastColumnAfterPos - lastColumn.nodeSize + 1, | ||
| lastColumnAfterPos - 1, | ||
| // Doesn't append anything. | ||
| Slice.empty, | ||
| 0, | ||
| false, | ||
| ), | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (lastColumnEmpty) { | ||
| tr.step( | ||
| new ReplaceAroundStep( | ||
| // Replaces `columnList`. | ||
| columnListPos, | ||
| columnListPos + columnList.nodeSize, | ||
| // Replaces with content of first `column`. | ||
| firstColumnBeforePos + 1, | ||
| firstColumnBeforePos + firstColumn.nodeSize - 1, | ||
| // Doesn't append anything. | ||
| Slice.empty, | ||
| 0, | ||
| false, | ||
| ), | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.