forked from neos/neos-ui
/
selectors.ts
484 lines (438 loc) · 17.7 KB
/
selectors.ts
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import {$get} from 'plow-js';
import {createSelector, defaultMemoize} from 'reselect';
import {GlobalState} from '../../System';
import {NodeContextPath, NodeMap, Node, NodeTypeName, ClipboardMode, NodeTypesRegistry} from '@neos-project/neos-ts-interfaces';
export const inlineValidationErrorsSelector = (state: GlobalState) => $get(['cr', 'nodes', 'inlineValidationErrors'], state);
export const nodesByContextPathSelector = (state: GlobalState) => $get(['cr', 'nodes', 'byContextPath'], state);
export const siteNodeContextPathSelector = (state: GlobalState) => $get(['cr', 'nodes', 'siteNode'], state);
export const documentNodeContextPathSelector = (state: GlobalState) => $get(['cr', 'nodes', 'documentNode'], state);
export const focusedNodePathsSelector = (state: GlobalState) => $get(['cr', 'nodes', 'focused', 'contextPaths'], state);
// This is internal, as in most cases you want `focusedNodePathSelector`, which is able to fallback to documentNode, when no node is focused
export const _focusedNodeContextPathSelector = createSelector(
[
focusedNodePathsSelector
],
focusedNodePaths => {
return focusedNodePaths && focusedNodePaths[0] ? focusedNodePaths[0] : null;
}
);
export const isDocumentNodeSelectedSelector = createSelector(
[
_focusedNodeContextPathSelector,
documentNodeContextPathSelector
],
(focused, currentContentCanvasContextPath) => {
return !focused || (focused === currentContentCanvasContextPath);
}
);
export const hasFocusedContentNode = createSelector(
[
_focusedNodeContextPathSelector,
documentNodeContextPathSelector
],
(focused, currentContentCanvasContextPath) => {
return Boolean(focused && (focused !== currentContentCanvasContextPath));
}
);
export const nodeByContextPath = (state: GlobalState) => (contextPath: NodeContextPath) =>
$get(['cr', 'nodes', 'byContextPath', contextPath], state);
export const makeGetDocumentNodes = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
nodesByContextPathSelector
],
nodesMap => {
const documentRole = nodeTypesRegistry.getRole('document');
if (!documentRole) {
throw new Error('Document role is not loaded!');
}
const documentSubNodeTypes = nodeTypesRegistry.getSubTypesOf(documentRole);
const result: NodeMap = {};
Object.keys(nodesMap).forEach(contextPath => {
const node = nodesMap[contextPath];
if (!node) {
throw new Error('This error should never be thrown, it\'s a way to fool TypeScript');
}
if (documentSubNodeTypes.includes(node.nodeType)) {
result[contextPath] = node;
}
});
return result;
}
);
export const makeGetCollapsibleDocumentNodes = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
nodesByContextPathSelector
],
nodesMap => {
const documentRole = nodeTypesRegistry.getRole('document');
if (!documentRole) {
throw new Error('Document role is not loaded!');
}
const documentSubNodeTypes = nodeTypesRegistry.getSubTypesOf(documentRole);
const result: NodeMap = {};
Object.keys(nodesMap).forEach(contextPath => {
const node = nodesMap[contextPath];
if (!node) {
throw new Error('This error should never be thrown, it\'s a way to fool TypeScript');
}
const isCollapsible = node.children.some(
child => child ? documentSubNodeTypes.includes(child.nodeType) : false
)
if (documentSubNodeTypes.includes(node.nodeType) && isCollapsible) {
result[contextPath] = node;
}
});
return result;
}
);
export const makeGetCollapsibleContentNodes = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
nodesByContextPathSelector
],
nodesMap => {
const contentRole = nodeTypesRegistry.getRole('content');
const collectionRole = nodeTypesRegistry.getRole('contentCollection');
if (!contentRole) {
throw new Error('Content role is not loaded!');
}
if (!collectionRole) {
throw new Error('ContentCollection role is not loaded!');
}
const contentSubNodeTypes = nodeTypesRegistry.getSubTypesOf(contentRole);
contentSubNodeTypes.push(...nodeTypesRegistry.getSubTypesOf(collectionRole))
const result: NodeMap = {};
Object.keys(nodesMap).forEach(contextPath => {
const node = nodesMap[contextPath];
if (!node) {
throw new Error('This error should never be thrown, it\'s a way to fool TypeScript');
}
const isCollapsible = node.children.some(
child => child ? contentSubNodeTypes.includes(child.nodeType) : false
)
if (contentSubNodeTypes.includes(node.nodeType) && isCollapsible) {
result[contextPath] = node;
}
});
return result;
}
);
export const makeGetNodeByContextPathSelector = (contextPath: NodeContextPath) => createSelector(
[
(state: GlobalState) => $get(['cr', 'nodes', 'byContextPath', contextPath], state)
],
node => node
);
export const makeHasChildrenSelector = (allowedNodeTypes: NodeTypeName[]) => createSelector(
[
(state: GlobalState, contextPath: NodeContextPath) => $get(['cr', 'nodes', 'byContextPath', contextPath], state)
],
node => (node && node.children || []).some(
childNodeEnvelope => childNodeEnvelope ? allowedNodeTypes.includes(childNodeEnvelope.nodeType) : false
)
);
export const makeChildrenOfSelector = (allowedNodeTypes: NodeTypeName[]) => createSelector(
[
(state: GlobalState, contextPath: NodeContextPath) => $get(['cr', 'nodes', 'byContextPath', contextPath], state),
nodesByContextPathSelector
],
(node, nodesByContextPath: NodeMap) => (node && node.children || [])
.filter(
childNodeEnvelope => {
return childNodeEnvelope !== null || childNodeEnvelope !== undefined;
}
)
.filter(
childNodeEnvelope => {
const nodeType = childNodeEnvelope ? childNodeEnvelope.nodeType : '';
return allowedNodeTypes.includes(nodeType) || nodeType === 'Neos.Neos:FallbackNode';
}
).map(
childNodeEnvelope => {
return nodesByContextPath[childNodeEnvelope.contextPath];
}
)
);
export const siteNodeSelector = createSelector(
[
siteNodeContextPathSelector,
nodesByContextPathSelector
],
(siteNodeContextPath, nodesByContextPath) => {
if (siteNodeContextPath) {
return nodesByContextPath[siteNodeContextPath] || null;
}
return null;
}
);
export const documentNodeSelector = createSelector(
[
documentNodeContextPathSelector,
nodesByContextPathSelector
],
(documentNodeContextPath, nodesByContextPath) => {
if (documentNodeContextPath) {
return nodesByContextPath[documentNodeContextPath] || null;
}
return null;
}
);
export const byContextPathSelector = defaultMemoize(
(contextPath: NodeContextPath) => createSelector(
[
nodeByContextPath
],
getNodeByContextPath => getNodeByContextPath(contextPath)
)
);
export const parentNodeSelector = (state: GlobalState) => (baseNode: Node) => {
if (baseNode.parent !== null) {
return byContextPathSelector(baseNode.parent)(state);
}
return null;
};
export const grandParentNodeSelector = (state: GlobalState) => (baseNode: Node) => {
if (baseNode.parent !== null) {
const parentNode = byContextPathSelector(baseNode.parent)(state);
if (parentNode && parentNode.parent) {
return byContextPathSelector(parentNode.parent)(state);
}
}
return null;
};
export const focusedNodePathSelector = createSelector(
[
_focusedNodeContextPathSelector,
documentNodeContextPathSelector
],
(focused, currentContentCanvasContextPath) => {
return focused || currentContentCanvasContextPath;
}
);
export const focusedSelector = createSelector(
[
focusedNodePathSelector,
nodeByContextPath
],
(focusedNodePath, getNodeByContextPath) => {
if (focusedNodePath !== null) {
return getNodeByContextPath(focusedNodePath) || null;
}
return null;
}
);
export const focusedNodeTypeSelector = createSelector(
[
focusedSelector
],
focused => focused && focused.nodeType
);
export const focusedNodeIdentifierSelector = createSelector(
[
focusedSelector
],
focused => focused && focused.identifier
);
export const focusedParentSelector = createSelector(
[
focusedSelector,
state => state
],
(focusedNode, state) => {
if (!focusedNode) {
return null;
}
return parentNodeSelector(state)(focusedNode);
}
);
export const focusedGrandParentSelector = createSelector(
[
focusedParentSelector,
state => state
],
(focusedParentNode, state) => {
if (!focusedParentNode) {
return null;
}
return parentNodeSelector(state)(focusedParentNode);
}
);
export const clipboardNodesContextPathsSelector = (state: GlobalState) => $get(['cr', 'nodes', 'clipboard'], state);
export const clipboardNodeContextPathSelector = createSelector(
[
clipboardNodesContextPathsSelector
],
clipboardNodesContextPaths => Boolean(console.warn('This selector is deprecated, use "clipboardNodesContextPathsSelector" instead')) || (clipboardNodesContextPaths && clipboardNodesContextPaths[0])
);
export const clipboardIsEmptySelector = createSelector(
[
clipboardNodeContextPathSelector
],
clipboardNodePath => Boolean(clipboardNodePath)
);
// TODO: deprecate
export const getPathInNode = (state: GlobalState, contextPath: NodeContextPath, propertyPath: any) => {
const node = $get(['cr', 'nodes', 'byContextPath', contextPath], state);
return $get(propertyPath, node);
};
export const makeGetAllowedChildNodeTypesSelector = (nodeTypesRegistry: NodeTypesRegistry, elevator: (id: string, state: GlobalState) => string | null = id => id) => createSelector(
[
(state: GlobalState, {reference}: {reference: NodeContextPath | null, role: string}) => {
if (reference === null) {
return null;
}
const elevatedReference = elevator(reference, state);
if (elevatedReference) {
return $get(['cr', 'nodes', 'byContextPath', elevatedReference], state) || null;
}
return null;
},
(state: GlobalState, {reference}: {reference: NodeContextPath | null, role: string}) => {
if (reference === null) {
return null;
}
const parentReference = getPathInNode(state, reference, 'parent');
if (parentReference !== null) {
const elevatedReferenceParent = elevator(parentReference, state);
if (elevatedReferenceParent !== null) {
return $get(['cr', 'nodes', 'byContextPath', elevatedReferenceParent], state) || null;
}
}
return null;
},
(_: GlobalState, {role}: {reference: NodeContextPath | null, role: string, subject: NodeContextPath | null}) => role
],
(referenceNode, referenceParentNode, role) => {
if (referenceNode === null || (referenceNode.policy && referenceNode.policy.canEdit === false)) {
return [];
}
const isSubjectNodeAutocreated = referenceNode.isAutoCreated;
const referenceParentName = referenceNode.name;
const referenceParentNodeType = referenceNode.nodeType;
const referenceGrandParentNodeType = referenceParentNode ? referenceParentNode.nodeType : null;
return nodeTypesRegistry
.getAllowedNodeTypesTakingAutoCreatedIntoAccount(isSubjectNodeAutocreated, referenceParentName, referenceParentNodeType, referenceGrandParentNodeType, role)
.filter(nodeType => !(referenceNode.policy && referenceNode.policy.disallowedNodeTypes.includes(nodeType)));
}
);
export const makeGetAllowedSiblingNodeTypesSelector = (nodeTypesRegistry: NodeTypesRegistry) =>
makeGetAllowedChildNodeTypesSelector(nodeTypesRegistry, (nodeContextPath, state) => getPathInNode(state, nodeContextPath, 'parent'));
export const makeIsAllowedToAddChildOrSiblingNodes = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
makeGetAllowedChildNodeTypesSelector(nodeTypesRegistry),
makeGetAllowedSiblingNodeTypesSelector(nodeTypesRegistry)
],
(allowedChildNodeTypes, allowedSiblingNodeTypes) =>
Boolean(allowedChildNodeTypes.length + allowedSiblingNodeTypes.length)
);
export const makeCanBeCopiedAlongsideSelector = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
(state: GlobalState, {subject}: {subject: NodeContextPath | null}) => subject ? $get(['cr', 'nodes', 'byContextPath', subject], state) : false,
makeGetAllowedSiblingNodeTypesSelector(nodeTypesRegistry)
],
(subjectNode, allowedNodeTypes) => subjectNode ? allowedNodeTypes.includes(subjectNode.nodeType) : false
);
export const makeCanBeCopiedIntoSelector = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
(state: GlobalState, {subject}: {subject: NodeContextPath | null}) => subject ? $get(['cr', 'nodes', 'byContextPath', subject], state) : false,
makeGetAllowedChildNodeTypesSelector(nodeTypesRegistry)
],
(subjectNode, allowedNodeTypes) => subjectNode ? allowedNodeTypes.includes(subjectNode.nodeType) : false
);
export const makeCanBeMovedIntoSelector = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
makeCanBeCopiedIntoSelector(nodeTypesRegistry),
(_, {subject, reference}: {subject: NodeContextPath | null, reference: NodeContextPath | null}) => {
const subjectPath = subject && subject.split('@')[0];
return subjectPath && reference ? reference.indexOf(subjectPath) === 0 : false;
}
],
(canBeInsertedInto, referenceIsDescendantOfSubject) => canBeInsertedInto && !referenceIsDescendantOfSubject
);
export const makeCanBeMovedAlongsideSelector = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
makeCanBeCopiedAlongsideSelector(nodeTypesRegistry),
(state: GlobalState, {subject, reference}) => {
if (reference === null) {
return false;
}
const subjectPath = subject && subject.split('@')[0];
const referenceParent = getPathInNode(state, reference, 'parent') as string;
if (!referenceParent) {
return false;
}
return subjectPath ? referenceParent.indexOf(subjectPath) === 0 : false;
}
],
(canBeInsertedInto, referenceIsDescendantOfSubject) => canBeInsertedInto && !referenceIsDescendantOfSubject
);
export const makeCanBeCopiedSelector = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
makeCanBeCopiedAlongsideSelector(nodeTypesRegistry),
makeCanBeCopiedIntoSelector(nodeTypesRegistry)
],
(canBeInsertedAlongside, canBeInsertedInto) => (canBeInsertedAlongside || canBeInsertedInto)
);
export const makeCanBeMovedSelector = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
makeCanBeMovedAlongsideSelector(nodeTypesRegistry),
makeCanBeMovedIntoSelector(nodeTypesRegistry)
],
(canBeMovedAlongside, canBeMovedInto) => (canBeMovedAlongside || canBeMovedInto)
);
export const makeCanBePastedSelector = (nodeTypesRegistry: NodeTypesRegistry) => createSelector(
[
makeCanBeMovedSelector(nodeTypesRegistry),
makeCanBeCopiedSelector(nodeTypesRegistry),
(state: GlobalState) => $get(['cr', 'nodes', 'clipboardMode'], state)
],
(canBeMoved, canBeCopied, mode) => mode === ClipboardMode.COPY ? canBeCopied : canBeMoved
);
export const destructiveOperationsAreDisabledSelector = createSelector(
[
siteNodeContextPathSelector,
_focusedNodeContextPathSelector,
focusedSelector
],
(siteNodeContextPath, focusedNodeContextPath, focusedNode) => {
return (
focusedNode === null ||
focusedNode.isAutoCreated ||
siteNodeContextPath === focusedNodeContextPath
);
}
);
export const destructiveOperationsAreDisabledForContentTreeSelector = createSelector(
[
siteNodeContextPathSelector,
focusedNodePathsSelector,
nodesByContextPathSelector
],
(siteNodeContextPath, focusedNodesContextPaths, nodesByContextPath) => {
return focusedNodesContextPaths.map(contextPath => {
const node = nodesByContextPath[contextPath];
return node && node.isAutoCreated || siteNodeContextPath === contextPath;
}).filter(Boolean).length > 0;
}
);
export const focusedNodeParentLineSelector = createSelector(
[
focusedSelector,
nodesByContextPathSelector
],
(focusedNode, nodesByContextPath) => {
const result = [focusedNode];
let currentNode = focusedNode;
while (currentNode) {
const {parent} = currentNode;
if (parent) {
currentNode = nodesByContextPath[parent] || null;
if (currentNode) {
result.push(currentNode);
}
} else {
break;
}
}
return result;
}
);