import type { IButtonMenu, IDomEditor } from '@wangeditor/core';
import previewRaw from '../icon/preview.svg?raw';

class PreviewButtonMenu implements IButtonMenu {
  readonly title = '预览';
  readonly iconSvg = previewRaw;
  readonly tag = 'button';

  getValue(editor: IDomEditor): string | boolean {
    // 无需获取 val
    return '';
  }

  isActive(editor: IDomEditor): boolean {
    // 无需 active
    return false;
  }

  isDisabled(editor: IDomEditor): boolean {
    return editor.getText().trim().length === 0;
  }

  exec(editor: IDomEditor, value: string | boolean) {
    if (this.isDisabled(editor)) return;
    editor.emit('preview', { mode: 'editor' });
  }
}

export default {
  key: 'preview',
  factory(): PreviewButtonMenu {
    return new PreviewButtonMenu();
  },
};
