mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-12-10 18:02:53 +00:00
68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
/*
|
|
* Tencent is pleased to support the open source community by making TMagicEditor available.
|
|
*
|
|
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
|
|
*
|
|
* 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 } from 'vue-demi';
|
|
|
|
import type Core from '@tmagic/core';
|
|
import { toLine } from '@tmagic/utils';
|
|
|
|
interface UseComponentOptions {
|
|
/** 组件类型 */
|
|
componentType?: string;
|
|
/** App 实例 */
|
|
app?: Core;
|
|
}
|
|
|
|
/**
|
|
* 通过组件类型在 App 内获取组件
|
|
* @param options 若为字符串则为组件类型,若为对象则为参数选项
|
|
* @returns 得到的组件,若未找到则返回带 magic-ui- 前缀的组件类型
|
|
*/
|
|
export function useComponent<Component = any>(options: string | UseComponentOptions = '') {
|
|
let componentType: string | undefined;
|
|
let app: Core | undefined;
|
|
let component: Component | undefined;
|
|
|
|
if (typeof options === 'string') {
|
|
componentType = options;
|
|
} else {
|
|
({ componentType, app } = options);
|
|
}
|
|
|
|
if (!componentType || componentType === '') {
|
|
componentType = 'container';
|
|
}
|
|
if (!app) {
|
|
app = inject('app');
|
|
}
|
|
|
|
component = resolveComponent({ componentType, app });
|
|
if (!component && !componentType.startsWith('magic-ui-')) {
|
|
componentType = `magic-ui-${toLine(componentType)}`;
|
|
component = resolveComponent({ componentType, app });
|
|
}
|
|
|
|
return component ?? componentType;
|
|
}
|
|
|
|
type resolveComponentOptions = Required<Pick<UseComponentOptions, 'componentType'>> & UseComponentOptions;
|
|
|
|
function resolveComponent<Component = any>({ componentType, app }: resolveComponentOptions): Component | undefined {
|
|
return app?.resolveComponent(componentType);
|
|
}
|