Skip to content

Commit 7268462

Browse files
authored
chore: moving events, interfaces and types to @openscd/core (#1507)
* feat(core): extract api from open-scd (events, types and interfaces) - places api into folder "foundation/deprecated" - (does not include code related to wizards or scl library) Signed-off-by: Juan Munoz <[email protected]> * chore: adding exports for new foundation files - duplicate entries without file extensions were necessary to overcome snowpack errors when building open-scd Signed-off-by: Juan Munoz <[email protected]> * chore: updating imports to new foundation files in @openscd/core Signed-off-by: Juan Munoz <[email protected]> * chore: updating package-lock Signed-off-by: Juan Munoz <[email protected]> * chore: updates package-lock Signed-off-by: Juan Munoz <[email protected]> --------- Signed-off-by: Juan Munoz <[email protected]>
1 parent dbd3607 commit 7268462

File tree

187 files changed

+950
-821
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+950
-821
lines changed

package-lock.json

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/** Inserts `new.element` to `new.parent` before `new.reference`. */
2+
export interface Create {
3+
new: { parent: Node; element: Node; reference?: Node | null };
4+
derived?: boolean;
5+
checkValidity?: () => boolean;
6+
}
7+
/** Removes `old.element` from `old.parent` before `old.reference`. */
8+
export interface Delete {
9+
old: { parent: Node; element: Node; reference?: Node | null };
10+
derived?: boolean;
11+
checkValidity?: () => boolean;
12+
}
13+
/** Reparents of `old.element` to `new.parent` before `new.reference`. */
14+
export interface Move {
15+
old: { parent: Element; element: Element; reference?: Node | null };
16+
new: { parent: Element; reference?: Node | null };
17+
derived?: boolean;
18+
checkValidity?: () => boolean;
19+
}
20+
/** Replaces `old.element` with `new.element`, keeping element children. */
21+
export interface Replace {
22+
old: { element: Element };
23+
new: { element: Element };
24+
derived?: boolean;
25+
checkValidity?: () => boolean;
26+
}
27+
/** Swaps `element`s `oldAttributes` with `newAttributes` */
28+
export interface Update {
29+
element: Element;
30+
oldAttributes: Record<string, string | null>;
31+
newAttributes: Record<string, string | null>;
32+
derived?: boolean;
33+
checkValidity?: () => boolean;
34+
}
35+
36+
export type SimpleAction = Update | Create | Replace | Delete | Move;
37+
export type ComplexAction = {
38+
actions: SimpleAction[];
39+
title: string;
40+
derived?: boolean;
41+
};
42+
/** Represents an intended or committed change to some `Element`. */
43+
export type EditorAction = SimpleAction | ComplexAction;
44+
45+
export function isCreate(action: EditorAction): action is Create {
46+
return (
47+
(action as Replace).old === undefined &&
48+
(action as Create).new?.parent !== undefined &&
49+
(action as Create).new?.element !== undefined
50+
);
51+
}
52+
export function isDelete(action: EditorAction): action is Delete {
53+
return (
54+
(action as Delete).old?.parent !== undefined &&
55+
(action as Delete).old?.element !== undefined &&
56+
(action as Replace).new === undefined
57+
);
58+
}
59+
export function isMove(action: EditorAction): action is Move {
60+
return (
61+
(action as Move).old?.parent !== undefined &&
62+
(action as Move).old?.element !== undefined &&
63+
(action as Move).new?.parent !== undefined &&
64+
(action as Replace).new?.element == undefined
65+
);
66+
}
67+
export function isReplace(action: EditorAction): action is Replace {
68+
return (
69+
(action as Move).old?.parent === undefined &&
70+
(action as Replace).old?.element !== undefined &&
71+
(action as Move).new?.parent === undefined &&
72+
(action as Replace).new?.element !== undefined
73+
);
74+
}
75+
export function isUpdate(action: EditorAction): action is Update {
76+
return (
77+
(action as Replace).old === undefined &&
78+
(action as Replace).new === undefined &&
79+
(action as Update).element !== undefined &&
80+
(action as Update).newAttributes !== undefined &&
81+
(action as Update).oldAttributes !== undefined
82+
);
83+
}
84+
export function isSimple(action: EditorAction): action is SimpleAction {
85+
return !((<ComplexAction>action).actions instanceof Array);
86+
}
87+
88+
//** return `Update` action for `element` adding `oldAttributes` */
89+
export function createUpdateAction(
90+
element: Element,
91+
newAttributes: Record<string, string | null>
92+
): Update {
93+
const oldAttributes: Record<string, string | null> = {};
94+
Array.from(element.attributes).forEach(attr => {
95+
oldAttributes[attr.name] = attr.value;
96+
});
97+
98+
return { element, oldAttributes, newAttributes };
99+
}
100+
101+
/** Throws an error bearing `message`, never returning. */
102+
export function unreachable(message: string): never {
103+
throw new Error(message);
104+
}
105+
106+
/** @returns an [[`EditorAction`]] with opposite effect of `action`. */
107+
export function invert(action: EditorAction): EditorAction {
108+
if (!isSimple(action)) {
109+
const inverse: ComplexAction = {
110+
title: action.title,
111+
derived: action.derived,
112+
actions: [],
113+
};
114+
action.actions.forEach(element =>
115+
inverse.actions.unshift(<SimpleAction>invert(element))
116+
);
117+
return inverse;
118+
}
119+
120+
const metaData = {
121+
derived: action.derived,
122+
checkValidity: action.checkValidity,
123+
};
124+
if (isCreate(action)) return { old: action.new, ...metaData };
125+
else if (isDelete(action)) return { new: action.old, ...metaData };
126+
else if (isMove(action))
127+
return {
128+
old: {
129+
parent: action.new.parent,
130+
element: action.old.element,
131+
reference: action.new.reference,
132+
},
133+
new: { parent: action.old.parent, reference: action.old.reference },
134+
...metaData,
135+
};
136+
else if (isReplace(action))
137+
return { new: action.old, old: action.new, ...metaData };
138+
else if (isUpdate(action))
139+
return {
140+
element: action.element,
141+
oldAttributes: action.newAttributes,
142+
newAttributes: action.oldAttributes,
143+
...metaData,
144+
};
145+
else return unreachable('Unknown EditorAction type in invert.');
146+
}
147+
148+
/** Represents some intended modification of a `Document` being edited. */
149+
export interface EditorActionDetail<T extends EditorAction> {
150+
action: T;
151+
}
152+
export type EditorActionEvent<T extends EditorAction> = CustomEvent<
153+
EditorActionDetail<T>
154+
>;
155+
156+
export function newActionEvent<T extends EditorAction>(
157+
action: T,
158+
eventInitDict?: CustomEventInit<Partial<EditorActionDetail<T>>>
159+
): EditorActionEvent<T> {
160+
return new CustomEvent<EditorActionDetail<T>>('editor-action', {
161+
bubbles: true,
162+
composed: true,
163+
...eventInitDict,
164+
detail: { action, ...eventInitDict?.detail },
165+
});
166+
}
167+
168+
169+
declare global {
170+
interface ElementEventMap {
171+
['editor-action']: EditorActionEvent<EditorAction>;
172+
}
173+
}
174+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { EditorAction } from './editor';
2+
3+
type InfoEntryKind = 'info' | 'warning' | 'error';
4+
5+
export type LogEntryType = 'info' | 'warning' | 'error' | 'action' | 'reset';
6+
7+
/** The basic information contained in each [[`LogEntry`]]. */
8+
export interface LogDetailBase {
9+
title: string;
10+
message?: string;
11+
}
12+
/** The [[`LogEntry`]] for a committed [[`EditorAction`]]. */
13+
export interface CommitDetail extends LogDetailBase {
14+
kind: 'action';
15+
action: EditorAction;
16+
}
17+
/** A [[`LogEntry`]] for notifying the user. */
18+
export interface InfoDetail extends LogDetailBase {
19+
kind: InfoEntryKind;
20+
cause?: LogEntry;
21+
}
22+
23+
export interface ResetDetail {
24+
kind: 'reset';
25+
}
26+
27+
export type LogDetail = InfoDetail | CommitDetail | ResetDetail;
28+
export type LogEvent = CustomEvent<LogDetail>;
29+
30+
export interface IssueDetail extends LogDetailBase {
31+
validatorId: string;
32+
element?: Element;
33+
}
34+
export type IssueEvent = CustomEvent<IssueDetail>;
35+
36+
/** [[`LogEntry`]]s are timestamped upon being committed to the `history`. */
37+
interface Timestamped {
38+
time: Date | null;
39+
}
40+
41+
export type CommitEntry = Timestamped & CommitDetail;
42+
export type InfoEntry = Timestamped & InfoDetail;
43+
44+
export type LogEntry = InfoEntry | CommitEntry;
45+
46+
47+
export function newLogEvent(
48+
detail: LogDetail,
49+
eventInitDict?: CustomEventInit<LogDetail>
50+
): LogEvent {
51+
return new CustomEvent<LogDetail>('log', {
52+
bubbles: true,
53+
composed: true,
54+
...eventInitDict,
55+
detail: { ...detail, ...eventInitDict?.detail },
56+
});
57+
}
58+
59+
export function newIssueEvent(
60+
detail: IssueDetail,
61+
eventInitDict?: CustomEventInit<IssueDetail>
62+
): IssueEvent {
63+
return new CustomEvent<IssueDetail>('issue', {
64+
bubbles: true,
65+
composed: true,
66+
...eventInitDict,
67+
detail: { ...detail, ...eventInitDict?.detail },
68+
});
69+
}
70+
71+
declare global {
72+
interface ElementEventMap {
73+
['log']: LogEvent;
74+
['issue']: IssueEvent;
75+
}
76+
}
77+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/** Represents a document to be opened. */
2+
export interface OpenDocDetail {
3+
doc: XMLDocument;
4+
docName: string;
5+
docId?: string;
6+
}
7+
export type OpenDocEvent = CustomEvent<OpenDocDetail>;
8+
export function newOpenDocEvent(
9+
doc: XMLDocument,
10+
docName: string,
11+
eventInitDict?: CustomEventInit<Partial<OpenDocDetail>>
12+
): OpenDocEvent {
13+
return new CustomEvent<OpenDocDetail>('open-doc', {
14+
bubbles: true,
15+
composed: true,
16+
...eventInitDict,
17+
detail: { doc, docName, ...eventInitDict?.detail },
18+
});
19+
}
20+
21+
declare global {
22+
interface ElementEventMap {
23+
['open-doc']: OpenDocEvent;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
export type Language = 'en' | 'de';
2+
3+
export type Settings = {
4+
language: Language;
5+
theme: 'light' | 'dark';
6+
mode: 'safe' | 'pro';
7+
showieds: 'on' | 'off';
8+
'IEC 61850-7-2': string | undefined;
9+
'IEC 61850-7-3': string | undefined;
10+
'IEC 61850-7-4': string | undefined;
11+
'IEC 61850-8-1': string | undefined;
12+
};
13+
14+
export type NsdVersion = {
15+
version: string | undefined;
16+
revision: string | undefined;
17+
release: string | undefined;
18+
};
19+
20+
export type NsdVersions = {
21+
'IEC 61850-7-2': NsdVersion;
22+
'IEC 61850-7-3': NsdVersion;
23+
'IEC 61850-7-4': NsdVersion;
24+
'IEC 61850-8-1': NsdVersion;
25+
};
26+
27+
/** Represents a document to be opened. */
28+
export interface LoadNsdocDetail {
29+
nsdoc: string;
30+
filename: string;
31+
}
32+
export type LoadNsdocEvent = CustomEvent<LoadNsdocDetail>;
33+
export function newLoadNsdocEvent(
34+
nsdoc: string,
35+
filename: string
36+
): LoadNsdocEvent {
37+
return new CustomEvent<LoadNsdocDetail>('load-nsdoc', {
38+
bubbles: true,
39+
composed: true,
40+
detail: { nsdoc, filename },
41+
});
42+
}
43+
44+
export interface SettingsUIDetail {
45+
show: boolean;
46+
}
47+
48+
export type SettingsUIEvent = CustomEvent<SettingsUIDetail>;
49+
50+
export function newSettingsUIEvent(
51+
show: boolean,
52+
eventInitDict?: CustomEventInit<Partial<SettingsUIDetail>>
53+
): SettingsUIEvent {
54+
return new CustomEvent<SettingsUIDetail>('oscd-settings', {
55+
bubbles: true,
56+
composed: true,
57+
...eventInitDict,
58+
detail: {
59+
show,
60+
...eventInitDict?.detail,
61+
},
62+
});
63+
}
64+
65+
66+
declare global {
67+
interface ElementEventMap {
68+
['oscd-settings']: SettingsUIEvent;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/** Represents a request for validation. */
2+
export type ValidateEvent = CustomEvent<void>;
3+
export function newValidateEvent(
4+
eventInitDict?: CustomEventInit<void>
5+
): ValidateEvent {
6+
return new CustomEvent<void>('validate', {
7+
bubbles: true,
8+
composed: true,
9+
...eventInitDict,
10+
});
11+
}
12+
13+
14+
declare global {
15+
interface ElementEventMap {
16+
['validate']: ValidateEvent;
17+
}
18+
}
19+

0 commit comments

Comments
 (0)