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

View File

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