Skip to content

Commit 4460d3c

Browse files
committed
keep old format
1 parent 340924d commit 4460d3c

File tree

8 files changed

+71
-21
lines changed

8 files changed

+71
-21
lines changed

src/features/constraintMenu/ConstraintMenu.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export class ConstraintMenu extends AbstractUIExtension implements Switchable {
4949
if (this.editor) {
5050
const editorText = this.editor.getValue();
5151
// Only update the editor if the constraints have changed
52-
if (editorText !== this.constraintRegistry.getConstraints()) {
53-
this.editor.setValue(this.constraintRegistry.getConstraints() || "");
52+
if (editorText !== this.constraintRegistry.getConstraintsAsText()) {
53+
this.editor.setValue(this.constraintRegistry.getConstraintsAsText() || "");
5454
}
5555
}
5656
});
@@ -124,7 +124,7 @@ export class ConstraintMenu extends AbstractUIExtension implements Switchable {
124124
readOnly: this.forceReadOnly,
125125
});
126126

127-
this.editor?.setValue(this.constraintRegistry.getConstraints() || "");
127+
this.editor?.setValue(this.constraintRegistry.getConstraintsAsText() || "");
128128

129129
this.editor?.onDidChangeModelContent(() => {
130130
if (!this.editor) {
Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,57 @@
11
import { injectable } from "inversify";
2+
import { generateRandomSprottyId } from "../../utils";
3+
4+
export interface Constraint {
5+
id: string;
6+
name: string;
7+
constraint: string;
8+
}
29

310
@injectable()
411
export class ConstraintRegistry {
5-
private constraints: string = "";
12+
private constraints: Constraint[] = [];
613
private updateCallbacks: (() => void)[] = [];
714

815
public setConstraints(constraints: string): void {
9-
this.constraints = constraints;
16+
this.constraints = constraints.split("\r?\n").map(this.constraintFromLine);
17+
this.constraintListChanged();
18+
}
19+
20+
public setConstraintsFromArray(constraints: Constraint[]): void {
21+
this.constraints = constraints.map((c) => ({
22+
id: c.id || generateRandomSprottyId(),
23+
name: c.name,
24+
constraint: c.constraint,
25+
}));
1026
this.constraintListChanged();
1127
}
1228

29+
private constraintFromLine(line: string): Constraint {
30+
const parts = line.split(" ");
31+
if (parts.length < 2) {
32+
return {
33+
id: generateRandomSprottyId(),
34+
name: "",
35+
constraint: "",
36+
};
37+
}
38+
const name = parts[1].endsWith(":") ? parts[1].slice(0, -1) : parts[1];
39+
if (parts.length < 3) {
40+
return {
41+
id: generateRandomSprottyId(),
42+
name: name,
43+
constraint: "",
44+
};
45+
}
46+
return {
47+
id: generateRandomSprottyId(),
48+
name: name,
49+
constraint: parts.slice(2).join(" "),
50+
};
51+
}
52+
1353
public clearConstraints(): void {
14-
this.constraints = "";
54+
this.constraints = [];
1555
this.constraintListChanged();
1656
}
1757

@@ -23,7 +63,11 @@ export class ConstraintRegistry {
2363
this.updateCallbacks.push(callback);
2464
}
2565

26-
public getConstraints(): string {
66+
public getConstraintsAsText(): string {
67+
return this.constraints.map((c) => `- ${c.name}: ${c.constraint}`).join("\n");
68+
}
69+
70+
public getConstraintList(): Constraint[] {
2771
return this.constraints;
2872
}
2973
}

src/features/serialize/analyze.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class AnalyzeDiagramCommand extends Command {
6060
const diagram: SavedDiagram = {
6161
model: modelCopy,
6262
labelTypes: this.labelTypeRegistry?.getLabelTypes(),
63-
constraints: this.constraintRegistry?.getConstraints(),
63+
constraints: this.constraintRegistry?.getConstraintList(),
6464
mode: this.editorModeController?.getCurrentMode(),
6565
};
6666
const diagramJson = JSON.stringify(diagram, undefined, 4);

src/features/serialize/defaultDiagram.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,12 @@
612612
]
613613
}
614614
],
615-
"constraints": "- default: data Sensitivity.Personal neverFlows vertex Location.nonEU",
615+
"constraints": [
616+
{
617+
"id": "j4nz39",
618+
"name": "Test",
619+
"constraint": "data Sensitivity.Personal neverFlows vertex Location.nonEU"
620+
}
621+
],
616622
"mode": "edit"
617623
}

src/features/serialize/load.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { SavedDiagram } from "./save";
1818
import { LabelType, LabelTypeRegistry } from "../labels/labelTypeRegistry";
1919
import { LayoutModelAction } from "../autoLayout/command";
2020
import { EditorMode, EditorModeController } from "../editorMode/editorModeController";
21-
import { ConstraintRegistry } from "../constraintMenu/constraintRegistry";
21+
import { Constraint, ConstraintRegistry } from "../constraintMenu/constraintRegistry";
2222
import { LoadingIndicator } from "../../common/loadingIndicator";
2323

2424
export interface LoadDiagramAction extends Action {
@@ -84,8 +84,8 @@ export class LoadDiagramCommand extends Command {
8484
private newEditorMode: EditorMode | undefined;
8585
private oldFileName: string | undefined;
8686
private newFileName: string | undefined;
87-
private oldConstrains: string | undefined;
88-
private newConstrains: string | undefined;
87+
private oldConstrains: Constraint[] | undefined;
88+
private newConstrains: Constraint[] | undefined;
8989

9090
/**
9191
* Gets the model file from the action or opens a file picker dialog if no file is provided.
@@ -213,11 +213,11 @@ export class LoadDiagramCommand extends Command {
213213

214214
if (this.constraintRegistry) {
215215
// Load label types
216-
this.oldConstrains = this.constraintRegistry.getConstraints();
216+
this.oldConstrains = this.constraintRegistry.getConstraintList();
217217
this.newConstrains = newDiagram?.constraints;
218218
this.constraintRegistry.clearConstraints();
219219
if (newDiagram?.constraints) {
220-
this.constraintRegistry.setConstraints(newDiagram.constraints);
220+
this.constraintRegistry.setConstraintsFromArray(newDiagram.constraints);
221221

222222
this.logger.info(this, "Constraints loaded successfully");
223223
}
@@ -281,7 +281,7 @@ export class LoadDiagramCommand extends Command {
281281
}
282282
this.constraintRegistry?.clearConstraints();
283283
if (this.oldConstrains) {
284-
this.constraintRegistry?.setConstraints(this.oldConstrains);
284+
this.constraintRegistry?.setConstraintsFromArray(this.oldConstrains);
285285
}
286286
setFileNameInPageTitle(this.oldFileName);
287287

@@ -302,7 +302,7 @@ export class LoadDiagramCommand extends Command {
302302
}
303303
this.constraintRegistry?.clearConstraints();
304304
if (this.newConstrains) {
305-
this.constraintRegistry?.setConstraints(this.newConstrains);
305+
this.constraintRegistry?.setConstraintsFromArray(this.newConstrains);
306306
}
307307
setFileNameInPageTitle(this.newFileName);
308308

src/features/serialize/loadDefaultDiagram.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class LoadDefaultDiagramCommand extends Command {
9595
// Load label types
9696
this.constraintRegistry.clearConstraints();
9797
if (defaultDiagram?.constraints) {
98-
this.constraintRegistry.setConstraints(defaultDiagram.constraints);
98+
this.constraintRegistry.setConstraintsFromArray(defaultDiagram.constraints);
9999
this.logger.info(this, "Constraints loaded successfully");
100100
}
101101
}

src/features/serialize/save.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Action, SModelRoot } from "sprotty-protocol";
44
import { LabelType, LabelTypeRegistry } from "../labels/labelTypeRegistry";
55
import { DynamicChildrenProcessor } from "../dfdElements/dynamicChildren";
66
import { EditorMode, EditorModeController } from "../editorMode/editorModeController";
7-
import { ConstraintRegistry } from "../constraintMenu/constraintRegistry";
7+
import { Constraint, ConstraintRegistry } from "../constraintMenu/constraintRegistry";
88

99
/**
1010
* Type that contains all data related to a diagram.
@@ -13,7 +13,7 @@ import { ConstraintRegistry } from "../constraintMenu/constraintRegistry";
1313
export interface SavedDiagram {
1414
model: SModelRoot;
1515
labelTypes?: LabelType[];
16-
constraints?: string;
16+
constraints?: Constraint[];
1717
mode?: EditorMode;
1818
}
1919

@@ -64,7 +64,7 @@ export class SaveDiagramCommand extends Command {
6464
const diagram: SavedDiagram = {
6565
model: modelCopy,
6666
labelTypes: this.labelTypeRegistry?.getLabelTypes(),
67-
constraints: this.constraintRegistry?.getConstraints(),
67+
constraints: this.constraintRegistry?.getConstraintList(),
6868
mode: this.editorModeController?.getCurrentMode(),
6969
};
7070
const diagramJson = JSON.stringify(diagram, undefined, 4);

src/features/serialize/saveDFDandDD.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class SaveDFDandDDCommand extends Command {
5656
const diagram: SavedDiagram = {
5757
model: modelCopy,
5858
labelTypes: this.labelTypeRegistry?.getLabelTypes(),
59-
constraints: this.constraintRegistry?.getConstraints(),
59+
constraints: this.constraintRegistry?.getConstraintList(),
6060
mode: this.editorModeController?.getCurrentMode(),
6161
};
6262
const diagramJson = JSON.stringify(diagram, undefined, 4);

0 commit comments

Comments
 (0)