1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-25 16:20:49 +02:00

Refactor text decorations method (#5033)

* refactor text decorations method

* rename begin to start
This commit is contained in:
Kris Kalavantavanich
2022-07-04 17:07:24 +07:00
committed by GitHub
parent 736662f808
commit 82d20b58a3

View File

@@ -106,15 +106,17 @@ export const Text: TextInterface = {
const { anchor, focus, ...rest } = dec const { anchor, focus, ...rest } = dec
const [start, end] = Range.edges(dec) const [start, end] = Range.edges(dec)
const next = [] const next = []
let o = 0 let leafEnd = 0
const decorationStart = start.offset
const decorationEnd = end.offset
for (const leaf of leaves) { for (const leaf of leaves) {
const { length } = leaf.text const { length } = leaf.text
const offset = o const leafStart = leafEnd
o += length leafEnd += length
// If the range encompases the entire leaf, add the range. // If the range encompasses the entire leaf, add the range.
if (start.offset <= offset && end.offset >= o) { if (decorationStart <= leafStart && leafEnd <= decorationEnd) {
Object.assign(leaf, rest) Object.assign(leaf, rest)
next.push(leaf) next.push(leaf)
continue continue
@@ -122,11 +124,11 @@ export const Text: TextInterface = {
// If the range expanded and match the leaf, or starts after, or ends before it, continue. // If the range expanded and match the leaf, or starts after, or ends before it, continue.
if ( if (
(start.offset !== end.offset && (decorationStart !== decorationEnd &&
(start.offset === o || end.offset === offset)) || (decorationStart === leafEnd || decorationEnd === leafStart)) ||
start.offset > o || decorationStart > leafEnd ||
end.offset < offset || decorationEnd < leafStart ||
(end.offset === offset && offset !== 0) (decorationEnd === leafStart && leafStart !== 0)
) { ) {
next.push(leaf) next.push(leaf)
continue continue
@@ -139,14 +141,14 @@ export const Text: TextInterface = {
let before let before
let after let after
if (end.offset < o) { if (decorationEnd < leafEnd) {
const off = end.offset - offset const off = decorationEnd - leafStart
after = { ...middle, text: middle.text.slice(off) } after = { ...middle, text: middle.text.slice(off) }
middle = { ...middle, text: middle.text.slice(0, off) } middle = { ...middle, text: middle.text.slice(0, off) }
} }
if (start.offset > offset) { if (decorationStart > leafStart) {
const off = start.offset - offset const off = decorationStart - leafStart
before = { ...middle, text: middle.text.slice(0, off) } before = { ...middle, text: middle.text.slice(0, off) }
middle = { ...middle, text: middle.text.slice(off) } middle = { ...middle, text: middle.text.slice(off) }
} }