make extension configs extendable
This commit is contained in:
@@ -4,162 +4,164 @@ import { InputRule } from 'prosemirror-inputrules'
|
|||||||
import { Editor } from './Editor'
|
import { Editor } from './Editor'
|
||||||
import { Node } from './Node'
|
import { Node } from './Node'
|
||||||
import mergeDeep from './utilities/mergeDeep'
|
import mergeDeep from './utilities/mergeDeep'
|
||||||
import { GlobalAttributes, RawCommands } from './types'
|
import { GlobalAttributes, RawCommands, ExtensionConfig } from './types'
|
||||||
|
|
||||||
export interface ExtensionConfig<Options = any> {
|
declare module '@tiptap/core' {
|
||||||
/**
|
interface ExtensionConfig<Options = any> {
|
||||||
* Name
|
/**
|
||||||
*/
|
* Name
|
||||||
name: string,
|
*/
|
||||||
|
name: string,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default options
|
* Default options
|
||||||
*/
|
*/
|
||||||
defaultOptions?: Options,
|
defaultOptions?: Options,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global attributes
|
* Global attributes
|
||||||
*/
|
*/
|
||||||
addGlobalAttributes?: (this: {
|
addGlobalAttributes?: (this: {
|
||||||
options: Options,
|
|
||||||
}) => GlobalAttributes | {},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Raw
|
|
||||||
*/
|
|
||||||
addCommands?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
}) => Partial<RawCommands>,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Keyboard shortcuts
|
|
||||||
*/
|
|
||||||
addKeyboardShortcuts?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
}) => {
|
|
||||||
[key: string]: ProseMirrorCommand,
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Input rules
|
|
||||||
*/
|
|
||||||
addInputRules?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
}) => InputRule[],
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Paste rules
|
|
||||||
*/
|
|
||||||
addPasteRules?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
}) => Plugin[],
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ProseMirror plugins
|
|
||||||
*/
|
|
||||||
addProseMirrorPlugins?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
}) => Plugin[],
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extend Node Schema
|
|
||||||
*/
|
|
||||||
extendNodeSchema?: ((
|
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
},
|
}) => GlobalAttributes | {},
|
||||||
extension: Node,
|
|
||||||
) => {
|
|
||||||
[key: string]: any,
|
|
||||||
}) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extend Mark Schema
|
* Raw
|
||||||
*/
|
*/
|
||||||
extendMarkSchema?: ((
|
addCommands?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
|
||||||
},
|
|
||||||
extension: Node,
|
|
||||||
) => {
|
|
||||||
[key: string]: any,
|
|
||||||
}) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The editor is ready.
|
|
||||||
*/
|
|
||||||
onCreate?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
}) => void) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The content has changed.
|
|
||||||
*/
|
|
||||||
onUpdate?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
}) => void) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The selection has changed.
|
|
||||||
*/
|
|
||||||
onSelection?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
}) => void) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The editor state has changed.
|
|
||||||
*/
|
|
||||||
onTransaction?: ((
|
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
},
|
}) => Partial<RawCommands>,
|
||||||
props: {
|
|
||||||
transaction: Transaction,
|
|
||||||
},
|
|
||||||
) => void) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor is focused.
|
* Keyboard shortcuts
|
||||||
*/
|
*/
|
||||||
onFocus?: ((
|
addKeyboardShortcuts?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
|
}) => {
|
||||||
|
[key: string]: ProseMirrorCommand,
|
||||||
},
|
},
|
||||||
props: {
|
|
||||||
event: FocusEvent,
|
|
||||||
},
|
|
||||||
) => void) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor isn’t focused anymore.
|
* Input rules
|
||||||
*/
|
*/
|
||||||
onBlur?: ((
|
addInputRules?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
},
|
}) => InputRule[],
|
||||||
props: {
|
|
||||||
event: FocusEvent,
|
|
||||||
},
|
|
||||||
) => void) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor is destroyed.
|
* Paste rules
|
||||||
*/
|
*/
|
||||||
onDestroy?: ((this: {
|
addPasteRules?: (this: {
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
}) => void) | null,
|
}) => Plugin[],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ProseMirror plugins
|
||||||
|
*/
|
||||||
|
addProseMirrorPlugins?: (this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
}) => Plugin[],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extend Node Schema
|
||||||
|
*/
|
||||||
|
extendNodeSchema?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
extension: Node,
|
||||||
|
) => {
|
||||||
|
[key: string]: any,
|
||||||
|
}) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extend Mark Schema
|
||||||
|
*/
|
||||||
|
extendMarkSchema?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
extension: Node,
|
||||||
|
) => {
|
||||||
|
[key: string]: any,
|
||||||
|
}) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor is ready.
|
||||||
|
*/
|
||||||
|
onCreate?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The content has changed.
|
||||||
|
*/
|
||||||
|
onUpdate?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The selection has changed.
|
||||||
|
*/
|
||||||
|
onSelection?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor state has changed.
|
||||||
|
*/
|
||||||
|
onTransaction?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
transaction: Transaction,
|
||||||
|
},
|
||||||
|
) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor is focused.
|
||||||
|
*/
|
||||||
|
onFocus?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
event: FocusEvent,
|
||||||
|
},
|
||||||
|
) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor isn’t focused anymore.
|
||||||
|
*/
|
||||||
|
onBlur?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
event: FocusEvent,
|
||||||
|
},
|
||||||
|
) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor is destroyed.
|
||||||
|
*/
|
||||||
|
onDestroy?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
}) => void) | null,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Extension<Options = any> {
|
export class Extension<Options = any> {
|
||||||
|
|||||||
@@ -7,188 +7,235 @@ import {
|
|||||||
import { Plugin, Transaction } from 'prosemirror-state'
|
import { Plugin, Transaction } from 'prosemirror-state'
|
||||||
import { Command as ProseMirrorCommand } from 'prosemirror-commands'
|
import { Command as ProseMirrorCommand } from 'prosemirror-commands'
|
||||||
import { InputRule } from 'prosemirror-inputrules'
|
import { InputRule } from 'prosemirror-inputrules'
|
||||||
import { ExtensionConfig } from './Extension'
|
|
||||||
import mergeDeep from './utilities/mergeDeep'
|
import mergeDeep from './utilities/mergeDeep'
|
||||||
import { Attributes, Overwrite, RawCommands } from './types'
|
import {
|
||||||
|
Attributes,
|
||||||
|
RawCommands,
|
||||||
|
GlobalAttributes,
|
||||||
|
MarkConfig,
|
||||||
|
} from './types'
|
||||||
import { Editor } from './Editor'
|
import { Editor } from './Editor'
|
||||||
|
|
||||||
export interface MarkConfig<Options = any> extends Overwrite<ExtensionConfig<Options>, {
|
declare module '@tiptap/core' {
|
||||||
/**
|
export interface MarkConfig<Options = any> {
|
||||||
* Inclusive
|
/**
|
||||||
*/
|
* Name
|
||||||
inclusive?: MarkSpec['inclusive'] | ((this: { options: Options }) => MarkSpec['inclusive']),
|
*/
|
||||||
|
name: string,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Excludes
|
* Default options
|
||||||
*/
|
*/
|
||||||
excludes?: MarkSpec['excludes'] | ((this: { options: Options }) => MarkSpec['excludes']),
|
defaultOptions?: Options,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Group
|
* Global attributes
|
||||||
*/
|
*/
|
||||||
group?: MarkSpec['group'] | ((this: { options: Options }) => MarkSpec['group']),
|
addGlobalAttributes?: (this: {
|
||||||
|
|
||||||
/**
|
|
||||||
* Spanning
|
|
||||||
*/
|
|
||||||
spanning?: MarkSpec['spanning'] | ((this: { options: Options }) => MarkSpec['spanning']),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse HTML
|
|
||||||
*/
|
|
||||||
parseHTML?: (
|
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
},
|
}) => GlobalAttributes | {},
|
||||||
) => MarkSpec['parseDOM'],
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render HTML
|
* Raw
|
||||||
*/
|
*/
|
||||||
renderHTML?: ((
|
addCommands?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
|
||||||
},
|
|
||||||
props: {
|
|
||||||
mark: ProseMirrorMark,
|
|
||||||
HTMLAttributes: { [key: string]: any },
|
|
||||||
}
|
|
||||||
) => DOMOutputSpec) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attributes
|
|
||||||
*/
|
|
||||||
addAttributes?: (
|
|
||||||
this: {
|
|
||||||
options: Options,
|
|
||||||
},
|
|
||||||
) => Attributes | {},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Commands
|
|
||||||
*/
|
|
||||||
addCommands?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: MarkType,
|
|
||||||
}) => Partial<RawCommands>,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Keyboard shortcuts
|
|
||||||
*/
|
|
||||||
addKeyboardShortcuts?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: MarkType,
|
|
||||||
}) => {
|
|
||||||
[key: string]: ProseMirrorCommand,
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Input rules
|
|
||||||
*/
|
|
||||||
addInputRules?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: MarkType,
|
|
||||||
}) => InputRule[],
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Paste rules
|
|
||||||
*/
|
|
||||||
addPasteRules?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: MarkType,
|
|
||||||
}) => Plugin[],
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ProseMirror plugins
|
|
||||||
*/
|
|
||||||
addProseMirrorPlugins?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: MarkType,
|
|
||||||
}) => Plugin[],
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The editor is ready.
|
|
||||||
*/
|
|
||||||
onCreate?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: MarkType,
|
|
||||||
}) => void) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The content has changed.
|
|
||||||
*/
|
|
||||||
onUpdate?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: MarkType,
|
|
||||||
}) => void) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The selection has changed.
|
|
||||||
*/
|
|
||||||
onSelection?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: MarkType,
|
|
||||||
}) => void) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The editor state has changed.
|
|
||||||
*/
|
|
||||||
onTransaction?: ((
|
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
type: MarkType,
|
type: MarkType,
|
||||||
},
|
}) => Partial<RawCommands>,
|
||||||
props: {
|
|
||||||
transaction: Transaction,
|
|
||||||
},
|
|
||||||
) => void) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor is focused.
|
* Keyboard shortcuts
|
||||||
*/
|
*/
|
||||||
onFocus?: ((
|
addKeyboardShortcuts?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
type: MarkType,
|
type: MarkType,
|
||||||
|
}) => {
|
||||||
|
[key: string]: ProseMirrorCommand,
|
||||||
},
|
},
|
||||||
props: {
|
|
||||||
event: FocusEvent,
|
|
||||||
},
|
|
||||||
) => void) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor isn’t focused anymore.
|
* Input rules
|
||||||
*/
|
*/
|
||||||
onBlur?: ((
|
addInputRules?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
type: MarkType,
|
type: MarkType,
|
||||||
},
|
}) => InputRule[],
|
||||||
props: {
|
|
||||||
event: FocusEvent,
|
|
||||||
},
|
|
||||||
) => void) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor is destroyed.
|
* Paste rules
|
||||||
*/
|
*/
|
||||||
onDestroy?: ((this: {
|
addPasteRules?: (this: {
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
type: MarkType,
|
type: MarkType,
|
||||||
}) => void) | null,
|
}) => Plugin[],
|
||||||
}> {}
|
|
||||||
|
/**
|
||||||
|
* ProseMirror plugins
|
||||||
|
*/
|
||||||
|
addProseMirrorPlugins?: (this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: MarkType,
|
||||||
|
}) => Plugin[],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extend Node Schema
|
||||||
|
*/
|
||||||
|
extendNodeSchema?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
extension: Node,
|
||||||
|
) => {
|
||||||
|
[key: string]: any,
|
||||||
|
}) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extend Mark Schema
|
||||||
|
*/
|
||||||
|
extendMarkSchema?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
extension: Node,
|
||||||
|
) => {
|
||||||
|
[key: string]: any,
|
||||||
|
}) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor is ready.
|
||||||
|
*/
|
||||||
|
onCreate?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: MarkType,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The content has changed.
|
||||||
|
*/
|
||||||
|
onUpdate?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: MarkType,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The selection has changed.
|
||||||
|
*/
|
||||||
|
onSelection?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: MarkType,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor state has changed.
|
||||||
|
*/
|
||||||
|
onTransaction?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: MarkType,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
transaction: Transaction,
|
||||||
|
},
|
||||||
|
) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor is focused.
|
||||||
|
*/
|
||||||
|
onFocus?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: MarkType,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
event: FocusEvent,
|
||||||
|
},
|
||||||
|
) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor isn’t focused anymore.
|
||||||
|
*/
|
||||||
|
onBlur?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: MarkType,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
event: FocusEvent,
|
||||||
|
},
|
||||||
|
) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor is destroyed.
|
||||||
|
*/
|
||||||
|
onDestroy?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: MarkType,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inclusive
|
||||||
|
*/
|
||||||
|
inclusive?: MarkSpec['inclusive'] | ((this: { options: Options }) => MarkSpec['inclusive']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Excludes
|
||||||
|
*/
|
||||||
|
excludes?: MarkSpec['excludes'] | ((this: { options: Options }) => MarkSpec['excludes']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Group
|
||||||
|
*/
|
||||||
|
group?: MarkSpec['group'] | ((this: { options: Options }) => MarkSpec['group']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spanning
|
||||||
|
*/
|
||||||
|
spanning?: MarkSpec['spanning'] | ((this: { options: Options }) => MarkSpec['spanning']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse HTML
|
||||||
|
*/
|
||||||
|
parseHTML?: (
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
) => MarkSpec['parseDOM'],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render HTML
|
||||||
|
*/
|
||||||
|
renderHTML?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
mark: ProseMirrorMark,
|
||||||
|
HTMLAttributes: { [key: string]: any },
|
||||||
|
}
|
||||||
|
) => DOMOutputSpec) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attributes
|
||||||
|
*/
|
||||||
|
addAttributes?: (
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
) => Attributes | {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class Mark<Options = any> {
|
export class Mark<Options = any> {
|
||||||
type = 'mark'
|
type = 'mark'
|
||||||
|
|||||||
@@ -7,251 +7,294 @@ import {
|
|||||||
import { Command as ProseMirrorCommand } from 'prosemirror-commands'
|
import { Command as ProseMirrorCommand } from 'prosemirror-commands'
|
||||||
import { Plugin, Transaction } from 'prosemirror-state'
|
import { Plugin, Transaction } from 'prosemirror-state'
|
||||||
import { InputRule } from 'prosemirror-inputrules'
|
import { InputRule } from 'prosemirror-inputrules'
|
||||||
import { ExtensionConfig } from './Extension'
|
|
||||||
import mergeDeep from './utilities/mergeDeep'
|
import mergeDeep from './utilities/mergeDeep'
|
||||||
import {
|
import {
|
||||||
Attributes,
|
Attributes,
|
||||||
NodeViewRenderer,
|
NodeViewRenderer,
|
||||||
Overwrite,
|
GlobalAttributes,
|
||||||
RawCommands,
|
RawCommands,
|
||||||
|
NodeConfig,
|
||||||
} from './types'
|
} from './types'
|
||||||
import { Editor } from './Editor'
|
import { Editor } from './Editor'
|
||||||
|
|
||||||
export interface NodeConfig<Options = any> extends Overwrite<ExtensionConfig<Options>, {
|
declare module '@tiptap/core' {
|
||||||
/**
|
interface NodeConfig<Options = any> {
|
||||||
* TopNode
|
/**
|
||||||
*/
|
* Name
|
||||||
topNode?: boolean,
|
*/
|
||||||
|
name: string,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Content
|
* Default options
|
||||||
*/
|
*/
|
||||||
content?: NodeSpec['content'] | ((this: { options: Options }) => NodeSpec['content']),
|
defaultOptions?: Options,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Marks
|
* Global attributes
|
||||||
*/
|
*/
|
||||||
marks?: NodeSpec['marks'] | ((this: { options: Options }) => NodeSpec['marks']),
|
addGlobalAttributes?: (this: {
|
||||||
|
|
||||||
/**
|
|
||||||
* Group
|
|
||||||
*/
|
|
||||||
group?: NodeSpec['group'] | ((this: { options: Options }) => NodeSpec['group']),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inline
|
|
||||||
*/
|
|
||||||
inline?: NodeSpec['inline'] | ((this: { options: Options }) => NodeSpec['inline']),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Atom
|
|
||||||
*/
|
|
||||||
atom?: NodeSpec['atom'] | ((this: { options: Options }) => NodeSpec['atom']),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Selectable
|
|
||||||
*/
|
|
||||||
selectable?: NodeSpec['selectable'] | ((this: { options: Options }) => NodeSpec['selectable']),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Draggable
|
|
||||||
*/
|
|
||||||
draggable?: NodeSpec['draggable'] | ((this: { options: Options }) => NodeSpec['draggable']),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Code
|
|
||||||
*/
|
|
||||||
code?: NodeSpec['code'] | ((this: { options: Options }) => NodeSpec['code']),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Defining
|
|
||||||
*/
|
|
||||||
defining?: NodeSpec['defining'] | ((this: { options: Options }) => NodeSpec['defining']),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Isolating
|
|
||||||
*/
|
|
||||||
isolating?: NodeSpec['isolating'] | ((this: { options: Options }) => NodeSpec['isolating']),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse HTML
|
|
||||||
*/
|
|
||||||
parseHTML?: (
|
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
},
|
}) => GlobalAttributes | {},
|
||||||
) => NodeSpec['parseDOM'],
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render HTML
|
* Raw
|
||||||
*/
|
*/
|
||||||
renderHTML?: ((
|
addCommands?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
|
||||||
},
|
|
||||||
props: {
|
|
||||||
node: ProseMirrorNode,
|
|
||||||
HTMLAttributes: { [key: string]: any },
|
|
||||||
}
|
|
||||||
) => DOMOutputSpec) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Render Text
|
|
||||||
*/
|
|
||||||
renderText?: ((
|
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
type: NodeType,
|
type: NodeType,
|
||||||
},
|
}) => Partial<RawCommands>,
|
||||||
props: {
|
|
||||||
node: ProseMirrorNode,
|
|
||||||
}
|
|
||||||
) => string) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add Attributes
|
* Keyboard shortcuts
|
||||||
*/
|
*/
|
||||||
addAttributes?: (
|
addKeyboardShortcuts?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
|
||||||
},
|
|
||||||
) => Attributes | {},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Commands
|
|
||||||
*/
|
|
||||||
addCommands?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
}) => Partial<RawCommands>,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Keyboard shortcuts
|
|
||||||
*/
|
|
||||||
addKeyboardShortcuts?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
}) => {
|
|
||||||
[key: string]: ProseMirrorCommand,
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Input rules
|
|
||||||
*/
|
|
||||||
addInputRules?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
}) => InputRule[],
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Paste rules
|
|
||||||
*/
|
|
||||||
addPasteRules?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
}) => Plugin[],
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ProseMirror plugins
|
|
||||||
*/
|
|
||||||
addProseMirrorPlugins?: (this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
}) => Plugin[],
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Node View
|
|
||||||
*/
|
|
||||||
addNodeView?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
}) => NodeViewRenderer) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The editor is ready.
|
|
||||||
*/
|
|
||||||
onCreate?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
}) => void) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The content has changed.
|
|
||||||
*/
|
|
||||||
onUpdate?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
}) => void) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The selection has changed.
|
|
||||||
*/
|
|
||||||
onSelection?: ((this: {
|
|
||||||
options: Options,
|
|
||||||
editor: Editor,
|
|
||||||
type: NodeType,
|
|
||||||
}) => void) | null,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The editor state has changed.
|
|
||||||
*/
|
|
||||||
onTransaction?: ((
|
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
type: NodeType,
|
type: NodeType,
|
||||||
|
}) => {
|
||||||
|
[key: string]: ProseMirrorCommand,
|
||||||
},
|
},
|
||||||
props: {
|
|
||||||
transaction: Transaction,
|
|
||||||
},
|
|
||||||
) => void) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor is focused.
|
* Input rules
|
||||||
*/
|
*/
|
||||||
onFocus?: ((
|
addInputRules?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
type: NodeType,
|
type: NodeType,
|
||||||
},
|
}) => InputRule[],
|
||||||
props: {
|
|
||||||
event: FocusEvent,
|
|
||||||
},
|
|
||||||
) => void) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor isn’t focused anymore.
|
* Paste rules
|
||||||
*/
|
*/
|
||||||
onBlur?: ((
|
addPasteRules?: (this: {
|
||||||
this: {
|
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
type: NodeType,
|
type: NodeType,
|
||||||
},
|
}) => Plugin[],
|
||||||
props: {
|
|
||||||
event: FocusEvent,
|
|
||||||
},
|
|
||||||
) => void) | null,
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor is destroyed.
|
* ProseMirror plugins
|
||||||
*/
|
*/
|
||||||
onDestroy?: ((this: {
|
addProseMirrorPlugins?: (this: {
|
||||||
options: Options,
|
options: Options,
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
type: NodeType,
|
type: NodeType,
|
||||||
}) => void) | null,
|
}) => Plugin[],
|
||||||
}> {}
|
|
||||||
|
/**
|
||||||
|
* Extend Node Schema
|
||||||
|
*/
|
||||||
|
extendNodeSchema?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
extension: Node,
|
||||||
|
) => {
|
||||||
|
[key: string]: any,
|
||||||
|
}) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extend Mark Schema
|
||||||
|
*/
|
||||||
|
extendMarkSchema?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
extension: Node,
|
||||||
|
) => {
|
||||||
|
[key: string]: any,
|
||||||
|
}) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor is ready.
|
||||||
|
*/
|
||||||
|
onCreate?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The content has changed.
|
||||||
|
*/
|
||||||
|
onUpdate?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The selection has changed.
|
||||||
|
*/
|
||||||
|
onSelection?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor state has changed.
|
||||||
|
*/
|
||||||
|
onTransaction?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
transaction: Transaction,
|
||||||
|
},
|
||||||
|
) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor is focused.
|
||||||
|
*/
|
||||||
|
onFocus?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
event: FocusEvent,
|
||||||
|
},
|
||||||
|
) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor isn’t focused anymore.
|
||||||
|
*/
|
||||||
|
onBlur?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
event: FocusEvent,
|
||||||
|
},
|
||||||
|
) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The editor is destroyed.
|
||||||
|
*/
|
||||||
|
onDestroy?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
}) => void) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Node View
|
||||||
|
*/
|
||||||
|
addNodeView?: ((this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
}) => NodeViewRenderer) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TopNode
|
||||||
|
*/
|
||||||
|
topNode?: boolean,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Content
|
||||||
|
*/
|
||||||
|
content?: NodeSpec['content'] | ((this: { options: Options }) => NodeSpec['content']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marks
|
||||||
|
*/
|
||||||
|
marks?: NodeSpec['marks'] | ((this: { options: Options }) => NodeSpec['marks']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Group
|
||||||
|
*/
|
||||||
|
group?: NodeSpec['group'] | ((this: { options: Options }) => NodeSpec['group']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inline
|
||||||
|
*/
|
||||||
|
inline?: NodeSpec['inline'] | ((this: { options: Options }) => NodeSpec['inline']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Atom
|
||||||
|
*/
|
||||||
|
atom?: NodeSpec['atom'] | ((this: { options: Options }) => NodeSpec['atom']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Selectable
|
||||||
|
*/
|
||||||
|
selectable?: NodeSpec['selectable'] | ((this: { options: Options }) => NodeSpec['selectable']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draggable
|
||||||
|
*/
|
||||||
|
draggable?: NodeSpec['draggable'] | ((this: { options: Options }) => NodeSpec['draggable']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Code
|
||||||
|
*/
|
||||||
|
code?: NodeSpec['code'] | ((this: { options: Options }) => NodeSpec['code']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defining
|
||||||
|
*/
|
||||||
|
defining?: NodeSpec['defining'] | ((this: { options: Options }) => NodeSpec['defining']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Isolating
|
||||||
|
*/
|
||||||
|
isolating?: NodeSpec['isolating'] | ((this: { options: Options }) => NodeSpec['isolating']),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse HTML
|
||||||
|
*/
|
||||||
|
parseHTML?: (
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
) => NodeSpec['parseDOM'],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render HTML
|
||||||
|
*/
|
||||||
|
renderHTML?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
node: ProseMirrorNode,
|
||||||
|
HTMLAttributes: { [key: string]: any },
|
||||||
|
}
|
||||||
|
) => DOMOutputSpec) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render Text
|
||||||
|
*/
|
||||||
|
renderText?: ((
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
},
|
||||||
|
props: {
|
||||||
|
node: ProseMirrorNode,
|
||||||
|
}
|
||||||
|
) => string) | null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add Attributes
|
||||||
|
*/
|
||||||
|
addAttributes?: (
|
||||||
|
this: {
|
||||||
|
options: Options,
|
||||||
|
},
|
||||||
|
) => Attributes | {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class Node<Options = any> {
|
export class Node<Options = any> {
|
||||||
type = 'node'
|
type = 'node'
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import { NodeSpec, MarkSpec, Schema } from 'prosemirror-model'
|
import { NodeSpec, MarkSpec, Schema } from 'prosemirror-model'
|
||||||
import { Extensions } from '../types'
|
import {
|
||||||
import { ExtensionConfig } from '../Extension'
|
Extensions,
|
||||||
|
ExtensionConfig,
|
||||||
|
NodeConfig,
|
||||||
|
MarkConfig,
|
||||||
|
} from '../types'
|
||||||
import splitExtensions from './splitExtensions'
|
import splitExtensions from './splitExtensions'
|
||||||
import getAttributesFromExtensions from './getAttributesFromExtensions'
|
import getAttributesFromExtensions from './getAttributesFromExtensions'
|
||||||
import getRenderedAttributes from './getRenderedAttributes'
|
import getRenderedAttributes from './getRenderedAttributes'
|
||||||
@@ -22,8 +26,16 @@ export default function getSchema(extensions: Extensions): Schema {
|
|||||||
const allAttributes = getAttributesFromExtensions(extensions)
|
const allAttributes = getAttributesFromExtensions(extensions)
|
||||||
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
||||||
const topNode = nodeExtensions.find(extension => extension.config.topNode)?.config.name
|
const topNode = nodeExtensions.find(extension => extension.config.topNode)?.config.name
|
||||||
const nodeSchemaExtenders: ExtensionConfig['extendNodeSchema'][] = []
|
const nodeSchemaExtenders: (
|
||||||
const markSchemaExtenders: ExtensionConfig['extendMarkSchema'][] = []
|
| ExtensionConfig['extendNodeSchema']
|
||||||
|
| NodeConfig['extendNodeSchema']
|
||||||
|
| MarkConfig['extendNodeSchema']
|
||||||
|
)[] = []
|
||||||
|
const markSchemaExtenders: (
|
||||||
|
| ExtensionConfig['extendNodeSchema']
|
||||||
|
| NodeConfig['extendNodeSchema']
|
||||||
|
| MarkConfig['extendNodeSchema']
|
||||||
|
)[] = []
|
||||||
|
|
||||||
extensions.forEach(extension => {
|
extensions.forEach(extension => {
|
||||||
if (typeof extension.config.extendNodeSchema === 'function') {
|
if (typeof extension.config.extendNodeSchema === 'function') {
|
||||||
|
|||||||
@@ -23,3 +23,12 @@ export { default as isCellSelection } from './helpers/isCellSelection'
|
|||||||
export { default as findParentNodeClosestToPos } from './helpers/findParentNodeClosestToPos'
|
export { default as findParentNodeClosestToPos } from './helpers/findParentNodeClosestToPos'
|
||||||
|
|
||||||
export interface Commands {}
|
export interface Commands {}
|
||||||
|
|
||||||
|
// eslint-disable-next-line
|
||||||
|
export interface ExtensionConfig<Options = any> {}
|
||||||
|
|
||||||
|
// eslint-disable-next-line
|
||||||
|
export interface NodeConfig<Options = any> {}
|
||||||
|
|
||||||
|
// eslint-disable-next-line
|
||||||
|
export interface MarkConfig<Options = any> {}
|
||||||
|
|||||||
@@ -14,9 +14,19 @@ import { Extension } from './Extension'
|
|||||||
import { Node } from './Node'
|
import { Node } from './Node'
|
||||||
import { Mark } from './Mark'
|
import { Mark } from './Mark'
|
||||||
import { Editor } from './Editor'
|
import { Editor } from './Editor'
|
||||||
import { Commands } from '.'
|
import {
|
||||||
|
Commands,
|
||||||
|
ExtensionConfig,
|
||||||
|
NodeConfig,
|
||||||
|
MarkConfig,
|
||||||
|
} from '.'
|
||||||
|
|
||||||
export { Commands }
|
export {
|
||||||
|
Commands,
|
||||||
|
ExtensionConfig,
|
||||||
|
NodeConfig,
|
||||||
|
MarkConfig,
|
||||||
|
}
|
||||||
|
|
||||||
export type Extensions = (Extension | Node | Mark)[]
|
export type Extensions = (Extension | Node | Mark)[]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user