77//
88//////////////////////////////////////////////////////////////
99import React , { useCallback , useEffect , useState } from 'react' ;
10+ import _ from 'lodash' ;
1011import { styled } from '@mui/material/styles' ;
1112import { Box , useTheme } from '@mui/material' ;
1213import { PgButtonGroup , PgIconButton } from '../../../../../../static/js/components/Buttons' ;
@@ -51,7 +52,7 @@ const StyledBox = styled(Box)(({theme}) => ({
5152 ...theme . mixins . panelBorder . bottom ,
5253} ) ) ;
5354
54- export function MainToolBar ( { preferences, eventBus, fillColor, textColor, notation, onNotationChange, connectionInfo} ) {
55+ export function MainToolBar ( { preferences, eventBus, fillColor, textColor, notation, onNotationChange, connectionInfo, toolbarPrefs } ) {
5556 const theme = useTheme ( ) ;
5657 const [ buttonsDisabled , setButtonsDisabled ] = useState ( {
5758 'save' : true ,
@@ -72,6 +73,7 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
7273 const notationMenuRef = React . useRef ( null ) ;
7374 const isDirtyRef = React . useRef ( null ) ;
7475 const [ checkedMenuItems , setCheckedMenuItems ] = React . useState ( { } ) ;
76+ const notationRef = React . useRef ( notation ) ;
7577 const modal = useModal ( ) ;
7678
7779 const setDisableButton = useCallback ( ( name , disable = true ) => {
@@ -86,6 +88,15 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
8688 [ e . value ] : newVal ,
8789 } ;
8890 } ) ;
91+ setSaveERDData ( ( prev ) => {
92+ return {
93+ ...prev ,
94+ toolbarPrefs : {
95+ ...prev ?. toolbarPrefs ,
96+ [ e . value ] : ! prev ?. toolbarPrefs ?. [ e . value ] ,
97+ } ,
98+ } ;
99+ } ) ;
89100 } , [ ] ) ;
90101
91102 const onHelpClick = ( ) => {
@@ -111,15 +122,31 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
111122 } ;
112123
113124 useEffect ( ( ) => {
114- if ( preferences ) {
125+ if ( ! _ . isUndefined ( toolbarPrefs ) && ! _ . isNull ( toolbarPrefs ) && Object . keys ( toolbarPrefs ) . length > 0 ) {
126+ /* Apply toolbar prefs */
127+ if ( ! _ . isUndefined ( toolbarPrefs . sql_with_drop ) ) {
128+ setCheckedMenuItems ( ( prev ) => ( {
129+ ...prev ,
130+ sql_with_drop : toolbarPrefs . sql_with_drop ,
131+ } ) ) ;
132+ }
133+ if ( ! _ . isUndefined ( toolbarPrefs . cardinality ) ) {
134+ notationRef . current = toolbarPrefs . cardinality ;
135+ onNotationChange ( { 'value' : toolbarPrefs . cardinality } ) ;
136+ } else {
137+ notationRef . current = notation ;
138+ }
139+ }
140+ else if ( preferences ) {
115141 /* Get the prefs first time */
116142 if ( _ . isUndefined ( checkedMenuItems . sql_with_drop ) ) {
117143 setCheckedMenuItems ( {
118144 sql_with_drop : preferences . sql_with_drop ,
119145 } ) ;
120146 }
147+ notationRef . current = notation ;
121148 }
122- } , [ preferences ] ) ;
149+ } , [ preferences , toolbarPrefs , notation , onNotationChange ] ) ;
123150
124151 useEffect ( ( ) => {
125152 const events = [
@@ -134,11 +161,17 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
134161 [ ERD_EVENTS . ANY_ITEM_SELECTED , ( selected ) => {
135162 setDisableButton ( 'drop-table' , ! selected ) ;
136163 } ] ,
137- [ ERD_EVENTS . DIRTY , ( isDirty , data , fileName ) => {
164+ [ ERD_EVENTS . DIRTY , ( isDirty , data , fileName , toolbarPrefs ) => {
138165 isDirtyRef . current = isDirty ;
139166 setDisableButton ( 'save' , ! isDirty ) ;
140167 if ( ( isDirty || fileName ) && isSaveToolDataEnabled ( 'ERD' ) ) {
141- setSaveERDData ( { data, fileName, isDirty} ) ;
168+ setSaveERDData ( ( prev ) => ( {
169+ ...prev ,
170+ data,
171+ fileName,
172+ isDirty,
173+ ...( toolbarPrefs !== undefined && { toolbarPrefs } ) ,
174+ } ) ) ;
142175 }
143176 } ] ,
144177 ] ;
@@ -153,8 +186,10 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
153186 } , [ ] ) ;
154187
155188 const [ saveERDData , setSaveERDData ] = useState ( null ) ;
156- useDelayDebounce ( ( { data, fileName, isDirty} ) => {
157- saveToolData ( 'ERD' , { ...connectionInfo , 'open_file_name' :fileName , 'is_editor_dirty' : isDirty } , connectionInfo . trans_id , data ) ;
189+ useDelayDebounce ( ( saveData ) => {
190+ if ( saveData ?. data !== undefined ) {
191+ saveToolData ( 'ERD' , { ...connectionInfo , 'open_file_name' : saveData . fileName , 'is_editor_dirty' : saveData . isDirty , 'preferences' : saveData . toolbarPrefs } , connectionInfo . trans_id , saveData . data ) ;
192+ }
158193 } , saveERDData , 500 ) ;
159194
160195 useEffect ( ( ) => {
@@ -167,6 +202,20 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
167202 } ;
168203 } , [ checkedMenuItems [ 'sql_with_drop' ] ] ) ;
169204
205+ const onCardinalityNotationChange = useCallback ( ( e ) => {
206+ setSaveERDData ( ( prev ) => {
207+ return {
208+ ...prev ,
209+ toolbarPrefs : {
210+ ...prev ?. toolbarPrefs ,
211+ cardinality : e . value ,
212+ } ,
213+ } ;
214+ } ) ;
215+ notationRef . current = e . value ;
216+ onNotationChange ( e ) ;
217+ } , [ onNotationChange ] ) ;
218+
170219 return (
171220 ( < >
172221 < StyledBox >
@@ -336,8 +385,8 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
336385 label = { gettext ( 'Cardinality Notation' ) }
337386
338387 >
339- < PgMenuItem hasCheck closeOnCheck value = "crows" checked = { notation == 'crows' } onClick = { onNotationChange } > { gettext ( 'Crow\'s Foot Notation' ) } </ PgMenuItem >
340- < PgMenuItem hasCheck closeOnCheck value = "chen" checked = { notation == 'chen' } onClick = { onNotationChange } > { gettext ( 'Chen Notation' ) } </ PgMenuItem >
388+ < PgMenuItem hasCheck closeOnCheck value = "crows" checked = { notationRef . current == 'crows' } onClick = { onCardinalityNotationChange } > { gettext ( 'Crow\'s Foot Notation' ) } </ PgMenuItem >
389+ < PgMenuItem hasCheck closeOnCheck value = "chen" checked = { notationRef . current == 'chen' } onClick = { onCardinalityNotationChange } > { gettext ( 'Chen Notation' ) } </ PgMenuItem >
341390 </ PgMenu >
342391 </ > )
343392 ) ;
@@ -351,6 +400,7 @@ MainToolBar.propTypes = {
351400 notation : PropTypes . string ,
352401 onNotationChange : PropTypes . func ,
353402 connectionInfo : PropTypes . object ,
403+ toolbarPrefs : PropTypes . object ,
354404} ;
355405
356406const ColorButton = withColorPicker ( PgIconButton ) ;
0 commit comments