1
1
import { AppSummaryInfo , updateApplication } from "redux/reduxActions/applicationActions" ;
2
2
import { useDispatch , useSelector } from "react-redux" ;
3
3
import { getExternalEditorState } from "redux/selectors/configSelectors" ;
4
- import { useEffect , useMemo , useState } from "react" ;
4
+ import { useCallback , useEffect , useMemo , useState } from "react" ;
5
5
import {
6
6
ExternalEditorContext ,
7
7
ExternalEditorContextState ,
@@ -26,6 +26,7 @@ import { RootCompInstanceType } from "./useRootCompInstance";
26
26
import { getCurrentUser } from "redux/selectors/usersSelectors" ;
27
27
import React from "react" ;
28
28
import { isEqual } from "lodash" ;
29
+ import { isPublicApplication } from "@lowcoder-ee/redux/selectors/applicationSelector" ;
29
30
30
31
/**
31
32
* FIXME: optimize the logic of saving comps
@@ -88,6 +89,40 @@ function useSaveComp(
88
89
} , [ comp , applicationId , readOnly , dispatch ] ) ;
89
90
}
90
91
92
+ const exportAppToJson = ( appDSL ?: any ) => {
93
+ if ( ! appDSL ) return ;
94
+
95
+ const id = `t--export-app-link` ;
96
+ const existingLink = document . getElementById ( id ) ;
97
+ existingLink && existingLink . remove ( ) ;
98
+ const link = document . createElement ( "a" ) ;
99
+ const time = new Date ( ) . getTime ( ) ;
100
+
101
+ const applicationName = `test_app_${ time } ` ;
102
+ const exportObj = {
103
+ applicationInfo : {
104
+ name : 'Test App' ,
105
+ createAt : time ,
106
+ createBy : '' ,
107
+ applicationId : '' ,
108
+ applicationType : AppTypeEnum . Application ,
109
+ } ,
110
+ applicationDSL : appDSL ,
111
+ } ;
112
+ const blob = new Blob ( [ JSON . stringify ( exportObj ) ] , {
113
+ type : "application/json" ,
114
+ } ) ;
115
+ const url = URL . createObjectURL ( blob ) ;
116
+ link . href = url ;
117
+ link . download = applicationName + ".json" ;
118
+ link . id = id ;
119
+ document . body . appendChild ( link ) ;
120
+ link . click ( ) ;
121
+ link . remove ( ) ;
122
+ URL . revokeObjectURL ( url ) ;
123
+ return ;
124
+ }
125
+
91
126
interface AppEditorInternalViewProps {
92
127
readOnly : boolean ;
93
128
blockEditing ?: boolean ;
@@ -100,6 +135,7 @@ interface AppEditorInternalViewProps {
100
135
export const AppEditorInternalView = React . memo ( ( props : AppEditorInternalViewProps ) => {
101
136
const isUserViewMode = useUserViewMode ( ) ;
102
137
const extraExternalEditorState = useSelector ( getExternalEditorState ) ;
138
+ const isPublicApp = useSelector ( isPublicApplication ) ;
103
139
const dispatch = useDispatch ( ) ;
104
140
const { readOnly, blockEditing, appInfo, compInstance, fetchApplication } = props ;
105
141
@@ -111,6 +147,11 @@ export const AppEditorInternalView = React.memo((props: AppEditorInternalViewPro
111
147
appType : AppTypeEnum . Application ,
112
148
} ) ;
113
149
150
+ const exportPublicAppToJson = useCallback ( ( ) => {
151
+ const appDsl = compInstance . comp ?. toJsonValue ( ) ;
152
+ exportAppToJson ( appDsl ) ;
153
+ } , [ compInstance . comp ] )
154
+
114
155
useEffect ( ( ) => {
115
156
setExternalEditorState ( ( s ) => ( {
116
157
...s ,
@@ -121,9 +162,11 @@ export const AppEditorInternalView = React.memo((props: AppEditorInternalViewPro
121
162
hideHeader : window . location . pathname . split ( "/" ) [ 3 ] === "admin" ,
122
163
blockEditing,
123
164
fetchApplication : fetchApplication ,
165
+ exportPublicAppToJson : isPublicApp ? exportPublicAppToJson : undefined ,
124
166
...extraExternalEditorState ,
125
167
} ) ) ;
126
168
} , [
169
+ exportPublicAppToJson ,
127
170
compInstance ?. history ,
128
171
extraExternalEditorState ,
129
172
readOnly ,
0 commit comments