1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-25 16:20:49 +02:00

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

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

This reverts commit b940640fc8.

* Add changeset
This commit is contained in:
Dylan Schiemann
2022-04-26 18:28:58 +01:00
committed by GitHub
parent acdc5c3417
commit 3b7a1bf72d
13 changed files with 377 additions and 436 deletions

View File

@@ -0,0 +1,5 @@
---
'slate': minor
---
Added types for options and common string literals, thanks @JoshuaKGoldberg

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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