fix: 🐛 codeout btn fix

This commit is contained in:
春希 2020-07-20 22:24:00 +08:00
parent e65a038795
commit afda7d4259
2 changed files with 43 additions and 25 deletions

View File

@ -48,6 +48,7 @@
"@types/events": "^3.0.0",
"@types/react": "^16.8.3",
"@types/react-dom": "^16.8.2",
"@types/streamsaver": "^2.0.0",
"build-plugin-fusion": "^0.1.0",
"build-plugin-moment-locales": "^0.1.0",
"build-plugin-react-app": "^1.1.2",

View File

@ -2,25 +2,40 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
import React from 'react';
import { Button } from '@alifd/next';
import { PluginProps } from '@ali/lowcode-types';
import { PluginProps, NpmInfo } from '@ali/lowcode-types';
import { Designer } from '@ali/lowcode-designer';
import streamSaver from 'streamsaver';
import './codeout.scss';
interface BasicSection {
componentName: string;
title: string;
description?: string;
docUrl?: string;
screenshot?: string;
icon?: string;
tags?: string[];
devMode?: 'proCode' | 'lowCode';
npm: NpmInfo;
[k: string]: any;
}
const CODEOUT_SERVICE_HOST = '30.8.52.239:3000';
const Codeout = ({ editor }: PluginProps) => {
const handleClick = () => {
const designer = editor.get(Designer);
if (designer) {
const assets = editor.get('assets');
console.log(assets.components);
const assets = editor.get('assets') as { components: BasicSection[] };
const components = assets.components;
const componentsMap = assets.components.map((c) => ({
componentName: c.componentName,
...(c.npm || {}),
}));
const componentsMap = components
.filter((c) => !!c.npm)
.map((c) => ({
componentName: c.componentName,
...(c.npm || {}),
}));
const fullSchema = {
...designer.schema,
@ -35,24 +50,26 @@ const Codeout = ({ editor }: PluginProps) => {
};
console.info('codeout schema:', fullSchema);
// fetch(`http://${CODEOUT_SERVICE_HOST}/api/generate/project`, {
// method: 'POST',
// body: JSON.stringify({ schema: JSON.stringify(fullSchema) }),
// headers: new Headers({
// 'Content-Type': 'application/json',
// }),
// mode: 'cors',
// }).then((res) => {
// const fileStream = streamSaver.createWriteStream('demoProject.zip');
// res.body.pipeTo(fileStream).then(
// () => {
// console.log('success');
// },
// (err) => {
// console.log(err);
// },
// );
// });
fetch(`http://${CODEOUT_SERVICE_HOST}/api/generate/project`, {
method: 'POST',
body: JSON.stringify({ schema: JSON.stringify(fullSchema) }),
headers: new Headers({
'Content-Type': 'application/json',
}),
mode: 'cors',
}).then((res) => {
const fileStream = streamSaver.createWriteStream('demoProject.zip');
if (res.body !== null) {
res.body.pipeTo(fileStream).then(
() => {
console.log('success');
},
(err) => {
console.log(err);
},
);
}
});
}
};