-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to select all (Ctrl-A, Command-A) with tables in certain conditions. Due to a tiptap/ProseMirror bug tables with some empty cells and no content preceding or following the table, selecting the whole table did not work. This fix makes sure that a non-empty paragraph is added at the end, if no other content is present at the end of the document. Follow-up from: - #61 More information here: - ueberdosis/tiptap#2401 - ueberdosis/tiptap#3651
- Loading branch information
Showing
3 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,65 @@ | ||
import { Node, mergeAttributes } from "@tiptap/core"; | ||
import { Plugin } from "prosemirror-state"; | ||
|
||
export const factory = () => { | ||
return Node.create({ | ||
name: "fixed-paragraph", | ||
|
||
addOptions() { | ||
return { | ||
HTMLAttributes: { | ||
class: "fixed-paragraph", | ||
}, | ||
}; | ||
}, | ||
|
||
group: "block", | ||
isolating: true, | ||
selectable: false, | ||
|
||
parseHTML() { | ||
return [ | ||
{ | ||
tag: "p", | ||
getAttrs: (node) => node.matches(".fixed-paragraph") && null, | ||
}, | ||
]; | ||
}, | ||
|
||
renderHTML({ HTMLAttributes }) { | ||
return [ | ||
"p", | ||
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), | ||
" ", // Add a space to the end of the paragraph to make it selectable | ||
]; | ||
}, | ||
|
||
addProseMirrorPlugins() { | ||
return [ | ||
new Plugin({ | ||
filterTransaction(transaction, state) { | ||
let result = true; // true for keep, false for stop transaction | ||
const replaceSteps = []; | ||
transaction.steps.forEach((step, index) => { | ||
if (step.jsonID === "replace") { | ||
replaceSteps.push(index); | ||
} | ||
}); | ||
|
||
replaceSteps.forEach((index) => { | ||
const map = transaction.mapping.maps[index]; | ||
const oldStart = map.ranges[0]; | ||
const oldEnd = map.ranges[0] + map.ranges[1]; | ||
state.doc.nodesBetween(oldStart, oldEnd, (node) => { | ||
if (node.type.name === "fixed-paragraph") { | ||
result = false; | ||
} | ||
}); | ||
}); | ||
return result; | ||
}, | ||
}), | ||
]; | ||
}, | ||
}); | ||
}; |
This file contains 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 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