Skip to content

Commit 986047d

Browse files
committed
refactor
1 parent e7feb10 commit 986047d

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

.changeset/slate-react.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'slate-react': minor
33
---
44

5-
- Update `RenderLeafProps` to use the new `Leaf` type from `slate`, that includes `start` and `end` properties representing the offset range within the original `Text` node.
5+
- Update `RenderLeafProps` to receive the `Leaf` type from `slate`, which may include an optional nested `position: { start, end, isFirst, isLast }` property if the text node was split by decorations. Useful to render something only in a single leaf per text node.

.changeset/slate.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
'slate': minor
33
---
44

5-
- Update `Text.decorations` to return `Leaf[]` instead of `Text[]`.
6-
- Add new `Leaf` type, which includes `start` and `end` properties representing the offset range within the original `Text` node.
5+
- Update `Text.decorations` to return `Leaf[]`.
6+
- Add `position?: { start, end, isFirst, isLast }` property to `Leaf` type.
7+
- The `position` property is only added if decorations cause the text node to be split into multiple leaves.

packages/slate/src/interfaces/text.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ export interface BaseText {
1515

1616
export type Text = ExtendedType<'Text', BaseText>
1717

18-
export type Leaf = Text & { start: number; end: number }
18+
export type Leaf = Text & {
19+
/**
20+
* The position of the leaf in the original text node, defined only if the text node was split by decorations.
21+
*/
22+
position?: {
23+
start: number
24+
end: number
25+
isFirst: boolean
26+
isLast: boolean
27+
}
28+
}
1929

2030
export interface TextEqualsOptions {
2131
loose?: boolean
@@ -114,13 +124,7 @@ export const Text: TextInterface = {
114124
},
115125

116126
decorations(node: Text, decorations: DecoratedRange[]): Leaf[] {
117-
let leaves: Leaf[] = [
118-
{
119-
...node,
120-
start: 0,
121-
end: node.text.length,
122-
},
123-
]
127+
let leaves: Leaf[] = [{ ...node }]
124128

125129
for (const dec of decorations) {
126130
const { anchor, focus, merge: mergeDecoration, ...rest } = dec
@@ -190,14 +194,19 @@ export const Text: TextInterface = {
190194
leaves = next
191195
}
192196

193-
let currentOffset = 0
194-
for (const leaf of leaves) {
195-
const start = currentOffset
196-
const end = start + leaf.text.length
197-
198-
leaf.start = start
199-
leaf.end = end
200-
currentOffset = end
197+
if (leaves.length > 1) {
198+
let currentOffset = 0
199+
for (const [index, leaf] of leaves.entries()) {
200+
const start = currentOffset
201+
const end = start + leaf.text.length
202+
leaf.position = {
203+
start,
204+
end,
205+
isFirst: index === 0,
206+
isLast: index === leaves.length - 1,
207+
}
208+
currentOffset = end
209+
}
201210
}
202211

203212
return leaves

0 commit comments

Comments
 (0)