move some types

This commit is contained in:
Philipp Kühn 2020-11-16 15:56:44 +01:00
parent be9b6170aa
commit 282cdfebd6
2 changed files with 32 additions and 62 deletions

View File

@ -2,6 +2,8 @@ import { EditorState, Transaction } from 'prosemirror-state'
import {
Editor,
CommandSpec,
SingleCommands,
ChainedCommands,
} from './Editor'
import getAllMethodNames from './utils/getAllMethodNames'
@ -56,7 +58,7 @@ export default class CommandManager {
}
return [name, method]
})) as Tiptap.SingleCommands
})) as SingleCommands
}
public createChain(startTr?: Transaction, shouldDispatch = true) {
@ -90,7 +92,7 @@ export default class CommandManager {
return proxy
}
},
}) as Tiptap.ChainedCommands
}) as ChainedCommands
}
public createCan(startTr?: Transaction) {
@ -103,7 +105,7 @@ export default class CommandManager {
.entries(commands)
.map(([name, command]) => {
return [name, (...args: any[]) => command(...args)({ ...props, dispatch })]
})) as Tiptap.SingleCommands
})) as SingleCommands
return {
...formattedCommands,

View File

@ -24,9 +24,9 @@ import style from './style'
export type Command = (props: {
editor: Editor,
tr: Transaction,
commands: Tiptap.SingleCommands,
can: () => Tiptap.SingleCommands & { chain: () => Tiptap.ChainedCommands },
chain: () => Tiptap.ChainedCommands,
commands: SingleCommands,
can: () => SingleCommands & { chain: () => ChainedCommands },
chain: () => ChainedCommands,
state: EditorState,
view: EditorView,
dispatch: ((args?: any) => any) | undefined,
@ -41,68 +41,36 @@ export interface CommandsSpec {
declare global {
namespace Tiptap {
export interface AllExtensions {}
export type UnfilteredCommands = {
[Item in keyof AllExtensions]: AllExtensions[Item] extends Extension<any, infer ExtensionCommands>
? ExtensionCommands
: AllExtensions[Item] extends Node<any, infer NodeCommands>
? NodeCommands
: AllExtensions[Item] extends Mark<any, infer MarkCommands>
? MarkCommands
: never
}
type ValuesOf<T> = T[keyof T];
type KeysWithTypeOf<T, Type> = ({[P in keyof T]: T[P] extends Type ? P : never })[keyof T]
type AllCommands = UnionToIntersection<ValuesOf<Pick<UnfilteredCommands, KeysWithTypeOf<UnfilteredCommands, {}>>>>
export type SingleCommands = {
[Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
? (...args: Parameters<AllCommands[Item]>) => boolean
: never
}
export type ChainedCommands = {
[Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
? (...args: Parameters<AllCommands[Item]>) => ChainedCommands
: never
} & {
run: () => boolean
}
}
}
// type blub = Tiptap.AllExtensions
type UnfilteredCommands = {
[Item in keyof Tiptap.AllExtensions]: Tiptap.AllExtensions[Item] extends Extension<any, infer ExtensionCommands>
? ExtensionCommands
: Tiptap.AllExtensions[Item] extends Node<any, infer NodeCommands>
? NodeCommands
: Tiptap.AllExtensions[Item] extends Mark<any, infer MarkCommands>
? MarkCommands
: never
}
// export interface AllExtensions {}
type ValuesOf<T> = T[keyof T];
type KeysWithTypeOf<T, Type> = ({[P in keyof T]: T[P] extends Type ? P : never })[keyof T]
type AllCommands = UnionToIntersection<ValuesOf<Pick<UnfilteredCommands, KeysWithTypeOf<UnfilteredCommands, {}>>>>
// export type UnfilteredCommands = {
// [Item in keyof Tiptap.AllExtensions]: AllExtensions[Item] extends Extension<any, infer ExtensionCommands>
// ? ExtensionCommands
// : AllExtensions[Item] extends Node<any, infer NodeCommands>
// ? NodeCommands
// : AllExtensions[Item] extends Mark<any, infer MarkCommands>
// ? MarkCommands
// : never
// }
export type SingleCommands = {
[Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
? (...args: Parameters<AllCommands[Item]>) => boolean
: never
}
// type ValuesOf<T> = T[keyof T];
// type KeysWithTypeOf<T, Type> = ({[P in keyof T]: T[P] extends Type ? P : never })[keyof T]
// type AllCommands = UnionToIntersection<ValuesOf<Pick<UnfilteredCommands, KeysWithTypeOf<UnfilteredCommands, {}>>>>
// export type SingleCommands = {
// [Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
// ? (...args: Parameters<AllCommands[Item]>) => boolean
// : never
// }
// export type ChainedCommands = {
// [Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
// ? (...args: Parameters<AllCommands[Item]>) => ChainedCommands
// : never
// } & {
// run: () => boolean
// }
export type ChainedCommands = {
[Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
? (...args: Parameters<AllCommands[Item]>) => ChainedCommands
: never
} & {
run: () => boolean
}
type EditorContent = string | JSON | null