-
-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3086 from udecode/fix/table-mark
fix: table-marks
- Loading branch information
Showing
5 changed files
with
110 additions
and
1 deletion.
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,5 @@ | ||
--- | ||
"@udecode/slate-utils": patch | ||
--- | ||
|
||
`toggleMark` use `editor.removeMark` |
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,5 @@ | ||
--- | ||
"@udecode/plate-table": patch | ||
--- | ||
|
||
fix add/removeMark behavior When selection is aboving table. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { | ||
getNodeEntries, | ||
isCollapsed, | ||
isText, | ||
PlateEditor, | ||
setNodes, | ||
unsetNodes, | ||
Value, | ||
} from '@udecode/plate-common'; | ||
|
||
import { getTableGridAbove } from './queries'; | ||
|
||
export const withMarkTable = < | ||
V extends Value = Value, | ||
E extends PlateEditor<V> = PlateEditor<V>, | ||
>( | ||
editor: E | ||
) => { | ||
const { addMark, removeMark, getMarks } = editor; | ||
|
||
editor.addMark = (key: string, value: any) => { | ||
const { selection } = editor; | ||
if (!selection || isCollapsed(selection)) return addMark(key, value); | ||
|
||
const matchesCell = getTableGridAbove(editor, { format: 'cell' }); | ||
|
||
if (matchesCell.length === 0) return addMark(key, value); | ||
|
||
matchesCell.forEach(([cell, cellPath]) => { | ||
setNodes( | ||
editor, | ||
//@ts-ignore | ||
{ | ||
[key]: value, | ||
}, | ||
{ | ||
at: cellPath, | ||
match: (n) => isText(n), | ||
split: true, | ||
voids: true, | ||
} | ||
); | ||
}); | ||
}; | ||
|
||
editor.removeMark = (key: string) => { | ||
const { selection } = editor; | ||
if (!selection || isCollapsed(selection)) return removeMark(key); | ||
|
||
const matchesCell = getTableGridAbove(editor, { format: 'cell' }); | ||
|
||
if (matchesCell.length === 0) return removeMark(key); | ||
|
||
matchesCell.forEach(([cell, cellPath]) => { | ||
unsetNodes(editor, key, { | ||
at: cellPath, | ||
match: (n) => isText(n), | ||
split: true, | ||
voids: true, | ||
}); | ||
}); | ||
}; | ||
|
||
editor.getMarks = () => { | ||
const { selection } = editor; | ||
|
||
if (!selection || isCollapsed(selection)) return getMarks(); | ||
|
||
const matchesCell = getTableGridAbove(editor, { format: 'cell' }); | ||
|
||
if (matchesCell.length === 0) return getMarks(); | ||
|
||
const totalMarks: Record<string, any> = {}; | ||
|
||
matchesCell.forEach(([cell, cellPath]) => { | ||
const textNodeEntry = getNodeEntries(editor, { | ||
at: cellPath, | ||
match: (n) => isText(n), | ||
}); | ||
|
||
Array.from(textNodeEntry, (item) => item[0]).forEach((item) => { | ||
const keys = Object.keys(item); | ||
if (keys.length === 1) return; | ||
|
||
keys.splice(keys.indexOf('text'), 1); | ||
|
||
keys.forEach((k) => { | ||
totalMarks[k] = true; | ||
}); | ||
}); | ||
}); | ||
|
||
return totalMarks; | ||
}; | ||
|
||
return editor; | ||
}; |
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