Repository navigation
fix(core): normalize imported tables before block conversion #3294
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4a53da4
fix(core): normalize imported tables before block conversion
nperez0111 16a7cc0
fix(core): align imported table widths with normalized spans
nperez0111 98cd836
fix(core): limit imported table headers and simplify tests
nperez0111 af4715e
docs(core): document HTML import normalization
nperez0111 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
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
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,44 @@ | ||
| import { Node } from "prosemirror-model"; | ||
| import { EditorState, Transaction } from "prosemirror-state"; | ||
| import { fixTables, TableMap } from "prosemirror-tables"; | ||
|
|
||
| /** Repair imported ProseMirror tables before projecting them to BlockNote JSON. */ | ||
| export function normalizeImportedTables(doc: Node): Node { | ||
| let state: EditorState | undefined; | ||
| let transaction: Transaction | undefined; | ||
| doc.descendants((node, pos) => { | ||
| if (node.type.spec.tableRole !== "table") { | ||
| return; | ||
| } | ||
| const map = TableMap.get(node); | ||
| if (map.problems) { | ||
| state ??= EditorState.create({ doc }); | ||
| transaction ??= state.tr; | ||
| // Keep a wholly empty table editable. fixTables would delete the table | ||
| // and leave its enclosing BlockNote block container without content. | ||
| if (map.width === 0) { | ||
| const { tableRow, tableCell } = doc.type.schema.nodes; | ||
| transaction.replaceWith( | ||
| transaction.mapping.map(pos), | ||
| transaction.mapping.map(pos + node.nodeSize), | ||
| node.type.create( | ||
| node.attrs, | ||
| tableRow.create(null, tableCell.createAndFill()), | ||
| node.marks, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
| return false; | ||
| }); | ||
| if (!state || !transaction) { | ||
| return doc; | ||
| } | ||
| state = state.apply(transaction); | ||
| // Collision repair can expose missing cells: one upstream pass is not | ||
| // always sufficient. This state has no live editor plugins or side effects. | ||
| for (let repair = fixTables(state); repair; repair = fixTables(state)) { | ||
| state = state.apply(repair); | ||
| } | ||
| return state.doc; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 4737
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 12006
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 365
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 9252
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 3060
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 7194
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 6147
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 9189
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 5683
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 4954
Trim
colwidthto the parsedcolspanin both cell parsers.When
colspanis clamped, for example fromcolspan="0", the cell keeps everycolwidthentry.nodeToBlockadds all of them tocolumnWidthsfor first-row cells, so later columns get the wrong widths. Trimcolwidthto the parsed span in both the header and cell parsers.Suggested fix
@@ TiptapTableHeader addAttributes colwidth parseHTML (block.ts ~line 87) const colwidth = element.getAttribute("colwidth"); const value = colwidth - ? colwidth.split(",").map((width) => parseInt(width, 10)) + ? colwidth + .split(",") + .slice(0, parseTableSpan(element, "colspan")) + .map((width) => parseInt(width, 10)) : null; @@ TiptapTableCell addAttributes colwidth parseHTML (block.ts ~line 152) const colwidth = element.getAttribute("colwidth"); const value = colwidth - ? colwidth.split(",").map((width) => parseInt(width, 10)) + ? colwidth + .split(",") + .slice(0, parseTableSpan(element, "colspan")) + .map((width) => parseInt(width, 10)) : null;