mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 10:51:44 +02:00
transform.delete and transform.insertFragment performance optimize (#5137)
* feat: transform.delete and transform.insertFragment performance optimize * feat: add changeset * feat: optimize code Co-authored-by: mainhanu <chijun89@gmail.com>
This commit is contained in:
5
.changeset/new-needles-itch.md
Normal file
5
.changeset/new-needles-itch.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
transform.delete and transform.insertFragment performance optimize
|
@@ -1,4 +1,3 @@
|
|||||||
import { produce } from 'immer'
|
|
||||||
import { Operation } from '..'
|
import { Operation } from '..'
|
||||||
import { TextDirection } from './types'
|
import { TextDirection } from './types'
|
||||||
|
|
||||||
@@ -373,16 +372,15 @@ export const Path: PathInterface = {
|
|||||||
operation: Operation,
|
operation: Operation,
|
||||||
options: PathTransformOptions = {}
|
options: PathTransformOptions = {}
|
||||||
): Path | null {
|
): Path | null {
|
||||||
return produce(path, p => {
|
if (!path) return null
|
||||||
|
|
||||||
|
// PERF: use destructing instead of immer
|
||||||
|
const p = [...path]
|
||||||
const { affinity = 'forward' } = options
|
const { affinity = 'forward' } = options
|
||||||
|
|
||||||
// PERF: Exit early if the operation is guaranteed not to have an effect.
|
// PERF: Exit early if the operation is guaranteed not to have an effect.
|
||||||
if (!path || path?.length === 0) {
|
if (path.length === 0) {
|
||||||
return
|
return p
|
||||||
}
|
|
||||||
|
|
||||||
if (p === null) {
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (operation.type) {
|
switch (operation.type) {
|
||||||
@@ -451,7 +449,7 @@ export const Path: PathInterface = {
|
|||||||
|
|
||||||
// If the old and new path are the same, it's a no-op.
|
// If the old and new path are the same, it's a no-op.
|
||||||
if (Path.equals(op, onp)) {
|
if (Path.equals(op, onp)) {
|
||||||
return
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Path.isAncestor(op, p) || Path.equals(op, p)) {
|
if (Path.isAncestor(op, p) || Path.equals(op, p)) {
|
||||||
@@ -492,6 +490,7 @@ export const Path: PathInterface = {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
return p
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -187,10 +187,11 @@ export const TextTransforms: TextTransforms = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const pathRef of pathRefs) {
|
pathRefs
|
||||||
const path = pathRef.unref()!
|
.reverse()
|
||||||
Transforms.removeNodes(editor, { at: path, voids })
|
.map(r => r.unref())
|
||||||
}
|
.filter((r): r is Path => r !== null)
|
||||||
|
.forEach(p => Transforms.removeNodes(editor, { at: p, voids }))
|
||||||
|
|
||||||
if (!endVoid) {
|
if (!endVoid) {
|
||||||
const point = endRef.current!
|
const point = endRef.current!
|
||||||
|
Reference in New Issue
Block a user