mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-07 09:25:29 +08:00
This commit is contained in:
parent
14f62dddb3
commit
54e85fd284
2
.gitignore
vendored
2
.gitignore
vendored
@ -23,3 +23,5 @@ yarn-error.log*
|
|||||||
|
|
||||||
tests/cypress/videos
|
tests/cypress/videos
|
||||||
/tests/cypress/screenshots
|
/tests/cypress/screenshots
|
||||||
|
# Ignore intellij project files
|
||||||
|
.idea
|
||||||
|
@ -23,7 +23,7 @@ import {
|
|||||||
CanCommands,
|
CanCommands,
|
||||||
ChainedCommands,
|
ChainedCommands,
|
||||||
SingleCommands,
|
SingleCommands,
|
||||||
TextSerializer,
|
TextSerializer, EditorEvents,
|
||||||
} from './types'
|
} from './types'
|
||||||
import * as extensions from './extensions'
|
import * as extensions from './extensions'
|
||||||
import style from './style'
|
import style from './style'
|
||||||
@ -34,7 +34,7 @@ export interface HTMLElement {
|
|||||||
editor?: Editor
|
editor?: Editor
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Editor extends EventEmitter {
|
export class Editor extends EventEmitter<EditorEvents> {
|
||||||
|
|
||||||
private commandManager!: CommandManager
|
private commandManager!: CommandManager
|
||||||
|
|
||||||
@ -195,7 +195,7 @@ export class Editor extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Unregister a ProseMirror plugin.
|
* Unregister a ProseMirror plugin.
|
||||||
*
|
*
|
||||||
* @param name The plugins name
|
* @param nameOrPluginKey The plugins name
|
||||||
*/
|
*/
|
||||||
public unregisterPlugin(nameOrPluginKey: string | PluginKey): void {
|
public unregisterPlugin(nameOrPluginKey: string | PluginKey): void {
|
||||||
if (this.isDestroyed) {
|
if (this.isDestroyed) {
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
export default class EventEmitter {
|
type StringKeyOf<T> = Extract<keyof T, string>;
|
||||||
|
type CallbackType<T extends Record<string, any>, EVENT extends StringKeyOf<T>> = T[EVENT] extends any[] ? T[EVENT] : [T[EVENT]];
|
||||||
|
type CallbackFunction<T extends Record<string, any>, EVENT extends StringKeyOf<T>> = (...props: CallbackType<T, EVENT>) => any
|
||||||
|
|
||||||
|
export default class EventEmitter<T extends Record<string, any>> {
|
||||||
|
|
||||||
private callbacks: { [key: string]: Function[] } = {}
|
private callbacks: { [key: string]: Function[] } = {}
|
||||||
|
|
||||||
public on(event: string, fn: Function): this {
|
public on<EVENT extends StringKeyOf<T>>(event: EVENT, fn: CallbackFunction<T, EVENT>): this {
|
||||||
if (!this.callbacks[event]) {
|
if (!this.callbacks[event]) {
|
||||||
this.callbacks[event] = []
|
this.callbacks[event] = []
|
||||||
}
|
}
|
||||||
@ -12,7 +16,7 @@ export default class EventEmitter {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
protected emit(event: string, ...args: any): this {
|
protected emit<EVENT extends StringKeyOf<T>>(event: EVENT, ...args: CallbackType<T, EVENT>): this {
|
||||||
const callbacks = this.callbacks[event]
|
const callbacks = this.callbacks[event]
|
||||||
|
|
||||||
if (callbacks) {
|
if (callbacks) {
|
||||||
@ -22,7 +26,7 @@ export default class EventEmitter {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public off(event: string, fn?: Function): this {
|
public off<EVENT extends StringKeyOf<T>>(event: EVENT, fn?: CallbackFunction<T, EVENT>): this {
|
||||||
const callbacks = this.callbacks[event]
|
const callbacks = this.callbacks[event]
|
||||||
|
|
||||||
if (callbacks) {
|
if (callbacks) {
|
||||||
|
@ -39,6 +39,17 @@ export type MaybeReturnType<T> = T extends (...args: any) => any
|
|||||||
? ReturnType<T>
|
? ReturnType<T>
|
||||||
: T
|
: T
|
||||||
|
|
||||||
|
export interface EditorEvents {
|
||||||
|
beforeCreate: { editor: Editor }
|
||||||
|
create: { editor: Editor }
|
||||||
|
update: { editor: Editor, transaction: Transaction }
|
||||||
|
selectionUpdate: { editor: Editor, transaction: Transaction }
|
||||||
|
transaction: { editor: Editor, transaction: Transaction }
|
||||||
|
focus: { editor: Editor, event: FocusEvent, transaction: Transaction }
|
||||||
|
blur: { editor: Editor, event: FocusEvent, transaction: Transaction }
|
||||||
|
destroy: void
|
||||||
|
}
|
||||||
|
|
||||||
export interface EditorOptions {
|
export interface EditorOptions {
|
||||||
element: Element,
|
element: Element,
|
||||||
content: Content,
|
content: Content,
|
||||||
@ -51,14 +62,14 @@ export interface EditorOptions {
|
|||||||
enableInputRules: boolean,
|
enableInputRules: boolean,
|
||||||
enablePasteRules: boolean,
|
enablePasteRules: boolean,
|
||||||
enableCoreExtensions: boolean,
|
enableCoreExtensions: boolean,
|
||||||
onBeforeCreate: (props: { editor: Editor }) => void,
|
onBeforeCreate: (props: EditorEvents['beforeCreate']) => void,
|
||||||
onCreate: (props: { editor: Editor }) => void,
|
onCreate: (props: EditorEvents['create']) => void,
|
||||||
onUpdate: (props: { editor: Editor, transaction: Transaction }) => void,
|
onUpdate: (props: EditorEvents['update']) => void,
|
||||||
onSelectionUpdate: (props: { editor: Editor, transaction: Transaction }) => void,
|
onSelectionUpdate: (props: EditorEvents['selectionUpdate']) => void,
|
||||||
onTransaction: (props: { editor: Editor, transaction: Transaction }) => void,
|
onTransaction: (props: EditorEvents['transaction']) => void,
|
||||||
onFocus: (props: { editor: Editor, event: FocusEvent, transaction: Transaction }) => void,
|
onFocus: (props: EditorEvents['focus']) => void,
|
||||||
onBlur: (props: { editor: Editor, event: FocusEvent, transaction: Transaction }) => void,
|
onBlur: (props: EditorEvents['blur']) => void,
|
||||||
onDestroy: () => void,
|
onDestroy: (props: EditorEvents['destroy']) => void,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type HTMLContent = string
|
export type HTMLContent = string
|
||||||
@ -122,7 +133,7 @@ export type GlobalAttributes = {
|
|||||||
|
|
||||||
export type PickValue<T, K extends keyof T> = T[K]
|
export type PickValue<T, K extends keyof T> = T[K]
|
||||||
|
|
||||||
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I)=>void)
|
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void)
|
||||||
? I
|
? I
|
||||||
: never
|
: never
|
||||||
|
|
||||||
@ -133,7 +144,7 @@ export type Overwrite<T, U> = Pick<T, Diff<keyof T, keyof U>> & U;
|
|||||||
|
|
||||||
export type ValuesOf<T> = T[keyof T];
|
export type ValuesOf<T> = T[keyof T];
|
||||||
|
|
||||||
export type KeysWithTypeOf<T, Type> = ({[P in keyof T]: T[P] extends Type ? P : never })[keyof T]
|
export type KeysWithTypeOf<T, Type> = ({ [P in keyof T]: T[P] extends Type ? P : never })[keyof T]
|
||||||
|
|
||||||
export type NodeViewProps = {
|
export type NodeViewProps = {
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
|
Loading…
Reference in New Issue
Block a user