1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-21 14:41:23 +02:00

Revert "Added types for options and common string literals (#4968)" (#4969)

This reverts commit ef09c8cf6e.
This commit is contained in:
Dylan Schiemann
2022-04-26 06:26:45 +01:00
committed by GitHub
parent ef09c8cf6e
commit b940640fc8
12 changed files with 436 additions and 372 deletions

View File

@@ -14,7 +14,6 @@ import {
Transforms,
} from './'
import { DIRTY_PATHS, DIRTY_PATH_KEYS, FLUSHING } from './utils/weak-maps'
import { TextUnit } from './interfaces/types'
/**
* Create a new Slate `Editor` object.
@@ -122,7 +121,7 @@ export const createEditor = (): Editor => {
}
},
deleteBackward: (unit: TextUnit) => {
deleteBackward: (unit: 'character' | 'word' | 'line' | 'block') => {
const { selection } = editor
if (selection && Range.isCollapsed(selection)) {
@@ -130,7 +129,7 @@ export const createEditor = (): Editor => {
}
},
deleteForward: (unit: TextUnit) => {
deleteForward: (unit: 'character' | 'word' | 'line' | 'block') => {
const { selection } = editor
if (selection && Range.isCollapsed(selection)) {

View File

@@ -15,6 +15,7 @@ import {
RangeRef,
Span,
Text,
Transforms,
} from '..'
import {
DIRTY_PATHS,
@@ -31,22 +32,11 @@ import {
} from '../utils/string'
import { Descendant } from './node'
import { Element } from './element'
import {
LeafEdge,
SelectionMode,
TextDirection,
TextUnit,
TextUnitAdjustment,
RangeDirection,
MaximizeMode,
} from './types'
export type BaseSelection = Range | null
export type Selection = ExtendedType<'Selection', BaseSelection>
export type EditorMarks = Omit<Text, 'text'>
/**
* The `Editor` interface stores all the state of a Slate editor. It is extended
* by plugins that wish to add their own helpers and implement new behaviors.
@@ -56,7 +46,7 @@ export interface BaseEditor {
children: Descendant[]
selection: Selection
operations: Operation[]
marks: EditorMarks | null
marks: Omit<Text, 'text'> | null
// Schema-specific node behaviors.
isInline: (element: Element) => boolean
@@ -67,9 +57,9 @@ export interface BaseEditor {
// Overrideable core actions.
addMark: (key: string, value: any) => void
apply: (operation: Operation) => void
deleteBackward: (unit: TextUnit) => void
deleteForward: (unit: TextUnit) => void
deleteFragment: (direction?: TextDirection) => void
deleteBackward: (unit: 'character' | 'word' | 'line' | 'block') => void
deleteForward: (unit: 'character' | 'word' | 'line' | 'block') => void
deleteFragment: (direction?: 'forward' | 'backward') => void
getFragment: () => Descendant[]
insertBreak: () => void
insertSoftBreak: () => void
@@ -81,151 +71,52 @@ export interface BaseEditor {
export type Editor = ExtendedType<'Editor', BaseEditor>
export interface EditorAboveOptions<T extends Ancestor> {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
voids?: boolean
}
export interface EditorAfterOptions {
distance?: number
unit?: TextUnitAdjustment
voids?: boolean
}
export interface EditorBeforeOptions {
distance?: number
unit?: TextUnitAdjustment
voids?: boolean
}
export interface EditorDirectedDeletionOptions {
unit?: TextUnit
}
export interface EditorFragmentDeletionOptions {
direction?: TextDirection
}
export interface EditorLeafOptions {
depth?: number
edge?: LeafEdge
}
export interface EditorLevelsOptions<T extends Node> {
at?: Location
match?: NodeMatch<T>
reverse?: boolean
voids?: boolean
}
export interface EditorNextOptions<T extends Descendant> {
at?: Location
match?: NodeMatch<T>
mode?: SelectionMode
voids?: boolean
}
export interface EditorNodeOptions {
depth?: number
edge?: LeafEdge
}
export interface EditorNodesOptions<T extends Node> {
at?: Location | Span
match?: NodeMatch<T>
mode?: SelectionMode
universal?: boolean
reverse?: boolean
voids?: boolean
}
export interface EditorNormalizeOptions {
force?: boolean
}
export interface EditorParentOptions {
depth?: number
edge?: LeafEdge
}
export interface EditorPathOptions {
depth?: number
edge?: LeafEdge
}
export interface EditorPathRefOptions {
affinity?: TextDirection | null
}
export interface EditorPointOptions {
edge?: LeafEdge
}
export interface EditorPointRefOptions {
affinity?: TextDirection | null
}
export interface EditorPositionsOptions {
at?: Location
unit?: TextUnitAdjustment
reverse?: boolean
voids?: boolean
}
export interface EditorPreviousOptions<T extends Node> {
at?: Location
match?: NodeMatch<T>
mode?: SelectionMode
voids?: boolean
}
export interface EditorRangeRefOptions {
affinity?: RangeDirection | null
}
export interface EditorStringOptions {
voids?: boolean
}
export interface EditorUnhangRangeOptions {
voids?: boolean
}
export interface EditorVoidOptions {
at?: Location
mode?: MaximizeMode
voids?: boolean
}
export interface EditorInterface {
above: <T extends Ancestor>(
editor: Editor,
options?: EditorAboveOptions<T>
options?: {
at?: Location
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
voids?: boolean
}
) => NodeEntry<T> | undefined
addMark: (editor: Editor, key: string, value: any) => void
after: (
editor: Editor,
at: Location,
options?: EditorAfterOptions
options?: {
distance?: number
unit?: 'offset' | 'character' | 'word' | 'line' | 'block'
voids?: boolean
}
) => Point | undefined
before: (
editor: Editor,
at: Location,
options?: EditorBeforeOptions
options?: {
distance?: number
unit?: 'offset' | 'character' | 'word' | 'line' | 'block'
voids?: boolean
}
) => Point | undefined
deleteBackward: (
editor: Editor,
options?: EditorDirectedDeletionOptions
options?: {
unit?: 'character' | 'word' | 'line' | 'block'
}
) => void
deleteForward: (
editor: Editor,
options?: EditorDirectedDeletionOptions
options?: {
unit?: 'character' | 'word' | 'line' | 'block'
}
) => void
deleteFragment: (
editor: Editor,
options?: EditorFragmentDeletionOptions
options?: {
direction?: 'forward' | 'backward'
}
) => void
edges: (editor: Editor, at: Location) => [Point, Point]
end: (editor: Editor, at: Location) => Point
@@ -253,55 +144,119 @@ export interface EditorInterface {
leaf: (
editor: Editor,
at: Location,
options?: EditorLeafOptions
options?: {
depth?: number
edge?: 'start' | 'end'
}
) => NodeEntry<Text>
levels: <T extends Node>(
editor: Editor,
options?: EditorLevelsOptions<T>
options?: {
at?: Location
match?: NodeMatch<T>
reverse?: boolean
voids?: boolean
}
) => Generator<NodeEntry<T>, void, undefined>
marks: (editor: Editor) => Omit<Text, 'text'> | null
next: <T extends Descendant>(
editor: Editor,
options?: EditorNextOptions<T>
options?: {
at?: Location
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
voids?: boolean
}
) => NodeEntry<T> | undefined
node: (editor: Editor, at: Location, options?: EditorNodeOptions) => NodeEntry
node: (
editor: Editor,
at: Location,
options?: {
depth?: number
edge?: 'start' | 'end'
}
) => NodeEntry
nodes: <T extends Node>(
editor: Editor,
options?: EditorNodesOptions<T>
options?: {
at?: Location | Span
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
universal?: boolean
reverse?: boolean
voids?: boolean
}
) => Generator<NodeEntry<T>, void, undefined>
normalize: (editor: Editor, options?: EditorNormalizeOptions) => void
normalize: (
editor: Editor,
options?: {
force?: boolean
}
) => void
parent: (
editor: Editor,
at: Location,
options?: EditorParentOptions
options?: {
depth?: number
edge?: 'start' | 'end'
}
) => NodeEntry<Ancestor>
path: (editor: Editor, at: Location, options?: EditorPathOptions) => Path
path: (
editor: Editor,
at: Location,
options?: {
depth?: number
edge?: 'start' | 'end'
}
) => Path
pathRef: (
editor: Editor,
path: Path,
options?: EditorPathRefOptions
options?: {
affinity?: 'backward' | 'forward' | null
}
) => PathRef
pathRefs: (editor: Editor) => Set<PathRef>
point: (editor: Editor, at: Location, options?: EditorPointOptions) => Point
point: (
editor: Editor,
at: Location,
options?: {
edge?: 'start' | 'end'
}
) => Point
pointRef: (
editor: Editor,
point: Point,
options?: EditorPointRefOptions
options?: {
affinity?: 'backward' | 'forward' | null
}
) => PointRef
pointRefs: (editor: Editor) => Set<PointRef>
positions: (
editor: Editor,
options?: EditorPositionsOptions
options?: {
at?: Location
unit?: 'offset' | 'character' | 'word' | 'line' | 'block'
reverse?: boolean
voids?: boolean
}
) => Generator<Point, void, undefined>
previous: <T extends Node>(
editor: Editor,
options?: EditorPreviousOptions<T>
options?: {
at?: Location
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
voids?: boolean
}
) => NodeEntry<T> | undefined
range: (editor: Editor, at: Location, to?: Location) => Range
rangeRef: (
editor: Editor,
range: Range,
options?: EditorRangeRefOptions
options?: {
affinity?: 'backward' | 'forward' | 'outward' | 'inward' | null
}
) => RangeRef
rangeRefs: (editor: Editor) => Set<RangeRef>
removeMark: (editor: Editor, key: string) => void
@@ -310,16 +265,24 @@ export interface EditorInterface {
string: (
editor: Editor,
at: Location,
options?: EditorStringOptions
options?: {
voids?: boolean
}
) => string
unhangRange: (
editor: Editor,
range: Range,
options?: EditorUnhangRangeOptions
options?: {
voids?: boolean
}
) => Range
void: (
editor: Editor,
options?: EditorVoidOptions
options?: {
at?: Location
mode?: 'highest' | 'lowest'
voids?: boolean
}
) => NodeEntry<Element> | undefined
withoutNormalizing: (editor: Editor, fn: () => void) => void
}
@@ -333,7 +296,12 @@ export const Editor: EditorInterface = {
above<T extends Ancestor>(
editor: Editor,
options: EditorAboveOptions<T> = {}
options: {
at?: Location
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
voids?: boolean
} = {}
): NodeEntry<T> | undefined {
const {
voids = false,
@@ -379,7 +347,11 @@ export const Editor: EditorInterface = {
after(
editor: Editor,
at: Location,
options: EditorAfterOptions = {}
options: {
distance?: number
unit?: 'offset' | 'character' | 'word' | 'line' | 'block'
voids?: boolean
} = {}
): Point | undefined {
const anchor = Editor.point(editor, at, { edge: 'end' })
const focus = Editor.end(editor, [])
@@ -413,7 +385,11 @@ export const Editor: EditorInterface = {
before(
editor: Editor,
at: Location,
options: EditorBeforeOptions = {}
options: {
distance?: number
unit?: 'offset' | 'character' | 'word' | 'line' | 'block'
voids?: boolean
} = {}
): Point | undefined {
const anchor = Editor.start(editor, [])
const focus = Editor.point(editor, at, { edge: 'start' })
@@ -447,7 +423,9 @@ export const Editor: EditorInterface = {
deleteBackward(
editor: Editor,
options: EditorDirectedDeletionOptions = {}
options: {
unit?: 'character' | 'word' | 'line' | 'block'
} = {}
): void {
const { unit = 'character' } = options
editor.deleteBackward(unit)
@@ -459,7 +437,9 @@ export const Editor: EditorInterface = {
deleteForward(
editor: Editor,
options: EditorDirectedDeletionOptions = {}
options: {
unit?: 'character' | 'word' | 'line' | 'block'
} = {}
): void {
const { unit = 'character' } = options
editor.deleteForward(unit)
@@ -471,7 +451,9 @@ export const Editor: EditorInterface = {
deleteFragment(
editor: Editor,
options: EditorFragmentDeletionOptions = {}
options: {
direction?: 'forward' | 'backward'
} = {}
): void {
const { direction = 'forward' } = options
editor.deleteFragment(direction)
@@ -717,7 +699,10 @@ export const Editor: EditorInterface = {
leaf(
editor: Editor,
at: Location,
options: EditorLeafOptions = {}
options: {
depth?: number
edge?: 'start' | 'end'
} = {}
): NodeEntry<Text> {
const path = Editor.path(editor, at, options)
const node = Node.leaf(editor, path)
@@ -730,7 +715,12 @@ export const Editor: EditorInterface = {
*levels<T extends Node>(
editor: Editor,
options: EditorLevelsOptions<T> = {}
options: {
at?: Location
match?: NodeMatch<T>
reverse?: boolean
voids?: boolean
} = {}
): Generator<NodeEntry<T>, void, undefined> {
const { at = editor.selection, reverse = false, voids = false } = options
let { match } = options
@@ -822,7 +812,12 @@ export const Editor: EditorInterface = {
next<T extends Descendant>(
editor: Editor,
options: EditorNextOptions<T> = {}
options: {
at?: Location
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
voids?: boolean
} = {}
): NodeEntry<T> | undefined {
const { mode = 'lowest', voids = false } = options
let { match, at = editor.selection } = options
@@ -863,7 +858,10 @@ export const Editor: EditorInterface = {
node(
editor: Editor,
at: Location,
options: EditorNodeOptions = {}
options: {
depth?: number
edge?: 'start' | 'end'
} = {}
): NodeEntry {
const path = Editor.path(editor, at, options)
const node = Node.get(editor, path)
@@ -876,7 +874,14 @@ export const Editor: EditorInterface = {
*nodes<T extends Node>(
editor: Editor,
options: EditorNodesOptions<T> = {}
options: {
at?: Location | Span
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
universal?: boolean
reverse?: boolean
voids?: boolean
} = {}
): Generator<NodeEntry<T>, void, undefined> {
const {
at = editor.selection,
@@ -977,7 +982,12 @@ export const Editor: EditorInterface = {
* Normalize any dirty objects in the editor.
*/
normalize(editor: Editor, options: EditorNormalizeOptions = {}): void {
normalize(
editor: Editor,
options: {
force?: boolean
} = {}
): void {
const { force = false } = options
const getDirtyPaths = (editor: Editor) => {
return DIRTY_PATHS.get(editor) || []
@@ -1062,7 +1072,10 @@ export const Editor: EditorInterface = {
parent(
editor: Editor,
at: Location,
options: EditorParentOptions = {}
options: {
depth?: number
edge?: 'start' | 'end'
} = {}
): NodeEntry<Ancestor> {
const path = Editor.path(editor, at, options)
const parentPath = Path.parent(path)
@@ -1074,7 +1087,14 @@ export const Editor: EditorInterface = {
* Get the path of a location.
*/
path(editor: Editor, at: Location, options: EditorPathOptions = {}): Path {
path(
editor: Editor,
at: Location,
options: {
depth?: number
edge?: 'start' | 'end'
} = {}
): Path {
const { depth, edge } = options
if (Path.isPath(at)) {
@@ -1120,7 +1140,9 @@ export const Editor: EditorInterface = {
pathRef(
editor: Editor,
path: Path,
options: EditorPathRefOptions = {}
options: {
affinity?: 'backward' | 'forward' | null
} = {}
): PathRef {
const { affinity = 'forward' } = options
const ref: PathRef = {
@@ -1159,7 +1181,13 @@ export const Editor: EditorInterface = {
* Get the start or end point of a location.
*/
point(editor: Editor, at: Location, options: EditorPointOptions = {}): Point {
point(
editor: Editor,
at: Location,
options: {
edge?: 'start' | 'end'
} = {}
): Point {
const { edge = 'start' } = options
if (Path.isPath(at)) {
@@ -1200,7 +1228,9 @@ export const Editor: EditorInterface = {
pointRef(
editor: Editor,
point: Point,
options: EditorPointRefOptions = {}
options: {
affinity?: 'backward' | 'forward' | null
} = {}
): PointRef {
const { affinity = 'forward' } = options
const ref: PointRef = {
@@ -1250,7 +1280,12 @@ export const Editor: EditorInterface = {
*positions(
editor: Editor,
options: EditorPositionsOptions = {}
options: {
at?: Location
unit?: 'offset' | 'character' | 'word' | 'line' | 'block'
reverse?: boolean
voids?: boolean
} = {}
): Generator<Point, void, undefined> {
const {
at = editor.selection,
@@ -1434,7 +1469,12 @@ export const Editor: EditorInterface = {
previous<T extends Node>(
editor: Editor,
options: EditorPreviousOptions<T> = {}
options: {
at?: Location
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
voids?: boolean
} = {}
): NodeEntry<T> | undefined {
const { mode = 'lowest', voids = false } = options
let { match, at = editor.selection } = options
@@ -1501,7 +1541,9 @@ export const Editor: EditorInterface = {
rangeRef(
editor: Editor,
range: Range,
options: EditorRangeRefOptions = {}
options: {
affinity?: 'backward' | 'forward' | 'outward' | 'inward' | null
} = {}
): RangeRef {
const { affinity = 'forward' } = options
const ref: RangeRef = {
@@ -1576,7 +1618,9 @@ export const Editor: EditorInterface = {
string(
editor: Editor,
at: Location,
options: EditorStringOptions = {}
options: {
voids?: boolean
} = {}
): string {
const { voids = false } = options
const range = Editor.range(editor, at)
@@ -1611,7 +1655,9 @@ export const Editor: EditorInterface = {
unhangRange(
editor: Editor,
range: Range,
options: EditorUnhangRangeOptions = {}
options: {
voids?: boolean
} = {}
): Range {
const { voids = false } = options
let [start, end] = Range.edges(range)
@@ -1656,7 +1702,11 @@ export const Editor: EditorInterface = {
void(
editor: Editor,
options: EditorVoidOptions = {}
options: {
at?: Location
mode?: 'highest' | 'lowest'
voids?: boolean
} = {}
): NodeEntry<Element> | undefined {
return Editor.above(editor, {
...options,

View File

@@ -10,68 +10,42 @@ import { Element, ElementEntry } from './element'
export type BaseNode = Editor | Element | Text
export type Node = Editor | Element | Text
export interface NodeAncestorsOptions {
reverse?: boolean
}
export interface NodeChildrenOptions {
reverse?: boolean
}
export interface NodeDescendantsOptions {
from?: Path
to?: Path
reverse?: boolean
pass?: (node: NodeEntry) => boolean
}
export interface NodeElementsOptions {
from?: Path
to?: Path
reverse?: boolean
pass?: (node: NodeEntry) => boolean
}
export interface NodeLevelsOptions {
reverse?: boolean
}
export interface NodeNodesOptions {
from?: Path
to?: Path
reverse?: boolean
pass?: (entry: NodeEntry) => boolean
}
export interface NodeTextsOptions {
from?: Path
to?: Path
reverse?: boolean
pass?: (node: NodeEntry) => boolean
}
export interface NodeInterface {
ancestor: (root: Node, path: Path) => Ancestor
ancestors: (
root: Node,
path: Path,
options?: NodeAncestorsOptions
options?: {
reverse?: boolean
}
) => Generator<NodeEntry<Ancestor>, void, undefined>
child: (root: Node, index: number) => Descendant
children: (
root: Node,
path: Path,
options?: NodeChildrenOptions
options?: {
reverse?: boolean
}
) => Generator<NodeEntry<Descendant>, void, undefined>
common: (root: Node, path: Path, another: Path) => NodeEntry
descendant: (root: Node, path: Path) => Descendant
descendants: (
root: Node,
options?: NodeDescendantsOptions
options?: {
from?: Path
to?: Path
reverse?: boolean
pass?: (node: NodeEntry) => boolean
}
) => Generator<NodeEntry<Descendant>, void, undefined>
elements: (
root: Node,
options?: NodeElementsOptions
options?: {
from?: Path
to?: Path
reverse?: boolean
pass?: (node: NodeEntry) => boolean
}
) => Generator<ElementEntry, void, undefined>
extractProps: (node: Node) => NodeProps
first: (root: Node, path: Path) => NodeEntry
@@ -85,18 +59,30 @@ export interface NodeInterface {
levels: (
root: Node,
path: Path,
options?: NodeLevelsOptions
options?: {
reverse?: boolean
}
) => Generator<NodeEntry, void, undefined>
matches: (node: Node, props: Partial<Node>) => boolean
nodes: (
root: Node,
options?: NodeNodesOptions
options?: {
from?: Path
to?: Path
reverse?: boolean
pass?: (entry: NodeEntry) => boolean
}
) => Generator<NodeEntry, void, undefined>
parent: (root: Node, path: Path) => Ancestor
string: (node: Node) => string
texts: (
root: Node,
options?: NodeTextsOptions
options?: {
from?: Path
to?: Path
reverse?: boolean
pass?: (node: NodeEntry) => boolean
}
) => Generator<NodeEntry<Text>, void, undefined>
}
@@ -129,7 +115,9 @@ export const Node: NodeInterface = {
*ancestors(
root: Node,
path: Path,
options: NodeAncestorsOptions = {}
options: {
reverse?: boolean
} = {}
): Generator<NodeEntry<Ancestor>, void, undefined> {
for (const p of Path.ancestors(path, options)) {
const n = Node.ancestor(root, p)
@@ -169,7 +157,9 @@ export const Node: NodeInterface = {
*children(
root: Node,
path: Path,
options: NodeChildrenOptions = {}
options: {
reverse?: boolean
} = {}
): Generator<NodeEntry<Descendant>, void, undefined> {
const { reverse = false } = options
const ancestor = Node.ancestor(root, path)
@@ -216,7 +206,12 @@ export const Node: NodeInterface = {
*descendants(
root: Node,
options: NodeDescendantsOptions = {}
options: {
from?: Path
to?: Path
reverse?: boolean
pass?: (node: NodeEntry) => boolean
} = {}
): Generator<NodeEntry<Descendant>, void, undefined> {
for (const [node, path] of Node.nodes(root, options)) {
if (path.length !== 0) {
@@ -235,7 +230,12 @@ export const Node: NodeInterface = {
*elements(
root: Node,
options: NodeElementsOptions = {}
options: {
from?: Path
to?: Path
reverse?: boolean
pass?: (node: NodeEntry) => boolean
} = {}
): Generator<ElementEntry, void, undefined> {
for (const [node, path] of Node.nodes(root, options)) {
if (Element.isElement(node)) {
@@ -445,7 +445,9 @@ export const Node: NodeInterface = {
*levels(
root: Node,
path: Path,
options: NodeLevelsOptions = {}
options: {
reverse?: boolean
} = {}
): Generator<NodeEntry, void, undefined> {
for (const p of Path.levels(path, options)) {
const n = Node.get(root, p)
@@ -476,7 +478,12 @@ export const Node: NodeInterface = {
*nodes(
root: Node,
options: NodeNodesOptions = {}
options: {
from?: Path
to?: Path
reverse?: boolean
pass?: (entry: NodeEntry) => boolean
} = {}
): Generator<NodeEntry, void, undefined> {
const { pass, reverse = false } = options
const { from = [], to } = options
@@ -582,7 +589,12 @@ export const Node: NodeInterface = {
*texts(
root: Node,
options: NodeTextsOptions = {}
options: {
from?: Path
to?: Path
reverse?: boolean
pass?: (node: NodeEntry) => boolean
} = {}
): Generator<NodeEntry<Text>, void, undefined> {
for (const [node, path] of Node.nodes(root, options)) {
if (Text.isText(node)) {

View File

@@ -1,6 +1,5 @@
import { produce } from 'immer'
import { Operation } from '..'
import { TextDirection } from './types'
/**
* `Path` arrays are a list of indexes that describe a node's exact position in
@@ -10,20 +9,8 @@ import { TextDirection } from './types'
export type Path = number[]
export interface PathAncestorsOptions {
reverse?: boolean
}
export interface PathLevelsOptions {
reverse?: boolean
}
export interface PathTransformOptions {
affinity?: TextDirection | null
}
export interface PathInterface {
ancestors: (path: Path, options?: PathAncestorsOptions) => Path[]
ancestors: (path: Path, options?: { reverse?: boolean }) => Path[]
common: (path: Path, another: Path) => Path
compare: (path: Path, another: Path) => -1 | 0 | 1
endsAfter: (path: Path, another: Path) => boolean
@@ -40,7 +27,12 @@ export interface PathInterface {
isParent: (path: Path, another: Path) => boolean
isPath: (value: any) => value is Path
isSibling: (path: Path, another: Path) => boolean
levels: (path: Path, options?: PathLevelsOptions) => Path[]
levels: (
path: Path,
options?: {
reverse?: boolean
}
) => Path[]
next: (path: Path) => Path
operationCanTransformPath: (operation: Operation) => boolean
parent: (path: Path) => Path
@@ -49,7 +41,7 @@ export interface PathInterface {
transform: (
path: Path,
operation: Operation,
options?: PathTransformOptions
options?: { affinity?: 'forward' | 'backward' | null }
) => Path | null
}
@@ -61,7 +53,7 @@ export const Path: PathInterface = {
* `reverse: true` option is passed, they are reversed.
*/
ancestors(path: Path, options: PathAncestorsOptions = {}): Path[] {
ancestors(path: Path, options: { reverse?: boolean } = {}): Path[] {
const { reverse = false } = options
let paths = Path.levels(path, options)
@@ -265,7 +257,12 @@ export const Path: PathInterface = {
* true` option is passed, they are reversed.
*/
levels(path: Path, options: PathLevelsOptions = {}): Path[] {
levels(
path: Path,
options: {
reverse?: boolean
} = {}
): Path[] {
const { reverse = false } = options
const list: Path[] = []
@@ -370,7 +367,7 @@ export const Path: PathInterface = {
transform(
path: Path | null,
operation: Operation,
options: PathTransformOptions = {}
options: { affinity?: 'forward' | 'backward' | null } = {}
): Path | null {
return produce(path, p => {
const { affinity = 'forward' } = options

View File

@@ -1,5 +1,4 @@
import { Operation, Point } from '..'
import { TextDirection } from './types'
/**
* `PointRef` objects keep a specific point in a document synced over time as new
@@ -9,7 +8,7 @@ import { TextDirection } from './types'
export interface PointRef {
current: Point | null
affinity: TextDirection | null
affinity: 'forward' | 'backward' | null
unref(): Point | null
}

View File

@@ -1,7 +1,6 @@
import { isPlainObject } from 'is-plain-object'
import { produce } from 'immer'
import { ExtendedType, Operation, Path } from '..'
import { TextDirection } from './types'
/**
* `Point` objects refer to a specific location in a text node in a Slate
@@ -17,10 +16,6 @@ export interface BasePoint {
export type Point = ExtendedType<'Point', BasePoint>
export interface PointTransformOptions {
affinity?: TextDirection | null
}
export interface PointInterface {
compare: (point: Point, another: Point) => -1 | 0 | 1
isAfter: (point: Point, another: Point) => boolean
@@ -30,7 +25,7 @@ export interface PointInterface {
transform: (
point: Point,
op: Operation,
options?: PointTransformOptions
options?: { affinity?: 'forward' | 'backward' | null }
) => Point | null
}
@@ -98,7 +93,7 @@ export const Point: PointInterface = {
transform(
point: Point | null,
op: Operation,
options: PointTransformOptions = {}
options: { affinity?: 'forward' | 'backward' | null } = {}
): Point | null {
return produce(point, p => {
if (p === null) {

View File

@@ -1,7 +1,6 @@
import { produce } from 'immer'
import { isPlainObject } from 'is-plain-object'
import { ExtendedType, Operation, Path, Point, PointEntry } from '..'
import { RangeDirection } from './types'
/**
* `Range` objects are a set of points that refer to a specific span of a Slate
@@ -16,16 +15,13 @@ export interface BaseRange {
export type Range = ExtendedType<'Range', BaseRange>
export interface RangeEdgesOptions {
reverse?: boolean
}
export interface RangeTransformOptions {
affinity?: RangeDirection | null
}
export interface RangeInterface {
edges: (range: Range, options?: RangeEdgesOptions) => [Point, Point]
edges: (
range: Range,
options?: {
reverse?: boolean
}
) => [Point, Point]
end: (range: Range) => Point
equals: (range: Range, another: Range) => boolean
includes: (range: Range, target: Path | Point | Range) => boolean
@@ -40,7 +36,9 @@ export interface RangeInterface {
transform: (
range: Range,
op: Operation,
options?: RangeTransformOptions
options?: {
affinity?: 'forward' | 'backward' | 'outward' | 'inward' | null
}
) => Range | null
}
@@ -50,7 +48,12 @@ export const Range: RangeInterface = {
* in the document.
*/
edges(range: Range, options: RangeEdgesOptions = {}): [Point, Point] {
edges(
range: Range,
options: {
reverse?: boolean
} = {}
): [Point, Point] {
const { reverse = false } = options
const { anchor, focus } = range
return Range.isBackward(range) === reverse
@@ -206,7 +209,9 @@ export const Range: RangeInterface = {
transform(
range: Range | null,
op: Operation,
options: RangeTransformOptions = {}
options: {
affinity?: 'forward' | 'backward' | 'outward' | 'inward' | null
} = {}
): Range | null {
return produce(range, r => {
if (r === null) {

View File

@@ -15,12 +15,8 @@ export interface BaseText {
export type Text = ExtendedType<'Text', BaseText>
export interface TextEqualsOptions {
loose?: boolean
}
export interface TextInterface {
equals: (text: Text, another: Text, options?: TextEqualsOptions) => boolean
equals: (text: Text, another: Text, options?: { loose?: boolean }) => boolean
isText: (value: any) => value is Text
isTextList: (value: any) => value is Text[]
isTextProps: (props: any) => props is Partial<Text>
@@ -35,7 +31,11 @@ export const Text: TextInterface = {
* When loose is set, the text is not compared. This is
* used to check whether sibling text nodes can be merged.
*/
equals(text: Text, another: Text, options: TextEqualsOptions = {}): boolean {
equals(
text: Text,
another: Text,
options: { loose?: boolean } = {}
): boolean {
const { loose = false } = options
function omitText(obj: Record<any, any>) {

View File

@@ -1,19 +0,0 @@
export type LeafEdge = 'start' | 'end'
export type MaximizeMode = RangeMode | 'all'
export type MoveUnit = 'offset' | 'character' | 'word' | 'line'
export type RangeDirection = TextDirection | 'outward' | 'inward'
export type RangeMode = 'highest' | 'lowest'
export type SelectionEdge = 'anchor' | 'focus' | 'start' | 'end'
export type SelectionMode = 'all' | 'highest' | 'lowest'
export type TextDirection = 'forward' | 'backward'
export type TextUnit = 'character' | 'word' | 'line' | 'block'
export type TextUnitAdjustment = TextUnit | 'offset'

View File

@@ -13,7 +13,6 @@ import {
} from '..'
import { NodeMatch, PropsCompare, PropsMerge } from '../interfaces/editor'
import { PointRef } from '../interfaces/point-ref'
import { RangeMode, MaximizeMode } from '../interfaces/types'
export interface NodeTransforms {
insertNodes: <T extends Node>(
@@ -22,7 +21,7 @@ export interface NodeTransforms {
options?: {
at?: Location
match?: NodeMatch<T>
mode?: RangeMode
mode?: 'highest' | 'lowest'
hanging?: boolean
select?: boolean
voids?: boolean
@@ -33,7 +32,7 @@ export interface NodeTransforms {
options?: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
voids?: boolean
}
) => void
@@ -42,7 +41,7 @@ export interface NodeTransforms {
options?: {
at?: Location
match?: NodeMatch<T>
mode?: RangeMode
mode?: 'highest' | 'lowest'
hanging?: boolean
voids?: boolean
}
@@ -52,7 +51,7 @@ export interface NodeTransforms {
options: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
to: Path
voids?: boolean
}
@@ -62,7 +61,7 @@ export interface NodeTransforms {
options?: {
at?: Location
match?: NodeMatch<T>
mode?: RangeMode
mode?: 'highest' | 'lowest'
hanging?: boolean
voids?: boolean
}
@@ -73,7 +72,7 @@ export interface NodeTransforms {
options?: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
hanging?: boolean
split?: boolean
voids?: boolean
@@ -86,7 +85,7 @@ export interface NodeTransforms {
options?: {
at?: Location
match?: NodeMatch<T>
mode?: RangeMode
mode?: 'highest' | 'lowest'
always?: boolean
height?: number
voids?: boolean
@@ -98,7 +97,7 @@ export interface NodeTransforms {
options?: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
}
@@ -108,7 +107,7 @@ export interface NodeTransforms {
options?: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
}
@@ -119,7 +118,7 @@ export interface NodeTransforms {
options?: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
}
@@ -137,7 +136,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: RangeMode
mode?: 'highest' | 'lowest'
hanging?: boolean
select?: boolean
voids?: boolean
@@ -256,7 +255,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
voids?: boolean
} = {}
): void {
@@ -320,7 +319,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: RangeMode
mode?: 'highest' | 'lowest'
hanging?: boolean
voids?: boolean
} = {}
@@ -460,7 +459,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
to: Path
voids?: boolean
}
@@ -521,7 +520,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: RangeMode
mode?: 'highest' | 'lowest'
hanging?: boolean
voids?: boolean
} = {}
@@ -568,7 +567,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
hanging?: boolean
split?: boolean
voids?: boolean
@@ -693,7 +692,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: RangeMode
mode?: 'highest' | 'lowest'
always?: boolean
height?: number
voids?: boolean
@@ -822,7 +821,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
} = {}
@@ -850,7 +849,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
} = {}
@@ -916,7 +915,7 @@ export const NodeTransforms: NodeTransforms = {
options: {
at?: Location
match?: NodeMatch<T>
mode?: MaximizeMode
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
} = {}

View File

@@ -1,30 +1,29 @@
import { Editor, Location, Point, Range, Transforms } from '..'
import { SelectionEdge, MoveUnit } from '../interfaces/types'
export interface SelectionCollapseOptions {
edge?: SelectionEdge
}
export interface SelectionMoveOptions {
distance?: number
unit?: MoveUnit
reverse?: boolean
edge?: SelectionEdge
}
export interface SelectionSetPointOptions {
edge?: SelectionEdge
}
export interface SelectionTransforms {
collapse: (editor: Editor, options?: SelectionCollapseOptions) => void
collapse: (
editor: Editor,
options?: {
edge?: 'anchor' | 'focus' | 'start' | 'end'
}
) => void
deselect: (editor: Editor) => void
move: (editor: Editor, options?: SelectionMoveOptions) => void
move: (
editor: Editor,
options?: {
distance?: number
unit?: 'offset' | 'character' | 'word' | 'line'
reverse?: boolean
edge?: 'anchor' | 'focus' | 'start' | 'end'
}
) => void
select: (editor: Editor, target: Location) => void
setPoint: (
editor: Editor,
props: Partial<Point>,
options?: SelectionSetPointOptions
options?: {
edge?: 'anchor' | 'focus' | 'start' | 'end'
}
) => void
setSelection: (editor: Editor, props: Partial<Range>) => void
}
@@ -34,7 +33,12 @@ export const SelectionTransforms: SelectionTransforms = {
* Collapse the selection.
*/
collapse(editor: Editor, options: SelectionCollapseOptions = {}): void {
collapse(
editor: Editor,
options: {
edge?: 'anchor' | 'focus' | 'start' | 'end'
} = {}
): void {
const { edge = 'anchor' } = options
const { selection } = editor
@@ -73,7 +77,15 @@ export const SelectionTransforms: SelectionTransforms = {
* Move the selection's point forward or backward.
*/
move(editor: Editor, options: SelectionMoveOptions = {}): void {
move(
editor: Editor,
options: {
distance?: number
unit?: 'offset' | 'character' | 'word' | 'line'
reverse?: boolean
edge?: 'anchor' | 'focus' | 'start' | 'end'
} = {}
): void {
const { selection } = editor
const { distance = 1, unit = 'character', reverse = false } = options
let { edge = null } = options
@@ -152,7 +164,9 @@ export const SelectionTransforms: SelectionTransforms = {
setPoint(
editor: Editor,
props: Partial<Point>,
options: SelectionSetPointOptions = {}
options: {
edge?: 'anchor' | 'focus' | 'start' | 'end'
} = {}
): void {
const { selection } = editor
let { edge = 'both' } = options

View File

@@ -10,39 +10,35 @@ import {
Range,
Transforms,
} from '..'
import { TextUnit } from '../interfaces/types'
export interface TextDeleteOptions {
at?: Location
distance?: number
unit?: TextUnit
reverse?: boolean
hanging?: boolean
voids?: boolean
}
export interface TextInsertFragmentOptions {
at?: Location
hanging?: boolean
voids?: boolean
}
export interface TextInsertTextOptions {
at?: Location
voids?: boolean
}
export interface TextTransforms {
delete: (editor: Editor, options?: TextDeleteOptions) => void
delete: (
editor: Editor,
options?: {
at?: Location
distance?: number
unit?: 'character' | 'word' | 'line' | 'block'
reverse?: boolean
hanging?: boolean
voids?: boolean
}
) => void
insertFragment: (
editor: Editor,
fragment: Node[],
options?: TextInsertFragmentOptions
options?: {
at?: Location
hanging?: boolean
voids?: boolean
}
) => void
insertText: (
editor: Editor,
text: string,
options?: TextInsertTextOptions
options?: {
at?: Location
voids?: boolean
}
) => void
}
@@ -51,7 +47,17 @@ export const TextTransforms: TextTransforms = {
* Delete content in the editor.
*/
delete(editor: Editor, options: TextDeleteOptions = {}): void {
delete(
editor: Editor,
options: {
at?: Location
distance?: number
unit?: 'character' | 'word' | 'line' | 'block'
reverse?: boolean
hanging?: boolean
voids?: boolean
} = {}
): void {
Editor.withoutNormalizing(editor, () => {
const {
reverse = false,
@@ -225,7 +231,11 @@ export const TextTransforms: TextTransforms = {
insertFragment(
editor: Editor,
fragment: Node[],
options: TextInsertFragmentOptions = {}
options: {
at?: Location
hanging?: boolean
voids?: boolean
} = {}
): void {
Editor.withoutNormalizing(editor, () => {
const { hanging = false, voids = false } = options
@@ -452,7 +462,10 @@ export const TextTransforms: TextTransforms = {
insertText(
editor: Editor,
text: string,
options: TextInsertTextOptions = {}
options: {
at?: Location
voids?: boolean
} = {}
): void {
Editor.withoutNormalizing(editor, () => {
const { voids = false } = options