import { EditorState } from "prosemirror-state"; import { Command } from "../types"; export interface FailedCommandResult { success: false; } export interface SuccessCommandResult { success: true; newState: EditorState; } export type CommandResult = FailedCommandResult | SuccessCommandResult; export function applyCommand(command: Command, initial: EditorState): CommandResult { let newState = initial; const success = command(initial, tr => { // It's important to apply the transaction to `newState` (as opposed to // `initial`), to allow the command to internally execute multiple commands. newState = newState.apply(tr); }); return success ? { success, newState } : { success }; }