-
-
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.
- Loading branch information
1 parent
2ac69c2
commit 5751ce4
Showing
3 changed files
with
100 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
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