UNPKG

7.82 kBJavaScriptView Raw
1/**
2 * External dependencies
3 */
4import createSelector from 'rememo';
5import { get, includes, some, flatten, values } from 'lodash';
6/**
7 * Returns the current editing mode.
8 *
9 * @param {Object} state Global application state.
10 *
11 * @return {string} Editing mode.
12 */
13
14export function getEditorMode(state) {
15 return getPreference(state, 'editorMode', 'visual');
16}
17/**
18 * Returns true if the editor sidebar is opened.
19 *
20 * @param {Object} state Global application state
21 *
22 * @return {boolean} Whether the editor sidebar is opened.
23 */
24
25export function isEditorSidebarOpened(state) {
26 var activeGeneralSidebar = getActiveGeneralSidebarName(state);
27 return includes(['edit-post/document', 'edit-post/block'], activeGeneralSidebar);
28}
29/**
30 * Returns true if the plugin sidebar is opened.
31 *
32 * @param {Object} state Global application state
33 * @return {boolean} Whether the plugin sidebar is opened.
34 */
35
36export function isPluginSidebarOpened(state) {
37 var activeGeneralSidebar = getActiveGeneralSidebarName(state);
38 return !!activeGeneralSidebar && !isEditorSidebarOpened(state);
39}
40/**
41 * Returns the current active general sidebar name, or null if there is no
42 * general sidebar active. The active general sidebar is a unique name to
43 * identify either an editor or plugin sidebar.
44 *
45 * Examples:
46 *
47 * - `edit-post/document`
48 * - `my-plugin/insert-image-sidebar`
49 *
50 * @param {Object} state Global application state.
51 *
52 * @return {?string} Active general sidebar name.
53 */
54
55export function getActiveGeneralSidebarName(state) {
56 // Dismissal takes precedent.
57 var isDismissed = getPreference(state, 'isGeneralSidebarDismissed', false);
58
59 if (isDismissed) {
60 return null;
61 }
62
63 return state.activeGeneralSidebar;
64}
65/**
66 * Returns the preferences (these preferences are persisted locally).
67 *
68 * @param {Object} state Global application state.
69 *
70 * @return {Object} Preferences Object.
71 */
72
73export function getPreferences(state) {
74 return state.preferences;
75}
76/**
77 *
78 * @param {Object} state Global application state.
79 * @param {string} preferenceKey Preference Key.
80 * @param {Mixed} defaultValue Default Value.
81 *
82 * @return {Mixed} Preference Value.
83 */
84
85export function getPreference(state, preferenceKey, defaultValue) {
86 var preferences = getPreferences(state);
87 var value = preferences[preferenceKey];
88 return value === undefined ? defaultValue : value;
89}
90/**
91 * Returns true if the publish sidebar is opened.
92 *
93 * @param {Object} state Global application state
94 *
95 * @return {boolean} Whether the publish sidebar is open.
96 */
97
98export function isPublishSidebarOpened(state) {
99 return state.publishSidebarActive;
100}
101/**
102 * Returns true if the given panel was programmatically removed, or false otherwise.
103 * All panels are not removed by default.
104 *
105 * @param {Object} state Global application state.
106 * @param {string} panelName A string that identifies the panel.
107 *
108 * @return {boolean} Whether or not the panel is removed.
109 */
110
111export function isEditorPanelRemoved(state, panelName) {
112 return includes(state.removedPanels, panelName);
113}
114/**
115 * Returns true if the given panel is enabled, or false otherwise. Panels are
116 * enabled by default.
117 *
118 * @param {Object} state Global application state.
119 * @param {string} panelName A string that identifies the panel.
120 *
121 * @return {boolean} Whether or not the panel is enabled.
122 */
123
124export function isEditorPanelEnabled(state, panelName) {
125 var panels = getPreference(state, 'panels');
126 return !isEditorPanelRemoved(state, panelName) && get(panels, [panelName, 'enabled'], true);
127}
128/**
129 * Returns true if the given panel is open, or false otherwise. Panels are
130 * closed by default.
131 *
132 * @param {Object} state Global application state.
133 * @param {string} panelName A string that identifies the panel.
134 *
135 * @return {boolean} Whether or not the panel is open.
136 */
137
138export function isEditorPanelOpened(state, panelName) {
139 var panels = getPreference(state, 'panels');
140 return panels[panelName] === true || get(panels, [panelName, 'opened'], false);
141}
142/**
143 * Returns true if a modal is active, or false otherwise.
144 *
145 * @param {Object} state Global application state.
146 * @param {string} modalName A string that uniquely identifies the modal.
147 *
148 * @return {boolean} Whether the modal is active.
149 */
150
151export function isModalActive(state, modalName) {
152 return state.activeModal === modalName;
153}
154/**
155 * Returns whether the given feature is enabled or not.
156 *
157 * @param {Object} state Global application state.
158 * @param {string} feature Feature slug.
159 *
160 * @return {boolean} Is active.
161 */
162
163export function isFeatureActive(state, feature) {
164 return !!state.preferences.features[feature];
165}
166/**
167 * Returns true if the plugin item is pinned to the header.
168 * When the value is not set it defaults to true.
169 *
170 * @param {Object} state Global application state.
171 * @param {string} pluginName Plugin item name.
172 *
173 * @return {boolean} Whether the plugin item is pinned.
174 */
175
176export function isPluginItemPinned(state, pluginName) {
177 var pinnedPluginItems = getPreference(state, 'pinnedPluginItems', {});
178 return get(pinnedPluginItems, [pluginName], true);
179}
180/**
181 * Returns an array of active meta box locations.
182 *
183 * @param {Object} state Post editor state.
184 *
185 * @return {string[]} Active meta box locations.
186 */
187
188export var getActiveMetaBoxLocations = createSelector(function (state) {
189 return Object.keys(state.metaBoxes.locations).filter(function (location) {
190 return isMetaBoxLocationActive(state, location);
191 });
192}, function (state) {
193 return [state.metaBoxes.locations];
194});
195/**
196 * Returns true if a metabox location is active and visible
197 *
198 * @param {Object} state Post editor state.
199 * @param {string} location Meta box location to test.
200 *
201 * @return {boolean} Whether the meta box location is active and visible.
202 */
203
204export function isMetaBoxLocationVisible(state, location) {
205 return isMetaBoxLocationActive(state, location) && some(getMetaBoxesPerLocation(state, location), function (_ref) {
206 var id = _ref.id;
207 return isEditorPanelEnabled(state, "meta-box-".concat(id));
208 });
209}
210/**
211 * Returns true if there is an active meta box in the given location, or false
212 * otherwise.
213 *
214 * @param {Object} state Post editor state.
215 * @param {string} location Meta box location to test.
216 *
217 * @return {boolean} Whether the meta box location is active.
218 */
219
220export function isMetaBoxLocationActive(state, location) {
221 var metaBoxes = getMetaBoxesPerLocation(state, location);
222 return !!metaBoxes && metaBoxes.length !== 0;
223}
224/**
225 * Returns the list of all the available meta boxes for a given location.
226 *
227 * @param {Object} state Global application state.
228 * @param {string} location Meta box location to test.
229 *
230 * @return {?Array} List of meta boxes.
231 */
232
233export function getMetaBoxesPerLocation(state, location) {
234 return state.metaBoxes.locations[location];
235}
236/**
237 * Returns the list of all the available meta boxes.
238 *
239 * @param {Object} state Global application state.
240 *
241 * @return {Array} List of meta boxes.
242 */
243
244export var getAllMetaBoxes = createSelector(function (state) {
245 return flatten(values(state.metaBoxes.locations));
246}, function (state) {
247 return [state.metaBoxes.locations];
248});
249/**
250 * Returns true if the post is using Meta Boxes
251 *
252 * @param {Object} state Global application state
253 *
254 * @return {boolean} Whether there are metaboxes or not.
255 */
256
257export function hasMetaBoxes(state) {
258 return getActiveMetaBoxLocations(state).length > 0;
259}
260/**
261 * Returns true if the Meta Boxes are being saved.
262 *
263 * @param {Object} state Global application state.
264 *
265 * @return {boolean} Whether the metaboxes are being saved.
266 */
267
268export function isSavingMetaBoxes(state) {
269 return state.metaBoxes.isSaving;
270}
271//# sourceMappingURL=selectors.js.map
\No newline at end of file