use global namespace

This commit is contained in:
Philipp Kühn
2020-11-16 15:40:05 +01:00
parent 3a6479eeca
commit 24c3a9abd3
39 changed files with 243 additions and 138 deletions

View File

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

View File

@@ -24,9 +24,9 @@ import style from './style'
export type Command = (props: {
editor: Editor,
tr: Transaction,
commands: SingleCommands,
can: () => SingleCommands & { chain: () => ChainedCommands },
chain: () => ChainedCommands,
commands: Tiptap.SingleCommands,
can: () => Tiptap.SingleCommands & { chain: () => Tiptap.ChainedCommands },
chain: () => Tiptap.ChainedCommands,
state: EditorState,
view: EditorView,
dispatch: ((args?: any) => any) | undefined,
@@ -38,35 +38,71 @@ export interface CommandsSpec {
[key: string]: CommandSpec
}
export interface AllExtensions {}
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
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 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, {}>>>>
// type blub = Tiptap.AllExtensions
export type SingleCommands = {
[Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
? (...args: Parameters<AllCommands[Item]>) => boolean
: never
}
// export interface AllExtensions {}
export type ChainedCommands = {
[Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
? (...args: Parameters<AllCommands[Item]>) => ChainedCommands
: never
} & {
run: () => boolean
}
// 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
// }
// 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 EditorContent = string | JSON | null

View File

@@ -147,8 +147,10 @@ export const Commands = Extension.create({
},
})
declare module '@tiptap/core' {
interface AllExtensions {
Commands: typeof Commands,
declare global {
namespace Tiptap {
interface AllExtensions {
Commands: typeof Commands,
}
}
}

View File

@@ -14,8 +14,10 @@ export const Editable = Extension.create({
},
})
declare module '@tiptap/core' {
interface AllExtensions {
Editable: typeof Editable,
declare global {
namespace Tiptap {
interface AllExtensions {
Editable: typeof Editable,
}
}
}

View File

@@ -36,8 +36,10 @@ export const FocusEvents = Extension.create({
},
})
declare module '@tiptap/core' {
interface AllExtensions {
FocusEvents: typeof FocusEvents,
declare global {
namespace Tiptap {
interface AllExtensions {
FocusEvents: typeof FocusEvents,
}
}
}

View File

@@ -45,8 +45,10 @@ export const Keymap = Extension.create({
},
})
declare module '@tiptap/core' {
interface AllExtensions {
Keymap: typeof Keymap,
declare global {
namespace Tiptap {
interface AllExtensions {
Keymap: typeof Keymap,
}
}
}

View File

@@ -2,7 +2,6 @@ export {
Editor,
Command,
CommandsSpec,
AllExtensions,
} from './Editor'
export * from './Extension'