feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 { bindContributions, bindContributionProvider } from '@flowgram-adapter/common';
|
||||
|
||||
import { definePluginCreator, LifecycleContribution } from '../common';
|
||||
import { PreferencesManager } from './preferences-manager';
|
||||
import { PreferencesContribution } from './preferences-contribution';
|
||||
import { PreferenceContribution } from './preference-contribution';
|
||||
|
||||
interface PreferencesPluginOptions {
|
||||
defaultData?: any;
|
||||
}
|
||||
|
||||
const createPreferencesPlugin = definePluginCreator<PreferencesPluginOptions>({
|
||||
onBind({ bind }) {
|
||||
bind(PreferencesManager).toSelf().inSingletonScope();
|
||||
bindContributions(bind, PreferencesContribution, [LifecycleContribution]);
|
||||
bindContributionProvider(bind, PreferenceContribution);
|
||||
},
|
||||
onInit(ctx, opts) {
|
||||
ctx.container.get(PreferencesManager).init(opts?.defaultData);
|
||||
},
|
||||
});
|
||||
|
||||
export { createPreferencesPlugin, type PreferencesPluginOptions };
|
||||
25
frontend/packages/project-ide/core/src/preference/index.ts
Normal file
25
frontend/packages/project-ide/core/src/preference/index.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 {
|
||||
PreferenceContribution,
|
||||
type PreferenceSchema,
|
||||
} from './preference-contribution';
|
||||
export { PreferencesManager } from './preferences-manager';
|
||||
export {
|
||||
createPreferencesPlugin,
|
||||
type PreferencesPluginOptions,
|
||||
} from './create-preferences-plugin';
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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 SchemaDecoration } from '@flowgram-adapter/common';
|
||||
|
||||
interface PreferenceSchema {
|
||||
properties: Record<string, SchemaDecoration>;
|
||||
}
|
||||
|
||||
interface PreferenceContribution {
|
||||
configuration: PreferenceSchema;
|
||||
}
|
||||
|
||||
const PreferenceContribution = Symbol('PreferenceContribution');
|
||||
|
||||
export { PreferenceContribution, type PreferenceSchema };
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 { inject, injectable, named } from 'inversify';
|
||||
import { ContributionProvider } from '@flowgram-adapter/common';
|
||||
|
||||
import { type LifecycleContribution } from '../common';
|
||||
import { PreferencesManager } from './preferences-manager';
|
||||
import { PreferenceContribution } from './preference-contribution';
|
||||
|
||||
@injectable()
|
||||
class PreferencesContribution implements LifecycleContribution {
|
||||
@inject(ContributionProvider)
|
||||
@named(PreferenceContribution)
|
||||
protected readonly preferenceContributions: ContributionProvider<PreferenceContribution>;
|
||||
|
||||
@inject(PreferencesManager)
|
||||
protected readonly preferencesManager: PreferencesManager;
|
||||
|
||||
onInit() {
|
||||
this.preferenceContributions.getContributions().forEach(contrib => {
|
||||
this.preferencesManager.setSchema(contrib.configuration);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export { PreferencesContribution };
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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 } from 'inversify';
|
||||
import { isObject, type SchemaDecoration, Emitter } from '@flowgram-adapter/common';
|
||||
|
||||
import { type PreferenceSchema } from './preference-contribution';
|
||||
|
||||
@injectable()
|
||||
class PreferencesManager {
|
||||
private readonly preferences: Record<string, any> = {};
|
||||
|
||||
readonly schema: PreferenceSchema = {
|
||||
properties: {},
|
||||
};
|
||||
|
||||
private readonly preferencesChange = new Emitter<void>();
|
||||
|
||||
onDidPreferencesChange = this.preferencesChange.event;
|
||||
|
||||
public init(data: any) {
|
||||
/**
|
||||
* 从远程或者本地读取用户配置
|
||||
*/
|
||||
Object.assign(this.preferences, data);
|
||||
this.preferencesChange.fire();
|
||||
}
|
||||
|
||||
public setSchema(schema: PreferenceSchema) {
|
||||
const { properties } = schema;
|
||||
/** 这里先做简单校验,后面要做整个 validateSchema */
|
||||
if (!properties || !isObject(properties)) {
|
||||
return;
|
||||
}
|
||||
Object.entries<SchemaDecoration>(properties).forEach(([key, value]) => {
|
||||
if (this.schema.properties[key]) {
|
||||
// 重复定义的不覆盖,先报个警告
|
||||
console.error(
|
||||
'Preference name collision detected in the schema for property: ',
|
||||
key,
|
||||
);
|
||||
return;
|
||||
}
|
||||
this.schema.properties[key] = value;
|
||||
});
|
||||
}
|
||||
|
||||
getPreferenceData(key: string) {
|
||||
return this.preferences[key];
|
||||
}
|
||||
}
|
||||
|
||||
export { PreferencesManager };
|
||||
Reference in New Issue
Block a user