From 11ef4f5d38ee557563198dc2397473f9b27a16c8 Mon Sep 17 00:00:00 2001 From: YangusKhan <9inchsamurai@gmail.com> Date: Mon, 16 Jul 2018 15:05:49 -0600 Subject: [PATCH 1/3] remove the module declaration from the base slate package to simplify importing --- types/slate-base64-serializer/index.d.ts | 10 +- .../slate-base64-serializer-tests.ts | 4 +- types/slate-html-serializer/index.d.ts | 22 +- types/slate-plain-serializer/index.d.ts | 10 +- .../slate-plain-serializer-tests.ts | 4 +- types/slate-react/index.d.ts | 72 +- types/slate-react/slate-react-tests.tsx | 8 +- types/slate/index.d.ts | 1413 +++++++++-------- types/slate/slate-tests.ts | 4 +- 9 files changed, 774 insertions(+), 773 deletions(-) diff --git a/types/slate-base64-serializer/index.d.ts b/types/slate-base64-serializer/index.d.ts index 86f626fc86..6d356aa1c3 100644 --- a/types/slate-base64-serializer/index.d.ts +++ b/types/slate-base64-serializer/index.d.ts @@ -3,9 +3,9 @@ // Definitions by: Brandon Shelton // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 -import { Slate } from "slate"; +import { Value, Node } from "slate"; -export function deserialize(string: string, options?: object): Slate.Value; -export function deserializeNode(string: string, options?: object): Slate.Node; -export function serialize(value: Slate.Value, options?: object): string; -export function serializeNode(node: Slate.Node, options?: object): string; +export function deserialize(string: string, options?: object): Value; +export function deserializeNode(string: string, options?: object): Node; +export function serialize(value: Value, options?: object): string; +export function serializeNode(node: Node, options?: object): string; diff --git a/types/slate-base64-serializer/slate-base64-serializer-tests.ts b/types/slate-base64-serializer/slate-base64-serializer-tests.ts index 3d1b2888b6..680bcf62bd 100644 --- a/types/slate-base64-serializer/slate-base64-serializer-tests.ts +++ b/types/slate-base64-serializer/slate-base64-serializer-tests.ts @@ -1,6 +1,6 @@ import * as Base64 from "slate-base64-serializer"; -import { Slate } from "slate"; +import { Value } from "slate"; -const val = Slate.Value.create(); +const val = Value.create(); const serialized = Base64.serialize(val); const deserialized = Base64.deserialize(serialized); diff --git a/types/slate-html-serializer/index.d.ts b/types/slate-html-serializer/index.d.ts index eb9290955a..cee3041d69 100644 --- a/types/slate-html-serializer/index.d.ts +++ b/types/slate-html-serializer/index.d.ts @@ -4,7 +4,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 import * as React from 'react'; -import { Slate } from 'slate'; +import { BlockProperties, ValueJSON, Value, Node, Mark, Leaf } from 'slate'; export interface Rule { deserialize?: (el: Element, next: (elements: Element[] | NodeList | Array) => any) => any; @@ -13,28 +13,28 @@ export interface Rule { export interface HtmlOptions { rules?: Rule[]; - defaultBlock?: Slate.BlockProperties; + defaultBlock?: BlockProperties; parseHtml?: (html: string) => HTMLElement; } export default class Html { constructor(options?: HtmlOptions); - deserialize(html: string, options: { toJSON: true }): Slate.ValueJSON; - deserialize(html: string, options?: { toJSON?: false }): Slate.Value; + deserialize(html: string, options: { toJSON: true }): ValueJSON; + deserialize(html: string, options?: { toJSON?: false }): Value; - serialize(value: Slate.Value, options?: { render?: true }): string; - serialize(value: Slate.Value, options: { render: false }): Element[]; + serialize(value: Value, options?: { render?: true }): string; + serialize(value: Value, options: { render: false }): Element[]; protected rules: Rule[]; - protected defaultBlock: Slate.BlockProperties; + protected defaultBlock: BlockProperties; protected parseHtml: (html: string) => HTMLElement; - protected deserializeElements: (elements: HTMLElement[]) => Slate.Node[]; + protected deserializeElements: (elements: HTMLElement[]) => Node[]; protected deserializeElement: (element: HTMLElement) => any; - protected deserializeMark: (mark: Slate.Mark) => Slate.Node[]; + protected deserializeMark: (mark: Mark) => Node[]; - protected serializeNode: (node: Slate.Node) => string; - protected serializeLeaf: (leaf: Slate.Leaf) => string; + protected serializeNode: (node: Node) => string; + protected serializeLeaf: (leaf: Leaf) => string; protected serializeString: (string: string) => string; } diff --git a/types/slate-plain-serializer/index.d.ts b/types/slate-plain-serializer/index.d.ts index 778f523168..b781b4f9e7 100644 --- a/types/slate-plain-serializer/index.d.ts +++ b/types/slate-plain-serializer/index.d.ts @@ -3,13 +3,13 @@ // Definitions by: Brandon Shelton // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 -import { Slate } from 'slate'; +import { BlockProperties, MarkProperties, Value } from 'slate'; export interface DeserializeOptions { toJson?: boolean; - defaultBlock?: Slate.BlockProperties; - defaultMarks?: Slate.MarkProperties[] | Set; + defaultBlock?: BlockProperties; + defaultMarks?: MarkProperties[] | Set; } -export function deserialize(string: string, options?: DeserializeOptions): Slate.Value; -export function serialize(value: Slate.Value): string; +export function deserialize(string: string, options?: DeserializeOptions): Value; +export function serialize(value: Value): string; diff --git a/types/slate-plain-serializer/slate-plain-serializer-tests.ts b/types/slate-plain-serializer/slate-plain-serializer-tests.ts index 7d7db6ff84..c7b7f97e07 100644 --- a/types/slate-plain-serializer/slate-plain-serializer-tests.ts +++ b/types/slate-plain-serializer/slate-plain-serializer-tests.ts @@ -1,6 +1,6 @@ import * as Plain from "slate-plain-serializer"; -import { Slate } from "slate"; +import { Value } from "slate"; -const val = Slate.Value.create(); +const val = Value.create(); const serialized = Plain.serialize(val); const deserialized = Plain.deserialize(serialized); diff --git a/types/slate-react/index.d.ts b/types/slate-react/index.d.ts index 492156363a..3ef179a14b 100644 --- a/types/slate-react/index.d.ts +++ b/types/slate-react/index.d.ts @@ -7,7 +7,7 @@ // Brandon Shelton // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 -import { Slate } from 'slate'; +import { Mark, Node, Block, Change, Schema, Value, Stack } from 'slate'; import * as Immutable from 'immutable'; import * as React from 'react'; @@ -20,9 +20,9 @@ export interface RenderMarkProps { attributes: RenderAttributes; children: React.ReactNode; editor: Editor; - mark: Slate.Mark; - marks: Immutable.Set; - node: Slate.Node; + mark: Mark; + marks: Immutable.Set; + node: Node; offset: number; text: string; } @@ -33,26 +33,26 @@ export interface RenderNodeProps { editor: Editor; isSelected: boolean; key: string; - node: Slate.Block; - parent: Slate.Node; + node: Block; + parent: Node; } export interface Plugin { - onBeforeInput?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onBlur?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onFocus?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onClick?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onCopy?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onCut?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onDrop?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onKeyDown?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onKeyUp?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onPaste?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onSelect?: (event: Event, change: Slate.Change, editor: Editor) => Slate.Change | void; - onChange?: (change: Slate.Change) => any; + onBeforeInput?: (event: Event, change: Change, editor: Editor) => Change | void; + onBlur?: (event: Event, change: Change, editor: Editor) => Change | void; + onFocus?: (event: Event, change: Change, editor: Editor) => Change | void; + onClick?: (event: Event, change: Change, editor: Editor) => Change | void; + onCopy?: (event: Event, change: Change, editor: Editor) => Change | void; + onCut?: (event: Event, change: Change, editor: Editor) => Change | void; + onDrop?: (event: Event, change: Change, editor: Editor) => Change | void; + onKeyDown?: (event: Event, change: Change, editor: Editor) => Change | void; + onKeyUp?: (event: Event, change: Change, editor: Editor) => Change | void; + onPaste?: (event: Event, change: Change, editor: Editor) => Change | void; + onSelect?: (event: Event, change: Change, editor: Editor) => Change | void; + onChange?: (change: Change) => any; renderEditor?: (props: RenderAttributes, editor: Editor) => object | void; - schema?: Slate.Schema; - decorateNode?: (node: Slate.Node) => Slate.Range[] | void; + schema?: Schema; + decorateNode?: (node: Node) => Range[] | void; renderMark?: (props: RenderMarkProps) => any; renderNode?: (props: RenderNodeProps) => any; renderPlaceholder?: (props: RenderAttributes) => any; @@ -61,16 +61,16 @@ export interface Plugin { } export interface BasicEditorProps { - value: Slate.Value; + value: Value; autoCorrect?: boolean; autoFocus?: boolean; className?: string; - onChange?: (change: Slate.Change) => any; + onChange?: (change: Change) => any; placeholder?: any; plugins?: Plugin[]; readOnly?: boolean; role?: string; - schema?: Slate.Schema; + schema?: Schema; spellCheck?: boolean; style?: { [key: string]: string }; tabIndex?: number; @@ -80,29 +80,29 @@ export interface BasicEditorProps { export type EditorProps = BasicEditorProps & Plugin; export interface EditorState { - schema: Slate.Schema; - value: Slate.Value; - stack: Slate.Stack; // [TODO] define stack + schema: Schema; + value: Value; + stack: Stack; // [TODO] define stack } export class Editor extends React.Component { - schema: Slate.Schema; - value: Slate.Value; - stack: Slate.Stack; + schema: Schema; + value: Value; + stack: Stack; // Instance Methods blur(): void; - change(fn: (change: Slate.Change) => any): void; + change(fn: (change: Change) => any): void; change(...args: any[]): void; focus(): void; } export type SlateType = 'fragment' | 'html' | 'node' | 'rich' | 'text' | 'files'; -export function findDOMNode(node: Slate.Node): Element; -export function findDOMRange(range: Slate.Range): Range; -export function findNode(element: Element, value: Slate.Value): Slate.Node; -export function findRange(selection: Selection, value: Slate.Value): Slate.Range; -export function getEventRange(event: Event, value: Slate.Value): Slate.Range; -export function getEventTransfer(event: Event): { type: SlateType; node: Slate.Node }; +export function findDOMNode(node: Node): Element; +export function findDOMRange(range: Range): Range; +export function findNode(element: Element, value: Value): Node; +export function findRange(selection: Selection, value: Value): Range; +export function getEventRange(event: Event, value: Value): Range; +export function getEventTransfer(event: Event): { type: SlateType; node: Node }; export function setEventTransfer(event: Event, type: SlateType, data: any): void; diff --git a/types/slate-react/slate-react-tests.tsx b/types/slate-react/slate-react-tests.tsx index f576ea580c..074f6caa9b 100644 --- a/types/slate-react/slate-react-tests.tsx +++ b/types/slate-react/slate-react-tests.tsx @@ -1,9 +1,9 @@ import { Editor, Plugin, EditorProps } from "slate-react"; -import { Slate } from "slate"; +import { Change, Value } from "slate"; import * as React from "react"; class MyPlugin implements Plugin { - onChange(change: Slate.Change): void { + onChange(change: Change): void { change.blur(); } } @@ -11,14 +11,14 @@ class MyPlugin implements Plugin { const myPlugin = new MyPlugin(); interface MyEditorState { - value: Slate.Value; + value: Value; } class MyEditor extends React.Component { constructor(props: EditorProps) { super(props); this.state = { - value: Slate.Value.create() + value: Value.create() }; } diff --git a/types/slate/index.d.ts b/types/slate/index.d.ts index b73746414d..91fc084056 100644 --- a/types/slate/index.d.ts +++ b/types/slate/index.d.ts @@ -8,714 +8,715 @@ // TypeScript Version: 2.3 import * as Immutable from 'immutable'; -export namespace Slate { - interface Data { - [key: string]: any; - } +export interface Data { + [key: string]: any; +} - interface RulesByNodeType { - [key: string]: Rules; - } +export interface RulesByNodeType { + [key: string]: Rules; +} - interface KindsAndTypes { +export interface KindsAndTypes { + kinds?: string[]; + types?: string[]; +} + +export type InvalidReason = + | 'child_kind_invalid' + | 'child_required' + | 'child_type_invalid' + | 'child_unknown' + | 'first_child_kind_invalid' + | 'first_child_type_invalid' + | 'last_child_kind_invalid' + | 'last_child_type_invalid' + | 'node_data_invalid' + | 'node_is_void_invalid' + | 'node_mark_invalid' + | 'node_text_invalid' + | 'parent_kind_invalid' + | 'parent_type_invalid'; + +export interface Rules { + data?: { + [key: string]: (v: any) => boolean; + }; + first?: KindsAndTypes; + isVoid?: boolean; + last?: KindsAndTypes; + nodes?: Array<{ kinds?: string[]; types?: string[]; - } - - type InvalidReason = - | 'child_kind_invalid' - | 'child_required' - | 'child_type_invalid' - | 'child_unknown' - | 'first_child_kind_invalid' - | 'first_child_type_invalid' - | 'last_child_kind_invalid' - | 'last_child_type_invalid' - | 'node_data_invalid' - | 'node_is_void_invalid' - | 'node_mark_invalid' - | 'node_text_invalid' - | 'parent_kind_invalid' - | 'parent_type_invalid'; - - interface Rules { - data?: { - [key: string]: (v: any) => boolean; - }; - first?: KindsAndTypes; - isVoid?: boolean; - last?: KindsAndTypes; - nodes?: Array<{ - kinds?: string[]; - types?: string[]; - min?: number; - max?: number; - }>; - normalize?: ( - change: Change, - reason: InvalidReason, - context: { [key: string]: any } - ) => void; - parent?: KindsAndTypes; - text?: RegExp; - } - - interface SchemaProperties { - document?: Rules; - blocks?: RulesByNodeType; - inlines?: RulesByNodeType; - } - - class Schema extends Immutable.Record({}) { - document: Rules; - blocks: RulesByNodeType; - inlines: RulesByNodeType; - - static create(properties: SchemaProperties | Schema): Schema; - static fromJSON(object: SchemaProperties): Schema; - static isSchema(maybeSchema: any): maybeSchema is Schema; - - toJSON(): SchemaProperties; - } - - interface ValueProperties { - document?: Document; - selection?: Range; - history?: History; - schema?: Schema; - data?: Data; - decorations?: Immutable.List | null; - } - - interface ValueJSON { - document?: DocumentJSON; - selection?: Range; - history?: History; - schema?: Schema; - data?: Data; - decorations?: Immutable.List | null; - object?: 'value'; - } - - class Value extends Immutable.Record({}) { - document: Document; - selection: Range; - history: History; - schema: Schema; - data: Data; - object: 'value'; - decorations: Immutable.List | null; - - readonly anchorText: Text; - readonly focusText: Text; - readonly startText: Text; - readonly endText: Text; - - readonly anchorBlock: Block; - readonly focusBlock: Block; - readonly startBlock: Block; - readonly endBlock: Block; - - readonly marks: Immutable.Set; - readonly activeMarks: Immutable.Set; - readonly blocks: Immutable.List; - readonly fragment: Document; - readonly inlines: Immutable.List; - readonly text: Immutable.List; - readonly characters: Immutable.List; - readonly hasUndos: boolean; - readonly hasRedos: boolean; - - readonly anchorKey: string; - readonly focusKey: string; - readonly startKey: string; - readonly endKey: string; - readonly anchorOffset: number; - readonly focusOffset: number; - readonly startOffset: number; - readonly endOffset: number; - readonly isBackward: boolean; - readonly isBlurred: boolean; - readonly isCollapsed: boolean; - readonly isExpanded: boolean; - readonly isFocused: boolean; - readonly isForward: boolean; - - static create(properties?: ValueProperties | Value): Value; - static fromJSON(properties: ValueJSON): Value; - static isValue(maybeValue: any): maybeValue is Value; - - change(): Change; - toJSON(): ValueJSON; - } - - interface DocumentProperties { - nodes?: Immutable.List | Node[]; - key?: string; - data?: Immutable.Map | { [key: string]: any }; - } - - interface DocumentJSON { - nodes?: NodeJSON[]; - key?: string; - data?: { [key: string]: any }; - object?: 'document'; - } - - class Document extends BaseNode< - DataMap - > { - object: 'document'; - nodes: Immutable.List; - - static create( - properties: DocumentProperties | Document | Immutable.List | Node[] - ): Document; - static fromJSON(properties: DocumentProperties | Document): Document; - static isDocument(maybeDocument: any): maybeDocument is Document; - - toJSON(): DocumentJSON; - } - - interface BlockProperties { - type: string; - key?: string; - nodes?: Immutable.List; - isVoid?: boolean; - data?: Immutable.Map | { [key: string]: any }; - } - - interface BlockJSON { - type: string; - key?: string; - nodes?: NodeJSON[]; - isVoid?: boolean; - data?: { [key: string]: any }; - object: 'block'; - } - - class Block extends BaseNode { - isVoid: boolean; - object: 'block'; - nodes: Immutable.List; - - static create(properties: BlockProperties | Block | string): Block; - static createList( - array: (BlockProperties[] | Block[] | string[]) - ): Immutable.List; - static fromJSON(properties: BlockProperties | Block): Block; - static isBlock(maybeBlock: any): maybeBlock is Block; - - toJSON(): BlockJSON; - } - - interface InlineProperties { - type: string; - key?: string; - nodes?: Immutable.List; - isVoid?: boolean; - data?: Immutable.Map | { [key: string]: any }; - } - - interface InlineJSON { - type: string; - key?: string; - nodes?: NodeJSON[]; - isVoid?: boolean; - data?: { [key: string]: any }; - object: 'inline'; - } - - class Inline extends BaseNode { - isVoid: boolean; - object: 'inline'; - nodes: Immutable.List; - - static create(properties: InlineProperties | Inline | string): Inline; - static createList( - array: (InlineProperties[] | Inline[] | string[]) - ): Immutable.List; - static fromJSON(properties: InlineProperties | Inline): Inline; - static isInline(maybeInline: any): maybeInline is Inline; - - toJSON(): InlineJSON; - } - - interface Leaf { - marks?: Mark[]; - text: string; - } - - interface TextProperties { - key?: string; - characters: Immutable.List; - } - - interface TextJSON { - key?: string; - characters?: Character[]; - leaves: Leaf[]; - object: 'text'; - } - - class Text extends Immutable.Record({}) { - object: 'text'; - characters: Immutable.List; - key: string; - text: string; - - static create(properties: TextProperties | Text | string): Text; - static fromJSON(properties: TextProperties | Text): Text; - static isText(maybeText: any): maybeText is Text; - - toJSON(): TextJSON; - } - - type Node = Document | Block | Inline | Text; - type NodeJSON = DocumentJSON | BlockJSON | InlineJSON | TextJSON; - - class BaseNode extends Immutable.Record( - {} - ) { - data: Immutable.Map; - type: string; - key: string; - object: 'document' | 'block' | 'inline' | 'text'; - nodes: Immutable.List; - readonly text: string; - - filterDescendants(iterator: (node: Node) => boolean): Immutable.List; - findDescendants(iterator: (node: Node) => boolean): Node | null; - getBlocksAtRange(range: Range): Immutable.List; - getBlocks(): Immutable.List; - getCharactersAtRange(range: Range): Immutable.List; - getChild(key: string | Node): Node | null; - getClosestBlock(key: string | Node): Block | null; - getClosestInline(key: string | Node): Inline | null; - getClosest(key: string | Node, match: (node: Node) => boolean): Node | null; - getDepth(key: string | Node): number; - getDescendant(key: string | Node): Node | null; - getFirstText(): Text | null; - getFragmentAtRange(range: Range): Document; - getFurthest(key: string, iterator: (node: Node) => boolean): Node | null; - getFurthestAncestor(key: string): Node | null; - getFurthestBlock(key: string): Block | null; - getFurthestInline(key: string): Inline | null; - getFurthestOnlyChildAncestor(key: string): Node | null; - getInlinesAtRange(range: Range): Immutable.List; - getLastText(): Text | null; - getMarksAtRange(range: Range): Immutable.Set; - getNextBlock(key: string | Node): Block | null; - getNextSibling(key: string | Node): Node | null; - getNextText(key: string | Node): Text | null; - getParent(key: string | Node): Node | null; - getPreviousBlock(key: string | Node): Block | null; - getPreviousSibling(key: string | Node): Node | null; - getPreviousText(key: string | Node): Text | null; - getTexts(): Immutable.List; - getTextsAsArray(): Text[]; - getTextAtOffset(offset: number): Text | null; - getTextsAtRange(range: Range): Immutable.List; - getTextsAtRangeAsArray(range: Range): Text[]; - hasChild(key: string | Node): boolean; - } - - interface CharacterProperties { - marks?: Immutable.Set | Mark[]; - text: string; - } - - class Character extends Immutable.Record({}) { - object: 'character'; - marks: Immutable.Set; - text: string; - - static create( - properties: CharacterProperties | Character | string - ): Character; - static createList( - array: (CharacterProperties[] | Character[] | string[]) - ): Immutable.List; - static fromJSON(properties: CharacterProperties | Character): Character; - static isCharacter(maybeCharacter: any): maybeCharacter is Character; - - toJSON(): CharacterProperties; - } - - interface MarkProperties { - type: string; - data?: Immutable.Map | { [key: string]: any }; - } - - interface MarkJSON { - type: string; - data?: { [key: string]: any }; - } - - class Mark extends Immutable.Record({}) { - object: 'mark'; - type: string; - data: Immutable.Map; - - static create(properties: MarkProperties | Mark | string): Mark; - static createSet( - array: (MarkProperties[] | Mark[] | string[]) - ): Immutable.Set; - static fromJSON(properties: MarkJSON | Mark): Mark; - static isMark(maybeMark: any): maybeMark is Mark; - - toJSON(): MarkProperties; - } - - class Change extends Immutable.Record({}) { - object: 'change'; - value: Value; - operations: Immutable.List; - - call(customChange: (change: Change, ...args: any[]) => Change): Change; - - applyOperations(operations: Operation[]): Change; - applyOperation(operation: Operation): Change; - - // Full Value Change - setValue(properties: Value | ValueProperties): Change; - - // Current Value Changes - deleteBackward(n: number): Change; - deleteForward(n: number): Change; - delete(): Change; - insertBlock(block: Block | BlockProperties | string): Change; - insertFragment(fragment: Document): Change; - insertInline(inline: Inline | InlineProperties): Change; - insertText(text: string): Change; - addMark(mark: Mark | MarkProperties | string): Change; - setBlocks(properties: BlockProperties | string): Change; - setInlines(properties: InlineProperties | string): Change; - splitBlock(depth: number): Change; - splitInline(depth: number): Change; - removeMark(mark: Mark | MarkProperties | string): Change; - toggleMark(mark: Mark | MarkProperties | string): Change; - unwrapBlock(properties: BlockProperties | string): Change; - unwrapInline(properties: InlineProperties | string): Change; - wrapBlock(properties: BlockProperties | string): Change; - wrapInline(properties: InlineProperties | string): Change; - wrapText(prefix: string, suffix?: string): Change; - - // Selection Changes - blur(): Change; - collapseToAnchor(): Change; - collapseToFocus(): Change; - collapseToStart(): Change; - collapseToEnd(): Change; - collapseToStartOf(node: Node): Change; - collapseToEndOf(node: Node): Change; - collapseToStartOfNextBlock(): Change; - collapseToEndOfNextBlock(): Change; - collapseToStartOfPreviousBlock(): Change; - collapseToEndOfPreviousBlock(): Change; - collapseToStartOfNextText(): Change; - collapseToEndOfNextText(): Change; - collapseToStartOfPreviousText(): Change; - collapseToEndOfPreviousText(): Change; - extend(n: number): Change; - extendToStartOf(node: Node): Change; - extendToEndOf(node: Node): Change; - flip(): Change; - focus(): Change; - move(n: number): Change; - moveStart(n: number): Change; - moveEnd(n: number): Change; - moveOffsetsTo(anchorOffset: number, focusOffset: number): Change; - moveToRangeOf(node: Node): Change; - select(properties: Range | RangeProperties): Change; - selectAll(): Change; - deselect(): Change; - - // Document Changes - deleteBackwardAtRange(range: Range, n: number): Change; - deleteForwardAtRange(range: Range, n: number): Change; - deleteAtRange(range: Range): Change; - insertBlockAtRange( - range: Range, - block: Block | BlockProperties | string - ): Change; - insertFragmentAtRange(range: Range, fragment: Document): Change; - insertInlineAtRange( - range: Range, - inline: Inline | InlineProperties - ): Change; - insertTextAtRange(range: Range, text: string): Change; - addMarkAtRange(range: Range, mark: Mark | MarkProperties | string): Change; - setBlocksAtRange(range: Range, properties: BlockProperties | string): Change; - setInlinesAtRange( - range: Range, - properties: InlineProperties | string - ): Change; - splitBlockAtRange(range: Range, depth: number): Change; - splitInlineAtRange(range: Range, depth: number): Change; - removeMarkAtRange( - range: Range, - mark: Mark | MarkProperties | string - ): Change; - toggleMarkAtRange( - range: Range, - mark: Mark | MarkProperties | string - ): Change; - unwrapBlockAtRange( - range: Range, - properties: BlockProperties | string - ): Change; - unwrapInlineAtRange( - range: Range, - properties: InlineProperties | string - ): Change; - wrapBlockAtRange( - range: Range, - properties: BlockProperties | string - ): Change; - wrapInlineAtRange( - range: Range, - properties: InlineProperties | string - ): Change; - wrapTextAtRange(range: Range, prefix: string, suffix?: string): Change; - - // Node Changes - addMarkByKey( - key: string, - offset: number, - length: number, - mark: Mark - ): Change; - insertNodeByKey(key: string, index: number, node: Node): Change; - insertFragmentByKey(key: string, index: number, fragment: Document): Change; - insertTextByKey( - key: string, - offset: number, - text: string, - marks?: Immutable.Set | Mark[] - ): Change; - moveNodeByKey(key: string, newKey: string, newIndex: number): Change; - removeMarkByKey( - key: string, - offset: number, - length: number, - mark: Mark - ): Change; - removeNodeByKey(key: string): Change; - replaceNodeByKey(key: string, node: Node): Change; - removeTextByKey(key: string, offset: number, length: number): Change; - setMarkByKey( - key: string, - offset: number, - length: number, - mark: Mark, - properties: MarkProperties - ): Change; - setNodeByKey( - key: string, - properties: BlockProperties | InlineProperties | string - ): Change; - splitNodeByKey(key: string, offset: number): Change; - unwrapInlineByKey( - key: string, - properties: InlineProperties | string - ): Change; - unwrapBlockByKey(key: string, properties: BlockProperties | string): Change; - unwrapNodeByKey(key: string): Change; - wrapInlineByKey(key: string, properties: InlineProperties | string): Change; - wrapBlockByKey(key: string, properties: BlockProperties | string): Change; - - // History Changes - redo(): Change; - undo(): Change; - } - - interface RangeProperties { - anchorKey?: string | null; - anchorOffset?: number; - focusKey?: string | null; - focusOffset?: number; - isFocused?: boolean; - isBackward?: boolean | null; - marks?: Immutable.Set | null; - } - - interface RangeJSON { - anchorKey?: string | null; - anchorOffset?: number; - focusKey?: string | null; - focusOffset?: number; - isFocused?: boolean; - isBackward?: boolean | null; - marks?: MarkJSON[] | null; - } - - class Range extends Immutable.Record({}) { - object: 'range'; - anchorKey: string | null; - anchorOffset: number; - focusKey: string | null; - focusOffset: number; - isFocused: boolean; - isBackward: boolean | null; - marks: Immutable.Set | null; - - readonly isBlurred: boolean; - readonly isCollapsed: boolean; - readonly isExpanded: boolean; - readonly isForward: boolean; - readonly startKey: string; - readonly startOffset: number; - readonly endKey: string; - readonly endOffset: number; - - static create(properties: RangeProperties | Range): Range; - static fromJSON(properties: RangeJSON): Range; - static isRange(maybeRange: any): maybeRange is Range; - - toJSON(): RangeProperties; - - hasAnchorAtStartOf(node: Node): boolean; - hasFocusAtStartOf(node: Node): boolean; - hasStartAtStartOf(node: Node): boolean; - hasEndAtStartOf(node: Node): boolean; - hasEdgeAtStartOf(node: Node): boolean; - - hasAnchorAtEndOf(node: Node): boolean; - hasFocusAtEndOf(node: Node): boolean; - hasStartAtEndOf(node: Node): boolean; - hasEndAtEndOf(node: Node): boolean; - hasEdgeAtEndOf(node: Node): boolean; - - hasAnchorBetween(node: Node, start: number, end: number): boolean; - hasFocusBetween(node: Node, start: number, end: number): boolean; - hasStartBetween(node: Node, start: number, end: number): boolean; - hasEndBetween(node: Node, start: number, end: number): boolean; - hasEdgeBetween(node: Node, start: number, end: number): boolean; - - hasAnchorIn(node: Node): boolean; - hasFocusIn(node: Node): boolean; - hasStartIn(node: Node): boolean; - hasEndIn(node: Node): boolean; - hasEdgeIn(node: Node): boolean; - - isAtStartOf(node: Node): boolean; - isAtEndOf(node: Node): boolean; - } - - type Operation = - | InsertTextOperation - | RemoveTextOperation - | AddMarkOperation - | RemoveMarkOperation - | SetMarkOperation - | InsertNodeOperation - | MergeNodeOperation - | MoveNodeOperation - | RemoveNodeOperation - | SetNodeOperation - | SplitNodeOperation - | SetSelectionOperation - | SetValueOperation; - - interface InsertTextOperation { - type: 'insert_text'; - path: number[]; - offset: number; - text: string; - marks: Mark[]; - } - - interface RemoveTextOperation { - type: 'remove_text'; - path: number[]; - offset: number; - text: string; - } - - interface AddMarkOperation { - type: 'add_mark'; - path: number[]; - offset: number; - length: number; - mark: Mark; - } - - interface RemoveMarkOperation { - type: 'remove_mark'; - path: number[]; - offset: number; - length: number; - mark: Mark; - } - - interface SetMarkOperation { - type: 'set_mark'; - path: number[]; - offset: number; - length: number; - mark: Mark; - properties: MarkProperties; - } - - interface InsertNodeOperation { - type: 'insert_node'; - path: number[]; - node: Node; - } - - interface MergeNodeOperation { - type: 'merge_node'; - path: number[]; - position: number; - } - - interface MoveNodeOperation { - type: 'move_node'; - path: number[]; - newPath: number[]; - } - - interface RemoveNodeOperation { - type: 'remove_node'; - path: number[]; - node: Node; - } - - interface SetNodeOperation { - type: 'set_node'; - path: number[]; - properties: BlockProperties | InlineProperties | TextProperties; - } - - interface SplitNodeOperation { - type: 'split_node'; - path: number[]; - position: number; - target: number; - } - - interface SetSelectionOperation { - type: 'set_selection'; - properties: RangeProperties; - selection: Range; - } - - interface SetValueOperation { - type: 'set_value'; - properties: ValueProperties; - value: Value; - } - - const Operations: { - apply: (value: Value, operation: Operation) => Value; - invert: (operation: Operation) => Operation; - }; - - class Stack extends Immutable.Record({}) { - plugins: any[]; - } - - function setKeyGenerator(func: () => string): null; - function resetKeyGenerator(): null; + min?: number; + max?: number; + }>; + normalize?: ( + change: Change, + reason: InvalidReason, + context: { [key: string]: any } + ) => void; + parent?: KindsAndTypes; + text?: RegExp; } + +export interface SchemaProperties { + document?: Rules; + blocks?: RulesByNodeType; + inlines?: RulesByNodeType; +} + +export class Schema extends Immutable.Record({}) { + document: Rules; + blocks: RulesByNodeType; + inlines: RulesByNodeType; + + static create(properties: SchemaProperties | Schema): Schema; + static fromJSON(object: SchemaProperties): Schema; + static isSchema(maybeSchema: any): maybeSchema is Schema; + + toJSON(): SchemaProperties; +} + +export interface ValueProperties { + document?: Document; + selection?: Range; + history?: History; + schema?: Schema; + data?: Data; + decorations?: Immutable.List | null; +} + +export interface ValueJSON { + document?: DocumentJSON; + selection?: Range; + history?: History; + schema?: Schema; + data?: Data; + decorations?: Immutable.List | null; + object?: 'value'; +} + +export class Value extends Immutable.Record({}) { + document: Document; + selection: Range; + history: History; + schema: Schema; + data: Data; + object: 'value'; + decorations: Immutable.List | null; + + readonly anchorText: Text; + readonly focusText: Text; + readonly startText: Text; + readonly endText: Text; + + readonly anchorBlock: Block; + readonly focusBlock: Block; + readonly startBlock: Block; + readonly endBlock: Block; + + readonly marks: Immutable.Set; + readonly activeMarks: Immutable.Set; + readonly blocks: Immutable.List; + readonly fragment: Document; + readonly inlines: Immutable.List; + readonly text: Immutable.List; + readonly characters: Immutable.List; + readonly hasUndos: boolean; + readonly hasRedos: boolean; + + readonly anchorKey: string; + readonly focusKey: string; + readonly startKey: string; + readonly endKey: string; + readonly anchorOffset: number; + readonly focusOffset: number; + readonly startOffset: number; + readonly endOffset: number; + readonly isBackward: boolean; + readonly isBlurred: boolean; + readonly isCollapsed: boolean; + readonly isExpanded: boolean; + readonly isFocused: boolean; + readonly isForward: boolean; + + static create(properties?: ValueProperties | Value): Value; + static fromJSON(properties: ValueJSON): Value; + static isValue(maybeValue: any): maybeValue is Value; + + change(): Change; + toJSON(): ValueJSON; +} + +export interface DocumentProperties { + nodes?: Immutable.List | Node[]; + key?: string; + data?: Immutable.Map | { [key: string]: any }; +} + +export interface DocumentJSON { + nodes?: NodeJSON[]; + key?: string; + data?: { [key: string]: any }; + object?: 'document'; +} + +export class Document extends BaseNode< + DataMap +> { + object: 'document'; + nodes: Immutable.List; + + static create( + properties: DocumentProperties | Document | Immutable.List | Node[] + ): Document; + static fromJSON(properties: DocumentProperties | Document): Document; + static isDocument(maybeDocument: any): maybeDocument is Document; + + toJSON(): DocumentJSON; +} + +export interface BlockProperties { + type: string; + key?: string; + nodes?: Immutable.List; + isVoid?: boolean; + data?: Immutable.Map | { [key: string]: any }; +} + +export interface BlockJSON { + type: string; + key?: string; + nodes?: NodeJSON[]; + isVoid?: boolean; + data?: { [key: string]: any }; + object: 'block'; +} + +export class Block extends BaseNode { + isVoid: boolean; + object: 'block'; + nodes: Immutable.List; + + static create(properties: BlockProperties | Block | string): Block; + static createList( + array: (BlockProperties[] | Block[] | string[]) + ): Immutable.List; + static fromJSON(properties: BlockProperties | Block): Block; + static isBlock(maybeBlock: any): maybeBlock is Block; + + toJSON(): BlockJSON; +} + +export interface InlineProperties { + type: string; + key?: string; + nodes?: Immutable.List; + isVoid?: boolean; + data?: Immutable.Map | { [key: string]: any }; +} + +export interface InlineJSON { + type: string; + key?: string; + nodes?: NodeJSON[]; + isVoid?: boolean; + data?: { [key: string]: any }; + object: 'inline'; +} + +export class Inline extends BaseNode { + isVoid: boolean; + object: 'inline'; + nodes: Immutable.List; + + static create(properties: InlineProperties | Inline | string): Inline; + static createList( + array: (InlineProperties[] | Inline[] | string[]) + ): Immutable.List; + static fromJSON(properties: InlineProperties | Inline): Inline; + static isInline(maybeInline: any): maybeInline is Inline; + + toJSON(): InlineJSON; +} + +export interface Leaf { + marks?: Mark[]; + text: string; +} + +export interface TextProperties { + key?: string; + characters: Immutable.List; +} + +export interface TextJSON { + key?: string; + characters?: Character[]; + leaves: Leaf[]; + object: 'text'; +} + +export class Text extends Immutable.Record({}) { + object: 'text'; + characters: Immutable.List; + key: string; + text: string; + + static create(properties: TextProperties | Text | string): Text; + static fromJSON(properties: TextProperties | Text): Text; + static isText(maybeText: any): maybeText is Text; + + toJSON(): TextJSON; +} + +export type Node = Document | Block | Inline | Text; +export type NodeJSON = DocumentJSON | BlockJSON | InlineJSON | TextJSON; + +// tslint:disable-next-line strict-export-declare-modifiers +declare class BaseNode extends Immutable.Record( + {} +) { + data: Immutable.Map; + type: string; + key: string; + object: 'document' | 'block' | 'inline' | 'text'; + nodes: Immutable.List; + readonly text: string; + + filterDescendants(iterator: (node: Node) => boolean): Immutable.List; + findDescendants(iterator: (node: Node) => boolean): Node | null; + getBlocksAtRange(range: Range): Immutable.List; + getBlocks(): Immutable.List; + getCharactersAtRange(range: Range): Immutable.List; + getChild(key: string | Node): Node | null; + getClosestBlock(key: string | Node): Block | null; + getClosestInline(key: string | Node): Inline | null; + getClosest(key: string | Node, match: (node: Node) => boolean): Node | null; + getDepth(key: string | Node): number; + getDescendant(key: string | Node): Node | null; + getFirstText(): Text | null; + getFragmentAtRange(range: Range): Document; + getFurthest(key: string, iterator: (node: Node) => boolean): Node | null; + getFurthestAncestor(key: string): Node | null; + getFurthestBlock(key: string): Block | null; + getFurthestInline(key: string): Inline | null; + getFurthestOnlyChildAncestor(key: string): Node | null; + getInlinesAtRange(range: Range): Immutable.List; + getLastText(): Text | null; + getMarksAtRange(range: Range): Immutable.Set; + getNextBlock(key: string | Node): Block | null; + getNextSibling(key: string | Node): Node | null; + getNextText(key: string | Node): Text | null; + getParent(key: string | Node): Node | null; + getPreviousBlock(key: string | Node): Block | null; + getPreviousSibling(key: string | Node): Node | null; + getPreviousText(key: string | Node): Text | null; + getTexts(): Immutable.List; + getTextsAsArray(): Text[]; + getTextAtOffset(offset: number): Text | null; + getTextsAtRange(range: Range): Immutable.List; + getTextsAtRangeAsArray(range: Range): Text[]; + hasChild(key: string | Node): boolean; +} + +export interface CharacterProperties { + marks?: Immutable.Set | Mark[]; + text: string; +} + +export class Character extends Immutable.Record({}) { + object: 'character'; + marks: Immutable.Set; + text: string; + + static create( + properties: CharacterProperties | Character | string + ): Character; + static createList( + array: (CharacterProperties[] | Character[] | string[]) + ): Immutable.List; + static fromJSON(properties: CharacterProperties | Character): Character; + static isCharacter(maybeCharacter: any): maybeCharacter is Character; + + toJSON(): CharacterProperties; +} + +export interface MarkProperties { + type: string; + data?: Immutable.Map | { [key: string]: any }; +} + +export interface MarkJSON { + type: string; + data?: { [key: string]: any }; +} + +export class Mark extends Immutable.Record({}) { + object: 'mark'; + type: string; + data: Immutable.Map; + + static create(properties: MarkProperties | Mark | string): Mark; + static createSet( + array: (MarkProperties[] | Mark[] | string[]) + ): Immutable.Set; + static fromJSON(properties: MarkJSON | Mark): Mark; + static isMark(maybeMark: any): maybeMark is Mark; + + toJSON(): MarkProperties; +} + +export class Change extends Immutable.Record({}) { + object: 'change'; + value: Value; + operations: Immutable.List; + + call(customChange: (change: Change, ...args: any[]) => Change): Change; + + applyOperations(operations: Operation[]): Change; + applyOperation(operation: Operation): Change; + + // Full Value Change + setValue(properties: Value | ValueProperties): Change; + + // Current Value Changes + deleteBackward(n: number): Change; + deleteForward(n: number): Change; + delete(): Change; + insertBlock(block: Block | BlockProperties | string): Change; + insertFragment(fragment: Document): Change; + insertInline(inline: Inline | InlineProperties): Change; + insertText(text: string): Change; + addMark(mark: Mark | MarkProperties | string): Change; + setBlocks(properties: BlockProperties | string): Change; + setInlines(properties: InlineProperties | string): Change; + splitBlock(depth: number): Change; + splitInline(depth: number): Change; + removeMark(mark: Mark | MarkProperties | string): Change; + toggleMark(mark: Mark | MarkProperties | string): Change; + unwrapBlock(properties: BlockProperties | string): Change; + unwrapInline(properties: InlineProperties | string): Change; + wrapBlock(properties: BlockProperties | string): Change; + wrapInline(properties: InlineProperties | string): Change; + wrapText(prefix: string, suffix?: string): Change; + + // Selection Changes + blur(): Change; + collapseToAnchor(): Change; + collapseToFocus(): Change; + collapseToStart(): Change; + collapseToEnd(): Change; + collapseToStartOf(node: Node): Change; + collapseToEndOf(node: Node): Change; + collapseToStartOfNextBlock(): Change; + collapseToEndOfNextBlock(): Change; + collapseToStartOfPreviousBlock(): Change; + collapseToEndOfPreviousBlock(): Change; + collapseToStartOfNextText(): Change; + collapseToEndOfNextText(): Change; + collapseToStartOfPreviousText(): Change; + collapseToEndOfPreviousText(): Change; + extend(n: number): Change; + extendToStartOf(node: Node): Change; + extendToEndOf(node: Node): Change; + flip(): Change; + focus(): Change; + move(n: number): Change; + moveStart(n: number): Change; + moveEnd(n: number): Change; + moveOffsetsTo(anchorOffset: number, focusOffset: number): Change; + moveToRangeOf(node: Node): Change; + select(properties: Range | RangeProperties): Change; + selectAll(): Change; + deselect(): Change; + + // Document Changes + deleteBackwardAtRange(range: Range, n: number): Change; + deleteForwardAtRange(range: Range, n: number): Change; + deleteAtRange(range: Range): Change; + insertBlockAtRange( + range: Range, + block: Block | BlockProperties | string + ): Change; + insertFragmentAtRange(range: Range, fragment: Document): Change; + insertInlineAtRange( + range: Range, + inline: Inline | InlineProperties + ): Change; + insertTextAtRange(range: Range, text: string): Change; + addMarkAtRange(range: Range, mark: Mark | MarkProperties | string): Change; + setBlocksAtRange(range: Range, properties: BlockProperties | string): Change; + setInlinesAtRange( + range: Range, + properties: InlineProperties | string + ): Change; + splitBlockAtRange(range: Range, depth: number): Change; + splitInlineAtRange(range: Range, depth: number): Change; + removeMarkAtRange( + range: Range, + mark: Mark | MarkProperties | string + ): Change; + toggleMarkAtRange( + range: Range, + mark: Mark | MarkProperties | string + ): Change; + unwrapBlockAtRange( + range: Range, + properties: BlockProperties | string + ): Change; + unwrapInlineAtRange( + range: Range, + properties: InlineProperties | string + ): Change; + wrapBlockAtRange( + range: Range, + properties: BlockProperties | string + ): Change; + wrapInlineAtRange( + range: Range, + properties: InlineProperties | string + ): Change; + wrapTextAtRange(range: Range, prefix: string, suffix?: string): Change; + + // Node Changes + addMarkByKey( + key: string, + offset: number, + length: number, + mark: Mark + ): Change; + insertNodeByKey(key: string, index: number, node: Node): Change; + insertFragmentByKey(key: string, index: number, fragment: Document): Change; + insertTextByKey( + key: string, + offset: number, + text: string, + marks?: Immutable.Set | Mark[] + ): Change; + moveNodeByKey(key: string, newKey: string, newIndex: number): Change; + removeMarkByKey( + key: string, + offset: number, + length: number, + mark: Mark + ): Change; + removeNodeByKey(key: string): Change; + replaceNodeByKey(key: string, node: Node): Change; + removeTextByKey(key: string, offset: number, length: number): Change; + setMarkByKey( + key: string, + offset: number, + length: number, + mark: Mark, + properties: MarkProperties + ): Change; + setNodeByKey( + key: string, + properties: BlockProperties | InlineProperties | string + ): Change; + splitNodeByKey(key: string, offset: number): Change; + unwrapInlineByKey( + key: string, + properties: InlineProperties | string + ): Change; + unwrapBlockByKey(key: string, properties: BlockProperties | string): Change; + unwrapNodeByKey(key: string): Change; + wrapInlineByKey(key: string, properties: InlineProperties | string): Change; + wrapBlockByKey(key: string, properties: BlockProperties | string): Change; + + // History Changes + redo(): Change; + undo(): Change; +} + +export interface RangeProperties { + anchorKey?: string | null; + anchorOffset?: number; + focusKey?: string | null; + focusOffset?: number; + isFocused?: boolean; + isBackward?: boolean | null; + marks?: Immutable.Set | null; +} + +export interface RangeJSON { + anchorKey?: string | null; + anchorOffset?: number; + focusKey?: string | null; + focusOffset?: number; + isFocused?: boolean; + isBackward?: boolean | null; + marks?: MarkJSON[] | null; +} + +export class Range extends Immutable.Record({}) { + object: 'range'; + anchorKey: string | null; + anchorOffset: number; + focusKey: string | null; + focusOffset: number; + isFocused: boolean; + isBackward: boolean | null; + marks: Immutable.Set | null; + + readonly isBlurred: boolean; + readonly isCollapsed: boolean; + readonly isExpanded: boolean; + readonly isForward: boolean; + readonly startKey: string; + readonly startOffset: number; + readonly endKey: string; + readonly endOffset: number; + + static create(properties: RangeProperties | Range): Range; + static fromJSON(properties: RangeJSON): Range; + static isRange(maybeRange: any): maybeRange is Range; + + toJSON(): RangeProperties; + + hasAnchorAtStartOf(node: Node): boolean; + hasFocusAtStartOf(node: Node): boolean; + hasStartAtStartOf(node: Node): boolean; + hasEndAtStartOf(node: Node): boolean; + hasEdgeAtStartOf(node: Node): boolean; + + hasAnchorAtEndOf(node: Node): boolean; + hasFocusAtEndOf(node: Node): boolean; + hasStartAtEndOf(node: Node): boolean; + hasEndAtEndOf(node: Node): boolean; + hasEdgeAtEndOf(node: Node): boolean; + + hasAnchorBetween(node: Node, start: number, end: number): boolean; + hasFocusBetween(node: Node, start: number, end: number): boolean; + hasStartBetween(node: Node, start: number, end: number): boolean; + hasEndBetween(node: Node, start: number, end: number): boolean; + hasEdgeBetween(node: Node, start: number, end: number): boolean; + + hasAnchorIn(node: Node): boolean; + hasFocusIn(node: Node): boolean; + hasStartIn(node: Node): boolean; + hasEndIn(node: Node): boolean; + hasEdgeIn(node: Node): boolean; + + isAtStartOf(node: Node): boolean; + isAtEndOf(node: Node): boolean; +} + +export type Operation = + | InsertTextOperation + | RemoveTextOperation + | AddMarkOperation + | RemoveMarkOperation + | SetMarkOperation + | InsertNodeOperation + | MergeNodeOperation + | MoveNodeOperation + | RemoveNodeOperation + | SetNodeOperation + | SplitNodeOperation + | SetSelectionOperation + | SetValueOperation; + +export interface InsertTextOperation { + type: 'insert_text'; + path: number[]; + offset: number; + text: string; + marks: Mark[]; +} + +export interface RemoveTextOperation { + type: 'remove_text'; + path: number[]; + offset: number; + text: string; +} + +export interface AddMarkOperation { + type: 'add_mark'; + path: number[]; + offset: number; + length: number; + mark: Mark; +} + +export interface RemoveMarkOperation { + type: 'remove_mark'; + path: number[]; + offset: number; + length: number; + mark: Mark; +} + +export interface SetMarkOperation { + type: 'set_mark'; + path: number[]; + offset: number; + length: number; + mark: Mark; + properties: MarkProperties; +} + +export interface InsertNodeOperation { + type: 'insert_node'; + path: number[]; + node: Node; +} + +export interface MergeNodeOperation { + type: 'merge_node'; + path: number[]; + position: number; +} + +export interface MoveNodeOperation { + type: 'move_node'; + path: number[]; + newPath: number[]; +} + +export interface RemoveNodeOperation { + type: 'remove_node'; + path: number[]; + node: Node; +} + +export interface SetNodeOperation { + type: 'set_node'; + path: number[]; + properties: BlockProperties | InlineProperties | TextProperties; +} + +export interface SplitNodeOperation { + type: 'split_node'; + path: number[]; + position: number; + target: number; +} + +export interface SetSelectionOperation { + type: 'set_selection'; + properties: RangeProperties; + selection: Range; +} + +export interface SetValueOperation { + type: 'set_value'; + properties: ValueProperties; + value: Value; +} + +export const Operations: { + apply: (value: Value, operation: Operation) => Value; + invert: (operation: Operation) => Operation; +}; + +export class Stack extends Immutable.Record({}) { + plugins: any[]; +} + +export function setKeyGenerator(func: () => string): null; +export function resetKeyGenerator(): null; + +export {}; diff --git a/types/slate/slate-tests.ts b/types/slate/slate-tests.ts index 45aca77e7e..2c07a4571b 100644 --- a/types/slate/slate-tests.ts +++ b/types/slate/slate-tests.ts @@ -1,6 +1,6 @@ -import { Slate } from "slate"; +import { Value } from "slate"; -const value = Slate.Value.create(); +const value = Value.create(); value.change() .focus() .selectAll() From c3101e546fdb9c8da768a464d8a0eaa013a11d3b Mon Sep 17 00:00:00 2001 From: YangusKhan <9inchsamurai@gmail.com> Date: Fri, 10 Aug 2018 09:26:20 -0600 Subject: [PATCH 2/3] update typings to 0.33. add History class. change RenderNodeProps to accept a Node instead of Block. add methods to Data class. --- types/slate-react/index.d.ts | 2 +- types/slate-react/slate-react-tests.tsx | 23 +++++- types/slate/index.d.ts | 94 +++++++++++++++++-------- types/slate/slate-tests.ts | 68 ++++++++++++------ 4 files changed, 135 insertions(+), 52 deletions(-) diff --git a/types/slate-react/index.d.ts b/types/slate-react/index.d.ts index 0bf5c255a9..c79b945a5b 100644 --- a/types/slate-react/index.d.ts +++ b/types/slate-react/index.d.ts @@ -33,7 +33,7 @@ export interface RenderNodeProps { editor: Editor; isSelected: boolean; key: string; - node: Block; + node: Node; parent: Node; } diff --git a/types/slate-react/slate-react-tests.tsx b/types/slate-react/slate-react-tests.tsx index 074f6caa9b..d8c8ea0073 100644 --- a/types/slate-react/slate-react-tests.tsx +++ b/types/slate-react/slate-react-tests.tsx @@ -1,8 +1,24 @@ -import { Editor, Plugin, EditorProps } from "slate-react"; +import { Editor, Plugin, EditorProps, RenderNodeProps } from "slate-react"; import { Change, Value } from "slate"; import * as React from "react"; class MyPlugin implements Plugin { + renderNode(props: RenderNodeProps) { + const { node } = props; + if (node) { + switch (node.object) { + case "block": + return
; + case "inline": + return Hello world; + case "text": + return

Hello world

; + default: + return undefined; + } + } + } + onChange(change: Change): void { change.blur(); } @@ -23,6 +39,9 @@ class MyEditor extends React.Component { } render() { - return ; + return ; } } diff --git a/types/slate/index.d.ts b/types/slate/index.d.ts index 781257488a..4eaa88a123 100644 --- a/types/slate/index.d.ts +++ b/types/slate/index.d.ts @@ -5,48 +5,54 @@ // Jan Löbel // Brandon Shelton // Kalley Powell +// Francesco Agnoletto // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 import * as Immutable from 'immutable'; -export interface Data { +type StringObject = Record; + +export class Data { [key: string]: any; + static create(options: Data): Data; + static fromJSON(object: StringObject): Data; + static fromJS(object: StringObject): Data; } export interface RulesByNodeType { [key: string]: Rules; } -export interface KindsAndTypes { - kinds?: string[]; +export interface ObjectsAndTypes { + objects?: string[]; types?: string[]; } export type InvalidReason = - | 'child_kind_invalid' + | 'child_object_invalid' | 'child_required' | 'child_type_invalid' | 'child_unknown' - | 'first_child_kind_invalid' + | 'first_child_object_invalid' | 'first_child_type_invalid' - | 'last_child_kind_invalid' + | 'last_child_object_invalid' | 'last_child_type_invalid' | 'node_data_invalid' | 'node_is_void_invalid' | 'node_mark_invalid' | 'node_text_invalid' - | 'parent_kind_invalid' + | 'parent_object_invalid' | 'parent_type_invalid'; export interface Rules { data?: { [key: string]: (v: any) => boolean; }; - first?: KindsAndTypes; + first?: ObjectsAndTypes; isVoid?: boolean; - last?: KindsAndTypes; + last?: ObjectsAndTypes; nodes?: Array<{ - kinds?: string[]; + objects?: string[]; types?: string[]; min?: number; max?: number; @@ -54,9 +60,9 @@ export interface Rules { normalize?: ( change: Change, reason: InvalidReason, - context: { [key: string]: any } + context: StringObject ) => void; - parent?: KindsAndTypes; + parent?: ObjectsAndTypes; text?: RegExp; } @@ -73,17 +79,37 @@ export class Schema extends Immutable.Record({}) { static create(properties: SchemaProperties | Schema): Schema; static fromJSON(object: SchemaProperties): Schema; + static fromJS(object: SchemaProperties): Schema; static isSchema(maybeSchema: any): maybeSchema is Schema; toJSON(): SchemaProperties; } +export interface HistoryJSON { + object?: 'history'; + redos?: Immutable.Stack; + undos?: Immutable.Stack; +} + +export class History extends Immutable.Record({ redos: Immutable.Stack(), undos: Immutable.Stack() }) { + object: 'history'; + + static create(properties: HistoryJSON | History): History; + static createOperationsList(operations: Operation[] | Immutable.List): Immutable.List; + static fromJSON(object: HistoryJSON): History; + static fromJS(object: HistoryJSON): History; + static isHistory(maybeHistory: any): maybeHistory is History; + + save(operation: Operation, options: { merge?: boolean, skip?: boolean }): History; + toJSON(): HistoryJSON; +} + export interface ValueProperties { document?: Document; selection?: Range; history?: History; schema?: Schema; - data?: Data; + data?: StringObject; decorations?: Immutable.List | null; } @@ -92,7 +118,7 @@ export interface ValueJSON { selection?: Range; history?: History; schema?: Schema; - data?: Data; + data?: StringObject; decorations?: Immutable.List | null; object?: 'value'; } @@ -102,7 +128,7 @@ export class Value extends Immutable.Record({}) { selection: Range; history: History; schema: Schema; - data: Data; + data: StringObject; object: 'value'; decorations: Immutable.List | null; @@ -148,6 +174,7 @@ export class Value extends Immutable.Record({}) { static create(properties?: ValueProperties | Value): Value; static fromJSON(properties: ValueJSON): Value; + static fromJS(properties: ValueJSON): Value; static isValue(maybeValue: any): maybeValue is Value; change(): Change; @@ -157,17 +184,17 @@ export class Value extends Immutable.Record({}) { export interface DocumentProperties { nodes?: Immutable.List | Node[]; key?: string; - data?: Immutable.Map | { [key: string]: any }; + data?: Immutable.Map | StringObject; } export interface DocumentJSON { nodes?: NodeJSON[]; key?: string; - data?: { [key: string]: any }; + data?: StringObject; object?: 'document'; } -export class Document extends BaseNode< +export class Document extends BaseNode< DataMap > { object: 'document'; @@ -176,7 +203,8 @@ export class Document extends BaseNode< static create( properties: DocumentProperties | Document | Immutable.List | Node[] ): Document; - static fromJSON(properties: DocumentProperties | Document): Document; + static fromJSON(properties: DocumentJSON | Document): Document; + static fromJS(properties: DocumentJSON | Document): Document; static isDocument(maybeDocument: any): maybeDocument is Document; toJSON(): DocumentJSON; @@ -187,7 +215,7 @@ export interface BlockProperties { key?: string; nodes?: Immutable.List; isVoid?: boolean; - data?: Immutable.Map | { [key: string]: any }; + data?: Immutable.Map | StringObject; } export interface BlockJSON { @@ -195,7 +223,7 @@ export interface BlockJSON { key?: string; nodes?: NodeJSON[]; isVoid?: boolean; - data?: { [key: string]: any }; + data?: StringObject; object: 'block'; } @@ -208,7 +236,8 @@ export class Block extends BaseNode { static createList( array: (BlockProperties[] | Block[] | string[]) ): Immutable.List; - static fromJSON(properties: BlockProperties | Block): Block; + static fromJSON(properties: BlockJSON | Block): Block; + static fromJS(properties: BlockJSON | Block): Block; static isBlock(maybeBlock: any): maybeBlock is Block; toJSON(): BlockJSON; @@ -219,7 +248,7 @@ export interface InlineProperties { key?: string; nodes?: Immutable.List; isVoid?: boolean; - data?: Immutable.Map | { [key: string]: any }; + data?: Immutable.Map | StringObject; } export interface InlineJSON { @@ -227,7 +256,7 @@ export interface InlineJSON { key?: string; nodes?: NodeJSON[]; isVoid?: boolean; - data?: { [key: string]: any }; + data?: StringObject; object: 'inline'; } @@ -240,13 +269,15 @@ export class Inline extends BaseNode { static createList( array: (InlineProperties[] | Inline[] | string[]) ): Immutable.List; - static fromJSON(properties: InlineProperties | Inline): Inline; + static fromJSON(properties: InlineJSON | Inline): Inline; + static fromJS(properties: InlineJSON | Inline): Inline; static isInline(maybeInline: any): maybeInline is Inline; toJSON(): InlineJSON; } export interface Leaf { + object: 'leaf'; marks?: Mark[]; text: string; } @@ -270,7 +301,8 @@ export class Text extends Immutable.Record({}) { text: string; static create(properties: TextProperties | Text | string): Text; - static fromJSON(properties: TextProperties | Text): Text; + static fromJSON(properties: TextJSON | Text): Text; + static fromJS(properties: TextJSON | Text): Text; static isText(maybeText: any): maybeText is Text; toJSON(): TextJSON; @@ -280,7 +312,7 @@ export type Node = Document | Block | Inline | Text; export type NodeJSON = DocumentJSON | BlockJSON | InlineJSON | TextJSON; // tslint:disable-next-line strict-export-declare-modifiers -declare class BaseNode extends Immutable.Record( +declare class BaseNode extends Immutable.Record( {} ) { data: Immutable.Map; @@ -343,6 +375,7 @@ export class Character extends Immutable.Record({}) { array: (CharacterProperties[] | Character[] | string[]) ): Immutable.List; static fromJSON(properties: CharacterProperties | Character): Character; + static fromJS(properties: CharacterProperties | Character): Character; static isCharacter(maybeCharacter: any): maybeCharacter is Character; toJSON(): CharacterProperties; @@ -350,12 +383,12 @@ export class Character extends Immutable.Record({}) { export interface MarkProperties { type: string; - data?: Immutable.Map | { [key: string]: any }; + data?: Immutable.Map | StringObject; } export interface MarkJSON { type: string; - data?: { [key: string]: any }; + data?: StringObject; } export class Mark extends Immutable.Record({}) { @@ -368,6 +401,7 @@ export class Mark extends Immutable.Record({}) { array: (MarkProperties[] | Mark[] | string[]) ): Immutable.Set; static fromJSON(properties: MarkJSON | Mark): Mark; + static fromJS(properties: MarkJSON | Mark): Mark; static isMark(maybeMark: any): maybeMark is Mark; toJSON(): MarkProperties; @@ -379,6 +413,7 @@ export class Change extends Immutable.Record({}) { operations: Immutable.List; call(customChange: (change: Change, ...args: any[]) => Change): Change; + withoutNormalization(customChange: (change: Change) => void): Change; applyOperations(operations: Operation[]): Change; applyOperation(operation: Operation): Change; @@ -577,6 +612,7 @@ export class Range extends Immutable.Record({}) { static create(properties: RangeProperties | Range): Range; static fromJSON(properties: RangeJSON): Range; + static fromJS(properties: RangeJSON): Range; static isRange(maybeRange: any): maybeRange is Range; toJSON(): RangeProperties; diff --git a/types/slate/slate-tests.ts b/types/slate/slate-tests.ts index 2c07a4571b..25f222b4e1 100644 --- a/types/slate/slate-tests.ts +++ b/types/slate/slate-tests.ts @@ -1,21 +1,49 @@ -import { Value } from "slate"; +import { Value, Data, NodeJSON, Document } from "slate"; -const value = Value.create(); -value.change() - .focus() - .selectAll() - .delete() - .insertText('A bit of rich text, followed by...') - .moveOffsetsTo(10, 14) - .addMark('bold') - .collapseToEndOfNextBlock() - .insertBlock({ - type: 'image', - isVoid: true, - data: { - src: 'http://placekitten.com/200/300', - alt: 'Kittens', - className: 'img-responsive', - }, - }) - .insertBlock('paragraph'); +const data = Data.create({ foo: "bar "}); +const value = Value.create({ data }); +const change = value.change() +.focus() +.selectAll() +.delete() +.insertText('A bit of rich text, followed by...') +.moveOffsetsTo(10, 14) +.addMark('bold') +.collapseToEndOfNextBlock() +.insertBlock({ + type: 'image', + isVoid: true, + data: { + src: 'http://placekitten.com/200/300', + alt: 'Kittens', + className: 'img-responsive', + }, +}) +.insertBlock('paragraph'); + +const node: NodeJSON = { + object: "block", + type: "paragraph", + isVoid: false, + data: {}, + nodes: [ + { + object: "text", + leaves: [ + { + object: 'leaf', + text: "example", + marks: [], + } + ] + } + ] +}; + +const doc = Document.fromJSON({ + object: "document", + data: {}, + nodes: [ + node + ], +}); From c94a5307e3beaab79eeadfb35d1422ed3bcb44b8 Mon Sep 17 00:00:00 2001 From: YangusKhan <9inchsamurai@gmail.com> Date: Fri, 10 Aug 2018 09:32:28 -0600 Subject: [PATCH 3/3] remove type alias and use Record directly --- types/slate/index.d.ts | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/types/slate/index.d.ts b/types/slate/index.d.ts index 4eaa88a123..c5d92e0e80 100644 --- a/types/slate/index.d.ts +++ b/types/slate/index.d.ts @@ -10,13 +10,11 @@ // TypeScript Version: 2.3 import * as Immutable from 'immutable'; -type StringObject = Record; - export class Data { [key: string]: any; static create(options: Data): Data; - static fromJSON(object: StringObject): Data; - static fromJS(object: StringObject): Data; + static fromJSON(object: Record): Data; + static fromJS(object: Record): Data; } export interface RulesByNodeType { @@ -60,7 +58,7 @@ export interface Rules { normalize?: ( change: Change, reason: InvalidReason, - context: StringObject + context: Record ) => void; parent?: ObjectsAndTypes; text?: RegExp; @@ -109,7 +107,7 @@ export interface ValueProperties { selection?: Range; history?: History; schema?: Schema; - data?: StringObject; + data?: Record; decorations?: Immutable.List | null; } @@ -118,7 +116,7 @@ export interface ValueJSON { selection?: Range; history?: History; schema?: Schema; - data?: StringObject; + data?: Record; decorations?: Immutable.List | null; object?: 'value'; } @@ -128,7 +126,7 @@ export class Value extends Immutable.Record({}) { selection: Range; history: History; schema: Schema; - data: StringObject; + data: Record; object: 'value'; decorations: Immutable.List | null; @@ -184,17 +182,17 @@ export class Value extends Immutable.Record({}) { export interface DocumentProperties { nodes?: Immutable.List | Node[]; key?: string; - data?: Immutable.Map | StringObject; + data?: Immutable.Map | Record; } export interface DocumentJSON { nodes?: NodeJSON[]; key?: string; - data?: StringObject; + data?: Record; object?: 'document'; } -export class Document extends BaseNode< +export class Document> extends BaseNode< DataMap > { object: 'document'; @@ -215,7 +213,7 @@ export interface BlockProperties { key?: string; nodes?: Immutable.List; isVoid?: boolean; - data?: Immutable.Map | StringObject; + data?: Immutable.Map | Record; } export interface BlockJSON { @@ -223,7 +221,7 @@ export interface BlockJSON { key?: string; nodes?: NodeJSON[]; isVoid?: boolean; - data?: StringObject; + data?: Record; object: 'block'; } @@ -248,7 +246,7 @@ export interface InlineProperties { key?: string; nodes?: Immutable.List; isVoid?: boolean; - data?: Immutable.Map | StringObject; + data?: Immutable.Map | Record; } export interface InlineJSON { @@ -256,7 +254,7 @@ export interface InlineJSON { key?: string; nodes?: NodeJSON[]; isVoid?: boolean; - data?: StringObject; + data?: Record; object: 'inline'; } @@ -312,7 +310,7 @@ export type Node = Document | Block | Inline | Text; export type NodeJSON = DocumentJSON | BlockJSON | InlineJSON | TextJSON; // tslint:disable-next-line strict-export-declare-modifiers -declare class BaseNode extends Immutable.Record( +declare class BaseNode> extends Immutable.Record( {} ) { data: Immutable.Map; @@ -383,12 +381,12 @@ export class Character extends Immutable.Record({}) { export interface MarkProperties { type: string; - data?: Immutable.Map | StringObject; + data?: Immutable.Map | Record; } export interface MarkJSON { type: string; - data?: StringObject; + data?: Record; } export class Mark extends Immutable.Record({}) {