Skip to content

Commit dadb0d4

Browse files
committed
fix: add closing tag to self closing element when an nested element is added
1 parent 0ed47d7 commit dadb0d4

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

frank-flow/src/frontend/src/app/shared/services/flow-structure.service.ts

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,51 +140,66 @@ export class FlowStructureService {
140140

141141
addConnection(sourceId: string, targetId: string): void {
142142
const sourceNode = this.getSourceNode(sourceId);
143-
if (!sourceNode) {
144-
return;
145-
}
146-
147143
const target = this.flowStructure.nodes.find(
148144
(node) => node.uid == targetId
149145
);
150146

151-
if (sourceNode.isSelfClosing) {
152-
this.addClosingBracket(sourceNode);
153-
this.addClosingTag(sourceNode);
154-
}
155-
const endline = sourceNode.endLine + (sourceNode.isSelfClosing ? 1 : 0);
147+
const endLine =
148+
(sourceNode?.endLine ?? 0) + (sourceNode?.isSelfClosing ? 1 : 0);
156149
const text = `\t\t\t\t<Forward name="success" path="${target?.name}" />\n`;
157150
const range = {
158-
startLineNumber: endline,
151+
startLineNumber: endLine,
159152
startColumn: 0,
160153
endColumn: 0,
161-
endLineNumber: endline,
154+
endLineNumber: endLine,
162155
};
163156

164-
this.monacoEditorComponent?.applyEdits([{ range, text }]);
157+
if (sourceNode?.isSelfClosing) {
158+
this.monacoEditorComponent?.applyEdits([
159+
this.getClosingBracketEditOperation(sourceNode),
160+
{ range, text },
161+
this.getClosingTagEditOperation(sourceNode),
162+
]);
163+
} else {
164+
this.monacoEditorComponent?.applyEdits([{ range, text }]);
165+
}
165166
}
166167

167168
getSourceNode(sourceId: string): FlowStructureNode | undefined {
168-
const sourceNode = this.flowStructure.pipes.find(
169+
return this.flowStructure.pipes.find(
169170
(pipe: FlowStructureNode) => pipe.uid === sourceId
170171
);
171-
172-
return sourceNode;
173172
}
174173

175-
addClosingBracket(sourceNode: FlowStructureNode): void {
176-
const text = `>\n`;
174+
getClosingBracketEditOperation(
175+
sourceNode: FlowStructureNode
176+
): monaco.editor.ISingleEditOperation {
177+
const text = `>`;
178+
let column = sourceNode.column - 2;
179+
if (this.hasSpaceBeforeClosingBracket(sourceNode)) {
180+
column -= 1;
181+
}
177182
const range = {
178183
startLineNumber: sourceNode.endLine,
179-
startColumn: sourceNode.column - 2,
184+
startColumn: column,
180185
endColumn: sourceNode.column,
181186
endLineNumber: sourceNode.endLine,
182187
};
183188

184-
this.monacoEditorComponent?.applyEdits([{ range, text }]);
189+
return { range, text };
190+
}
191+
192+
hasSpaceBeforeClosingBracket(sourceNode: FlowStructureNode): boolean {
193+
const lastAttribute = this.findLastAttribute(sourceNode.attributes);
194+
return (
195+
sourceNode.column - (lastAttribute?.endColumn ?? 0) >= 3 &&
196+
lastAttribute?.line === sourceNode.endLine
197+
);
185198
}
186199

187-
addClosingTag(sourceNode: FlowStructureNode): void {
200+
getClosingTagEditOperation(
201+
sourceNode: FlowStructureNode
202+
): monaco.editor.ISingleEditOperation {
188203
const text = `\t\t\t</${sourceNode.type}>\n`;
189204
const range = {
190205
startLineNumber: sourceNode.endLine + 1,
@@ -193,7 +208,7 @@ export class FlowStructureService {
193208
endLineNumber: sourceNode.endLine + 1,
194209
};
195210

196-
this.monacoEditorComponent?.applyEdits([{ range, text }]);
211+
return { range, text };
197212
}
198213

199214
deleteConnection(
@@ -777,7 +792,9 @@ export class FlowStructureService {
777792
parent: FlowStructureNode
778793
): void {
779794
const lastNestedElement = this.findLastNestedElement(parent);
780-
let text = `\t\t\t\t<${parameter.type} name="${parameter.name}">\n\t\t\t\t</${parameter.type}>\n`;
795+
let text = `\t\t\t\t<${parameter.type} name="${parameter.name}" />\n`;
796+
797+
const endLine = parent.endLine + (parent.isSelfClosing ? 1 : 0);
781798

782799
const range = lastNestedElement
783800
? {
@@ -787,13 +804,21 @@ export class FlowStructureService {
787804
endColumn: 0,
788805
}
789806
: {
790-
startLineNumber: parent.line + 1,
791-
endLineNumber: parent.line + 1,
807+
startLineNumber: endLine,
808+
endLineNumber: endLine,
792809
startColumn: 0,
793810
endColumn: 0,
794811
};
795812

796-
this.monacoEditorComponent?.applyEdits([{ text, range }], true);
813+
if (parent.isSelfClosing) {
814+
this.monacoEditorComponent?.applyEdits([
815+
this.getClosingBracketEditOperation(parent),
816+
{ range, text },
817+
this.getClosingTagEditOperation(parent),
818+
]);
819+
} else {
820+
this.monacoEditorComponent?.applyEdits([{ range, text }]);
821+
}
797822
}
798823

799824
findLastNestedElement(

0 commit comments

Comments
 (0)