@cratis/scene.engine 4.2.1 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/editing/DescriptorCatalog.js +42 -0
- package/dist/cjs/editing/DescriptorCatalog.js.map +1 -0
- package/dist/cjs/editing/EditSession.js +44 -0
- package/dist/cjs/editing/EditSession.js.map +1 -0
- package/dist/cjs/editing/applyEdit.js +83 -0
- package/dist/cjs/editing/applyEdit.js.map +1 -0
- package/dist/cjs/editing/applyEffectiveConfiguration.js +62 -0
- package/dist/cjs/editing/applyEffectiveConfiguration.js.map +1 -0
- package/dist/cjs/editing/collectComponents.js +34 -0
- package/dist/cjs/editing/collectComponents.js.map +1 -0
- package/dist/cjs/editing/collectIconUsages.js +86 -0
- package/dist/cjs/editing/collectIconUsages.js.map +1 -0
- package/dist/cjs/editing/describeNode.js +63 -0
- package/dist/cjs/editing/describeNode.js.map +1 -0
- package/dist/cjs/editing/diagnoseIcons.js +67 -0
- package/dist/cjs/editing/diagnoseIcons.js.map +1 -0
- package/dist/cjs/editing/diagnostics.js +36 -0
- package/dist/cjs/editing/diagnostics.js.map +1 -0
- package/dist/cjs/editing/documentPaths.js +45 -0
- package/dist/cjs/editing/documentPaths.js.map +1 -0
- package/dist/cjs/editing/exposureEdits.js +106 -0
- package/dist/cjs/editing/exposureEdits.js.map +1 -0
- package/dist/cjs/editing/exposureGrants.js +115 -0
- package/dist/cjs/editing/exposureGrants.js.map +1 -0
- package/dist/cjs/editing/iconDiagnostics.js +103 -0
- package/dist/cjs/editing/iconDiagnostics.js.map +1 -0
- package/dist/cjs/editing/indexDocument.js +400 -0
- package/dist/cjs/editing/indexDocument.js.map +1 -0
- package/dist/cjs/editing/inspect.js +154 -0
- package/dist/cjs/editing/inspect.js.map +1 -0
- package/dist/cjs/editing/instanceEdits.js +267 -0
- package/dist/cjs/editing/instanceEdits.js.map +1 -0
- package/dist/cjs/editing/layoutTypeDescriptors.js +425 -0
- package/dist/cjs/editing/layoutTypeDescriptors.js.map +1 -0
- package/dist/cjs/editing/layoutTypeEdits.js +141 -0
- package/dist/cjs/editing/layoutTypeEdits.js.map +1 -0
- package/dist/cjs/editing/nodeIds.js +115 -0
- package/dist/cjs/editing/nodeIds.js.map +1 -0
- package/dist/cjs/editing/pathAccess.js +53 -0
- package/dist/cjs/editing/pathAccess.js.map +1 -0
- package/dist/cjs/editing/propertyEdits.js +125 -0
- package/dist/cjs/editing/propertyEdits.js.map +1 -0
- package/dist/cjs/editing/resolveEffectiveConfiguration.js +200 -0
- package/dist/cjs/editing/resolveEffectiveConfiguration.js.map +1 -0
- package/dist/cjs/editing/resolveTemplateChain.js +100 -0
- package/dist/cjs/editing/resolveTemplateChain.js.map +1 -0
- package/dist/cjs/editing/structureEdits.js +231 -0
- package/dist/cjs/editing/structureEdits.js.map +1 -0
- package/dist/cjs/editing/validateBinding.js +111 -0
- package/dist/cjs/editing/validateBinding.js.map +1 -0
- package/dist/cjs/editing/validateValue.js +114 -0
- package/dist/cjs/editing/validateValue.js.map +1 -0
- package/dist/cjs/icons/EffectiveIconCatalog.js +212 -0
- package/dist/cjs/icons/EffectiveIconCatalog.js.map +1 -0
- package/dist/cjs/icons/EffectiveIconEntry.js +22 -0
- package/dist/cjs/icons/EffectiveIconEntry.js.map +1 -0
- package/dist/cjs/icons/analyzeIconImpact.js +48 -0
- package/dist/cjs/icons/analyzeIconImpact.js.map +1 -0
- package/dist/cjs/icons/matchesIconQuery.js +22 -0
- package/dist/cjs/icons/matchesIconQuery.js.map +1 -0
- package/dist/cjs/icons/resolveIconLibraries.js +45 -0
- package/dist/cjs/icons/resolveIconLibraries.js.map +1 -0
- package/dist/cjs/index.js +67 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/editing/DescriptorCatalog.d.ts +9 -0
- package/dist/esm/editing/DescriptorCatalog.d.ts.map +1 -0
- package/dist/esm/editing/DescriptorCatalog.js +38 -0
- package/dist/esm/editing/DescriptorCatalog.js.map +1 -0
- package/dist/esm/editing/DocumentIndex.d.ts +31 -0
- package/dist/esm/editing/DocumentIndex.d.ts.map +1 -0
- package/dist/esm/editing/DocumentIndex.js +2 -0
- package/dist/esm/editing/DocumentIndex.js.map +1 -0
- package/dist/esm/editing/EditOutcome.d.ts +7 -0
- package/dist/esm/editing/EditOutcome.d.ts.map +1 -0
- package/dist/esm/editing/EditOutcome.js +2 -0
- package/dist/esm/editing/EditOutcome.js.map +1 -0
- package/dist/esm/editing/EditSession.d.ts +20 -0
- package/dist/esm/editing/EditSession.d.ts.map +1 -0
- package/dist/esm/editing/EditSession.js +42 -0
- package/dist/esm/editing/EditSession.js.map +1 -0
- package/dist/esm/editing/EditingContext.d.ts +10 -0
- package/dist/esm/editing/EditingContext.d.ts.map +1 -0
- package/dist/esm/editing/EditingContext.js +2 -0
- package/dist/esm/editing/EditingContext.js.map +1 -0
- package/dist/esm/editing/TemplateChain.d.ts +13 -0
- package/dist/esm/editing/TemplateChain.d.ts.map +1 -0
- package/dist/esm/editing/TemplateChain.js +2 -0
- package/dist/esm/editing/TemplateChain.js.map +1 -0
- package/dist/esm/editing/applyEdit.d.ts +5 -0
- package/dist/esm/editing/applyEdit.d.ts.map +1 -0
- package/dist/esm/editing/applyEdit.js +81 -0
- package/dist/esm/editing/applyEdit.js.map +1 -0
- package/dist/esm/editing/applyEffectiveConfiguration.d.ts +3 -0
- package/dist/esm/editing/applyEffectiveConfiguration.d.ts.map +1 -0
- package/dist/esm/editing/applyEffectiveConfiguration.js +60 -0
- package/dist/esm/editing/applyEffectiveConfiguration.js.map +1 -0
- package/dist/esm/editing/collectComponents.d.ts +3 -0
- package/dist/esm/editing/collectComponents.d.ts.map +1 -0
- package/dist/esm/editing/collectComponents.js +32 -0
- package/dist/esm/editing/collectComponents.js.map +1 -0
- package/dist/esm/editing/collectIconUsages.d.ts +5 -0
- package/dist/esm/editing/collectIconUsages.d.ts.map +1 -0
- package/dist/esm/editing/collectIconUsages.js +84 -0
- package/dist/esm/editing/collectIconUsages.js.map +1 -0
- package/dist/esm/editing/describeNode.d.ts +11 -0
- package/dist/esm/editing/describeNode.d.ts.map +1 -0
- package/dist/esm/editing/describeNode.js +60 -0
- package/dist/esm/editing/describeNode.js.map +1 -0
- package/dist/esm/editing/diagnoseIcons.d.ts +4 -0
- package/dist/esm/editing/diagnoseIcons.d.ts.map +1 -0
- package/dist/esm/editing/diagnoseIcons.js +65 -0
- package/dist/esm/editing/diagnoseIcons.js.map +1 -0
- package/dist/esm/editing/diagnostics.d.ts +6 -0
- package/dist/esm/editing/diagnostics.d.ts.map +1 -0
- package/dist/esm/editing/diagnostics.js +32 -0
- package/dist/esm/editing/diagnostics.js.map +1 -0
- package/dist/esm/editing/documentPaths.d.ts +8 -0
- package/dist/esm/editing/documentPaths.d.ts.map +1 -0
- package/dist/esm/editing/documentPaths.js +41 -0
- package/dist/esm/editing/documentPaths.js.map +1 -0
- package/dist/esm/editing/exposureEdits.d.ts +5 -0
- package/dist/esm/editing/exposureEdits.d.ts.map +1 -0
- package/dist/esm/editing/exposureEdits.js +103 -0
- package/dist/esm/editing/exposureEdits.js.map +1 -0
- package/dist/esm/editing/exposureGrants.d.ts +21 -0
- package/dist/esm/editing/exposureGrants.d.ts.map +1 -0
- package/dist/esm/editing/exposureGrants.js +112 -0
- package/dist/esm/editing/exposureGrants.js.map +1 -0
- package/dist/esm/editing/given/a_layout_document.d.ts +14 -0
- package/dist/esm/editing/given/a_layout_document.d.ts.map +1 -0
- package/dist/esm/editing/given/a_layout_document.js +67 -0
- package/dist/esm/editing/given/a_layout_document.js.map +1 -0
- package/dist/esm/editing/given/a_scene_document.d.ts +20 -0
- package/dist/esm/editing/given/a_scene_document.d.ts.map +1 -0
- package/dist/esm/editing/given/a_scene_document.js +110 -0
- package/dist/esm/editing/given/a_scene_document.js.map +1 -0
- package/dist/esm/editing/iconDiagnostics.d.ts +11 -0
- package/dist/esm/editing/iconDiagnostics.d.ts.map +1 -0
- package/dist/esm/editing/iconDiagnostics.js +99 -0
- package/dist/esm/editing/iconDiagnostics.js.map +1 -0
- package/dist/esm/editing/index.d.ts +22 -0
- package/dist/esm/editing/index.d.ts.map +1 -0
- package/dist/esm/editing/index.js +22 -0
- package/dist/esm/editing/index.js.map +1 -0
- package/dist/esm/editing/indexDocument.d.ts +6 -0
- package/dist/esm/editing/indexDocument.d.ts.map +1 -0
- package/dist/esm/editing/indexDocument.js +396 -0
- package/dist/esm/editing/indexDocument.js.map +1 -0
- package/dist/esm/editing/inspect.d.ts +8 -0
- package/dist/esm/editing/inspect.d.ts.map +1 -0
- package/dist/esm/editing/inspect.js +151 -0
- package/dist/esm/editing/inspect.js.map +1 -0
- package/dist/esm/editing/instanceEdits.d.ts +9 -0
- package/dist/esm/editing/instanceEdits.d.ts.map +1 -0
- package/dist/esm/editing/instanceEdits.js +260 -0
- package/dist/esm/editing/instanceEdits.js.map +1 -0
- package/dist/esm/editing/layoutTypeDescriptors.d.ts +5 -0
- package/dist/esm/editing/layoutTypeDescriptors.d.ts.map +1 -0
- package/dist/esm/editing/layoutTypeDescriptors.js +421 -0
- package/dist/esm/editing/layoutTypeDescriptors.js.map +1 -0
- package/dist/esm/editing/layoutTypeEdits.d.ts +4 -0
- package/dist/esm/editing/layoutTypeEdits.d.ts.map +1 -0
- package/dist/esm/editing/layoutTypeEdits.js +139 -0
- package/dist/esm/editing/layoutTypeEdits.js.map +1 -0
- package/dist/esm/editing/nodeIds.d.ts +14 -0
- package/dist/esm/editing/nodeIds.d.ts.map +1 -0
- package/dist/esm/editing/nodeIds.js +102 -0
- package/dist/esm/editing/nodeIds.js.map +1 -0
- package/dist/esm/editing/pathAccess.d.ts +7 -0
- package/dist/esm/editing/pathAccess.d.ts.map +1 -0
- package/dist/esm/editing/pathAccess.js +48 -0
- package/dist/esm/editing/pathAccess.js.map +1 -0
- package/dist/esm/editing/propertyEdits.d.ts +15 -0
- package/dist/esm/editing/propertyEdits.d.ts.map +1 -0
- package/dist/esm/editing/propertyEdits.js +120 -0
- package/dist/esm/editing/propertyEdits.js.map +1 -0
- package/dist/esm/editing/resolveEffectiveConfiguration.d.ts +5 -0
- package/dist/esm/editing/resolveEffectiveConfiguration.d.ts.map +1 -0
- package/dist/esm/editing/resolveEffectiveConfiguration.js +198 -0
- package/dist/esm/editing/resolveEffectiveConfiguration.js.map +1 -0
- package/dist/esm/editing/resolveTemplateChain.d.ts +4 -0
- package/dist/esm/editing/resolveTemplateChain.d.ts.map +1 -0
- package/dist/esm/editing/resolveTemplateChain.js +98 -0
- package/dist/esm/editing/resolveTemplateChain.js.map +1 -0
- package/dist/esm/editing/structureEdits.d.ts +6 -0
- package/dist/esm/editing/structureEdits.d.ts.map +1 -0
- package/dist/esm/editing/structureEdits.js +227 -0
- package/dist/esm/editing/structureEdits.js.map +1 -0
- package/dist/esm/editing/validateBinding.d.ts +7 -0
- package/dist/esm/editing/validateBinding.d.ts.map +1 -0
- package/dist/esm/editing/validateBinding.js +108 -0
- package/dist/esm/editing/validateBinding.js.map +1 -0
- package/dist/esm/editing/validateValue.d.ts +8 -0
- package/dist/esm/editing/validateValue.d.ts.map +1 -0
- package/dist/esm/editing/validateValue.js +111 -0
- package/dist/esm/editing/validateValue.js.map +1 -0
- package/dist/esm/icons/EffectiveIconCatalog.d.ts +25 -0
- package/dist/esm/icons/EffectiveIconCatalog.d.ts.map +1 -0
- package/dist/esm/icons/EffectiveIconCatalog.js +210 -0
- package/dist/esm/icons/EffectiveIconCatalog.js.map +1 -0
- package/dist/esm/icons/EffectiveIconEntry.d.ts +9 -0
- package/dist/esm/icons/EffectiveIconEntry.d.ts.map +1 -0
- package/dist/esm/icons/EffectiveIconEntry.js +20 -0
- package/dist/esm/icons/EffectiveIconEntry.js.map +1 -0
- package/dist/esm/icons/IconCatalogSource.d.ts +6 -0
- package/dist/esm/icons/IconCatalogSource.d.ts.map +1 -0
- package/dist/esm/icons/IconCatalogSource.js +2 -0
- package/dist/esm/icons/IconCatalogSource.js.map +1 -0
- package/dist/esm/icons/IconDiagnostic.d.ts +9 -0
- package/dist/esm/icons/IconDiagnostic.d.ts.map +1 -0
- package/dist/esm/icons/IconDiagnostic.js +2 -0
- package/dist/esm/icons/IconDiagnostic.js.map +1 -0
- package/dist/esm/icons/IconImpactReport.d.ts +18 -0
- package/dist/esm/icons/IconImpactReport.d.ts.map +1 -0
- package/dist/esm/icons/IconImpactReport.js +2 -0
- package/dist/esm/icons/IconImpactReport.js.map +1 -0
- package/dist/esm/icons/IconLibraryResolution.d.ts +7 -0
- package/dist/esm/icons/IconLibraryResolution.d.ts.map +1 -0
- package/dist/esm/icons/IconLibraryResolution.js +2 -0
- package/dist/esm/icons/IconLibraryResolution.js.map +1 -0
- package/dist/esm/icons/IconQuery.d.ts +7 -0
- package/dist/esm/icons/IconQuery.d.ts.map +1 -0
- package/dist/esm/icons/IconQuery.js +2 -0
- package/dist/esm/icons/IconQuery.js.map +1 -0
- package/dist/esm/icons/IconResolution.d.ts +14 -0
- package/dist/esm/icons/IconResolution.d.ts.map +1 -0
- package/dist/esm/icons/IconResolution.js +2 -0
- package/dist/esm/icons/IconResolution.js.map +1 -0
- package/dist/esm/icons/IconSearchResult.d.ts +7 -0
- package/dist/esm/icons/IconSearchResult.d.ts.map +1 -0
- package/dist/esm/icons/IconSearchResult.js +2 -0
- package/dist/esm/icons/IconSearchResult.js.map +1 -0
- package/dist/esm/icons/IconUsage.d.ts +6 -0
- package/dist/esm/icons/IconUsage.d.ts.map +1 -0
- package/dist/esm/icons/IconUsage.js +2 -0
- package/dist/esm/icons/IconUsage.js.map +1 -0
- package/dist/esm/icons/ResolvedIconLibrary.d.ts +8 -0
- package/dist/esm/icons/ResolvedIconLibrary.d.ts.map +1 -0
- package/dist/esm/icons/ResolvedIconLibrary.js +2 -0
- package/dist/esm/icons/ResolvedIconLibrary.js.map +1 -0
- package/dist/esm/icons/analyzeIconImpact.d.ts +5 -0
- package/dist/esm/icons/analyzeIconImpact.d.ts.map +1 -0
- package/dist/esm/icons/analyzeIconImpact.js +46 -0
- package/dist/esm/icons/analyzeIconImpact.js.map +1 -0
- package/dist/esm/icons/index.d.ts +14 -0
- package/dist/esm/icons/index.d.ts.map +1 -0
- package/dist/esm/icons/index.js +14 -0
- package/dist/esm/icons/index.js.map +1 -0
- package/dist/esm/icons/matchesIconQuery.d.ts +4 -0
- package/dist/esm/icons/matchesIconQuery.d.ts.map +1 -0
- package/dist/esm/icons/matchesIconQuery.js +20 -0
- package/dist/esm/icons/matchesIconQuery.js.map +1 -0
- package/dist/esm/icons/resolveIconLibraries.d.ts +4 -0
- package/dist/esm/icons/resolveIconLibraries.d.ts.map +1 -0
- package/dist/esm/icons/resolveIconLibraries.js +43 -0
- package/dist/esm/icons/resolveIconLibraries.js.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +21 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/editing/DescriptorCatalog.ts +51 -0
- package/editing/DocumentIndex.ts +73 -0
- package/editing/EditOutcome.ts +21 -0
- package/editing/EditSession.ts +50 -0
- package/editing/EditingContext.ts +34 -0
- package/editing/TemplateChain.ts +36 -0
- package/editing/applyEdit.ts +69 -0
- package/editing/applyEffectiveConfiguration.ts +53 -0
- package/editing/collectComponents.ts +36 -0
- package/editing/collectIconUsages.ts +78 -0
- package/editing/describeNode.ts +61 -0
- package/editing/diagnoseIcons.ts +60 -0
- package/editing/diagnostics.ts +30 -0
- package/editing/documentPaths.ts +40 -0
- package/editing/exposureEdits.ts +119 -0
- package/editing/exposureGrants.ts +159 -0
- package/editing/given/a_layout_document.ts +88 -0
- package/editing/given/a_scene_document.ts +135 -0
- package/editing/iconDiagnostics.ts +90 -0
- package/editing/index.ts +24 -0
- package/editing/indexDocument.ts +237 -0
- package/editing/inspect.ts +165 -0
- package/editing/instanceEdits.ts +251 -0
- package/editing/layoutTypeDescriptors.ts +191 -0
- package/editing/layoutTypeEdits.ts +135 -0
- package/editing/nodeIds.ts +111 -0
- package/editing/pathAccess.ts +81 -0
- package/editing/propertyEdits.ts +127 -0
- package/editing/resolveEffectiveConfiguration.ts +202 -0
- package/editing/resolveTemplateChain.ts +109 -0
- package/editing/structureEdits.ts +234 -0
- package/editing/validateBinding.ts +103 -0
- package/editing/validateValue.ts +130 -0
- package/icons/EffectiveIconCatalog.ts +199 -0
- package/icons/EffectiveIconEntry.ts +36 -0
- package/icons/IconCatalogSource.ts +23 -0
- package/icons/IconDiagnostic.ts +45 -0
- package/icons/IconImpactReport.ts +63 -0
- package/icons/IconLibraryResolution.ts +22 -0
- package/icons/IconQuery.ts +27 -0
- package/icons/IconResolution.ts +25 -0
- package/icons/IconSearchResult.ts +21 -0
- package/icons/IconUsage.ts +21 -0
- package/icons/ResolvedIconLibrary.ts +32 -0
- package/icons/analyzeIconImpact.ts +41 -0
- package/icons/index.ts +16 -0
- package/icons/matchesIconQuery.ts +19 -0
- package/icons/resolveIconLibraries.ts +47 -0
- package/index.ts +2 -0
- package/package.json +3 -3
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
DiagnosticCode, DialogTemplate, ExternalComponent, FlowArrangement, FlowContainer, FlowLeaf, FlowNode,
|
|
6
|
+
FreeformArrangement, FreeformSlotArrangement, Layout, LayoutType, SceneDocument, SceneElement, SceneNodeKind, Screen,
|
|
7
|
+
ScreenTemplate, Slot, FlowContainerKind,
|
|
8
|
+
} from '@cratis/scene.model';
|
|
9
|
+
import { isContentControl, isExternalComponent, isItemsControl, isPanel } from '../elementKind';
|
|
10
|
+
import { isFlowContainer, isFlowLeaf, isFlowSlotLeaf } from '../flowNodeKind';
|
|
11
|
+
import { isCanvas, isDockPanel, isGrid, isStackPanel, isWrapPanel } from '../panelKind';
|
|
12
|
+
import { errorDiagnostic } from './diagnostics';
|
|
13
|
+
import { DocumentIndex, DocumentPath, ListLocation, NodeRecord } from './DocumentIndex';
|
|
14
|
+
import {
|
|
15
|
+
dialogTemplateNodeId, elementNodeId, flowNodeId, freeformNodeId, layoutNodeId, ownerArrangementId, placementNodeId,
|
|
16
|
+
screenNodeId, screenTemplateNodeId, slotNodeId,
|
|
17
|
+
} from './nodeIds';
|
|
18
|
+
|
|
19
|
+
type Owner = Layout | ScreenTemplate | DialogTemplate | Screen;
|
|
20
|
+
|
|
21
|
+
interface OwnerContext {
|
|
22
|
+
owner: string;
|
|
23
|
+
ownerKind: SceneNodeKind;
|
|
24
|
+
ownerId: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface Placement {
|
|
28
|
+
parentId?: string;
|
|
29
|
+
list?: ListLocation;
|
|
30
|
+
leafId?: string;
|
|
31
|
+
mirror?: NodeRecord['mirror'];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The layout type a panel element is, by its shape.
|
|
36
|
+
*/
|
|
37
|
+
export function panelLayoutType(element: SceneElement): LayoutType | undefined {
|
|
38
|
+
if (!isPanel(element)) return undefined;
|
|
39
|
+
if (isGrid(element)) return LayoutType.GridPanel;
|
|
40
|
+
if (isCanvas(element)) return LayoutType.Canvas;
|
|
41
|
+
if (isDockPanel(element)) return LayoutType.DockPanel;
|
|
42
|
+
if (isStackPanel(element)) return LayoutType.StackPanel;
|
|
43
|
+
if (isWrapPanel(element)) return LayoutType.WrapPanel;
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The layout type a flow node is.
|
|
49
|
+
*/
|
|
50
|
+
export function flowLayoutType(node: FlowNode): LayoutType {
|
|
51
|
+
if (isFlowContainer(node)) {
|
|
52
|
+
switch (node.kind) {
|
|
53
|
+
case FlowContainerKind.Row: return LayoutType.FlowRow;
|
|
54
|
+
case FlowContainerKind.Column: return LayoutType.FlowColumn;
|
|
55
|
+
default: return LayoutType.FlowGrid;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return isFlowSlotLeaf(node) ? LayoutType.FlowSlotLeaf : LayoutType.FlowLeaf;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
class Indexer {
|
|
63
|
+
readonly index: DocumentIndex = { nodes: new Map(), diagnostics: [] };
|
|
64
|
+
|
|
65
|
+
constructor(private readonly document: SceneDocument) {}
|
|
66
|
+
|
|
67
|
+
run(): DocumentIndex {
|
|
68
|
+
this.document.layouts.forEach((layout, position) =>
|
|
69
|
+
this.owner(layout, ['layouts', position], { owner: layout.name, ownerKind: SceneNodeKind.Layout, ownerId: layoutNodeId(layout.name) }, undefined));
|
|
70
|
+
this.document.screenTemplates.forEach((template, position) =>
|
|
71
|
+
this.owner(template, ['screenTemplates', position], { owner: template.name, ownerKind: SceneNodeKind.ScreenTemplate, ownerId: screenTemplateNodeId(template.name) }, 'content'));
|
|
72
|
+
this.document.dialogTemplates.forEach((dialog, position) =>
|
|
73
|
+
this.owner(dialog, ['dialogTemplates', position], { owner: dialog.name, ownerKind: SceneNodeKind.DialogTemplate, ownerId: dialogTemplateNodeId(dialog.name) }, 'content'));
|
|
74
|
+
this.document.screens.forEach((screen, position) =>
|
|
75
|
+
this.owner(screen, ['screens', position], { owner: screen.name, ownerKind: SceneNodeKind.Screen, ownerId: screenNodeId(screen.name) }, 'slotContent'));
|
|
76
|
+
return this.index;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private add(record: NodeRecord): boolean {
|
|
80
|
+
if (this.index.nodes.has(record.id)) return false;
|
|
81
|
+
this.index.nodes.set(record.id, record);
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private owner(owner: Owner, path: DocumentPath, context: OwnerContext, contentKey: 'content' | 'slotContent' | undefined): void {
|
|
86
|
+
this.add({ id: context.ownerId, kind: context.ownerKind, typeId: context.ownerKind, label: owner.name, path, value: owner, owner: context.owner, ownerKind: context.ownerKind });
|
|
87
|
+
|
|
88
|
+
const content = contentKey === undefined ? undefined : (owner as unknown as Record<string, Record<string, SceneElement[]> | undefined>)[contentKey];
|
|
89
|
+
const ownsSlots = 'slots' in owner;
|
|
90
|
+
const declared: Slot[] = ownsSlots ? owner.slots : this.slotsAvailableTo(owner as Screen);
|
|
91
|
+
const slotNames = new Set<string>([...declared.map(slot => slot.name), ...Object.keys(content ?? {})]);
|
|
92
|
+
|
|
93
|
+
for (const name of slotNames) {
|
|
94
|
+
const slotId = slotNodeId(context.ownerId, name);
|
|
95
|
+
const declaration = ownsSlots ? declared.findIndex(slot => slot.name === name) : -1;
|
|
96
|
+
const contentPath: DocumentPath | undefined = contentKey === undefined ? undefined : [...path, contentKey, name];
|
|
97
|
+
this.add({
|
|
98
|
+
id: slotId, kind: SceneNodeKind.Slot, typeId: 'slot', label: name, path: declaration >= 0 ? [...path, 'slots', declaration] : contentPath!,
|
|
99
|
+
value: declaration >= 0 ? declared[declaration] : content?.[name], parentId: context.ownerId, owner: context.owner,
|
|
100
|
+
ownerKind: context.ownerKind, slotName: name, contentPath,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (declaration >= 0 && declared[declaration].arrangement) {
|
|
104
|
+
this.arrangement(declared[declaration].arrangement!, [...path, 'slots', declaration, 'arrangement'], slotId, context);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
(content?.[name] ?? []).forEach((element, position) =>
|
|
108
|
+
this.element(element, [...contentPath!, position], context, { parentId: slotId, list: { path: contentPath!, index: position } }));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if ('arrangement' in owner && owner.arrangement) {
|
|
112
|
+
this.arrangement(owner.arrangement, [...path, 'arrangement'], ownerArrangementId(context.ownerId), context, context.ownerId);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if ('contributions' in owner) {
|
|
116
|
+
owner.contributions.forEach((contribution, position) =>
|
|
117
|
+
this.element(contribution.content, [...path, 'contributions', position, 'content'], context, { parentId: context.ownerId }));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private slotsAvailableTo(screen: Screen): Slot[] {
|
|
122
|
+
const template = screen.screenTemplate === undefined ? undefined : this.document.screenTemplates.find(candidate => candidate.name === screen.screenTemplate);
|
|
123
|
+
if (template) return template.slots;
|
|
124
|
+
return this.document.layouts.find(candidate => candidate.name === screen.layout)?.slots ?? [];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private element(element: SceneElement, path: DocumentPath, context: OwnerContext, placement: Placement): boolean {
|
|
128
|
+
const id = elementNodeId(element.id);
|
|
129
|
+
const panelType = panelLayoutType(element);
|
|
130
|
+
const typeId = isExternalComponent(element) ? element.componentName
|
|
131
|
+
: (panelType ?? (isContentControl(element) ? 'contentControl' : (isItemsControl(element) ? 'itemsControl' : 'element')));
|
|
132
|
+
const properties = (element as ExternalComponent).properties;
|
|
133
|
+
const label = (typeof properties?.label === 'string' && properties.label)
|
|
134
|
+
|| (isPanel(element) && (element as { name?: string }).name)
|
|
135
|
+
|| (isExternalComponent(element) && element.name)
|
|
136
|
+
|| typeId;
|
|
137
|
+
|
|
138
|
+
if (!this.add({
|
|
139
|
+
id, kind: SceneNodeKind.Element, typeId, label, path, value: element, owner: context.owner, ownerKind: context.ownerKind, ...placement,
|
|
140
|
+
})) {
|
|
141
|
+
this.index.diagnostics.push(errorDiagnostic(DiagnosticCode.DuplicateElementId, `The element id '${element.id}' is used more than once.`, { nodeId: id }));
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (isExternalComponent(element)) {
|
|
146
|
+
for (const [name, children] of Object.entries(element.slots)) {
|
|
147
|
+
children.forEach((child, position) =>
|
|
148
|
+
this.element(child, [...path, 'slots', name, position], context, { parentId: id, list: { path: [...path, 'slots', name], index: position }, mirror: placement.mirror }));
|
|
149
|
+
}
|
|
150
|
+
} else if (isContentControl(element)) {
|
|
151
|
+
this.element(element.content, [...path, 'content'], context, { parentId: id, mirror: placement.mirror });
|
|
152
|
+
} else if (isItemsControl(element)) {
|
|
153
|
+
this.element(element.itemTemplate, [...path, 'itemTemplate'], context, { parentId: id, mirror: placement.mirror });
|
|
154
|
+
} else if (isPanel(element)) {
|
|
155
|
+
element.children.forEach((child, position) =>
|
|
156
|
+
this.element(child, [...path, 'children', position], context, { parentId: id, list: { path: [...path, 'children'], index: position }, mirror: placement.mirror }));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private arrangement(arrangement: unknown, path: DocumentPath, holderId: string, context: OwnerContext, parentId = holderId): void {
|
|
163
|
+
if ((arrangement as FlowArrangement).root !== undefined) {
|
|
164
|
+
const flow = arrangement as FlowArrangement;
|
|
165
|
+
this.flowNode(flow.root, [...path, 'root'], holderId, 'root', [], context, { parentId });
|
|
166
|
+
flow.overrides?.forEach((override, position) =>
|
|
167
|
+
this.flowNode(override.root, [...path, 'overrides', position, 'root'], holderId, `override${position}`, [], context, { parentId }));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const variants = (arrangement as FreeformArrangement | FreeformSlotArrangement).variants;
|
|
172
|
+
if (!Array.isArray(variants)) return;
|
|
173
|
+
|
|
174
|
+
const freeformId = freeformNodeId(holderId);
|
|
175
|
+
this.add({ id: freeformId, kind: SceneNodeKind.Freeform, typeId: LayoutType.Freeform, label: 'Freeform', path, value: arrangement, parentId, owner: context.owner, ownerKind: context.ownerKind });
|
|
176
|
+
|
|
177
|
+
const firstCopy = new Map<string, DocumentPath>();
|
|
178
|
+
variants.forEach((variant, variantIndex) => {
|
|
179
|
+
(variant.placements as { element?: SceneElement; slotName?: string }[]).forEach((placement, placementIndex) => {
|
|
180
|
+
const placementPath = [...path, 'variants', variantIndex, 'placements', placementIndex];
|
|
181
|
+
const key = placement.element?.id ?? placement.slotName ?? String(placementIndex);
|
|
182
|
+
const placementId = placementNodeId(holderId, variantIndex, key);
|
|
183
|
+
this.add({
|
|
184
|
+
id: placementId, kind: SceneNodeKind.Placement, typeId: 'placement', label: key, path: placementPath, value: placement,
|
|
185
|
+
parentId: freeformId, owner: context.owner, ownerKind: context.ownerKind,
|
|
186
|
+
list: { path: [...path, 'variants', variantIndex, 'placements'], index: placementIndex },
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
if (!placement.element) return;
|
|
190
|
+
const elementPath = [...placementPath, 'element'];
|
|
191
|
+
const known = this.index.nodes.get(elementNodeId(placement.element.id));
|
|
192
|
+
const original = firstCopy.get(placement.element.id);
|
|
193
|
+
if (known && original) {
|
|
194
|
+
known.placementIds!.push(placementId);
|
|
195
|
+
known.mirror!.others.push(elementPath);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
firstCopy.set(placement.element.id, elementPath);
|
|
200
|
+
const mirror = { prefix: elementPath, others: [] as DocumentPath[] };
|
|
201
|
+
if (this.element(placement.element, elementPath, context, { parentId: placementId, mirror })) {
|
|
202
|
+
this.index.nodes.get(elementNodeId(placement.element.id))!.placementIds = [placementId];
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// Descendants of a freeform element carry the same mirror object, so a copy found later is seen by all of them.
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private flowNode(node: FlowNode, path: DocumentPath, holderId: string, variant: string, indexPath: number[], context: OwnerContext, placement: Placement): void {
|
|
211
|
+
const id = flowNodeId(holderId, variant, indexPath);
|
|
212
|
+
const type = flowLayoutType(node);
|
|
213
|
+
this.add({ id, kind: SceneNodeKind.FlowNode, typeId: type, label: type, path, value: node, owner: context.owner, ownerKind: context.ownerKind, ...placement });
|
|
214
|
+
|
|
215
|
+
if (isFlowContainer(node)) {
|
|
216
|
+
(node as FlowContainer).children.forEach((child, position) =>
|
|
217
|
+
this.flowNode(child, [...path, 'children', position], holderId, variant, [...indexPath, position], context,
|
|
218
|
+
{ parentId: id, list: { path: [...path, 'children'], index: position } }));
|
|
219
|
+
} else if (isFlowLeaf(node)) {
|
|
220
|
+
this.element((node as FlowLeaf).content, [...path, 'content'], context, { parentId: id, leafId: id });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Indexes every addressable node of a document: layouts, templates, dialog templates, screens, their slots, the
|
|
228
|
+
* elements in them, and the flow and freeform arrangements that position them.
|
|
229
|
+
*
|
|
230
|
+
* The index is what inspection and editing look nodes up in. Duplicate element ids are reported, and the second
|
|
231
|
+
* occurrence is left out of the index.
|
|
232
|
+
*
|
|
233
|
+
* @param document The document to index.
|
|
234
|
+
*/
|
|
235
|
+
export function indexDocument(document: SceneDocument): DocumentIndex {
|
|
236
|
+
return new Indexer(document).run();
|
|
237
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
DiagnosticCode, DiagnosticSeverity, EditTarget, InspectedCollectionItem, InspectedProperty, NodeInspection,
|
|
6
|
+
NodeProvenance, ProvenanceKind, PropertyDescriptor, PropertyValueType, SceneDiagnostic, SceneDocument, SceneNodeKind, ValueSource,
|
|
7
|
+
} from '@cratis/scene.model';
|
|
8
|
+
import { describeNode, readNodeValue } from './describeNode';
|
|
9
|
+
import { EffectiveIconCatalog } from '../icons/EffectiveIconCatalog';
|
|
10
|
+
import { EditingContext } from './EditingContext';
|
|
11
|
+
import { computeExposureGrants, ExposureGrant, grantKey } from './exposureGrants';
|
|
12
|
+
import { iconProblemsOfItems, iconProblemsOfProperty } from './iconDiagnostics';
|
|
13
|
+
import { indexDocument } from './indexDocument';
|
|
14
|
+
import { NodeRecord } from './DocumentIndex';
|
|
15
|
+
import { resolveEffectiveConfiguration } from './resolveEffectiveConfiguration';
|
|
16
|
+
import { resolveTemplateChain } from './resolveTemplateChain';
|
|
17
|
+
import { TemplateChain } from './TemplateChain';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Works out whether a node is the editing scope's own, and when it is not, where it comes from and whether any of
|
|
21
|
+
* it is configurable here.
|
|
22
|
+
*
|
|
23
|
+
* @param record The node.
|
|
24
|
+
* @param chain The chain the scope sits in.
|
|
25
|
+
* @param scopeGrants What the scope's own instance may configure.
|
|
26
|
+
*/
|
|
27
|
+
export function provenanceOf(record: NodeRecord, chain: TemplateChain, scopeGrants: Map<string, ExposureGrant> | undefined): NodeProvenance {
|
|
28
|
+
const scopeLevel = chain.levels.at(-1);
|
|
29
|
+
if (scopeLevel && record.owner === scopeLevel.owner && record.ownerKind === scopeLevel.kind) return { kind: ProvenanceKind.Local };
|
|
30
|
+
|
|
31
|
+
const configurable = record.kind === SceneNodeKind.Element
|
|
32
|
+
&& [...(scopeGrants?.values() ?? [])].some(grant => grant.component === (record.value as { id: string }).id);
|
|
33
|
+
return { kind: configurable ? ProvenanceKind.ConfigurableInherited : ProvenanceKind.Inherited, source: record.owner };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isRemovable(record: NodeRecord, provenance: NodeProvenance): boolean {
|
|
37
|
+
return provenance.kind === ProvenanceKind.Local
|
|
38
|
+
&& (record.list !== undefined || record.leafId !== undefined || record.placementIds !== undefined);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function toInspectedItems(raw: unknown, local: boolean): InspectedCollectionItem[] {
|
|
42
|
+
return (Array.isArray(raw) ? raw : []).map((item: Record<string, unknown>, position) => {
|
|
43
|
+
const { id, ...values } = item;
|
|
44
|
+
return { id: typeof id === 'string' ? id : `owner-${position}`, values, origin: 'owner', editable: local };
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function inspectProperty(
|
|
49
|
+
descriptor: PropertyDescriptor,
|
|
50
|
+
record: NodeRecord,
|
|
51
|
+
provenance: NodeProvenance,
|
|
52
|
+
scopeInstance: string | undefined,
|
|
53
|
+
grants: Map<string, ExposureGrant> | undefined,
|
|
54
|
+
configuredValues: { path: string; value: unknown; source: string; items?: { id: string; values: Record<string, unknown>; origin: string }[] }[] | undefined,
|
|
55
|
+
): InspectedProperty {
|
|
56
|
+
const currentValue = readNodeValue(record, descriptor.path);
|
|
57
|
+
const configured = configuredValues?.find(candidate => candidate.path === descriptor.path);
|
|
58
|
+
const local = provenance.kind === ProvenanceKind.Local;
|
|
59
|
+
const isCollection = descriptor.valueType === PropertyValueType.Collection;
|
|
60
|
+
|
|
61
|
+
const inspected: InspectedProperty = {
|
|
62
|
+
descriptor,
|
|
63
|
+
currentValue,
|
|
64
|
+
effectiveValue: configured ? configured.value : (currentValue ?? descriptor.default),
|
|
65
|
+
source: (configured?.source ?? (currentValue === undefined ? ValueSource.Default : ValueSource.Local)) as ValueSource,
|
|
66
|
+
editable: false,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const grant = !local && record.kind === SceneNodeKind.Element
|
|
70
|
+
? grants?.get(grantKey((record.value as { id: string }).id, descriptor.path))
|
|
71
|
+
: undefined;
|
|
72
|
+
|
|
73
|
+
if (local) {
|
|
74
|
+
inspected.editable = descriptor.readOnly !== true;
|
|
75
|
+
if (inspected.editable) inspected.editTarget = EditTarget.Node;
|
|
76
|
+
else inspected.reason = 'This property is read-only.';
|
|
77
|
+
} else if (grant) {
|
|
78
|
+
inspected.editable = true;
|
|
79
|
+
inspected.editTarget = EditTarget.Instance;
|
|
80
|
+
if (isCollection) inspected.operations = grant.operations;
|
|
81
|
+
} else {
|
|
82
|
+
inspected.reason = `Inherited from '${provenance.source}', which does not expose it.`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (isCollection) {
|
|
86
|
+
const items = configured?.items
|
|
87
|
+
? configured.items.map(item => ({ ...item, editable: local || (item.origin === scopeInstance && grant !== undefined && grant.operations.length > 0) }))
|
|
88
|
+
: toInspectedItems(currentValue, local);
|
|
89
|
+
inspected.items = items;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return inspected;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Reports icons the profile cannot supply - on the node itself and in configuration contributed to it. The values
|
|
97
|
+
* stay where they are; these are warnings.
|
|
98
|
+
*/
|
|
99
|
+
function iconDiagnosticsOf(properties: InspectedProperty[], iconCatalog: EffectiveIconCatalog, nodeId: string): SceneDiagnostic[] {
|
|
100
|
+
return properties.flatMap(property => {
|
|
101
|
+
const descriptor = property.descriptor;
|
|
102
|
+
if (descriptor.valueType !== PropertyValueType.Collection) {
|
|
103
|
+
return iconProblemsOfProperty(iconCatalog, descriptor, property.effectiveValue, DiagnosticSeverity.Warning, { nodeId });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const items = (property.items ?? []).map(item => ({ id: item.id, values: item.values, instance: item.origin === 'owner' ? undefined : item.origin }));
|
|
107
|
+
return iconProblemsOfItems(iconCatalog, descriptor.item, items, DiagnosticSeverity.Warning, { nodeId, path: descriptor.path });
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Describes one node for an editor: what it is, where it comes from, what can be edited on it - with current and
|
|
113
|
+
* effective values and effective editability - and what can be done with it.
|
|
114
|
+
*
|
|
115
|
+
* A property of a node the scope owns is edited on the node. A property of an inherited node is editable only
|
|
116
|
+
* when its owner exposed it, and then the edit goes to an instance contribution, never into the inherited node.
|
|
117
|
+
* Nothing is mutated.
|
|
118
|
+
*
|
|
119
|
+
* @param document The document to read.
|
|
120
|
+
* @param nodeId The node to describe; see the node id functions.
|
|
121
|
+
* @param context The catalog and the editing scope.
|
|
122
|
+
* @returns The inspection, or `undefined` when the document has no such node.
|
|
123
|
+
*/
|
|
124
|
+
export function inspect(document: SceneDocument, nodeId: string, context: EditingContext): NodeInspection | undefined {
|
|
125
|
+
const index = indexDocument(document);
|
|
126
|
+
const record = index.nodes.get(nodeId);
|
|
127
|
+
if (!record) return undefined;
|
|
128
|
+
|
|
129
|
+
const chain = resolveTemplateChain(document, context.scope);
|
|
130
|
+
const grants = computeExposureGrants(chain, context.catalog);
|
|
131
|
+
const scopeInstance = chain.levels.at(-1)?.instance;
|
|
132
|
+
const scopeGrants = scopeInstance === undefined ? undefined : grants.byInstance.get(scopeInstance);
|
|
133
|
+
const configuration = resolveEffectiveConfiguration(chain, document.instanceContributions, context.catalog);
|
|
134
|
+
|
|
135
|
+
const provenance = provenanceOf(record, chain, scopeGrants);
|
|
136
|
+
const description = describeNode(record, context.catalog);
|
|
137
|
+
const componentId = record.kind === SceneNodeKind.Element ? (record.value as { id: string }).id : undefined;
|
|
138
|
+
const configuredValues = configuration.components.find(candidate => candidate.component === componentId)?.values;
|
|
139
|
+
|
|
140
|
+
const diagnostics: SceneDiagnostic[] = [
|
|
141
|
+
...description.diagnostics,
|
|
142
|
+
...configuration.diagnostics.filter(diagnostic => diagnostic.component !== undefined && diagnostic.component === componentId),
|
|
143
|
+
];
|
|
144
|
+
if (chain.levels.length === 0) {
|
|
145
|
+
diagnostics.push(...chain.diagnostics.filter(diagnostic => diagnostic.code === DiagnosticCode.UnknownScope));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const properties = description.properties.map(descriptor => inspectProperty(descriptor, record, provenance, scopeInstance, scopeGrants, configuredValues));
|
|
149
|
+
if (context.iconCatalog) {
|
|
150
|
+
diagnostics.push(...iconDiagnosticsOf(properties, context.iconCatalog, nodeId));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
nodeId,
|
|
155
|
+
kind: record.kind,
|
|
156
|
+
typeId: record.typeId,
|
|
157
|
+
label: record.label,
|
|
158
|
+
parentId: record.parentId,
|
|
159
|
+
provenance,
|
|
160
|
+
properties,
|
|
161
|
+
capabilities: description.capabilities,
|
|
162
|
+
removable: isRemovable(record, provenance),
|
|
163
|
+
diagnostics,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
AddCollectionItemEdit, CollectionOperation, ContributedItem, DiagnosticCode, DiagnosticSeverity, EditCollectionItemEdit, InstanceContribution,
|
|
6
|
+
PropertyValueType, RemoveCollectionItemEdit, ReorderCollectionItemEdit, ResetInstanceValueEdit, SceneDiagnostic,
|
|
7
|
+
SceneDocument, SetInstanceValueEdit,
|
|
8
|
+
} from '@cratis/scene.model';
|
|
9
|
+
import { errorDiagnostic, hasErrors } from './diagnostics';
|
|
10
|
+
import { iconProblemsOfItems } from './iconDiagnostics';
|
|
11
|
+
import { EditSession } from './EditSession';
|
|
12
|
+
import { ExposureGrant, grantKey } from './exposureGrants';
|
|
13
|
+
import { cloneData } from './pathAccess';
|
|
14
|
+
import { checkValue } from './propertyEdits';
|
|
15
|
+
import { validateItemValues } from './validateValue';
|
|
16
|
+
|
|
17
|
+
interface InstanceEditTarget {
|
|
18
|
+
instance: string;
|
|
19
|
+
component: string;
|
|
20
|
+
path: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function resolveGrant(session: EditSession, edit: InstanceEditTarget & { instance?: string }, diagnostics: SceneDiagnostic[]): { instance: string; grant: ExposureGrant } | undefined {
|
|
24
|
+
const context = { instance: edit.instance, component: edit.component, path: edit.path };
|
|
25
|
+
const instance = session.scopeInstance;
|
|
26
|
+
if (instance === undefined) {
|
|
27
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.UnknownScope, `There is no ${session.context.scope.kind} named '${session.context.scope.name}'.`, context));
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (edit.instance !== undefined && edit.instance !== instance) {
|
|
32
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.UnknownInstance, `Edits are made as '${instance}', not as '${edit.instance}'.`, context));
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const grant = session.scopeGrants.get(grantKey(edit.component, edit.path));
|
|
37
|
+
if (!grant) {
|
|
38
|
+
diagnostics.push(errorDiagnostic(
|
|
39
|
+
DiagnosticCode.ContributionNotExposed,
|
|
40
|
+
`'${edit.path}' on '${edit.component}' is not exposed to '${instance}'. An owner exposes it, and each template between re-exposes it.`,
|
|
41
|
+
{ ...context, instance }));
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return { instance, grant };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function findContribution(clone: SceneDocument, instance: string, component: string, path: string): InstanceContribution | undefined {
|
|
49
|
+
return clone.instanceContributions.find(contribution =>
|
|
50
|
+
contribution.instance === instance && contribution.component === component && contribution.path === path);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function permitted(grant: ExposureGrant, operation: CollectionOperation, instance: string, diagnostics: SceneDiagnostic[]): boolean {
|
|
54
|
+
if (grant.descriptor.valueType !== PropertyValueType.Collection) {
|
|
55
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.InvalidEdit, `'${grant.descriptor.label}' is not a collection.`, { instance, component: grant.component, path: grant.path }));
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!grant.operations.includes(operation)) {
|
|
60
|
+
diagnostics.push(errorDiagnostic(
|
|
61
|
+
DiagnosticCode.ContributionOperationNotPermitted,
|
|
62
|
+
`'${grant.descriptor.label}' does not allow '${operation}' from '${instance}'.`,
|
|
63
|
+
{ instance, component: grant.component, path: grant.path }));
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function checkFields(session: EditSession, grant: ExposureGrant, values: Record<string, unknown>, itemId: string, instance: string, diagnostics: SceneDiagnostic[], requireAll: boolean): boolean {
|
|
71
|
+
const context = { instance, component: grant.component, path: grant.path, itemId };
|
|
72
|
+
let valid = true;
|
|
73
|
+
|
|
74
|
+
for (const field of Object.keys(values)) {
|
|
75
|
+
const known = grant.descriptor.item?.properties.some(candidate => candidate.path === field) ?? false;
|
|
76
|
+
if (known && grant.editableFields !== undefined && !grant.editableFields.includes(field)) {
|
|
77
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.ContributionOperationNotPermitted, `The field '${field}' of '${grant.descriptor.label}' is not exposed for editing.`, context));
|
|
78
|
+
valid = false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const itemDescriptor = grant.descriptor.item;
|
|
83
|
+
const problems = validateItemValues(itemDescriptor, values).filter(problem => requireAll || problem.problem !== 'a value is required');
|
|
84
|
+
for (const problem of problems) {
|
|
85
|
+
diagnostics.push(errorDiagnostic(
|
|
86
|
+
problem.problem.startsWith('is not a field') ? DiagnosticCode.UnknownCollectionField : DiagnosticCode.InvalidValue,
|
|
87
|
+
`'${problem.field}' ${problem.problem}.`,
|
|
88
|
+
context));
|
|
89
|
+
valid = false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const icons = session.context.iconCatalog;
|
|
93
|
+
if (icons && valid) {
|
|
94
|
+
const iconProblems = iconProblemsOfItems(icons, itemDescriptor, [{ id: itemId, values }], DiagnosticSeverity.Error, context);
|
|
95
|
+
diagnostics.push(...iconProblems);
|
|
96
|
+
valid = !hasErrors(iconProblems);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return valid;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function ownItems(clone: SceneDocument, instance: string, grant: ExposureGrant): { contribution: InstanceContribution | undefined; items: ContributedItem[] } {
|
|
103
|
+
const contribution = findContribution(clone, instance, grant.component, grant.path);
|
|
104
|
+
return { contribution, items: contribution?.items ?? [] };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function save(clone: SceneDocument, instance: string, grant: ExposureGrant, existing: InstanceContribution | undefined, items: ContributedItem[]): void {
|
|
108
|
+
if (items.length === 0) {
|
|
109
|
+
if (existing) clone.instanceContributions.splice(clone.instanceContributions.indexOf(existing), 1);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (existing) existing.items = items;
|
|
114
|
+
else clone.instanceContributions.push({ instance, component: grant.component, path: grant.path, items });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Applies a scalar value set from the editing scope's instance to a copy of the document.
|
|
119
|
+
*/
|
|
120
|
+
export function applySetInstanceValue(session: EditSession, edit: SetInstanceValueEdit, clone: SceneDocument, diagnostics: SceneDiagnostic[]): void {
|
|
121
|
+
const resolved = resolveGrant(session, { ...edit, instance: edit.instance } as InstanceEditTarget, diagnostics);
|
|
122
|
+
if (!resolved) return;
|
|
123
|
+
|
|
124
|
+
if (resolved.grant.descriptor.valueType === PropertyValueType.Collection) {
|
|
125
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.InvalidEdit, `'${resolved.grant.descriptor.label}' is a collection; add, remove, reorder and edit its items instead.`, { instance: resolved.instance, component: edit.component, path: edit.path }));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (!checkValue(session, resolved.grant.descriptor, edit.value, { component: edit.component, instance: resolved.instance }, diagnostics)) return;
|
|
130
|
+
|
|
131
|
+
const existing = findContribution(clone, resolved.instance, edit.component, edit.path);
|
|
132
|
+
if (existing) existing.value = cloneData(edit.value);
|
|
133
|
+
else clone.instanceContributions.push({ instance: resolved.instance, component: edit.component, path: edit.path, value: cloneData(edit.value) });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Applies a reset to a copy of the document: the instance's contribution is removed, so the inherited value applies.
|
|
138
|
+
*/
|
|
139
|
+
export function applyResetInstanceValue(session: EditSession, edit: ResetInstanceValueEdit, clone: SceneDocument, diagnostics: SceneDiagnostic[]): void {
|
|
140
|
+
const resolved = resolveGrant(session, edit as InstanceEditTarget, diagnostics);
|
|
141
|
+
if (!resolved) return;
|
|
142
|
+
|
|
143
|
+
const existing = findContribution(clone, resolved.instance, edit.component, edit.path);
|
|
144
|
+
if (existing) clone.instanceContributions.splice(clone.instanceContributions.indexOf(existing), 1);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Applies an item addition to a copy of the document.
|
|
149
|
+
*/
|
|
150
|
+
export function applyAddCollectionItem(session: EditSession, edit: AddCollectionItemEdit, clone: SceneDocument, diagnostics: SceneDiagnostic[]): void {
|
|
151
|
+
const resolved = resolveGrant(session, edit as InstanceEditTarget, diagnostics);
|
|
152
|
+
if (!resolved || !permitted(resolved.grant, CollectionOperation.Add, resolved.instance, diagnostics)) return;
|
|
153
|
+
|
|
154
|
+
const { grant, instance } = resolved;
|
|
155
|
+
const context = { instance, component: edit.component, path: edit.path, itemId: edit.item.id };
|
|
156
|
+
if (typeof edit.item.id !== 'string' || edit.item.id.length === 0) {
|
|
157
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.InvalidValue, 'An item needs an id.', context));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const effective = session.configuration.components.find(candidate => candidate.component === edit.component)?.values.find(value => value.path === edit.path);
|
|
162
|
+
const existingItems = effective?.items ?? [];
|
|
163
|
+
if (existingItems.some(item => item.id === edit.item.id)) {
|
|
164
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.DuplicateCollectionItem, `The item id '${edit.item.id}' is already used.`, context));
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const maximum = grant.descriptor.constraints?.maximumItems;
|
|
169
|
+
if (maximum !== undefined && existingItems.length >= maximum) {
|
|
170
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.InvalidValue, `'${grant.descriptor.label}' allows at most ${maximum} items.`, context));
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (!checkFields(session, grant, edit.item.values, edit.item.id, instance, diagnostics, true)) return;
|
|
175
|
+
|
|
176
|
+
const { contribution, items } = ownItems(clone, instance, grant);
|
|
177
|
+
if (edit.index !== undefined && (!Number.isInteger(edit.index) || edit.index < 0 || edit.index > items.length)) {
|
|
178
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.IndexOutOfRange, `There is no position ${edit.index} among ${items.length} items.`, context));
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const next = [...items];
|
|
183
|
+
next.splice(edit.index ?? next.length, 0, cloneData(edit.item));
|
|
184
|
+
save(clone, instance, grant, contribution, next);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function findOwnItem(items: ContributedItem[], itemId: string, instance: string, grant: ExposureGrant, session: EditSession, diagnostics: SceneDiagnostic[]): number {
|
|
188
|
+
const position = items.findIndex(item => item.id === itemId);
|
|
189
|
+
if (position >= 0) return position;
|
|
190
|
+
|
|
191
|
+
const effective = session.configuration.components.find(candidate => candidate.component === grant.component)?.values.find(value => value.path === grant.path);
|
|
192
|
+
const fixed = effective?.items?.find(item => item.id === itemId);
|
|
193
|
+
diagnostics.push(errorDiagnostic(
|
|
194
|
+
fixed ? DiagnosticCode.ContributionOperationNotPermitted : DiagnosticCode.CollectionItemNotFound,
|
|
195
|
+
fixed ? `The item '${itemId}' belongs to '${fixed.origin}' and cannot be changed from '${instance}'.` : `'${instance}' has no item '${itemId}' in '${grant.descriptor.label}'.`,
|
|
196
|
+
{ instance, component: grant.component, path: grant.path, itemId }));
|
|
197
|
+
return -1;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Applies an item removal to a copy of the document.
|
|
202
|
+
*/
|
|
203
|
+
export function applyRemoveCollectionItem(session: EditSession, edit: RemoveCollectionItemEdit, clone: SceneDocument, diagnostics: SceneDiagnostic[]): void {
|
|
204
|
+
const resolved = resolveGrant(session, edit as InstanceEditTarget, diagnostics);
|
|
205
|
+
if (!resolved || !permitted(resolved.grant, CollectionOperation.Remove, resolved.instance, diagnostics)) return;
|
|
206
|
+
|
|
207
|
+
const { contribution, items } = ownItems(clone, resolved.instance, resolved.grant);
|
|
208
|
+
const position = findOwnItem(items, edit.itemId, resolved.instance, resolved.grant, session, diagnostics);
|
|
209
|
+
if (position < 0) return;
|
|
210
|
+
|
|
211
|
+
save(clone, resolved.instance, resolved.grant, contribution, items.filter((_, index) => index !== position));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Applies an item reorder to a copy of the document.
|
|
216
|
+
*/
|
|
217
|
+
export function applyReorderCollectionItem(session: EditSession, edit: ReorderCollectionItemEdit, clone: SceneDocument, diagnostics: SceneDiagnostic[]): void {
|
|
218
|
+
const resolved = resolveGrant(session, edit as InstanceEditTarget, diagnostics);
|
|
219
|
+
if (!resolved || !permitted(resolved.grant, CollectionOperation.Reorder, resolved.instance, diagnostics)) return;
|
|
220
|
+
|
|
221
|
+
const { contribution, items } = ownItems(clone, resolved.instance, resolved.grant);
|
|
222
|
+
const position = findOwnItem(items, edit.itemId, resolved.instance, resolved.grant, session, diagnostics);
|
|
223
|
+
if (position < 0) return;
|
|
224
|
+
|
|
225
|
+
if (!Number.isInteger(edit.index) || edit.index < 0 || edit.index >= items.length) {
|
|
226
|
+
diagnostics.push(errorDiagnostic(DiagnosticCode.IndexOutOfRange, `There is no position ${edit.index} among ${items.length} items.`, { instance: resolved.instance, component: edit.component, path: edit.path, itemId: edit.itemId }));
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const next = [...items];
|
|
231
|
+
const [moved] = next.splice(position, 1);
|
|
232
|
+
next.splice(edit.index, 0, moved);
|
|
233
|
+
save(clone, resolved.instance, resolved.grant, contribution, next);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Applies an item field change to a copy of the document.
|
|
238
|
+
*/
|
|
239
|
+
export function applyEditCollectionItem(session: EditSession, edit: EditCollectionItemEdit, clone: SceneDocument, diagnostics: SceneDiagnostic[]): void {
|
|
240
|
+
const resolved = resolveGrant(session, edit as InstanceEditTarget, diagnostics);
|
|
241
|
+
if (!resolved || !permitted(resolved.grant, CollectionOperation.EditFields, resolved.instance, diagnostics)) return;
|
|
242
|
+
|
|
243
|
+
const { contribution, items } = ownItems(clone, resolved.instance, resolved.grant);
|
|
244
|
+
const position = findOwnItem(items, edit.itemId, resolved.instance, resolved.grant, session, diagnostics);
|
|
245
|
+
if (position < 0) return;
|
|
246
|
+
|
|
247
|
+
if (!checkFields(session, resolved.grant, { [edit.field]: edit.value }, edit.itemId, resolved.instance, diagnostics, false)) return;
|
|
248
|
+
|
|
249
|
+
const next = items.map((item, index) => index === position ? { ...item, values: { ...item.values, [edit.field]: cloneData(edit.value) } } : item);
|
|
250
|
+
save(clone, resolved.instance, resolved.grant, contribution, next);
|
|
251
|
+
}
|