File tree Expand file tree Collapse file tree 2 files changed +60
-5
lines changed
src/ui/src/builder/settings/composables Expand file tree Collapse file tree 2 files changed +60
-5
lines changed Original file line number Diff line number Diff line change @@ -136,4 +136,50 @@ describe(useKeyValueEditor.name, () => {
136136 } ) ;
137137 } ) ;
138138 } ) ;
139+
140+ describe ( "initial mode" , ( ) => {
141+ it ( "should open record in assisted" , ( ) => {
142+ const { mode } = useKeyValueEditor ( {
143+ foo : "bar" ,
144+ } ) ;
145+ expect ( mode . value ) . toBe ( "assisted" ) ;
146+ } ) ;
147+
148+ it ( "should open string record in assisted" , ( ) => {
149+ const { mode } = useKeyValueEditor (
150+ JSON . stringify ( {
151+ foo : "bar" ,
152+ } ) ,
153+ ) ;
154+ expect ( mode . value ) . toBe ( "assisted" ) ;
155+ } ) ;
156+
157+ it ( "should open simple template in freehand" , ( ) => {
158+ const { mode } = useKeyValueEditor (
159+ JSON . stringify ( {
160+ foo : "@{var}" ,
161+ } ) ,
162+ ) ;
163+ expect ( mode . value ) . toBe ( "freehand" ) ;
164+ } ) ;
165+
166+ it ( "should open full template in freehand" , ( ) => {
167+ const { mode } = useKeyValueEditor ( "@{var}" ) ;
168+ expect ( mode . value ) . toBe ( "freehand" ) ;
169+ } ) ;
170+
171+ it ( "should open malformed JSON in freehand" , ( ) => {
172+ const { mode } = useKeyValueEditor ( "{" ) ;
173+ expect ( mode . value ) . toBe ( "freehand" ) ;
174+ } ) ;
175+
176+ it ( "should open record in freehand" , ( ) => {
177+ const { mode } = useKeyValueEditor (
178+ JSON . stringify ( {
179+ foo : [ 1 , 2 ] ,
180+ } ) ,
181+ ) ;
182+ expect ( mode . value ) . toBe ( "freehand" ) ;
183+ } ) ;
184+ } ) ;
139185} ) ;
Original file line number Diff line number Diff line change @@ -21,6 +21,10 @@ function tryToParse(value: string) {
2121 }
2222}
2323
24+ function isFlatRecordString ( obj : object ) : obj is Record < string , string > {
25+ return Object . values ( obj ) . every ( ( v ) => typeof v === "string" ) ;
26+ }
27+
2428export function useKeyValueEditor ( originalValue : string | JSONValue ) {
2529 const getId = useId ( ) ;
2630
@@ -105,11 +109,16 @@ export function useKeyValueEditor(originalValue: string | JSONValue) {
105109 }
106110
107111 function computeInitialMode ( objectOrString : string | JSONValue ) : Mode {
108- return typeof objectOrString === "string" &&
109- ( TEMPLATE_REGEX . exec ( objectOrString ) ||
110- ! isValidJSON ( objectOrString ) )
111- ? "freehand"
112- : "assisted" ;
112+ if ( typeof objectOrString !== "string" ) {
113+ return isFlatRecordString ( objectOrString ) ? "assisted" : "freehand" ;
114+ }
115+
116+ if ( TEMPLATE_REGEX . exec ( objectOrString ) ) return "freehand" ;
117+ if ( ! isValidJSON ( objectOrString ) ) return "freehand" ;
118+
119+ return isFlatRecordString ( tryToParse ( objectOrString ) )
120+ ? "assisted"
121+ : "freehand" ;
113122 }
114123
115124 function initializeAssistedEntries ( objectOrString : string | JSONValue ) {
You can’t perform that action at this time.
0 commit comments