Home > DMT_Event > addEditorTabEventListener
DMT_Event.addEditorTabEventListener() method
此 API 当前处于 BETA 预览状态,希望得到开发者的反馈。它的任何功能都可能在接下来的开发进程中被修改,请不要将它用于任何正式环境。
新增编辑器标签页事件监听
签名
typescript
function addEditorTabEventListener(
id: string,
eventType: 'all' | EDMT_EditorTabEventType,
callFn: (
eventType: EDMT_EditorTabEventType,
props: { documentType: EDMT_EditorDocumentType; title: string; tabId: string },
) => void | Promise<void>,
onlyOnce?: boolean,
): void;1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
参数名
参数 | 类型 | 描述 |
|---|---|---|
id | string | 事件 ID,用以防止重复注册事件 |
eventType | 'all' | EDMT_EditorTabEventType | 事件类型 |
callFn | (eventType: EDMT_EditorTabEventType, props: { documentType: EDMT_EditorDocumentType; title: string; tabId: string }) => void | Promise<void> | 事件触发时的回调函数 |
onlyOnce | boolean | (可选) 是否仅监听一次 |
返回值
void
备注
注意:本接口仅扩展有效,在独立脚本环境内调用将始终 throw Error
在 标签页事件类型 为 关闭 或 打开 时,均会同时触发 切换 事件
示例
javascript
const listenerId = '嘉立创示例_tab_add';
// 1. 打开一个原理图页,保证编辑器里存在可切换的标签页
const pages = await eda.dmt_Schematic.getAllSchematicPagesInfo();
await eda.dmt_EditorControl.openDocument(pages[0].uuid);
// 2. 注册 toggle 事件监听(回调里拿到事件类型与标签页属性)
let fired = null;
eda.dmt_Event.addEditorTabEventListener(
listenerId,
'toggle',
(eventType, props) => {
fired = { eventType, title: props?.title, tabId: props?.tabId };
}
);
// 3. 回读确认注册成功(同 id 再注册也会被防重机制忽略)
const registered = eda.dmt_Event.isEventListenerAlreadyExist(listenerId);
console.log('registered:', registered);
// 4. 切换一次标签页触发 toggle 事件,观察回调被调用
if (pages[1]) {
await eda.dmt_EditorControl.openDocument(pages[1].uuid);
await eda.dmt_EditorControl.openDocument(pages[0].uuid);
}
else {
await eda.dmt_EditorControl.openDocument(pages[0].uuid);
}
await new Promise(r => setTimeout(r, 500));
console.log('fired:', fired ? JSON.stringify(fired) : 'null');
// 5. 清理监听,避免会话内残留
const removed = eda.dmt_Event.removeEventListener(listenerId);
console.log('removed:', removed);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34