Skip to content
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

Fix contenteditalbe firefox table selection #5491

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-falcons-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Fix firefox table selection if table is contentedtiable
68 changes: 59 additions & 9 deletions packages/slate-react/src/plugin/react-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,16 +836,66 @@ export const ReactEditor: ReactEditorInterface = {
const firstRange = domRange.getRangeAt(0)
const lastRange = domRange.getRangeAt(domRange.rangeCount - 1)

// Right to left
if (firstRange.startContainer === focusNode) {
anchorNode = lastRange.endContainer
anchorOffset = lastRange.endOffset
focusOffset = firstRange.startOffset
// Here we are in the contenteditable mode of a table in firefox
if (
focusNode instanceof HTMLTableRowElement &&
firstRange.startContainer instanceof HTMLTableRowElement &&
lastRange.startContainer instanceof HTMLTableRowElement
) {
// HTMLElement, becouse Element is a slate element
function getLastChildren(element: HTMLElement): HTMLElement {
if (element.childElementCount > 0) {
return getLastChildren(<HTMLElement>element.children[0])
} else {
return element
}
}

const firstNodeRow = <HTMLTableRowElement>firstRange.startContainer
const lastNodeRow = <HTMLTableRowElement>lastRange.startContainer

// This should never fail as "The HTMLElement interface represents any HTML element."
const firstNode = getLastChildren(
<HTMLElement>firstNodeRow.children[firstRange.startOffset]
)
const lastNode = getLastChildren(
<HTMLElement>lastNodeRow.children[lastRange.startOffset]
)

// Zero, as we allways take the right one as the anchor point
focusOffset = 0

if (lastNode.childNodes.length > 0) {
anchorNode = lastNode.childNodes[0]
} else {
anchorNode = lastNode
}

if (firstNode.childNodes.length > 0) {
focusNode = firstNode.childNodes[0]
} else {
focusNode = firstNode
}

if (lastNode instanceof HTMLElement) {
anchorOffset = (<HTMLElement>lastNode).innerHTML.length
} else {
// Fallback option
anchorOffset = 0
}
} else {
// Left to right
anchorNode = firstRange.startContainer
anchorOffset = firstRange.endOffset
focusOffset = lastRange.startOffset
// This is the read only mode of a firefox table
// Right to left
if (firstRange.startContainer === focusNode) {
anchorNode = lastRange.endContainer
anchorOffset = lastRange.endOffset
focusOffset = firstRange.startOffset
} else {
// Left to right
anchorNode = firstRange.startContainer
anchorOffset = firstRange.endOffset
focusOffset = lastRange.startOffset
}
}
} else {
anchorNode = domRange.anchorNode
Expand Down
Loading