Skip to content

Commit 00e23b0

Browse files
Chore: Moved mergeSubstation function outside the UpdateSubstation plugin, so it can be reused (openscd#1337)
1 parent a737c42 commit 00e23b0

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

packages/open-scd/src/menu/UpdateSubstation.ts

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,46 +79,54 @@ export function isValidReference(
7979
);
8080
}
8181

82+
export function mergeSubstation(
83+
element: Element,
84+
currentDoc: Document,
85+
docWithSubstation: Document
86+
): void {
87+
element.dispatchEvent(
88+
newWizardEvent(
89+
mergeWizard(
90+
// FIXME: doesn't work with multiple Substations!
91+
currentDoc.documentElement,
92+
docWithSubstation.documentElement,
93+
{
94+
title: get('updatesubstation.title'),
95+
selected: (diff: Diff<Element | string>): boolean =>
96+
diff.theirs instanceof Element
97+
? diff.theirs.tagName === 'LNode'
98+
? find(currentDoc, 'LNode', identity(diff.theirs)) === null &&
99+
isValidReference(docWithSubstation, identity(diff.theirs))
100+
: diff.theirs.tagName === 'Substation' ||
101+
!tags['SCL'].children.includes(<SCLTag>diff.theirs.tagName)
102+
: diff.theirs !== null,
103+
disabled: (diff: Diff<Element | string>): boolean =>
104+
diff.theirs instanceof Element &&
105+
diff.theirs.tagName === 'LNode' &&
106+
(find(currentDoc, 'LNode', identity(diff.theirs)) !== null ||
107+
!isValidReference(docWithSubstation, identity(diff.theirs))),
108+
auto: (): boolean => true,
109+
}
110+
)
111+
)
112+
);
113+
}
114+
82115
export default class UpdateSubstationPlugin extends LitElement {
83116
doc!: XMLDocument;
84117

85118
@query('#update-substation-plugin-input') pluginFileUI!: HTMLInputElement;
86119

87-
updateSubstation(event: Event): void {
120+
async updateSubstation(event: Event): Promise<void> {
88121
const file =
89122
(<HTMLInputElement | null>event.target)?.files?.item(0) ?? false;
90-
if (file)
91-
file.text().then(text => {
92-
const doc = new DOMParser().parseFromString(text, 'application/xml');
93-
this.dispatchEvent(
94-
newWizardEvent(
95-
mergeWizard(
96-
// FIXME: doesn't work with multiple Substations!
97-
this.doc.documentElement,
98-
doc.documentElement,
99-
{
100-
title: get('updatesubstation.title'),
101-
selected: (diff: Diff<Element | string>): boolean =>
102-
diff.theirs instanceof Element
103-
? diff.theirs.tagName === 'LNode'
104-
? find(this.doc, 'LNode', identity(diff.theirs)) ===
105-
null && isValidReference(doc, identity(diff.theirs))
106-
: diff.theirs.tagName === 'Substation' ||
107-
!tags['SCL'].children.includes(
108-
<SCLTag>diff.theirs.tagName
109-
)
110-
: diff.theirs !== null,
111-
disabled: (diff: Diff<Element | string>): boolean =>
112-
diff.theirs instanceof Element &&
113-
diff.theirs.tagName === 'LNode' &&
114-
(find(this.doc, 'LNode', identity(diff.theirs)) !== null ||
115-
!isValidReference(doc, identity(diff.theirs))),
116-
auto: (): boolean => true,
117-
}
118-
)
119-
)
120-
);
121-
});
123+
if (!file) {
124+
return;
125+
}
126+
const text = await file.text();
127+
const doc = new DOMParser().parseFromString(text, 'application/xml');
128+
129+
mergeSubstation(this, this.doc, doc);
122130
this.pluginFileUI.onchange = null;
123131
}
124132

0 commit comments

Comments
 (0)