Skip to content

Commit 108c9a4

Browse files
authored
make settings handling more robust (#560)
* make settings handling more robust * fix tests * apply formatting changes --------- Co-authored-by: Logende <Logende@users.noreply.github.com>
1 parent 2af7c85 commit 108c9a4

21 files changed

Lines changed: 112 additions & 94 deletions

meta_configurator/src/components/CombinedEditorComponent.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Main component of the application.
33
Combines the code editor and the gui editor.
44
-->
55
<script lang="ts" setup>
6-
import {computed, onMounted, ref, watch} from 'vue';
6+
import {computed, onMounted, type Ref, ref, watch} from 'vue';
77
import 'primeicons/primeicons.css';
88
import SplitterPanel from 'primevue/splitterpanel';
99
import Splitter from 'primevue/splitter';
@@ -31,15 +31,15 @@ const props = defineProps<{
3131
}>();
3232
3333
const settings = useSettings();
34-
let panelsDefinition: SettingsInterfacePanels = settings.panels;
34+
let panelsDefinition: SettingsInterfacePanels = settings.value.panels;
3535
3636
// update panelsDefinition only when underlying data changes. Otherwise, all panels will be rebuilt every time
3737
// any setting is changed, which is not necessary and leads to Ace Editor becoming blank if settings were modified via
3838
// Ace Editor
3939
watchImmediate(
4040
() => settings,
41-
(settings: SettingsInterfaceRoot) => {
42-
let panels = settings.panels;
41+
(settings: Ref<SettingsInterfaceRoot>) => {
42+
let panels = settings.value.panels;
4343
if (JSON.stringify(panels) !== JSON.stringify(panelsDefinition)) {
4444
panelsDefinition = panels;
4545
}

meta_configurator/src/components/panels/code-editor/AceEditor.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ onMounted(() => {
4646
*/
4747
function setupAceMode(editor: Editor) {
4848
watchImmediate(
49-
() => settings.dataFormat,
49+
() => settings.value.dataFormat,
5050
format => {
5151
if (format == 'json') {
5252
editor.getSession().setMode('ace/mode/json');
@@ -64,13 +64,13 @@ function setupAceProperties(editor: Editor) {
6464
});
6565
editor.setTheme('ace/theme/clouds');
6666
editor.setShowPrintMargin(false);
67-
editor.getSession().setTabSize(settings.codeEditor.tabSize);
67+
editor.getSession().setTabSize(settings.value.codeEditor.tabSize);
6868
6969
// it's not clear why timeout is needed here, but without it the
7070
// ace editor starts flashing and becomes unusable
7171
window.setTimeout(() => {
7272
watchImmediate(
73-
() => settings.codeEditor.fontSize,
73+
() => settings.value.codeEditor.fontSize,
7474
fontSize => {
7575
if (editor && fontSize && fontSize > 6 && fontSize < 65) {
7676
editor.setFontSize(fontSize.toString() + 'px');

meta_configurator/src/components/panels/gui-editor/PropertyMetadata.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function canZoomIn(): boolean {
7474
* depending on if the maximum depth has been reached or not.
7575
*/
7676
function onPressEnter() {
77-
if (props.node.data.depth === settings.guiEditor.maximumDepth) {
77+
if (props.node.data.depth === settings.value.guiEditor.maximumDepth) {
7878
zoomIntoPath();
7979
return;
8080
}

meta_configurator/src/components/panels/gui-editor/configTreeNodeResolver.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ interface TreeNodeResolvingParameters {
2424
depth: number;
2525
}
2626

27+
const settings = useSettings();
28+
2729
/**
2830
* Creates a {@link GuiEditorTreeNode} from a {@link JsonSchemaWrapper}.
2931
*
@@ -98,7 +100,7 @@ export class ConfigTreeNodeResolver {
98100
return (
99101
(!dependsOnUserSelection && data && typeof data !== 'object') || // primitive type in data
100102
(!schema.hasType('object') && !schema.hasType('array')) || // primitive type in schema
101-
depth >= useSettings().guiEditor.maximumDepth // maximum depth reached
103+
depth >= settings.value.guiEditor.maximumDepth // maximum depth reached
102104
);
103105
}
104106

@@ -111,6 +113,7 @@ export class ConfigTreeNodeResolver {
111113

112114
/**
113115
* Creates the children of a {@link GuiEditorTreeNode}.
116+
* @param mode
114117
* @param guiEditorTreeNode The node for which the children should be created.
115118
*/
116119
public createChildNodesOfNode(
@@ -150,7 +153,7 @@ export class ConfigTreeNodeResolver {
150153
effectiveSchema: EffectiveSchema,
151154
depth = 0
152155
): GuiEditorTreeNode[] {
153-
const depthLimit = useSettings().guiEditor.maximumDepth;
156+
const depthLimit = settings.value.guiEditor.maximumDepth;
154157
const schema = effectiveSchema.schema;
155158

156159
let children: GuiEditorTreeNode[] = [];
@@ -203,7 +206,7 @@ export class ConfigTreeNodeResolver {
203206
mode: SessionMode,
204207
parameters: TreeNodeResolvingParameters
205208
) {
206-
const propertySorting = useSettings().guiEditor.propertySorting;
209+
const propertySorting = settings.value.guiEditor.propertySorting;
207210
let result: GuiEditorTreeNode[] = [];
208211

209212
if (propertySorting === PropertySorting.SCHEMA_ORDER) {
@@ -621,7 +624,7 @@ export class ConfigTreeNodeResolver {
621624

622625
// if the user has not specified a custom schema for additional properties, we can hide the button
623626
if (
624-
useSettings().guiEditor.hideAddPropertyButton &&
627+
settings.value.guiEditor.hideAddPropertyButton &&
625628
schema.additionalProperties.isAlwaysTrue &&
626629
_.isEmpty(schema.patternProperties)
627630
) {

meta_configurator/src/components/panels/schema-diagram/DiagramOptionsPanel.vue

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ function rebuildGraph() {
1414
emit('rebuild_graph');
1515
emit('fit_view');
1616
}
17+
18+
const settings = useSettings();
1719
</script>
1820

1921
<template>
@@ -22,24 +24,20 @@ function rebuildGraph() {
2224
<div>
2325
<label
2426
>Edit Mode
25-
<InputSwitch
26-
v-model="useSettings().schemaDiagram.editMode"
27-
class="options-input-switch" />
27+
<InputSwitch v-model="settings.schemaDiagram.editMode" class="options-input-switch" />
2828
</label>
2929
</div>
3030
<div>
3131
<label
3232
>Graph direction vertical
33-
<InputSwitch
34-
v-model="useSettings().schemaDiagram.vertical"
35-
class="options-input-switch" />
33+
<InputSwitch v-model="settings.schemaDiagram.vertical" class="options-input-switch" />
3634
</label>
3735
</div>
3836
<div>
3937
<label
4038
>Show attributes
4139
<InputSwitch
42-
v-model="useSettings().schemaDiagram.showAttributes"
40+
v-model="settings.schemaDiagram.showAttributes"
4341
label="Automatic zoom"
4442
class="options-input-switch" />
4543
</label>
@@ -48,15 +46,15 @@ function rebuildGraph() {
4846
<label
4947
>Show enum values
5048
<InputSwitch
51-
v-model="useSettings().schemaDiagram.showEnumValues"
49+
v-model="settings.schemaDiagram.showEnumValues"
5250
class="options-input-switch" />
5351
</label>
5452
</div>
5553
<div>
5654
<label
5755
>Move view on element selection
5856
<InputSwitch
59-
v-model="useSettings().schemaDiagram.moveViewToSelectedElement"
57+
v-model="settings.schemaDiagram.moveViewToSelectedElement"
6058
class="options-input-switch" />
6159
</label>
6260
</div>

meta_configurator/src/components/panels/schema-diagram/SchemaEnumNode.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
import {
33
SchemaElementData,
44
SchemaEnumNodeData,
5-
SchemaObjectNodeData,
65
} from '@/components/panels/schema-diagram/schemaDiagramTypes';
76
import type {Path} from '@/utility/path';
87
import {Handle, Position} from '@vue-flow/core';
98
import {useSettings} from '@/settings/useSettings';
109
import {type Ref, ref} from 'vue';
1110
import InputText from 'primevue/inputtext';
12-
import {pathToString} from '@/utility/pathUtils';
1311
import Button from 'primevue/button';
1412
import {FontAwesomeIcon} from '@fortawesome/vue-fontawesome';
1513
@@ -38,7 +36,7 @@ function clickedNode() {
3836
}
3937
4038
function isEnumEditable() {
41-
return isHighlighted() && settings.schemaDiagram.editMode;
39+
return isHighlighted() && settings.value.schemaDiagram.editMode;
4240
}
4341
4442
function isDefinedInDefinitions() {

meta_configurator/src/components/panels/schema-diagram/SchemaObjectAttribute.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import Button from 'primevue/button';
1616
import {FontAwesomeIcon} from '@fortawesome/vue-fontawesome';
1717
import {useSettings} from '@/settings/useSettings';
1818
19+
const settings = useSettings();
20+
1921
const props = defineProps<{
2022
data: SchemaObjectAttributeData;
2123
selectedData?: SchemaElementData;
@@ -65,7 +67,7 @@ function deleteAttribute() {
6567
}
6668
6769
function isEditable() {
68-
return isHighlighted() && useSettings().schemaDiagram.editMode;
70+
return isHighlighted() && settings.value.schemaDiagram.editMode;
6971
}
7072
7173
function isHighlighted() {

meta_configurator/src/components/panels/schema-diagram/SchemaObjectNode.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ const emit = defineEmits<{
4848
}>();
4949
5050
const objectName = ref(props.data.name);
51+
const settings = useSettings();
5152
5253
function isObjectEditable() {
53-
return (isHighlighted() || isAttributeHighlighted()) && useSettings().schemaDiagram.editMode;
54+
return (isHighlighted() || isAttributeHighlighted()) && settings.value.schemaDiagram.editMode;
5455
}
5556
5657
function isNameEditable() {
@@ -183,7 +184,7 @@ function isAttributeHighlighted() {
183184

184185
<hr />
185186
<SchemaObjectAttribute
186-
v-if="useSettings().schemaDiagram.showAttributes"
187+
v-if="settings.schemaDiagram.showAttributes"
187188
v-for="attribute in props.data!.attributes"
188189
:data="attribute!"
189190
:key="attribute!.name + attribute.index + attribute.typeDescription"

meta_configurator/src/components/panels/schema-diagram/VueFlowPanel.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const activeEdges: Ref<Edge[]> = ref<Edge[]>([]);
6363
const graphDirection = computed(() => {
6464
// note that having edges from left ro right will usually lead to a more vertical graph, because usually it is
6565
// not very deeply nested, but there exist many nodes on the same levels
66-
return settings.schemaDiagram.vertical ? 'LR' : 'TB';
66+
return settings.value.schemaDiagram.vertical ? 'LR' : 'TB';
6767
});
6868
6969
const selectedNode: Ref<Node | undefined> = ref(undefined);
@@ -116,7 +116,7 @@ function fitViewForElementByPath(path: Path) {
116116
if (bestMatchingNode) {
117117
if (
118118
(previousBestMatchingNode && previousBestMatchingNode.id === bestMatchingNode.id) ||
119-
!settings.schemaDiagram.moveViewToSelectedElement
119+
!settings.value.schemaDiagram.moveViewToSelectedElement
120120
) {
121121
// if the node is already within the viewport, do not move the view
122122
if (areNodesAlreadyWithinViewport([bestMatchingNode])) {
@@ -134,8 +134,8 @@ function fitViewForNodes(nodes: Node[]) {
134134
nodes: nodes.map(node => node.id),
135135
duration: 1000,
136136
padding: 1,
137-
maxZoom: settings.schemaDiagram.automaticZoomMaxValue,
138-
minZoom: settings.schemaDiagram.automaticZoomMinValue,
137+
maxZoom: settings.value.schemaDiagram.automaticZoomMaxValue,
138+
minZoom: settings.value.schemaDiagram.automaticZoomMinValue,
139139
});
140140
});
141141
}
@@ -163,7 +163,7 @@ function updateGraph(forceRebuild: boolean = false) {
163163
const graph = constructSchemaGraph(schema);
164164
let graphNeedsLayouting = forceRebuild;
165165
166-
const vueFlowGraph = graph.toVueFlowGraph(settings.schemaDiagram.vertical);
166+
const vueFlowGraph = graph.toVueFlowGraph(settings.value.schemaDiagram.vertical);
167167
if (wasNodeAdded(activeNodes.value, vueFlowGraph.nodes)) {
168168
// node was added -> it is needed to update whole graph
169169
activeNodes.value = vueFlowGraph.nodes;

meta_configurator/src/components/panels/schema-diagram/__tests__/schemaDiagramHelper.test.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
import {beforeEach, describe, expect, it, vi} from 'vitest';
2-
import type {Path} from '@/utility/path';
32
import type {TopLevelSchema} from '@/schema/jsonSchemaType';
4-
import {
5-
EdgeType,
6-
SchemaEnumNodeData,
7-
SchemaGraph,
8-
SchemaObjectNodeData,
9-
} from '../schemaDiagramTypes';
10-
import {
11-
constructSchemaGraph,
12-
generateAttributeEdges,
13-
generateObjectAttributes,
14-
identifyObjects,
15-
populateGraph,
16-
trimGraph,
17-
trimNodeChildren,
18-
} from '../schemaGraphConstructor';
19-
import {useSettings} from '@/settings/useSettings';
3+
import {SchemaGraph} from '../schemaDiagramTypes';
4+
import {constructSchemaGraph} from '../schemaGraphConstructor';
205
import {findBestMatchingNode} from '../schemaDiagramHelper';
6+
import {ref} from 'vue';
217

228
vi.mock('@/dataformats/formatRegistry', () => ({
239
useDataConverter: () => ({
2410
stringify: (data: any) => JSON.stringify(data),
2511
parse: (data: string) => JSON.parse(data),
2612
}),
2713
useSettings() {
28-
return {
14+
return ref({
2915
schemaDiagram: {
3016
showEnumValues: true,
3117
maxEnumValuesToShow: 5,
3218
showAttributes: true,
3319
maxAttributesToShow: 5,
3420
},
35-
};
21+
});
3622
},
3723
}));
3824

0 commit comments

Comments
 (0)