feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { definePluginCreator } from '../common';
|
||||
import { EventContainerModule } from './event-container-module';
|
||||
|
||||
export const createEventPlugin = definePluginCreator<void>({
|
||||
containerModules: [EventContainerModule],
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ContainerModule } from 'inversify';
|
||||
import { bindContributionProvider, bindContributions } from '@flowgram-adapter/common';
|
||||
|
||||
import { LifecycleContribution } from '../common';
|
||||
import { EventRegistry } from './event-registry';
|
||||
import { EventContribution, EventService } from './event-contribution';
|
||||
|
||||
export const EventContainerModule = new ContainerModule(bind => {
|
||||
bindContributionProvider(bind, EventContribution);
|
||||
bind(EventService).toService(EventRegistry);
|
||||
bindContributions(bind, EventRegistry, [LifecycleContribution]);
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { type Disposable } from '@flowgram-adapter/common';
|
||||
|
||||
export const EventService = Symbol('EventService');
|
||||
|
||||
export type EventName = string;
|
||||
|
||||
export type SupportEvent =
|
||||
| MouseEvent
|
||||
| DragEvent
|
||||
| KeyboardEvent
|
||||
| UIEvent
|
||||
| TouchEvent
|
||||
| any;
|
||||
export type EventHandler = (event: SupportEvent) => boolean | undefined | void;
|
||||
|
||||
export interface EventRegsiter {
|
||||
handle: EventHandler;
|
||||
priority: number;
|
||||
}
|
||||
|
||||
export interface EventService {
|
||||
/**
|
||||
* 监听全局的事件
|
||||
* @param name 触发的事件名
|
||||
* @param handle 触发事件后执行
|
||||
* @param priority 优先级
|
||||
*/
|
||||
listenGlobalEvent: (
|
||||
name: EventName,
|
||||
handle: EventHandler,
|
||||
priority?: number,
|
||||
) => Disposable;
|
||||
}
|
||||
|
||||
export const EventContribution = Symbol('EventContribution');
|
||||
|
||||
export interface EventContribution {
|
||||
/**
|
||||
* 注册 event
|
||||
*/
|
||||
registerEvent: (service: EventService) => void;
|
||||
}
|
||||
114
frontend/packages/project-ide/core/src/event/event-registry.ts
Normal file
114
frontend/packages/project-ide/core/src/event/event-registry.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { injectable, multiInject, optional } from 'inversify';
|
||||
import { Disposable, DisposableCollection } from '@flowgram-adapter/common';
|
||||
|
||||
import { type LifecycleContribution } from '../';
|
||||
import {
|
||||
type EventService,
|
||||
EventContribution,
|
||||
type EventName,
|
||||
type SupportEvent,
|
||||
type EventHandler,
|
||||
type EventRegsiter,
|
||||
} from './event-contribution';
|
||||
|
||||
@injectable()
|
||||
export class EventRegistry implements EventService, LifecycleContribution {
|
||||
protected toDispose = new DisposableCollection();
|
||||
|
||||
protected globalEvents: {
|
||||
[key: string]: { handlers: EventRegsiter[] } & Disposable;
|
||||
} = {};
|
||||
|
||||
onDispose(): void {
|
||||
this.toDispose.dispose();
|
||||
}
|
||||
|
||||
@multiInject(EventContribution)
|
||||
@optional()
|
||||
protected readonly contributions: EventContribution[];
|
||||
|
||||
onInit() {
|
||||
for (const contrib of this.contributions) {
|
||||
contrib.registerEvent(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局监听事件
|
||||
*/
|
||||
listenGlobalEvent(
|
||||
name: EventName,
|
||||
handle: EventHandler,
|
||||
priority?: number | undefined,
|
||||
): Disposable {
|
||||
return this._listenEvent(name, handle, priority);
|
||||
}
|
||||
|
||||
// copy from pipelineRegistry
|
||||
private _listenEvent(
|
||||
name: SupportEvent,
|
||||
handle: EventHandler,
|
||||
priority = 0,
|
||||
): Disposable {
|
||||
const eventsCache = this.globalEvents;
|
||||
let eventRegister = eventsCache[name];
|
||||
if (!eventRegister) {
|
||||
const realHandler = {
|
||||
handleEvent: (e: SupportEvent) => {
|
||||
const list = eventRegister.handlers;
|
||||
for (let i = 0, len = list.length; i < len; i++) {
|
||||
const prevent = list[i].handle(e);
|
||||
/* v8 ignore next 1 */
|
||||
if (prevent) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
window.addEventListener(name, realHandler, false);
|
||||
eventRegister = eventsCache[name] = {
|
||||
handlers: [],
|
||||
dispose: () => {
|
||||
window.removeEventListener(name, realHandler);
|
||||
delete eventsCache[name];
|
||||
},
|
||||
};
|
||||
}
|
||||
const { handlers } = eventRegister;
|
||||
const item = { handle, priority };
|
||||
/**
|
||||
* handlers 排序:
|
||||
* 1. 后注册先执行 (符合冒泡规则)
|
||||
* 2. 按 priority 排序
|
||||
*/
|
||||
handlers.unshift(item);
|
||||
handlers.sort((a, b) => b.priority - a.priority);
|
||||
const dispose = Disposable.create(() => {
|
||||
const index = eventRegister.handlers.indexOf(item);
|
||||
if (index !== -1) {
|
||||
eventRegister.handlers.splice(index, 1);
|
||||
}
|
||||
if (eventRegister.handlers.length === 0) {
|
||||
eventRegister.dispose();
|
||||
}
|
||||
});
|
||||
this.toDispose.push(dispose);
|
||||
return dispose;
|
||||
}
|
||||
}
|
||||
18
frontend/packages/project-ide/core/src/event/index.ts
Normal file
18
frontend/packages/project-ide/core/src/event/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { createEventPlugin } from './create-event-plugin';
|
||||
export { EventContribution, EventService } from './event-contribution';
|
||||
Reference in New Issue
Block a user