Skip to content

Commit

Permalink
Avoid the loop entirely if op doesn't affect path
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-codaio committed Dec 16, 2021
1 parent 6d952a7 commit 1d22cb8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
26 changes: 15 additions & 11 deletions packages/slate/src/create-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Text,
Transforms,
} from './'
import { DIRTY_PATHS, FLUSHING } from './utils/weak-maps'
import { DIRTY_PATHS, DIRTY_PATH_KEYS, FLUSHING } from './utils/weak-maps'

/**
* Create a new Slate `Editor` object.
Expand Down Expand Up @@ -42,29 +42,32 @@ export const createEditor = (): Editor => {
RangeRef.transform(ref, op)
}

const set = new Set()
const dirtyPaths: Path[] = []
const oldDirtyPaths = DIRTY_PATHS.get(editor) || []
const oldDirtyPathKeys = DIRTY_PATH_KEYS.get(editor) || new Set()
let dirtyPaths: Path[]
let dirtyPathKeys: Set<string>

const add = (path: Path | null) => {
if (path) {
const key = path.join(',')

if (!set.has(key)) {
set.add(key)
if (!dirtyPathKeys.has(key)) {
dirtyPathKeys.add(key)
dirtyPaths.push(path)
}
}
}

const oldDirtyPaths = DIRTY_PATHS.get(editor) || []
const canTransformPath = Path.operationCanTransformPath(op)
for (const path of oldDirtyPaths) {
if (canTransformPath) {
if (Path.operationCanTransformPath(op)) {
dirtyPaths = []
dirtyPathKeys = new Set()
for (const path of oldDirtyPaths) {
const newPath = Path.transform(path, op)
add(newPath)
} else {
add(path)
}
} else {
dirtyPaths = oldDirtyPaths
dirtyPathKeys = oldDirtyPathKeys
}

const newDirtyPaths = getDirtyPaths(op)
Expand All @@ -73,6 +76,7 @@ export const createEditor = (): Editor => {
}

DIRTY_PATHS.set(editor, dirtyPaths)
DIRTY_PATH_KEYS.set(editor, dirtyPathKeys)
Transforms.transform(editor, op)
editor.operations.push(op)
Editor.normalize(editor)
Expand Down
16 changes: 15 additions & 1 deletion packages/slate/src/interfaces/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '..'
import {
DIRTY_PATHS,
DIRTY_PATH_KEYS,
NORMALIZING,
PATH_REFS,
POINT_REFS,
Expand Down Expand Up @@ -979,13 +980,26 @@ export const Editor: EditorInterface = {
return DIRTY_PATHS.get(editor) || []
}

const getDirtyPathKeys = (editor: Editor) => {
return DIRTY_PATH_KEYS.get(editor) || new Set()
}

const popDirtyPath = (editor: Editor): Path => {
const path = getDirtyPaths(editor).pop()!
const key = path.join(',')
getDirtyPathKeys(editor).delete(key)
return path
}

if (!Editor.isNormalizing(editor)) {
return
}

if (force) {
const allPaths = Array.from(Node.nodes(editor), ([, p]) => p)
const allPathKeys = new Set(allPaths.map(p => p.join(',')))
DIRTY_PATHS.set(editor, allPaths)
DIRTY_PATH_KEYS.set(editor, allPathKeys)
}

if (getDirtyPaths(editor).length === 0) {
Expand Down Expand Up @@ -1026,7 +1040,7 @@ export const Editor: EditorInterface = {
`)
}

const dirtyPath = getDirtyPaths(editor).pop()!
const dirtyPath = popDirtyPath(editor)

// If the node doesn't exist in the tree, it does not need to be normalized.
if (Node.has(editor, dirtyPath)) {
Expand Down
1 change: 1 addition & 0 deletions packages/slate/src/utils/weak-maps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Editor, Path, PathRef, PointRef, RangeRef } from '..'

export const DIRTY_PATHS: WeakMap<Editor, Path[]> = new WeakMap()
export const DIRTY_PATH_KEYS: WeakMap<Editor, Set<string>> = new WeakMap()
export const FLUSHING: WeakMap<Editor, boolean> = new WeakMap()
export const NORMALIZING: WeakMap<Editor, boolean> = new WeakMap()
export const PATH_REFS: WeakMap<Editor, Set<PathRef>> = new WeakMap()
Expand Down

0 comments on commit 1d22cb8

Please sign in to comment.