@@ -40,6 +40,19 @@ function _objectWithoutPropertiesLoose(source, excluded) {
4040 return target ;
4141}
4242
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+
4356const EditorContext = /*#__PURE__*/ React__default [ "default" ] . createContext ( ) ;
4457function capitalize$1 ( string ) {
4558 if ( ! string ) return '' ;
@@ -79,15 +92,23 @@ function getCsrfCookie() {
7992
8093 return null ;
8194}
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+ }
82103function getCoordsFromName ( name ) {
83104 /* Returns coordinates of a field in the data from
84105 * 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.
86107 * e.g.:
87- * name: rjf-0-field
108+ * name: rjf-0-field (where rjf- is the FIELD_NAME_PREFIX)
88109 * coords: 0-field
89110 */
90- return name . slice ( '4' ) ;
111+ return name . slice ( ( FIELD_NAME_PREFIX + JOIN_SYMBOL ) . length ) ;
91112}
92113function debounce ( func , wait ) {
93114 let timeout ;
@@ -205,21 +226,20 @@ function getSyncedArray(data, schema, getRef) {
205226
206227 let type = normalizeKeyword ( schema . items . type ) ;
207228 let minItems = schema . minItems || schema . min_items || 0 ;
208- const filler = '__JSONRORM_FILLER__' ; // filler for minItems
209229
210- while ( data . length < minItems ) data . push ( filler ) ;
230+ while ( data . length < minItems ) data . push ( FILLER ) ;
211231
212232 for ( let i = 0 ; i < data . length ; i ++ ) {
213233 let item = data [ i ] ;
214234
215235 if ( type === 'array' ) {
216- if ( item === filler ) item = [ ] ;
236+ if ( item === FILLER ) item = [ ] ;
217237 newData [ i ] = getSyncedArray ( item , schema . items , getRef ) ;
218238 } else if ( type === 'object' ) {
219- if ( item === filler ) item = { } ;
239+ if ( item === FILLER ) item = { } ;
220240 newData [ i ] = getSyncedObject ( item , schema . items , getRef ) ;
221241 } else {
222- if ( item === filler ) {
242+ if ( item === FILLER ) {
223243 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 || '' ;
224244 }
225245 }
@@ -2303,9 +2323,9 @@ function getArrayFormRow(args) {
23032323 } else {
23042324 for ( let i = 0 ; i < data . length ; i ++ ) {
23052325 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 ) ) ;
23092329
23102330 if ( type === 'array' ) {
23112331 groups . push ( getArrayFormRow ( nextArgs ) ) ;
@@ -2364,9 +2384,9 @@ function getArrayFormRow(args) {
23642384 className : "rjf-form-group-wrapper" ,
23652385 key : 'group_wrapper_' + name + '_' + index
23662386 } , /*#__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
23702390 } ) , i ) ) , addable && /*#__PURE__*/ React . createElement ( Button , {
23712391 className : "add" ,
23722392 onClick : e => onAdd ( getBlankData ( schema . items , args . getRef ) , coords ) ,
@@ -2396,7 +2416,7 @@ function getObjectFormRow(args) {
23962416 for ( let i = 0 ; i < keys . length ; i ++ ) {
23972417 let key = keys [ i ] ;
23982418 let value = data [ key ] ;
2399- let childName = name + '-' + key ;
2419+ let childName = joinCoords ( name , key ) ;
24002420 let schemaValue = schema_keys . hasOwnProperty ( key ) ? _extends ( { } , schema_keys [ key ] ) : undefined ;
24012421
24022422 if ( typeof schemaValue === 'undefined' ) {
@@ -2480,7 +2500,7 @@ function handleKeyValueAdd(data, coords, onAdd, newSchema, getRef) {
24802500 type : 'string'
24812501 } ;
24822502 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 ) ) ;
24842504}
24852505
24862506function handleKeyEdit ( data , key , value , coords , onEdit ) {
@@ -2491,10 +2511,10 @@ function handleKeyEdit(data, key, value, coords, onEdit) {
24912511 if ( newKey === key ) // same keys
24922512 return ;
24932513 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 ) ;
24952515 newCoords . pop ( ) ;
24962516 newCoords . push ( newKey ) ;
2497- newCoords = newCoords . join ( '-' ) ;
2517+ newCoords = joinCoords . apply ( null , newCoords ) ;
24982518 onEdit ( value , newCoords , coords ) ;
24992519}
25002520
@@ -2719,7 +2739,7 @@ class ReactJSONForm extends React__default["default"].Component {
27192739 a particular deeply nested item.
27202740 This first coordinate is not important and should be removed.
27212741 */
2722- coords = coords . split ( '-' ) ;
2742+ coords = splitCoords ( coords ) ;
27232743 coords . shift ( ) ; // remove first coord
27242744 // :TODO: use immutable JS instead of JSON-ising the data
27252745
@@ -2741,7 +2761,7 @@ class ReactJSONForm extends React__default["default"].Component {
27412761 let args = {
27422762 data : data ,
27432763 schema : schema ,
2744- name : 'rjf' ,
2764+ name : FIELD_NAME_PREFIX ,
27452765 onChange : this . handleChange ,
27462766 onAdd : this . addFieldset ,
27472767 onRemove : this . removeFieldset ,
@@ -2762,15 +2782,15 @@ class ReactJSONForm extends React__default["default"].Component {
27622782 } ;
27632783
27642784 this . addFieldset = ( blankData , coords ) => {
2765- coords = coords . split ( '-' ) ;
2785+ coords = splitCoords ( coords ) ;
27662786 coords . shift ( ) ; // :TODO: use immutable JS instead of JSON-ising the data
27672787
27682788 let data = addDataUsingCoords ( coords , JSON . parse ( JSON . stringify ( this . props . editorState . getData ( ) ) ) , blankData ) ;
27692789 this . props . onChange ( EditorState . update ( this . props . editorState , data ) ) ;
27702790 } ;
27712791
27722792 this . removeFieldset = coords => {
2773- coords = coords . split ( '-' ) ;
2793+ coords = splitCoords ( coords ) ;
27742794 coords . shift ( ) ; // :TODO: use immutable JS instead of JSON-ising the data
27752795
27762796 let data = removeDataUsingCoords ( coords , JSON . parse ( JSON . stringify ( this . props . editorState . getData ( ) ) ) ) ;
@@ -2782,19 +2802,19 @@ class ReactJSONForm extends React__default["default"].Component {
27822802 newCoords will be added
27832803 oldCoords willbe removed
27842804 */
2785- newCoords = newCoords . split ( '-' ) ;
2805+ newCoords = splitCoords ( newCoords ) ;
27862806 newCoords . shift ( ) ;
2787- oldCoords = oldCoords . split ( '-' ) ;
2807+ oldCoords = splitCoords ( oldCoords ) ;
27882808 oldCoords . shift ( ) ;
27892809 let data = addDataUsingCoords ( newCoords , JSON . parse ( JSON . stringify ( this . props . editorState . getData ( ) ) ) , value ) ;
27902810 data = removeDataUsingCoords ( oldCoords , data ) ;
27912811 this . props . onChange ( EditorState . update ( this . props . editorState , data ) ) ;
27922812 } ;
27932813
27942814 this . moveFieldset = ( oldCoords , newCoords ) => {
2795- oldCoords = oldCoords . split ( "-" ) ;
2815+ oldCoords = splitCoords ( oldCoords ) ;
27962816 oldCoords . shift ( ) ;
2797- newCoords = newCoords . split ( "-" ) ;
2817+ newCoords = splitCoords ( newCoords ) ;
27982818 newCoords . shift ( ) ; // :TODO: use immutable JS instead of JSON-ising the data
27992819
28002820 let data = moveDataUsingCoords ( oldCoords , newCoords , JSON . parse ( JSON . stringify ( this . props . editorState . getData ( ) ) ) ) ;
@@ -2951,8 +2971,8 @@ function DataValidator(schema) {
29512971 } ;
29522972
29532973 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 ) ;
29562976 return c ;
29572977 } ;
29582978
0 commit comments