11import { AppSummaryInfo , updateApplication } from "redux/reduxActions/applicationActions" ;
22import { useDispatch , useSelector } from "react-redux" ;
33import { getExternalEditorState } from "redux/selectors/configSelectors" ;
4- import { useEffect , useMemo , useState } from "react" ;
4+ import { useCallback , useEffect , useMemo , useState } from "react" ;
55import {
66 ExternalEditorContext ,
77 ExternalEditorContextState ,
@@ -26,6 +26,7 @@ import { RootCompInstanceType } from "./useRootCompInstance";
2626import { getCurrentUser } from "redux/selectors/usersSelectors" ;
2727import React from "react" ;
2828import { isEqual } from "lodash" ;
29+ import { isPublicApplication } from "@lowcoder-ee/redux/selectors/applicationSelector" ;
2930
3031/**
3132 * FIXME: optimize the logic of saving comps
@@ -88,6 +89,40 @@ function useSaveComp(
8889 } , [ comp , applicationId , readOnly , dispatch ] ) ;
8990}
9091
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+
91126interface AppEditorInternalViewProps {
92127 readOnly : boolean ;
93128 blockEditing ?: boolean ;
@@ -100,6 +135,7 @@ interface AppEditorInternalViewProps {
100135export const AppEditorInternalView = React . memo ( ( props : AppEditorInternalViewProps ) => {
101136 const isUserViewMode = useUserViewMode ( ) ;
102137 const extraExternalEditorState = useSelector ( getExternalEditorState ) ;
138+ const isPublicApp = useSelector ( isPublicApplication ) ;
103139 const dispatch = useDispatch ( ) ;
104140 const { readOnly, blockEditing, appInfo, compInstance, fetchApplication } = props ;
105141
@@ -111,6 +147,11 @@ export const AppEditorInternalView = React.memo((props: AppEditorInternalViewPro
111147 appType : AppTypeEnum . Application ,
112148 } ) ;
113149
150+ const exportPublicAppToJson = useCallback ( ( ) => {
151+ const appDsl = compInstance . comp ?. toJsonValue ( ) ;
152+ exportAppToJson ( appDsl ) ;
153+ } , [ compInstance . comp ] )
154+
114155 useEffect ( ( ) => {
115156 setExternalEditorState ( ( s ) => ( {
116157 ...s ,
@@ -121,9 +162,11 @@ export const AppEditorInternalView = React.memo((props: AppEditorInternalViewPro
121162 hideHeader : window . location . pathname . split ( "/" ) [ 3 ] === "admin" ,
122163 blockEditing,
123164 fetchApplication : fetchApplication ,
165+ exportPublicAppToJson : isPublicApp ? exportPublicAppToJson : undefined ,
124166 ...extraExternalEditorState ,
125167 } ) ) ;
126168 } , [
169+ exportPublicAppToJson ,
127170 compInstance ?. history ,
128171 extraExternalEditorState ,
129172 readOnly ,
0 commit comments