-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optional method to decoration object (#5776)
- Loading branch information
Showing
9 changed files
with
101 additions
and
19 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,5 @@ | ||
--- | ||
'slate': minor | ||
--- | ||
|
||
Add `merge` optional function to decorations and change related type signatures to `DecoratedRange`. Now developers can specify how two decoration object with the same key but different value are merged together if they overlap" |
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
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
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { createContext, useContext } from 'react' | ||
import { Range, NodeEntry } from 'slate' | ||
import { DecoratedRange, NodeEntry } from 'slate' | ||
|
||
/** | ||
* A React context for sharing the `decorate` prop of the editable. | ||
*/ | ||
|
||
export const DecorateContext = createContext<(entry: NodeEntry) => Range[]>( | ||
() => [] | ||
) | ||
export const DecorateContext = createContext< | ||
(entry: NodeEntry) => DecoratedRange[] | ||
>(() => []) | ||
|
||
/** | ||
* Get the current `decorate` prop of the editable. | ||
*/ | ||
|
||
export const useDecorate = (): ((entry: NodeEntry) => Range[]) => { | ||
export const useDecorate = (): ((entry: NodeEntry) => DecoratedRange[]) => { | ||
return useContext(DecorateContext) | ||
} |
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,54 @@ | ||
import { Text } from 'slate' | ||
|
||
const merge = (leaf: Text, dec: { decoration: number[] }) => { | ||
const { decoration, ...rest } = dec | ||
leaf.decoration = [...(leaf.decoration ?? []), ...decoration] | ||
Object.assign(leaf, rest) | ||
} | ||
|
||
export const input = [ | ||
{ | ||
anchor: { | ||
path: [0], | ||
offset: 0, | ||
}, | ||
focus: { | ||
path: [0], | ||
offset: 2, | ||
}, | ||
merge, | ||
decoration: [1, 2, 3], | ||
}, | ||
{ | ||
anchor: { | ||
path: [0], | ||
offset: 1, | ||
}, | ||
focus: { | ||
path: [0], | ||
offset: 3, | ||
}, | ||
merge, | ||
decoration: [4, 5, 6], | ||
}, | ||
] | ||
export const test = decorations => { | ||
return Text.decorations({ text: 'abc', mark: 'mark' }, decorations) | ||
} | ||
export const output = [ | ||
{ | ||
text: 'a', | ||
mark: 'mark', | ||
decoration: [1, 2, 3], | ||
}, | ||
{ | ||
text: 'b', | ||
mark: 'mark', | ||
decoration: [1, 2, 3, 4, 5, 6], | ||
}, | ||
{ | ||
text: 'c', | ||
mark: 'mark', | ||
decoration: [4, 5, 6], | ||
}, | ||
] |