/**
 * HorizontalRule Node Extension
 * 分割线节点
 */

import { NodeExtension } from '../../core/Extension';
import { InputRule } from 'prosemirror-inputrules';
import type { NodeSpec, Schema } from '../../core/types';

export class HorizontalRule extends NodeExtension {
  get name() {
    return 'horizontalRule';
  }

  get schema(): NodeSpec {
    return {
      group: 'block',
      parseDOM: [{ tag: 'hr' }],
      toDOM() {
        return ['hr', { class: 'editor-horizontal-rule' }];
      },
    };
  }

  inputRules(schema: Schema): InputRule[] {
    const type = schema.nodes[this.name];
    if (!type) return [];

    return [
      // --- 或 *** 或 ___ 转换为分割线
      new InputRule(/^(?:---|___|\*\*\*)\s$/, (state, match, start, end) => {
        const { tr } = state;
        tr.replaceWith(start - 1, end, type.create());
        return tr;
      }),
    ];
  }
}

export default HorizontalRule;
