mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 06:31:28 +02:00
remove unnecessary helpers (#3289)
This commit is contained in:
@@ -55,31 +55,6 @@ export const LocationQueries = {
|
|||||||
return target
|
return target
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the common ancestor node of a location.
|
|
||||||
*/
|
|
||||||
|
|
||||||
ancestor(
|
|
||||||
editor: Editor,
|
|
||||||
at: Location,
|
|
||||||
options: {
|
|
||||||
depth?: number
|
|
||||||
edge?: 'start' | 'end'
|
|
||||||
} = {}
|
|
||||||
): AncestorEntry {
|
|
||||||
if (Path.isPath(at) || Point.isPoint(at)) {
|
|
||||||
return Editor.parent(editor, at, options)
|
|
||||||
}
|
|
||||||
|
|
||||||
const path = Editor.path(editor, at, options)
|
|
||||||
const ancestorPath = Path.equals(at.anchor.path, at.focus.path)
|
|
||||||
? Path.parent(path)
|
|
||||||
: path
|
|
||||||
|
|
||||||
const ancestor = Node.get(editor, ancestorPath) as Ancestor
|
|
||||||
return [ancestor, ancestorPath]
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the point before a location.
|
* Get the point before a location.
|
||||||
*/
|
*/
|
||||||
@@ -174,20 +149,6 @@ export const LocationQueries = {
|
|||||||
return fragment
|
return fragment
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a point the start point of a location.
|
|
||||||
*/
|
|
||||||
|
|
||||||
isStart(editor: Editor, point: Point, at: Location): boolean {
|
|
||||||
// PERF: If the offset isn't `0` we know it's not the start.
|
|
||||||
if (point.offset !== 0) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const start = Editor.start(editor, at)
|
|
||||||
return Point.equals(point, start)
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a point is the end point of a location.
|
* Check if a point is the end point of a location.
|
||||||
*/
|
*/
|
||||||
@@ -205,6 +166,20 @@ export const LocationQueries = {
|
|||||||
return Editor.isStart(editor, point, at) || Editor.isEnd(editor, point, at)
|
return Editor.isStart(editor, point, at) || Editor.isEnd(editor, point, at)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a point is the start point of a location.
|
||||||
|
*/
|
||||||
|
|
||||||
|
isStart(editor: Editor, point: Point, at: Location): boolean {
|
||||||
|
// PERF: If the offset isn't `0` we know it's not the start.
|
||||||
|
if (point.offset !== 0) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const start = Editor.start(editor, at)
|
||||||
|
return Point.equals(point, start)
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the last node at a location.
|
* Get the last node at a location.
|
||||||
*/
|
*/
|
||||||
|
@@ -244,7 +244,7 @@ export const NodeTransforms = {
|
|||||||
|
|
||||||
// Determine if the merge will leave an ancestor of the path empty as a
|
// Determine if the merge will leave an ancestor of the path empty as a
|
||||||
// result, in which case we'll want to remove it after merging.
|
// result, in which case we'll want to remove it after merging.
|
||||||
const emptyAncestor = Node.furthest(editor, path, ([n, p]) => {
|
const emptyAncestor = Editor.match(editor, path, ([n, p]) => {
|
||||||
return (
|
return (
|
||||||
Path.isDescendant(p, commonPath) &&
|
Path.isDescendant(p, commonPath) &&
|
||||||
Path.isAncestor(p, path) &&
|
Path.isAncestor(p, path) &&
|
||||||
|
@@ -9,13 +9,6 @@ import { Editor, Element, ElementEntry, Path, Range, Text, TextEntry } from '..'
|
|||||||
export type Node = Editor | Element | Text
|
export type Node = Editor | Element | Text
|
||||||
|
|
||||||
export const Node = {
|
export const Node = {
|
||||||
matches(node: Node, props: Partial<Node>): boolean {
|
|
||||||
return (
|
|
||||||
(Element.isElement(node) && Element.matches(node, props)) ||
|
|
||||||
(Text.isText(node) && Text.matches(node, props))
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the node at a specific path, asserting that it's an ancestor node.
|
* Get the node at a specific path, asserting that it's an ancestor node.
|
||||||
*/
|
*/
|
||||||
@@ -101,22 +94,6 @@ export const Node = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Find the closest matching node entry starting from a specific path.
|
|
||||||
*/
|
|
||||||
|
|
||||||
closest(
|
|
||||||
root: Node,
|
|
||||||
path: Path,
|
|
||||||
predicate: (entry: NodeEntry) => boolean
|
|
||||||
): NodeEntry | undefined {
|
|
||||||
for (const entry of Node.levels(root, path, { reverse: true })) {
|
|
||||||
if (predicate(entry)) {
|
|
||||||
return entry
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an entry for the common ancesetor node of two paths.
|
* Get an entry for the common ancesetor node of two paths.
|
||||||
*/
|
*/
|
||||||
@@ -251,22 +228,6 @@ export const Node = {
|
|||||||
return newRoot.children
|
return newRoot.children
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Find the furthest matching node entry starting from a specific path.
|
|
||||||
*/
|
|
||||||
|
|
||||||
furthest(
|
|
||||||
root: Node,
|
|
||||||
path: Path,
|
|
||||||
predicate: (entry: NodeEntry) => boolean
|
|
||||||
): NodeEntry | undefined {
|
|
||||||
for (const entry of Node.levels(root, path)) {
|
|
||||||
if (predicate(entry)) {
|
|
||||||
return entry
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the descendant node referred to by a specific path. If the path is an
|
* Get the descendant node referred to by a specific path. If the path is an
|
||||||
* empty array, it refers to the root node itself.
|
* empty array, it refers to the root node itself.
|
||||||
@@ -387,6 +348,17 @@ export const Node = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a node matches a set of props.
|
||||||
|
*/
|
||||||
|
|
||||||
|
matches(node: Node, props: Partial<Node>): boolean {
|
||||||
|
return (
|
||||||
|
(Element.isElement(node) && Element.matches(node, props)) ||
|
||||||
|
(Text.isText(node) && Text.matches(node, props))
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an iterable of all the node entries of a root node. Each entry is
|
* Return an iterable of all the node entries of a root node. Each entry is
|
||||||
* returned as a `[Node, Path]` tuple, with the path referring to the node's
|
* returned as a `[Node, Path]` tuple, with the path referring to the node's
|
||||||
|
@@ -53,26 +53,6 @@ export const Range = {
|
|||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a range exists in a list or map of ranges.
|
|
||||||
*/
|
|
||||||
|
|
||||||
exists(range: Range, target: Range[] | Record<string, Range>): boolean {
|
|
||||||
if (Range.isRangeList(target)) {
|
|
||||||
return !!target.find(r => Range.equals(r, range))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Range.isRangeMap(target)) {
|
|
||||||
for (const key in target) {
|
|
||||||
if (Range.equals(range, target[key])) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a range includes a path, a point or part of another range.
|
* Check if a range includes a path, a point or part of another range.
|
||||||
*/
|
*/
|
||||||
@@ -176,32 +156,6 @@ export const Range = {
|
|||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a value is an array of `Range` objects.
|
|
||||||
*/
|
|
||||||
|
|
||||||
isRangeList(value: any): value is Range[] {
|
|
||||||
return (
|
|
||||||
Array.isArray(value) && (value.length === 0 || Range.isRange(value[0]))
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a value is a map of `Range` objects.
|
|
||||||
*/
|
|
||||||
|
|
||||||
isRangeMap(value: any): value is Record<string, Range> {
|
|
||||||
if (!isPlainObject(value)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const key in value) {
|
|
||||||
return Range.isRange(value[key])
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterate through all of the point entries in a range.
|
* Iterate through all of the point entries in a range.
|
||||||
*/
|
*/
|
||||||
|
@@ -1,25 +0,0 @@
|
|||||||
/** @jsx jsx */
|
|
||||||
|
|
||||||
import { Element, Node } from 'slate'
|
|
||||||
import { jsx } from 'slate-hyperscript'
|
|
||||||
|
|
||||||
export const input = (
|
|
||||||
<editor>
|
|
||||||
<element key="a">
|
|
||||||
<element key="b">
|
|
||||||
<text />
|
|
||||||
</element>
|
|
||||||
</element>
|
|
||||||
</editor>
|
|
||||||
)
|
|
||||||
|
|
||||||
export const test = value => {
|
|
||||||
return Node.closest(value, [0, 0, 0], ([e]) => Element.isElement(e))
|
|
||||||
}
|
|
||||||
|
|
||||||
export const output = [
|
|
||||||
<element key="b">
|
|
||||||
<text />
|
|
||||||
</element>,
|
|
||||||
[0, 0],
|
|
||||||
]
|
|
@@ -1,27 +0,0 @@
|
|||||||
/** @jsx jsx */
|
|
||||||
|
|
||||||
import { Element, Node } from 'slate'
|
|
||||||
import { jsx } from 'slate-hyperscript'
|
|
||||||
|
|
||||||
export const input = (
|
|
||||||
<editor>
|
|
||||||
<element key="a">
|
|
||||||
<element key="b">
|
|
||||||
<text />
|
|
||||||
</element>
|
|
||||||
</element>
|
|
||||||
</editor>
|
|
||||||
)
|
|
||||||
|
|
||||||
export const test = value => {
|
|
||||||
return Node.furthest(value, [0, 0, 0], ([e]) => Element.isElement(e))
|
|
||||||
}
|
|
||||||
|
|
||||||
export const output = [
|
|
||||||
<element key="a">
|
|
||||||
<element key="b">
|
|
||||||
<text />
|
|
||||||
</element>
|
|
||||||
</element>,
|
|
||||||
[0],
|
|
||||||
]
|
|
@@ -1,16 +0,0 @@
|
|||||||
/** @jsx jsx */
|
|
||||||
|
|
||||||
import { Editor } from 'slate'
|
|
||||||
import { jsx } from '../..'
|
|
||||||
|
|
||||||
export const input = (
|
|
||||||
<editor>
|
|
||||||
<block>one</block>
|
|
||||||
</editor>
|
|
||||||
)
|
|
||||||
|
|
||||||
export const run = editor => {
|
|
||||||
return Editor.ancestor(editor, [0, 0])
|
|
||||||
}
|
|
||||||
|
|
||||||
export const output = [<block>one</block>, [0]]
|
|
@@ -1,16 +0,0 @@
|
|||||||
/** @jsx jsx */
|
|
||||||
|
|
||||||
import { Editor } from 'slate'
|
|
||||||
import { jsx } from '../..'
|
|
||||||
|
|
||||||
export const input = (
|
|
||||||
<editor>
|
|
||||||
<block>one</block>
|
|
||||||
</editor>
|
|
||||||
)
|
|
||||||
|
|
||||||
export const run = editor => {
|
|
||||||
return Editor.ancestor(editor, { path: [0, 0], offset: 1 })
|
|
||||||
}
|
|
||||||
|
|
||||||
export const output = [<block>one</block>, [0]]
|
|
@@ -1,20 +0,0 @@
|
|||||||
/** @jsx jsx */
|
|
||||||
|
|
||||||
import { Editor } from 'slate'
|
|
||||||
import { jsx } from '../..'
|
|
||||||
|
|
||||||
export const input = (
|
|
||||||
<editor>
|
|
||||||
<block>one</block>
|
|
||||||
<block>two</block>
|
|
||||||
</editor>
|
|
||||||
)
|
|
||||||
|
|
||||||
export const run = editor => {
|
|
||||||
return Editor.ancestor(editor, {
|
|
||||||
anchor: { path: [0, 0], offset: 1 },
|
|
||||||
focus: { path: [0, 0], offset: 2 },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export const output = [<block>one</block>, [0]]
|
|
@@ -1,20 +0,0 @@
|
|||||||
/** @jsx jsx */
|
|
||||||
|
|
||||||
import { Editor } from 'slate'
|
|
||||||
import { jsx } from '../..'
|
|
||||||
|
|
||||||
export const input = (
|
|
||||||
<editor>
|
|
||||||
<block>one</block>
|
|
||||||
<block>two</block>
|
|
||||||
</editor>
|
|
||||||
)
|
|
||||||
|
|
||||||
export const run = editor => {
|
|
||||||
return Editor.ancestor(editor, {
|
|
||||||
anchor: { path: [0, 0], offset: 1 },
|
|
||||||
focus: { path: [1, 0], offset: 2 },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export const output = [input, []]
|
|
Reference in New Issue
Block a user