@@ -40,6 +40,19 @@ function _objectWithoutPropertiesLoose(source, excluded) {
40
40
return target ;
41
41
}
42
42
43
+ /* Symbol for joining coordinates.
44
+ * Earlier, a hyphen (-) was used. But that caused problems when
45
+ * object keys had hyphen in them. So, we're switching to a less
46
+ * commonly used symbol.
47
+ */
48
+ const JOIN_SYMBOL = '§' ;
49
+ /* HTML field name prefix */
50
+
51
+ const FIELD_NAME_PREFIX = 'rjf' ;
52
+ /* Filler item for arrays to make them at least minItems long */
53
+
54
+ const FILLER = '__RJF_FILLER__' ;
55
+
43
56
const EditorContext = /*#__PURE__*/ React__default [ "default" ] . createContext ( ) ;
44
57
function capitalize$1 ( string ) {
45
58
if ( ! string ) return '' ;
@@ -79,15 +92,23 @@ function getCsrfCookie() {
79
92
80
93
return null ;
81
94
}
95
+ function joinCoords ( ) {
96
+ /* Generates coordinates from given arguments */
97
+ return Array . from ( arguments ) . join ( JOIN_SYMBOL ) ;
98
+ }
99
+ function splitCoords ( coords ) {
100
+ /* Generates coordinates */
101
+ return coords . split ( JOIN_SYMBOL ) ;
102
+ }
82
103
function getCoordsFromName ( name ) {
83
104
/* Returns coordinates of a field in the data from
84
105
* the given name of the input.
85
- * Field names have rjf- prefix but the coordinates don't.
106
+ * Field names have FIELD_NAME_PREFIX prepended but the coordinates don't.
86
107
* e.g.:
87
- * name: rjf-0-field
108
+ * name: rjf-0-field (where rjf- is the FIELD_NAME_PREFIX)
88
109
* coords: 0-field
89
110
*/
90
- return name . slice ( '4' ) ;
111
+ return name . slice ( ( FIELD_NAME_PREFIX + JOIN_SYMBOL ) . length ) ;
91
112
}
92
113
function debounce ( func , wait ) {
93
114
let timeout ;
@@ -205,21 +226,20 @@ function getSyncedArray(data, schema, getRef) {
205
226
206
227
let type = normalizeKeyword ( schema . items . type ) ;
207
228
let minItems = schema . minItems || schema . min_items || 0 ;
208
- const filler = '__JSONRORM_FILLER__' ; // filler for minItems
209
229
210
- while ( data . length < minItems ) data . push ( filler ) ;
230
+ while ( data . length < minItems ) data . push ( FILLER ) ;
211
231
212
232
for ( let i = 0 ; i < data . length ; i ++ ) {
213
233
let item = data [ i ] ;
214
234
215
235
if ( type === 'array' ) {
216
- if ( item === filler ) item = [ ] ;
236
+ if ( item === FILLER ) item = [ ] ;
217
237
newData [ i ] = getSyncedArray ( item , schema . items , getRef ) ;
218
238
} else if ( type === 'object' ) {
219
- if ( item === filler ) item = { } ;
239
+ if ( item === FILLER ) item = { } ;
220
240
newData [ i ] = getSyncedObject ( item , schema . items , getRef ) ;
221
241
} else {
222
- if ( item === filler ) {
242
+ if ( item === FILLER ) {
223
243
if ( type === 'integer' || type === 'number' ) newData [ i ] = schema . items . default === 0 ? 0 : schema . items . default || null ; else if ( type === 'boolean' ) newData [ i ] = schema . items . default === false ? false : schema . items . default || null ; else newData [ i ] = schema . items . default || '' ;
224
244
}
225
245
}
@@ -2303,9 +2323,9 @@ function getArrayFormRow(args) {
2303
2323
} else {
2304
2324
for ( let i = 0 ; i < data . length ; i ++ ) {
2305
2325
nextArgs . data = data [ i ] ;
2306
- nextArgs . name = name + '-' + i ;
2307
- if ( i === 0 ) nextArgs . onMoveUp = null ; else nextArgs . onMoveUp = e => onMove ( name + '-' + i , name + '-' + ( i - 1 ) ) ;
2308
- if ( i === data . length - 1 ) nextArgs . onMoveDown = null ; else nextArgs . onMoveDown = e => onMove ( name + '-' + i , name + '-' + ( i + 1 ) ) ;
2326
+ nextArgs . name = joinCoords ( name , i ) ;
2327
+ if ( i === 0 ) nextArgs . onMoveUp = null ; else nextArgs . onMoveUp = e => onMove ( joinCoords ( name , i ) , joinCoords ( name , i - 1 ) ) ;
2328
+ if ( i === data . length - 1 ) nextArgs . onMoveDown = null ; else nextArgs . onMoveDown = e => onMove ( joinCoords ( name , i ) , joinCoords ( name , i + 1 ) ) ;
2309
2329
2310
2330
if ( type === 'array' ) {
2311
2331
groups . push ( getArrayFormRow ( nextArgs ) ) ;
@@ -2364,9 +2384,9 @@ function getArrayFormRow(args) {
2364
2384
className : "rjf-form-group-wrapper" ,
2365
2385
key : 'group_wrapper_' + name + '_' + index
2366
2386
} , /*#__PURE__*/ React . createElement ( FormRowControls , {
2367
- onRemove : removable ? e => onRemove ( name + '-' + index ) : null ,
2368
- onMoveUp : index > 0 ? e => onMove ( name + '-' + index , name + '-' + ( index - 1 ) ) : null ,
2369
- onMoveDown : index < groups . length - 1 ? e => onMove ( name + '-' + index , name + '-' + ( index + 1 ) ) : null
2387
+ onRemove : removable ? e => onRemove ( joinCoords ( name , index ) ) : null ,
2388
+ onMoveUp : index > 0 ? e => onMove ( joinCoords ( name , index ) , joinCoords ( name , index - 1 ) ) : null ,
2389
+ onMoveDown : index < groups . length - 1 ? e => onMove ( joinCoords ( name , index ) , joinCoords ( name , index + 1 ) ) : null
2370
2390
} ) , i ) ) , addable && /*#__PURE__*/ React . createElement ( Button , {
2371
2391
className : "add" ,
2372
2392
onClick : e => onAdd ( getBlankData ( schema . items , args . getRef ) , coords ) ,
@@ -2396,7 +2416,7 @@ function getObjectFormRow(args) {
2396
2416
for ( let i = 0 ; i < keys . length ; i ++ ) {
2397
2417
let key = keys [ i ] ;
2398
2418
let value = data [ key ] ;
2399
- let childName = name + '-' + key ;
2419
+ let childName = joinCoords ( name , key ) ;
2400
2420
let schemaValue = schema_keys . hasOwnProperty ( key ) ? _extends ( { } , schema_keys [ key ] ) : undefined ;
2401
2421
2402
2422
if ( typeof schemaValue === 'undefined' ) {
@@ -2480,7 +2500,7 @@ function handleKeyValueAdd(data, coords, onAdd, newSchema, getRef) {
2480
2500
type : 'string'
2481
2501
} ;
2482
2502
key = key . trim ( ) ;
2483
- if ( ! key ) alert ( "(!) Can't add empty key.\r\n\r\n" ) ; else if ( data . hasOwnProperty ( key ) ) alert ( "(!) Duplicate keys not allowed. This key already exists.\r\n\r\n" ) ; else onAdd ( getBlankData ( newSchema , getRef ) , coords + '-' + key ) ;
2503
+ if ( ! key ) alert ( "(!) Can't add empty key.\r\n\r\n" ) ; else if ( data . hasOwnProperty ( key ) ) alert ( "(!) Duplicate keys not allowed. This key already exists.\r\n\r\n" ) ; else onAdd ( getBlankData ( newSchema , getRef ) , joinCoords ( coords , key ) ) ;
2484
2504
}
2485
2505
2486
2506
function handleKeyEdit ( data , key , value , coords , onEdit ) {
@@ -2491,10 +2511,10 @@ function handleKeyEdit(data, key, value, coords, onEdit) {
2491
2511
if ( newKey === key ) // same keys
2492
2512
return ;
2493
2513
if ( ! newKey ) return alert ( "(!) Key name can't be empty.\r\n\r\n" ) ; else if ( data . hasOwnProperty ( newKey ) ) return alert ( "(!) Duplicate keys not allowed. This key already exists.\r\n\r\n" ) ;
2494
- let newCoords = coords . split ( '-' ) ;
2514
+ let newCoords = splitCoords ( coords ) ;
2495
2515
newCoords . pop ( ) ;
2496
2516
newCoords . push ( newKey ) ;
2497
- newCoords = newCoords . join ( '-' ) ;
2517
+ newCoords = joinCoords . apply ( null , newCoords ) ;
2498
2518
onEdit ( value , newCoords , coords ) ;
2499
2519
}
2500
2520
@@ -2719,7 +2739,7 @@ class ReactJSONForm extends React__default["default"].Component {
2719
2739
a particular deeply nested item.
2720
2740
This first coordinate is not important and should be removed.
2721
2741
*/
2722
- coords = coords . split ( '-' ) ;
2742
+ coords = splitCoords ( coords ) ;
2723
2743
coords . shift ( ) ; // remove first coord
2724
2744
// :TODO: use immutable JS instead of JSON-ising the data
2725
2745
@@ -2741,7 +2761,7 @@ class ReactJSONForm extends React__default["default"].Component {
2741
2761
let args = {
2742
2762
data : data ,
2743
2763
schema : schema ,
2744
- name : 'rjf' ,
2764
+ name : FIELD_NAME_PREFIX ,
2745
2765
onChange : this . handleChange ,
2746
2766
onAdd : this . addFieldset ,
2747
2767
onRemove : this . removeFieldset ,
@@ -2762,15 +2782,15 @@ class ReactJSONForm extends React__default["default"].Component {
2762
2782
} ;
2763
2783
2764
2784
this . addFieldset = ( blankData , coords ) => {
2765
- coords = coords . split ( '-' ) ;
2785
+ coords = splitCoords ( coords ) ;
2766
2786
coords . shift ( ) ; // :TODO: use immutable JS instead of JSON-ising the data
2767
2787
2768
2788
let data = addDataUsingCoords ( coords , JSON . parse ( JSON . stringify ( this . props . editorState . getData ( ) ) ) , blankData ) ;
2769
2789
this . props . onChange ( EditorState . update ( this . props . editorState , data ) ) ;
2770
2790
} ;
2771
2791
2772
2792
this . removeFieldset = coords => {
2773
- coords = coords . split ( '-' ) ;
2793
+ coords = splitCoords ( coords ) ;
2774
2794
coords . shift ( ) ; // :TODO: use immutable JS instead of JSON-ising the data
2775
2795
2776
2796
let data = removeDataUsingCoords ( coords , JSON . parse ( JSON . stringify ( this . props . editorState . getData ( ) ) ) ) ;
@@ -2782,19 +2802,19 @@ class ReactJSONForm extends React__default["default"].Component {
2782
2802
newCoords will be added
2783
2803
oldCoords willbe removed
2784
2804
*/
2785
- newCoords = newCoords . split ( '-' ) ;
2805
+ newCoords = splitCoords ( newCoords ) ;
2786
2806
newCoords . shift ( ) ;
2787
- oldCoords = oldCoords . split ( '-' ) ;
2807
+ oldCoords = splitCoords ( oldCoords ) ;
2788
2808
oldCoords . shift ( ) ;
2789
2809
let data = addDataUsingCoords ( newCoords , JSON . parse ( JSON . stringify ( this . props . editorState . getData ( ) ) ) , value ) ;
2790
2810
data = removeDataUsingCoords ( oldCoords , data ) ;
2791
2811
this . props . onChange ( EditorState . update ( this . props . editorState , data ) ) ;
2792
2812
} ;
2793
2813
2794
2814
this . moveFieldset = ( oldCoords , newCoords ) => {
2795
- oldCoords = oldCoords . split ( "-" ) ;
2815
+ oldCoords = splitCoords ( oldCoords ) ;
2796
2816
oldCoords . shift ( ) ;
2797
- newCoords = newCoords . split ( "-" ) ;
2817
+ newCoords = splitCoords ( newCoords ) ;
2798
2818
newCoords . shift ( ) ; // :TODO: use immutable JS instead of JSON-ising the data
2799
2819
2800
2820
let data = moveDataUsingCoords ( oldCoords , newCoords , JSON . parse ( JSON . stringify ( this . props . editorState . getData ( ) ) ) ) ;
@@ -2951,8 +2971,8 @@ function DataValidator(schema) {
2951
2971
} ;
2952
2972
2953
2973
this . joinCoords = function ( coords ) {
2954
- let c = coords . join ( '-' ) ;
2955
- if ( c . startsWith ( '-' ) ) c = c . slice ( 1 ) ;
2974
+ let c = joinCoords . apply ( null , coords ) ;
2975
+ if ( c . startsWith ( JOIN_SYMBOL ) ) c = c . slice ( 1 ) ;
2956
2976
return c ;
2957
2977
} ;
2958
2978
0 commit comments