Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit 0b5ef52

Browse files
committed
WIP: Add state query vars while saving
1 parent 54059d2 commit 0b5ef52

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

js/customize-snapshots.js

+59-9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
snapshot.data.title = snapshot.data.title || snapshot.data.uuid;
3333
api.state.create( 'changesetTitle', snapshot.data.title );
3434

35+
snapshot.extendPreviewerQuery();
3536
api.section( 'publish_settings', snapshot.addTitleControl );
3637
api.trigger( 'snapshots-ready', snapshot );
3738
} );
@@ -44,7 +45,7 @@
4445
* @return {void}
4546
*/
4647
addTitleControl: function( section ) {
47-
var snapshot = this, control, toggleControl, originalQuery;
48+
var snapshot = this, control, toggleControl;
4849

4950
control = new api.Control( 'changeset_title', {
5051
type: 'text',
@@ -67,14 +68,6 @@
6768
toggleControl( api.state( 'selectedChangesetStatus' ).get() );
6869
api.state( 'selectedChangesetStatus' ).bind( toggleControl );
6970

70-
originalQuery = api.previewer.query;
71-
72-
api.previewer.query = function() {
73-
var retval = originalQuery.apply( this, arguments );
74-
retval.customize_changeset_title = api.state( 'changesetTitle' );
75-
return retval;
76-
};
77-
7871
api.state( 'changesetTitle' ).bind( function() {
7972
api.state( 'saved' ).set( false );
8073
} );
@@ -85,6 +78,63 @@
8578
}
8679
return undefined;
8780
} );
81+
},
82+
83+
/**
84+
* Amend the preview query so we can update the snapshot during `changeset_save`.
85+
*
86+
* @return {void}
87+
*/
88+
extendPreviewerQuery: function extendPreviewerQuery() {
89+
var snapshot = this, originalQuery = api.previewer.query;
90+
91+
api.previewer.query = function() {
92+
var retval = originalQuery.apply( this, arguments );
93+
94+
if ( api.state( 'selectedChangesetStatus' ) && 'publish' !== api.state( 'selectedChangesetStatus' ) ) {
95+
retval.customizer_state_query_vars = JSON.stringify( snapshot.getStateQueryVars() );
96+
retval.customize_changeset_title = api.state( 'changesetTitle' );
97+
}
98+
99+
return retval;
100+
};
101+
},
102+
103+
/**
104+
* Get state query vars.
105+
* @todo Reuse method in compat mode?
106+
*
107+
* @return {{}} Query vars for scroll, device, url, and autofocus.
108+
*/
109+
getStateQueryVars: function() {
110+
var snapshot = this, queryVars;
111+
112+
queryVars = {
113+
'autofocus[control]': null,
114+
'autofocus[section]': null,
115+
'autofocus[panel]': null
116+
};
117+
118+
queryVars.scroll = parseInt( api.previewer.scroll, 10 ) || 0;
119+
queryVars.device = api.previewedDevice.get();
120+
queryVars.url = api.previewer.previewUrl.get();
121+
122+
if ( ! api.state( 'activated' ).get() || snapshot.isNotSavedPreviewingTheme ) {
123+
queryVars.previewing_theme = true;
124+
}
125+
126+
_.find( [ 'control', 'section', 'panel' ], function( constructType ) {
127+
var found = false;
128+
api[ constructType ].each( function( construct ) { // @todo Core needs to support more Backbone methods on wp.customize.Values().
129+
if ( ! found && construct.expanded && construct.expanded.get() ) {
130+
queryVars[ 'autofocus[' + constructType + ']' ] = construct.id;
131+
found = true;
132+
}
133+
} );
134+
return found;
135+
} );
136+
137+
return queryVars;
88138
}
89139
} );
90140

0 commit comments

Comments
 (0)