tmagic-editor/docs/api/editor/eventsServiceMethods.md
roymondchen 873a51fc87 docs: 升级 VitePress 至 v2 alpha,类型引用改为源码片段同步
- 升级 vitepress 到 ^2.0.0-alpha.17
- vite.optimizeDeps.rolldownOptions.transform.define 迁移至 vite.define 以适配 v2 API
- 同步升级 vitest/rolldown/vue/vite 等周边依赖
- 文档中类型链接统一改为 <<< 片段引用源码 region,避免 commit hash 链接失效
- packages/{core,editor,form-schema,schema,stage} 相关类型加 // #region 锚点
- 移除已废弃的 docs/guide/advanced/tmagic-ui.md 及侧栏入口

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-18 11:47:03 +08:00

3.2 KiB
Raw Blame History

eventsService方法

setEvents

  • 参数:

    • {Record<string, EventOption[]>} events 事件配置对象

    ::: details 查看 EventOption 类型定义 <<< @/../packages/core/src/utils.ts#EventOption{ts} :::

  • 返回:

    • {void}
  • 详情:

    批量设置多个组件类型的事件列表

  • 示例:

import { eventsService } from '@tmagic/editor';

eventsService.setEvents({
  page: [
    { label: '页面加载', value: 'load' },
    { label: '页面显示', value: 'show' },
  ],
  text: [
    { label: '点击', value: 'click' },
  ],
});

setEvent

  • 参数:

    • {string} type 组件类型
    • {EventOption[]} events 事件列表
  • 返回:

    • {void}
  • 详情:

    设置指定组件类型的事件列表

  • 示例:

import { eventsService } from '@tmagic/editor';

eventsService.setEvent('button', [
  { label: '点击', value: 'click' },
  { label: '长按', value: 'longpress' },
]);

getEvent

  • 参数:

    • {string} type 组件类型
  • 返回:

    • {EventOption[]} 事件列表
  • 详情:

    获取指定组件类型的事件列表

  • 示例:

import { eventsService } from '@tmagic/editor';

const events = eventsService.getEvent('button');
console.log(events); // [{ label: '点击', value: 'click' }, ...]

setMethods

  • 参数:

    • {Record<string, EventOption[]>} methods 方法配置对象
  • 返回:

    • {void}
  • 详情:

    批量设置多个组件类型的方法列表

  • 示例:

import { eventsService } from '@tmagic/editor';

eventsService.setMethods({
  page: [
    { label: '刷新', value: 'refresh' },
    { label: '滚动到顶部', value: 'scrollToTop' },
  ],
  video: [
    { label: '播放', value: 'play' },
    { label: '暂停', value: 'pause' },
  ],
});

setMethod

  • 参数:

    • {string} type 组件类型
    • {EventOption[]} methods 方法列表
  • 返回:

    • {void}
  • 详情:

    设置指定组件类型的方法列表

  • 示例:

import { eventsService } from '@tmagic/editor';

eventsService.setMethod('video', [
  { label: '播放', value: 'play' },
  { label: '暂停', value: 'pause' },
  { label: '停止', value: 'stop' },
]);

getMethod

  • 参数:

    • {string} type 组件类型
    • {string | number} _targetId 目标节点id保留参数便于扩展时按节点定制
  • 返回:

    • {EventOption[]} 方法列表
  • 详情:

    获取指定组件类型的方法列表

  • 示例:

import { eventsService } from '@tmagic/editor';

const methods = eventsService.getMethod('video', 'video_123');
console.log(methods); // [{ label: '播放', value: 'play' }, ...]

resetState

  • 参数:

  • 返回:

    • {void}
  • 详情:

    重置事件服务状态,清空所有事件和方法配置

  • 示例:

import { eventsService } from '@tmagic/editor';

eventsService.resetState();

destroy

  • 参数:

  • 返回:

    • {void}
  • 详情:

    销毁 eventsService重置状态并移除所有事件监听和插件

  • 示例:

import { eventsService } from '@tmagic/editor';

eventsService.destroy();