1+ import * as React from 'react' ;
2+ import { default as html2canvas } from 'html2canvas' ;
3+ import { Node , config , event } from '@alilc/lowcode-engine' ;
4+ import { Dialog , Form , Input } from '@alifd/next' ;
5+ import './index.scss' ;
6+
7+ const FormItem = Form . Item ;
8+
9+ interface SaveAsBlockProps {
10+ node : Node ;
11+ }
12+
13+ function checkBlockAPI ( ) {
14+ const apiList = config . get ( 'apiList' ) || { } ;
15+ const { block : blockAPI } = apiList ;
16+
17+ if ( ! blockAPI ?. createBlock ) {
18+ throw new Error ( '[BlockPane] block api required in engine config.' ) ;
19+ }
20+
21+ return blockAPI ;
22+ }
23+
24+ let dialog : any ;
25+
26+ const SaveAsBlock = ( props : SaveAsBlockProps ) => {
27+ const { createBlock } = checkBlockAPI ( ) ;
28+ const { node } = props ;
29+ const [ src , setSrc ] = React . useState ( ) ;
30+ React . useEffect ( ( ) => {
31+ const generateImage = async ( ) => {
32+ let dom2 = node . getDOMNode ( ) ;
33+ console . log ( 'html2canvas: ' , html2canvas ) ;
34+ const canvas = await html2canvas ?.( dom2 , { scale : 0.5 } ) ;
35+ const dataUrl = canvas . toDataURL ( ) ;
36+ setSrc ( dataUrl ) ;
37+ } ;
38+
39+ generateImage ( ) ;
40+ } , [ ] ) ;
41+
42+ const save = async ( values ) => {
43+ const { name, title } = values ;
44+ const { schema } = node ;
45+
46+ await createBlock ( {
47+ name,
48+ title,
49+ schema : JSON . stringify ( schema ) ,
50+ screenshot : src ,
51+ } ) ;
52+ dialog ?. hide ( ) ;
53+ event . emit ( 'BlockChanged' ) ;
54+ }
55+
56+ return < div >
57+ < Form colon >
58+ < FormItem
59+ name = "name"
60+ label = "英文名"
61+ required
62+ requiredMessage = "Please input name!"
63+ >
64+ < Input />
65+ </ FormItem >
66+ < FormItem
67+ name = "title"
68+ label = "中文名"
69+ required
70+ requiredMessage = "Please input title!"
71+ >
72+ < Input />
73+ </ FormItem >
74+ < FormItem
75+ name = "screenshot"
76+ label = "缩略图"
77+ >
78+ < div className = 'block-screenshot' >
79+
80+ < img src = { src } />
81+ </ div >
82+ < Input value = { src } style = { { display : 'none' } } />
83+ </ FormItem >
84+ < FormItem label = " " colon = { false } >
85+ < Form . Submit
86+ type = "primary"
87+ validate
88+ onClick = { save }
89+ style = { { marginRight : 8 } }
90+ >
91+ 保存
92+ </ Form . Submit >
93+ < Form . Reset > 重置</ Form . Reset >
94+ </ FormItem >
95+ </ Form >
96+ </ div >
97+ }
98+
99+
100+ export default {
101+ name : 'add' ,
102+ content : {
103+ icon : {
104+ type : 'add' ,
105+ size : 'xs'
106+ } ,
107+ title : '新增' ,
108+ action ( node : Node ) {
109+ console . log ( 'node: ' , node ) ;
110+ dialog = Dialog . show ( {
111+ v2 : true ,
112+ title : "保存为区块" ,
113+ content : < SaveAsBlock node = { node } /> ,
114+ footer : false
115+ } ) ;
116+ } ,
117+ } ,
118+ important : true ,
119+ } ;
0 commit comments