mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-03-01 05:30:40 +00:00
72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { Button, Dialog } from '@alifd/next';
|
|
import { PluginProps } from '@ali/lowcode-types';
|
|
import { Designer } from '@ali/lowcode-designer';
|
|
import { buildComponents, assetBundle, AssetList, AssetLevel, AssetLoader } from '@ali/lowcode-utils';
|
|
import ReactRenderer from '@ali/lowcode-react-renderer';
|
|
import './index.scss';
|
|
|
|
const SamplePreview = ({ editor }: PluginProps) => {
|
|
const [data, setData] = useState({});
|
|
const [visible, setVisible] = useState(false);
|
|
async function handleClick() {
|
|
if (!editor) {
|
|
return;
|
|
}
|
|
const designer = editor.get(Designer);
|
|
if (designer) {
|
|
const assets = await editor.get('assets');
|
|
const { packages } = assets;
|
|
const { componentsMap, schema } = designer;
|
|
|
|
console.info('save schema:', designer, assets);
|
|
|
|
const libraryMap = {};
|
|
const libraryAsset: AssetList = [];
|
|
packages.forEach(({ package, library, urls }) => {
|
|
libraryMap[package] = library;
|
|
if (urls) {
|
|
libraryAsset.push(urls);
|
|
}
|
|
});
|
|
|
|
const vendors = [
|
|
assetBundle(libraryAsset, AssetLevel.Library),
|
|
];
|
|
console.log('libraryMap&vendors', libraryMap, vendors);
|
|
|
|
// TODO asset may cause pollution
|
|
const assetLoader = new AssetLoader();
|
|
await assetLoader.load(libraryAsset);
|
|
const components = buildComponents(libraryMap, componentsMap);
|
|
console.log('components', components);
|
|
|
|
setData({
|
|
schema: schema.componentsTree[0],
|
|
components,
|
|
});
|
|
setVisible(true);
|
|
}
|
|
}
|
|
|
|
function handleClose() {
|
|
setVisible(false);
|
|
}
|
|
|
|
const { schema, components } = data;
|
|
return (
|
|
<div className="lowcode-plugin-sample-preview">
|
|
<Button type="primary" onClick={handleClick}>
|
|
预览
|
|
</Button>
|
|
<Dialog visible={visible} footer={false} onClose={handleClose}>
|
|
{visible &&
|
|
<ReactRenderer className="lowcode-plugin-sample-preview-content" schema={schema} components={components} />
|
|
}
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default SamplePreview;
|