2020-09-24 06:29:05 +08:00
|
|
|
import { EditorState, Transaction } from 'prosemirror-state'
|
2020-11-17 04:42:35 +08:00
|
|
|
import { Editor } from './Editor'
|
2020-11-03 00:18:12 +08:00
|
|
|
import {
|
2020-11-16 22:56:44 +08:00
|
|
|
SingleCommands,
|
|
|
|
ChainedCommands,
|
2020-11-17 00:25:55 +08:00
|
|
|
CanCommands,
|
2021-02-17 01:36:37 +08:00
|
|
|
RawCommands,
|
2021-01-28 16:50:17 +08:00
|
|
|
CommandProps,
|
2020-11-17 04:42:35 +08:00
|
|
|
} from './types'
|
2020-09-23 03:25:32 +08:00
|
|
|
|
|
|
|
export default class CommandManager {
|
|
|
|
|
|
|
|
editor: Editor
|
2020-09-24 06:29:05 +08:00
|
|
|
|
2021-02-17 01:36:37 +08:00
|
|
|
commands: RawCommands
|
2020-09-23 03:25:32 +08:00
|
|
|
|
2021-02-17 01:36:37 +08:00
|
|
|
constructor(editor: Editor, commands: RawCommands) {
|
2020-09-23 03:25:32 +08:00
|
|
|
this.editor = editor
|
2021-02-10 21:52:08 +08:00
|
|
|
this.commands = commands
|
2020-09-23 14:59:21 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 16:50:17 +08:00
|
|
|
public createCommands(): SingleCommands {
|
2020-11-13 18:42:04 +08:00
|
|
|
const { commands, editor } = this
|
2020-11-13 16:58:30 +08:00
|
|
|
const { state, view } = editor
|
2020-12-01 19:45:30 +08:00
|
|
|
const { tr } = state
|
|
|
|
const props = this.buildProps(tr)
|
2020-11-13 16:58:30 +08:00
|
|
|
|
|
|
|
return Object.fromEntries(Object
|
2020-11-13 18:42:04 +08:00
|
|
|
.entries(commands)
|
2020-11-13 16:58:30 +08:00
|
|
|
.map(([name, command]) => {
|
2021-04-27 05:33:45 +08:00
|
|
|
const method = (...args: never[]) => {
|
2021-02-17 01:00:40 +08:00
|
|
|
const callback = command(...args)(props)
|
2020-11-13 16:58:30 +08:00
|
|
|
|
2020-12-02 19:05:35 +08:00
|
|
|
if (!tr.getMeta('preventDispatch')) {
|
2020-12-02 16:28:55 +08:00
|
|
|
view.dispatch(tr)
|
|
|
|
}
|
2020-11-13 16:58:30 +08:00
|
|
|
|
|
|
|
return callback
|
|
|
|
}
|
|
|
|
|
|
|
|
return [name, method]
|
2020-11-16 22:56:44 +08:00
|
|
|
})) as SingleCommands
|
2020-11-13 16:58:30 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 16:50:17 +08:00
|
|
|
public createChain(startTr?: Transaction, shouldDispatch = true): ChainedCommands {
|
2020-11-13 18:42:04 +08:00
|
|
|
const { commands, editor } = this
|
2020-09-23 03:25:32 +08:00
|
|
|
const { state, view } = editor
|
|
|
|
const callbacks: boolean[] = []
|
2020-09-23 05:06:37 +08:00
|
|
|
const hasStartTransaction = !!startTr
|
2020-11-13 16:58:30 +08:00
|
|
|
const tr = startTr || state.tr
|
2020-09-23 03:25:32 +08:00
|
|
|
|
2021-02-10 22:10:03 +08:00
|
|
|
const run = () => {
|
|
|
|
if (!hasStartTransaction && shouldDispatch && !tr.getMeta('preventDispatch')) {
|
|
|
|
view.dispatch(tr)
|
|
|
|
}
|
2020-09-23 03:25:32 +08:00
|
|
|
|
2021-02-10 22:10:03 +08:00
|
|
|
return () => callbacks.every(callback => callback === true)
|
|
|
|
}
|
2020-09-23 03:25:32 +08:00
|
|
|
|
2021-02-10 22:10:03 +08:00
|
|
|
const chain = {
|
|
|
|
...Object.fromEntries(Object.entries(commands).map(([name, command]) => {
|
2021-04-27 05:33:45 +08:00
|
|
|
const chainedCommand = (...args: never[]) => {
|
2020-12-02 06:32:39 +08:00
|
|
|
const props = this.buildProps(tr, shouldDispatch)
|
2020-09-23 03:25:32 +08:00
|
|
|
const callback = command(...args)(props)
|
2020-12-02 06:32:39 +08:00
|
|
|
|
2020-09-23 03:25:32 +08:00
|
|
|
callbacks.push(callback)
|
|
|
|
|
2021-02-10 22:10:03 +08:00
|
|
|
return chain
|
2020-09-23 03:25:32 +08:00
|
|
|
}
|
2021-02-10 22:10:03 +08:00
|
|
|
|
|
|
|
return [name, chainedCommand]
|
|
|
|
})),
|
|
|
|
run,
|
|
|
|
} as unknown as ChainedCommands
|
|
|
|
|
|
|
|
return chain
|
2020-09-23 03:25:32 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 16:50:17 +08:00
|
|
|
public createCan(startTr?: Transaction): CanCommands {
|
2020-11-13 18:42:04 +08:00
|
|
|
const { commands, editor } = this
|
2020-11-03 00:18:12 +08:00
|
|
|
const { state } = editor
|
2021-02-16 18:27:58 +08:00
|
|
|
const dispatch = undefined
|
2020-11-13 16:58:30 +08:00
|
|
|
const tr = startTr || state.tr
|
2020-11-03 00:18:12 +08:00
|
|
|
const props = this.buildProps(tr, dispatch)
|
|
|
|
const formattedCommands = Object.fromEntries(Object
|
2020-11-13 18:42:04 +08:00
|
|
|
.entries(commands)
|
2020-11-03 00:18:12 +08:00
|
|
|
.map(([name, command]) => {
|
2021-04-27 05:33:45 +08:00
|
|
|
return [name, (...args: never[]) => command(...args)({ ...props, dispatch })]
|
2020-11-16 22:56:44 +08:00
|
|
|
})) as SingleCommands
|
2020-11-03 00:18:12 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
...formattedCommands,
|
|
|
|
chain: () => this.createChain(tr, dispatch),
|
2020-11-17 00:25:55 +08:00
|
|
|
} as CanCommands
|
2020-11-03 00:18:12 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 16:50:17 +08:00
|
|
|
public buildProps(tr: Transaction, shouldDispatch = true): CommandProps {
|
2020-11-13 18:42:04 +08:00
|
|
|
const { editor, commands } = this
|
2020-09-23 03:35:02 +08:00
|
|
|
const { state, view } = editor
|
|
|
|
|
2020-12-02 06:32:39 +08:00
|
|
|
if (state.storedMarks) {
|
|
|
|
tr.setStoredMarks(state.storedMarks)
|
|
|
|
}
|
|
|
|
|
2021-02-17 01:00:40 +08:00
|
|
|
const props: CommandProps = {
|
2020-09-23 05:06:37 +08:00
|
|
|
tr,
|
2020-09-23 03:35:02 +08:00
|
|
|
editor,
|
|
|
|
view,
|
2020-09-23 05:06:37 +08:00
|
|
|
state: this.chainableState(tr, state),
|
2020-11-02 21:29:58 +08:00
|
|
|
dispatch: shouldDispatch
|
2020-11-05 00:01:51 +08:00
|
|
|
? () => undefined
|
2020-11-02 21:29:58 +08:00
|
|
|
: undefined,
|
2020-09-23 05:06:37 +08:00
|
|
|
chain: () => this.createChain(tr),
|
2020-11-03 00:18:12 +08:00
|
|
|
can: () => this.createCan(tr),
|
2020-09-23 03:35:02 +08:00
|
|
|
get commands() {
|
|
|
|
return Object.fromEntries(Object
|
2020-11-13 18:42:04 +08:00
|
|
|
.entries(commands)
|
2020-09-23 03:35:02 +08:00
|
|
|
.map(([name, command]) => {
|
2021-04-27 05:33:45 +08:00
|
|
|
return [name, (...args: never[]) => command(...args)(props)]
|
2021-01-28 16:50:17 +08:00
|
|
|
})) as SingleCommands
|
2020-09-24 06:29:05 +08:00
|
|
|
},
|
2020-09-23 03:35:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
|
|
|
public chainableState(tr: Transaction, state: EditorState): EditorState {
|
2020-12-02 06:32:39 +08:00
|
|
|
let { selection } = tr
|
|
|
|
let { doc } = tr
|
|
|
|
let { storedMarks } = tr
|
|
|
|
|
2020-09-23 03:25:32 +08:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
schema: state.schema,
|
|
|
|
plugins: state.plugins,
|
|
|
|
apply: state.apply.bind(state),
|
|
|
|
applyTransaction: state.applyTransaction.bind(state),
|
|
|
|
reconfigure: state.reconfigure.bind(state),
|
|
|
|
toJSON: state.toJSON.bind(state),
|
|
|
|
get storedMarks() {
|
2020-12-02 06:32:39 +08:00
|
|
|
return storedMarks
|
2020-09-23 03:25:32 +08:00
|
|
|
},
|
|
|
|
get selection() {
|
2020-12-02 06:32:39 +08:00
|
|
|
return selection
|
2020-09-23 03:25:32 +08:00
|
|
|
},
|
|
|
|
get doc() {
|
2020-12-02 06:32:39 +08:00
|
|
|
return doc
|
2020-09-23 03:25:32 +08:00
|
|
|
},
|
|
|
|
get tr() {
|
2020-12-02 06:32:39 +08:00
|
|
|
selection = tr.selection
|
|
|
|
doc = tr.doc
|
|
|
|
storedMarks = tr.storedMarks
|
|
|
|
|
2020-09-23 03:25:32 +08:00
|
|
|
return tr
|
|
|
|
},
|
2020-09-24 06:29:05 +08:00
|
|
|
}
|
2020-09-23 03:25:32 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:05 +08:00
|
|
|
}
|