Skip to content

Commit 8d9bf30

Browse files
authored
Location type discriminators (ianstormtaylor#6000)
* implement `Location.isX` functions * added unit tests * changeset * fix Path.isPath to check whole array and add regression test
1 parent 07bbaaf commit 8d9bf30

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+360
-84
lines changed

.changeset/orange-phones-wave.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'slate': minor
3+
'slate-react': patch
4+
'slate-dom': patch
5+
---
6+
7+
Added `Location.isPath`, `Location.isPoint`, `Location.isRange`, and `Location.isSpan` functions, as efficient type discriminators.
8+
Use these instead of `Path.isPath`, `Point.isPoint`, `Range.isRange`, and `Span.isSpan` whenever possible.

packages/slate-dom/src/plugin/with-dom.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
22
BaseEditor,
33
Editor,
4+
Location,
45
Node,
56
Operation,
67
Path,
78
PathRef,
8-
Point,
99
Range,
1010
Transforms,
1111
} from 'slate'
@@ -142,7 +142,7 @@ export const withDOM = <T extends BaseEditor>(
142142

143143
const pendingAction = EDITOR_TO_PENDING_ACTION.get(e)
144144
if (pendingAction?.at) {
145-
const at = Point.isPoint(pendingAction?.at)
145+
const at = Location.isPoint(pendingAction?.at)
146146
? transformPendingPoint(e, pendingAction.at, op)
147147
: transformPendingRange(e, pendingAction.at, op)
148148

packages/slate-react/src/hooks/android-input-manager/android-input-manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DebouncedFunc } from 'lodash'
2-
import { Editor, Node, Path, Point, Range, Transforms } from 'slate'
2+
import { Editor, Location, Node, Path, Point, Range, Transforms } from 'slate'
33
import { ReactEditor } from '../../plugin/react-editor'
44
import {
55
applyStringDiff,
@@ -106,7 +106,7 @@ export function createAndroidInputManager({
106106
}
107107

108108
if (action.at) {
109-
const target = Point.isPoint(action.at)
109+
const target = Location.isPoint(action.at)
110110
? normalizePoint(editor, action.at)
111111
: normalizeRange(editor, action.at)
112112

packages/slate/src/editor/above.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Editor, EditorInterface } from '../interfaces/editor'
2-
import { Range } from '../interfaces'
2+
import { Location } from '../interfaces'
33
import { Path } from '../interfaces/path'
44

55
export const above: EditorInterface['above'] = (editor, options = {}) => {
@@ -18,7 +18,7 @@ export const above: EditorInterface['above'] = (editor, options = {}) => {
1818

1919
// If `at` is a Range that spans mulitple nodes, `path` will be their common ancestor.
2020
// Otherwise `path` will be a text node and/or the same as `at`, in which cases we want to start with its parent.
21-
if (!Range.isRange(at) || Path.equals(at.focus.path, at.anchor.path)) {
21+
if (!Location.isRange(at) || Path.equals(at.focus.path, at.anchor.path)) {
2222
if (path.length === 0) return
2323
path = Path.parent(path)
2424
}

packages/slate/src/editor/next.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Editor, EditorInterface } from '../interfaces/editor'
2-
import { Span } from '../interfaces/location'
3-
import { Path } from '../interfaces/path'
2+
import { Location, Span } from '../interfaces/location'
43

54
export const next: EditorInterface['next'] = (editor, options = {}) => {
65
const { mode = 'lowest', voids = false } = options
@@ -18,12 +17,12 @@ export const next: EditorInterface['next'] = (editor, options = {}) => {
1817

1918
const span: Span = [pointAfterLocation.path, to]
2019

21-
if (Path.isPath(at) && at.length === 0) {
20+
if (Location.isPath(at) && at.length === 0) {
2221
throw new Error(`Cannot get the next node from the root node!`)
2322
}
2423

2524
if (match == null) {
26-
if (Path.isPath(at)) {
25+
if (Location.isPath(at)) {
2726
const [parent] = Editor.parent(editor, at)
2827
match = n => parent.children.includes(n)
2928
} else {

packages/slate/src/editor/nodes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Node, NodeEntry } from '../interfaces/node'
22
import { Editor, EditorNodesOptions } from '../interfaces/editor'
3-
import { Span } from '../interfaces/location'
3+
import { Location } from '../interfaces/location'
44
import { Path } from '../interfaces/path'
55

66
export function* nodes<T extends Node>(
@@ -28,7 +28,7 @@ export function* nodes<T extends Node>(
2828
let from
2929
let to
3030

31-
if (Span.isSpan(at)) {
31+
if (Location.isSpan(at)) {
3232
from = at[0]
3333
to = at[1]
3434
} else {

packages/slate/src/editor/path.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { EditorInterface, Node, Path, Point, Range } from '../interfaces'
1+
import { EditorInterface, Location, Node, Path, Range } from '../interfaces'
22

33
export const path: EditorInterface['path'] = (editor, at, options = {}) => {
44
const { depth, edge } = options
55

6-
if (Path.isPath(at)) {
6+
if (Location.isPath(at)) {
77
if (edge === 'start') {
88
const [, firstPath] = Node.first(editor, at)
99
at = firstPath
@@ -13,7 +13,7 @@ export const path: EditorInterface['path'] = (editor, at, options = {}) => {
1313
}
1414
}
1515

16-
if (Range.isRange(at)) {
16+
if (Location.isRange(at)) {
1717
if (edge === 'start') {
1818
at = Range.start(at)
1919
} else if (edge === 'end') {
@@ -23,7 +23,7 @@ export const path: EditorInterface['path'] = (editor, at, options = {}) => {
2323
}
2424
}
2525

26-
if (Point.isPoint(at)) {
26+
if (Location.isPoint(at)) {
2727
at = at.path
2828
}
2929

packages/slate/src/editor/point.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { EditorInterface } from '../interfaces/editor'
2-
import { Path } from '../interfaces/path'
32
import { Node } from '../interfaces/node'
43
import { Range } from '../interfaces/range'
4+
import { Location } from '../interfaces'
55

66
export const point: EditorInterface['point'] = (editor, at, options = {}) => {
77
const { edge = 'start' } = options
88

9-
if (Path.isPath(at)) {
9+
if (Location.isPath(at)) {
1010
let path
1111

1212
if (edge === 'end') {
@@ -28,7 +28,7 @@ export const point: EditorInterface['point'] = (editor, at, options = {}) => {
2828
return { path, offset: edge === 'end' ? node.text.length : 0 }
2929
}
3030

31-
if (Range.isRange(at)) {
31+
if (Location.isRange(at)) {
3232
const [start, end] = Range.edges(at)
3333
return edge === 'start' ? start : end
3434
}

packages/slate/src/editor/previous.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Editor, EditorInterface } from '../interfaces/editor'
2-
import { Span } from '../interfaces/location'
3-
import { Path } from '../interfaces/path'
2+
import { Location, Span } from '../interfaces/location'
43

54
export const previous: EditorInterface['previous'] = (editor, options = {}) => {
65
const { mode = 'lowest', voids = false } = options
@@ -22,12 +21,12 @@ export const previous: EditorInterface['previous'] = (editor, options = {}) => {
2221
// the point before the location passed in
2322
const span: Span = [pointBeforeLocation.path, to]
2423

25-
if (Path.isPath(at) && at.length === 0) {
24+
if (Location.isPath(at) && at.length === 0) {
2625
throw new Error(`Cannot get the previous node from the root node!`)
2726
}
2827

2928
if (match == null) {
30-
if (Path.isPath(at)) {
29+
if (Location.isPath(at)) {
3130
const [parent] = Editor.parent(editor, at)
3231
match = n => parent.children.includes(n)
3332
} else {

packages/slate/src/editor/range.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { Location } from '../interfaces'
12
import { Editor, EditorInterface } from '../interfaces/editor'
2-
import { Range } from '../interfaces/range'
33

44
export const range: EditorInterface['range'] = (editor, at, to) => {
5-
if (Range.isRange(at) && !to) {
5+
if (Location.isRange(at) && !to) {
66
return at
77
}
88

0 commit comments

Comments
 (0)