-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
selectors.js
204 lines (186 loc) · 5.61 KB
/
selectors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* External dependencies
*/
import createSelector from 'rememo';
import { some } from 'lodash';
/**
* Returns the current editing mode.
*
* @param {Object} state Global application state.
*
* @return {string} Editing mode.
*/
export function getEditorMode( state ) {
return getPreference( state, 'editorMode', 'visual' );
}
/**
* Returns the current active panel for the sidebar.
*
* @param {Object} state Global application state.
*
* @return {string} Active sidebar panel.
*/
export function getActiveEditorPanel( state ) {
return getPreference( state, 'activeSidebarPanel', {} ).editor;
}
/**
* Returns the current active plugin for the plugin sidebar.
*
* @param {Object} state Global application state
* @return {string} Active plugin sidebar plugin
*/
export function getActivePlugin( state ) {
return getPreference( state, 'activeSidebarPanel', {} ).plugin;
}
/**
* Returns the preferences (these preferences are persisted locally).
*
* @param {Object} state Global application state.
*
* @return {Object} Preferences Object.
*/
export function getPreferences( state ) {
return state.preferences;
}
/**
*
* @param {Object} state Global application state.
* @param {string} preferenceKey Preference Key.
* @param {Mixed} defaultValue Default Value.
*
* @return {Mixed} Preference Value.
*/
export function getPreference( state, preferenceKey, defaultValue ) {
const preferences = getPreferences( state );
const value = preferences[ preferenceKey ];
return value === undefined ? defaultValue : value;
}
/**
* Returns the opened general sidebar and null if the sidebar is closed.
*
* @param {Object} state Global application state.
* @return {string} The opened general sidebar panel.
*/
export function getOpenedGeneralSidebar( state ) {
return getPreference( state, 'activeGeneralSidebar' );
}
/**
* Returns true if the panel is open in the currently opened sidebar.
*
* @param {Object} state Global application state
* @param {string} sidebar Sidebar name (leave undefined for the default sidebar)
* @param {string} panel Sidebar panel name (leave undefined for the default panel)
* @return {boolean} Whether the given general sidebar panel is open
*/
export function isGeneralSidebarPanelOpened( state, sidebar, panel ) {
const activeGeneralSidebar = getPreference( state, 'activeGeneralSidebar' );
const activeSidebarPanel = getPreference( state, 'activeSidebarPanel' );
return activeGeneralSidebar === sidebar && activeSidebarPanel === panel;
}
/**
* Returns true if the publish sidebar is opened.
*
* @param {Object} state Global application state
* @return {boolean} Whether the publish sidebar is open.
*/
export function isPublishSidebarOpened( state ) {
return state.publishSidebarActive;
}
/**
* Returns true if there's any open sidebar (mobile, desktop or publish).
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether sidebar is open.
*/
export function hasOpenSidebar( state ) {
const generalSidebarOpen = getPreference( state, 'activeGeneralSidebar' ) !== null;
const publishSidebarOpen = state.publishSidebarActive;
return generalSidebarOpen || publishSidebarOpen;
}
/**
* Returns true if the editor sidebar panel is open, or false otherwise.
*
* @param {Object} state Global application state.
* @param {string} panel Sidebar panel name.
* @return {boolean} Whether the sidebar panel is open.
*/
export function isEditorSidebarPanelOpened( state, panel ) {
const panels = getPreference( state, 'panels' );
return panels ? !! panels[ panel ] : false;
}
/**
* Returns true if the current window size corresponds to mobile resolutions (<= medium breakpoint).
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether current window size corresponds to
* mobile resolutions.
*/
export function isMobile( state ) {
return state.mobile;
}
/**
* Returns whether the toolbar should be fixed or not.
*
* @param {Object} state Global application state.
*
* @return {boolean} True if toolbar is fixed.
*/
export function hasFixedToolbar( state ) {
return ! isMobile( state ) && isFeatureActive( state, 'fixedToolbar' );
}
/**
* Returns whether the given feature is enabled or not.
*
* @param {Object} state Global application state.
* @param {string} feature Feature slug.
*
* @return {boolean} Is active.
*/
export function isFeatureActive( state, feature ) {
return !! state.preferences.features[ feature ];
}
/**
* Returns the state of legacy meta boxes.
*
* @param {Object} state Global application state.
* @return {Object} State of meta boxes.
*/
export function getMetaBoxes( state ) {
return state.metaBoxes;
}
/**
* Returns the state of legacy meta boxes.
*
* @param {Object} state Global application state.
* @param {string} location Location of the meta box.
*
* @return {Object} State of meta box at specified location.
*/
export function getMetaBox( state, location ) {
return getMetaBoxes( state )[ location ];
}
/**
* Returns true if the post is using Meta Boxes
*
* @param {Object} state Global application state
* @return {boolean} Whether there are metaboxes or not.
*/
export const hasMetaBoxes = createSelector(
( state ) => {
return some( getMetaBoxes( state ), ( metaBox ) => {
return metaBox.isActive;
} );
},
( state ) => state.metaBoxes,
);
/**
* Returns true if the the Meta Boxes are being saved.
*
* @param {Object} state Global application state.
* @return {boolean} Whether the metaboxes are being saved.
*/
export function isSavingMetaBoxes( state ) {
return state.isSavingMetaBoxes;
}