/**
 * History Plugin Extension
 * 撤销/重做历史记录
 */

import type { Schema, Plugin, Command } from '../../core/types';
import { PluginExtension } from '../../core/Extension';
import { history, undo, redo } from 'prosemirror-history';

export interface HistoryOptions {
  depth?: number;
  newGroupDelay?: number;
}

export class History extends PluginExtension<HistoryOptions> {
  get name(): string {
    return 'history';
  }

  get defaultOptions(): HistoryOptions {
    return {
      depth: 100,
      newGroupDelay: 500,
    };
  }

  plugins(_schema: Schema): Plugin[] {
    return [
      history({
        depth: this.options.depth,
        newGroupDelay: this.options.newGroupDelay,
      }),
    ];
  }

  keys(_schema: Schema): Record<string, Command> {
    return {
      'Mod-z': undo,
      'Mod-Z': undo,
      'Mod-y': redo,
      'Mod-Y': redo,
      'Mod-Shift-z': redo,
      'Mod-Shift-Z': redo,
    };
  }
}
