Home > DMT_Event > addEditorTabEventListener
DMT_Event.addEditorTabEventListener() method
This API is provided as a beta preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
Add an editor tab event listener
Signature
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;2
3
4
5
6
7
8
9
Parameters
Parameter | Type | Description |
|---|---|---|
id | string | Event ID, used to prevent duplicate event registration |
eventType | 'all' | EDMT_EditorTabEventType | Event type |
callFn | (eventType: EDMT_EditorTabEventType, props: { documentType: EDMT_EditorDocumentType; title: string; tabId: string }) => void | Promise<void> | The callback function triggered when the event fires |
onlyOnce | boolean | (Optional) Whether to listen only once |
Returns
void
Remarks
Note: This API is only valid for extensions. Calling it in a standalone script environment will always throw Error
When the tab event type is close or open, the switch event will also be triggered
Example
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);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