-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathactions.ts
118 lines (102 loc) · 3.45 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
getValue,
WizardActor,
WizardInputElement,
} from '@openscd/open-scd/src/foundation.js';
import {
cloneElement,
} from '@openscd/xml';
import {
ComplexAction,
EditorAction,
createUpdateAction
} from '@openscd/core/foundation/deprecated/editor';
import { get } from 'lit-translate';
import { updateReferences } from './references.js';
export function replaceNamingAction(element: Element): WizardActor {
return (inputs: WizardInputElement[]): EditorAction[] => {
const name = getValue(inputs.find(i => i.label === 'name')!)!;
const desc = getValue(inputs.find(i => i.label === 'desc')!);
if (
name === element.getAttribute('name') &&
desc === element.getAttribute('desc')
) {
return [];
}
const newElement = cloneElement(element, { name, desc });
return [{ old: { element }, new: { element: newElement } }];
};
}
export function replaceNamingAttributeWithReferencesAction(
element: Element,
messageTitleKey: string
): WizardActor {
return (inputs: WizardInputElement[]): EditorAction[] => {
const newName = getValue(inputs.find(i => i.label === 'name')!)!;
const oldName = element.getAttribute('name');
const newDesc = getValue(inputs.find(i => i.label === 'desc')!);
if (newName === oldName && newDesc === element.getAttribute('desc')) {
return [];
}
const newElement = cloneElement(element, { name: newName, desc: newDesc });
const complexAction: ComplexAction = {
actions: [],
title: get(messageTitleKey, { name: newName }),
};
complexAction.actions.push({
old: { element },
new: { element: newElement },
});
complexAction.actions.push(...updateReferences(element, oldName, newName));
return complexAction.actions.length ? [complexAction] : [];
};
}
export function updateNamingAttributeWithReferencesAction(
element: Element,
messageTitleKey: string
): WizardActor {
return (inputs: WizardInputElement[]): EditorAction[] => {
const newAttributes: Record<string, string | null> = {};
processNamingAttributes(newAttributes, element, inputs);
if (Object.keys(newAttributes).length == 0) {
return [];
}
addMissingAttributes(element, newAttributes);
const name = getValue(inputs.find(i => i.label === 'name')!)!;
const complexAction: ComplexAction = {
actions: [],
title: get(messageTitleKey, { name }),
};
complexAction.actions.push(createUpdateAction(element, newAttributes));
complexAction.actions.push(
...updateReferences(element, element.getAttribute('name'), name)
);
return complexAction.actions.length ? [complexAction] : [];
};
}
export function processNamingAttributes(
newAttributes: Record<string, string | null>,
element: Element,
inputs: WizardInputElement[]
): void {
const name = getValue(inputs.find(i => i.label === 'name')!)!;
const desc = getValue(inputs.find(i => i.label === 'desc')!);
if (name !== element.getAttribute('name')) {
newAttributes['name'] = name;
}
if (desc !== element.getAttribute('desc')) {
newAttributes['desc'] = desc;
}
}
export function addMissingAttributes(
element: Element,
newAttributes: Record<string, string | null>
): Record<string, string | null> {
const newAttributeKeys = Object.keys(newAttributes);
Array.from(element.attributes)
.filter(attr => !newAttributeKeys.includes(attr.name))
.forEach(attr => {
newAttributes[attr.name] = attr.value;
});
return newAttributes;
}