From b940640fc8f96abdd8725de1f0af0c6ba56156b9 Mon Sep 17 00:00:00 2001 From: Dylan Schiemann Date: Tue, 26 Apr 2022 06:26:45 +0100 Subject: [PATCH] Revert "Added types for options and common string literals (#4968)" (#4969) This reverts commit ef09c8cf6e5bd54428216a79bb0b4a568544c4e7. --- packages/slate/src/create-editor.ts | 5 +- packages/slate/src/interfaces/editor.ts | 408 ++++++++++++--------- packages/slate/src/interfaces/node.ts | 120 +++--- packages/slate/src/interfaces/path.ts | 35 +- packages/slate/src/interfaces/point-ref.ts | 3 +- packages/slate/src/interfaces/point.ts | 9 +- packages/slate/src/interfaces/range.ts | 31 +- packages/slate/src/interfaces/text.ts | 12 +- packages/slate/src/interfaces/types.ts | 19 - packages/slate/src/transforms/node.ts | 41 +-- packages/slate/src/transforms/selection.ts | 58 +-- packages/slate/src/transforms/text.ts | 67 ++-- 12 files changed, 436 insertions(+), 372 deletions(-) delete mode 100644 packages/slate/src/interfaces/types.ts diff --git a/packages/slate/src/create-editor.ts b/packages/slate/src/create-editor.ts index 8b8051478..7fc54974f 100644 --- a/packages/slate/src/create-editor.ts +++ b/packages/slate/src/create-editor.ts @@ -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)) { diff --git a/packages/slate/src/interfaces/editor.ts b/packages/slate/src/interfaces/editor.ts index 4d2b4aa1d..dd0cb7f87 100644 --- a/packages/slate/src/interfaces/editor.ts +++ b/packages/slate/src/interfaces/editor.ts @@ -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 - /** * 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 | 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 { - at?: Location - match?: NodeMatch - 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 { - at?: Location - match?: NodeMatch - reverse?: boolean - voids?: boolean -} - -export interface EditorNextOptions { - at?: Location - match?: NodeMatch - mode?: SelectionMode - voids?: boolean -} - -export interface EditorNodeOptions { - depth?: number - edge?: LeafEdge -} - -export interface EditorNodesOptions { - at?: Location | Span - match?: NodeMatch - 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 { - at?: Location - match?: NodeMatch - 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: ( editor: Editor, - options?: EditorAboveOptions + options?: { + at?: Location + match?: NodeMatch + mode?: 'highest' | 'lowest' + voids?: boolean + } ) => NodeEntry | 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 levels: ( editor: Editor, - options?: EditorLevelsOptions + options?: { + at?: Location + match?: NodeMatch + reverse?: boolean + voids?: boolean + } ) => Generator, void, undefined> marks: (editor: Editor) => Omit | null next: ( editor: Editor, - options?: EditorNextOptions + options?: { + at?: Location + match?: NodeMatch + mode?: 'all' | 'highest' | 'lowest' + voids?: boolean + } ) => NodeEntry | undefined - node: (editor: Editor, at: Location, options?: EditorNodeOptions) => NodeEntry + node: ( + editor: Editor, + at: Location, + options?: { + depth?: number + edge?: 'start' | 'end' + } + ) => NodeEntry nodes: ( editor: Editor, - options?: EditorNodesOptions + options?: { + at?: Location | Span + match?: NodeMatch + mode?: 'all' | 'highest' | 'lowest' + universal?: boolean + reverse?: boolean + voids?: boolean + } ) => Generator, 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 - 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 - 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 positions: ( editor: Editor, - options?: EditorPositionsOptions + options?: { + at?: Location + unit?: 'offset' | 'character' | 'word' | 'line' | 'block' + reverse?: boolean + voids?: boolean + } ) => Generator previous: ( editor: Editor, - options?: EditorPreviousOptions + options?: { + at?: Location + match?: NodeMatch + mode?: 'all' | 'highest' | 'lowest' + voids?: boolean + } ) => NodeEntry | 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 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 | undefined withoutNormalizing: (editor: Editor, fn: () => void) => void } @@ -333,7 +296,12 @@ export const Editor: EditorInterface = { above( editor: Editor, - options: EditorAboveOptions = {} + options: { + at?: Location + match?: NodeMatch + mode?: 'highest' | 'lowest' + voids?: boolean + } = {} ): NodeEntry | 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 { const path = Editor.path(editor, at, options) const node = Node.leaf(editor, path) @@ -730,7 +715,12 @@ export const Editor: EditorInterface = { *levels( editor: Editor, - options: EditorLevelsOptions = {} + options: { + at?: Location + match?: NodeMatch + reverse?: boolean + voids?: boolean + } = {} ): Generator, void, undefined> { const { at = editor.selection, reverse = false, voids = false } = options let { match } = options @@ -822,7 +812,12 @@ export const Editor: EditorInterface = { next( editor: Editor, - options: EditorNextOptions = {} + options: { + at?: Location + match?: NodeMatch + mode?: 'all' | 'highest' | 'lowest' + voids?: boolean + } = {} ): NodeEntry | 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( editor: Editor, - options: EditorNodesOptions = {} + options: { + at?: Location | Span + match?: NodeMatch + mode?: 'all' | 'highest' | 'lowest' + universal?: boolean + reverse?: boolean + voids?: boolean + } = {} ): Generator, 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 { 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 { const { at = editor.selection, @@ -1434,7 +1469,12 @@ export const Editor: EditorInterface = { previous( editor: Editor, - options: EditorPreviousOptions = {} + options: { + at?: Location + match?: NodeMatch + mode?: 'all' | 'highest' | 'lowest' + voids?: boolean + } = {} ): NodeEntry | 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 | undefined { return Editor.above(editor, { ...options, diff --git a/packages/slate/src/interfaces/node.ts b/packages/slate/src/interfaces/node.ts index d78339f1a..e12a3a764 100644 --- a/packages/slate/src/interfaces/node.ts +++ b/packages/slate/src/interfaces/node.ts @@ -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, void, undefined> child: (root: Node, index: number) => Descendant children: ( root: Node, path: Path, - options?: NodeChildrenOptions + options?: { + reverse?: boolean + } ) => Generator, 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, void, undefined> elements: ( root: Node, - options?: NodeElementsOptions + options?: { + from?: Path + to?: Path + reverse?: boolean + pass?: (node: NodeEntry) => boolean + } ) => Generator 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 matches: (node: Node, props: Partial) => boolean nodes: ( root: Node, - options?: NodeNodesOptions + options?: { + from?: Path + to?: Path + reverse?: boolean + pass?: (entry: NodeEntry) => boolean + } ) => Generator 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, void, undefined> } @@ -129,7 +115,9 @@ export const Node: NodeInterface = { *ancestors( root: Node, path: Path, - options: NodeAncestorsOptions = {} + options: { + reverse?: boolean + } = {} ): Generator, 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, 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, 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 { 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 { 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 { 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, void, undefined> { for (const [node, path] of Node.nodes(root, options)) { if (Text.isText(node)) { diff --git a/packages/slate/src/interfaces/path.ts b/packages/slate/src/interfaces/path.ts index 085508671..37981338d 100644 --- a/packages/slate/src/interfaces/path.ts +++ b/packages/slate/src/interfaces/path.ts @@ -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 diff --git a/packages/slate/src/interfaces/point-ref.ts b/packages/slate/src/interfaces/point-ref.ts index c46327e44..95222b971 100644 --- a/packages/slate/src/interfaces/point-ref.ts +++ b/packages/slate/src/interfaces/point-ref.ts @@ -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 } diff --git a/packages/slate/src/interfaces/point.ts b/packages/slate/src/interfaces/point.ts index 5ea788ff8..1997c34a6 100644 --- a/packages/slate/src/interfaces/point.ts +++ b/packages/slate/src/interfaces/point.ts @@ -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) { diff --git a/packages/slate/src/interfaces/range.ts b/packages/slate/src/interfaces/range.ts index 03668443b..16352acbb 100644 --- a/packages/slate/src/interfaces/range.ts +++ b/packages/slate/src/interfaces/range.ts @@ -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) { diff --git a/packages/slate/src/interfaces/text.ts b/packages/slate/src/interfaces/text.ts index a876a67db..a7a8a490e 100644 --- a/packages/slate/src/interfaces/text.ts +++ b/packages/slate/src/interfaces/text.ts @@ -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 @@ -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) { diff --git a/packages/slate/src/interfaces/types.ts b/packages/slate/src/interfaces/types.ts deleted file mode 100644 index 32a898144..000000000 --- a/packages/slate/src/interfaces/types.ts +++ /dev/null @@ -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' diff --git a/packages/slate/src/transforms/node.ts b/packages/slate/src/transforms/node.ts index 7574ebc84..11f3791dc 100644 --- a/packages/slate/src/transforms/node.ts +++ b/packages/slate/src/transforms/node.ts @@ -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: ( @@ -22,7 +21,7 @@ export interface NodeTransforms { options?: { at?: Location match?: NodeMatch - mode?: RangeMode + mode?: 'highest' | 'lowest' hanging?: boolean select?: boolean voids?: boolean @@ -33,7 +32,7 @@ export interface NodeTransforms { options?: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' voids?: boolean } ) => void @@ -42,7 +41,7 @@ export interface NodeTransforms { options?: { at?: Location match?: NodeMatch - mode?: RangeMode + mode?: 'highest' | 'lowest' hanging?: boolean voids?: boolean } @@ -52,7 +51,7 @@ export interface NodeTransforms { options: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' to: Path voids?: boolean } @@ -62,7 +61,7 @@ export interface NodeTransforms { options?: { at?: Location match?: NodeMatch - mode?: RangeMode + mode?: 'highest' | 'lowest' hanging?: boolean voids?: boolean } @@ -73,7 +72,7 @@ export interface NodeTransforms { options?: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' hanging?: boolean split?: boolean voids?: boolean @@ -86,7 +85,7 @@ export interface NodeTransforms { options?: { at?: Location match?: NodeMatch - mode?: RangeMode + mode?: 'highest' | 'lowest' always?: boolean height?: number voids?: boolean @@ -98,7 +97,7 @@ export interface NodeTransforms { options?: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' split?: boolean voids?: boolean } @@ -108,7 +107,7 @@ export interface NodeTransforms { options?: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' split?: boolean voids?: boolean } @@ -119,7 +118,7 @@ export interface NodeTransforms { options?: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' split?: boolean voids?: boolean } @@ -137,7 +136,7 @@ export const NodeTransforms: NodeTransforms = { options: { at?: Location match?: NodeMatch - mode?: RangeMode + mode?: 'highest' | 'lowest' hanging?: boolean select?: boolean voids?: boolean @@ -256,7 +255,7 @@ export const NodeTransforms: NodeTransforms = { options: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' voids?: boolean } = {} ): void { @@ -320,7 +319,7 @@ export const NodeTransforms: NodeTransforms = { options: { at?: Location match?: NodeMatch - mode?: RangeMode + mode?: 'highest' | 'lowest' hanging?: boolean voids?: boolean } = {} @@ -460,7 +459,7 @@ export const NodeTransforms: NodeTransforms = { options: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' to: Path voids?: boolean } @@ -521,7 +520,7 @@ export const NodeTransforms: NodeTransforms = { options: { at?: Location match?: NodeMatch - mode?: RangeMode + mode?: 'highest' | 'lowest' hanging?: boolean voids?: boolean } = {} @@ -568,7 +567,7 @@ export const NodeTransforms: NodeTransforms = { options: { at?: Location match?: NodeMatch - 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 - mode?: RangeMode + mode?: 'highest' | 'lowest' always?: boolean height?: number voids?: boolean @@ -822,7 +821,7 @@ export const NodeTransforms: NodeTransforms = { options: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' split?: boolean voids?: boolean } = {} @@ -850,7 +849,7 @@ export const NodeTransforms: NodeTransforms = { options: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' split?: boolean voids?: boolean } = {} @@ -916,7 +915,7 @@ export const NodeTransforms: NodeTransforms = { options: { at?: Location match?: NodeMatch - mode?: MaximizeMode + mode?: 'all' | 'highest' | 'lowest' split?: boolean voids?: boolean } = {} diff --git a/packages/slate/src/transforms/selection.ts b/packages/slate/src/transforms/selection.ts index 91275caed..d82dffc54 100644 --- a/packages/slate/src/transforms/selection.ts +++ b/packages/slate/src/transforms/selection.ts @@ -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, - options?: SelectionSetPointOptions + options?: { + edge?: 'anchor' | 'focus' | 'start' | 'end' + } ) => void setSelection: (editor: Editor, props: Partial) => 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, - options: SelectionSetPointOptions = {} + options: { + edge?: 'anchor' | 'focus' | 'start' | 'end' + } = {} ): void { const { selection } = editor let { edge = 'both' } = options diff --git a/packages/slate/src/transforms/text.ts b/packages/slate/src/transforms/text.ts index 406876554..51c9f6a0e 100644 --- a/packages/slate/src/transforms/text.ts +++ b/packages/slate/src/transforms/text.ts @@ -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