Skip to content

Commit

Permalink
refactor(foundation): make reference within editor-action optional (#325
Browse files Browse the repository at this point in the history
)

* refactor(foundation): change EditorAction api make reference optional

* refactor(Editor): get reference onAction with undefined reference

* test(Editing): add integration test

* fix(lnodetype-wizard): fix getReference with addig LNodeType from template

* refactor(templates): remove reference from create actions

* refactor(communication): remove reference from create actions

* refactor(substation): remove reference from create action

* refactor(templates): remove reference from create action

* refactor(communication mapping): remove reference from create action

* refactor(iedimport): remove reference from create action
  • Loading branch information
JakobVogelsang authored Nov 18, 2021
1 parent 81e1d3d commit b4c69c5
Show file tree
Hide file tree
Showing 27 changed files with 300 additions and 112 deletions.
20 changes: 20 additions & 0 deletions src/Editing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Delete,
EditorAction,
EditorActionEvent,
getReference,
isCreate,
isDelete,
isMove,
Expand All @@ -17,6 +18,7 @@ import {
newLogEvent,
newValidateEvent,
OpenDocEvent,
SCLTag,
SimpleAction,
Update,
} from './foundation.js';
Expand Down Expand Up @@ -72,6 +74,12 @@ export function Editing<TBase extends LitElementConstructor>(Base: TBase) {
private onCreate(action: Create) {
if (!this.checkCreateValidity(action)) return false;

if (action.new.reference === undefined)
action.new.reference = getReference(
action.new.parent,
<SCLTag>action.new.element.tagName
);

action.new.parent.insertBefore(action.new.element, action.new.reference);
return true;
}
Expand All @@ -89,6 +97,9 @@ export function Editing<TBase extends LitElementConstructor>(Base: TBase) {
}

private onDelete(action: Delete) {
if (!action.old.reference)
action.old.reference = action.old.element.nextSibling;

action.old.element.remove();
return true;
}
Expand Down Expand Up @@ -138,6 +149,15 @@ export function Editing<TBase extends LitElementConstructor>(Base: TBase) {
private onMove(action: Move) {
if (!this.checkMoveValidity(action)) return false;

if (!action.old.reference)
action.old.reference = action.old.element.nextSibling;

if (action.new.reference === undefined)
action.new.reference = getReference(
action.new.parent,
<SCLTag>action.old.element.tagName
);

action.new.parent.insertBefore(action.old.element, action.new.reference);
return true;
}
Expand Down
2 changes: 0 additions & 2 deletions src/editors/Communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
newWizardEvent,
newActionEvent,
createElement,
getReference,
} from '../foundation.js';

import { selectors, styles } from './communication/foundation.js';
Expand All @@ -25,7 +24,6 @@ export default class CommunicationPlugin extends LitElement {
new: {
parent: this.doc.documentElement,
element: createElement(this.doc, 'Communication', {}),
reference: getReference(this.doc.documentElement, 'Communication'),
},
})
);
Expand Down
5 changes: 0 additions & 5 deletions src/editors/Templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { translate } from 'lit-translate';

import {
createElement,
getReference,
identity,
newActionEvent,
newWizardEvent,
Expand Down Expand Up @@ -127,10 +126,6 @@ export default class TemplatesPlugin extends LitElement {
new: {
parent: this.doc.documentElement,
element: createElement(this.doc, 'DataTypeTemplates', {}),
reference: getReference(
this.doc.documentElement,
'DataTypeTemplates'
),
},
})
);
Expand Down
3 changes: 0 additions & 3 deletions src/editors/communication/connectedap-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
getValue,
createElement,
ComplexAction,
getReference,
} from '../../foundation.js';

import {
Expand Down Expand Up @@ -114,7 +113,6 @@ function createConnectedApAction(parent: Element): WizardActor {
iedName: value.iedName,
apName: value.apName,
}),
reference: getReference(parent, 'ConnectedAP'),
},
}
);
Expand Down Expand Up @@ -225,7 +223,6 @@ export function editConnectedApAction(parent: Element): WizardActor {
new: {
parent: parent,
element: newAddress,
reference: getReference(parent, 'Address'),
},
});

Expand Down
2 changes: 0 additions & 2 deletions src/editors/communication/subnetwork-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
patterns,
compareNames,
createElement,
getReference,
cloneElement,
} from '../../foundation.js';

Expand Down Expand Up @@ -149,7 +148,6 @@ export function createSubNetworkAction(parent: Element): WizardActor {
new: {
parent,
element,
reference: getReference(parent, 'SubNetwork'),
},
};

Expand Down
2 changes: 0 additions & 2 deletions src/editors/substation/guess-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
compareNames,
createElement,
EditorAction,
getReference,
Wizard,
WizardActor,
WizardInput,
Expand Down Expand Up @@ -205,7 +204,6 @@ function guessBasedOnCSWI(doc: XMLDocument): WizardActor {
new: {
parent: substation,
element: voltageLevel,
reference: getReference(substation, 'VoltageLevel'),
},
});

Expand Down
3 changes: 0 additions & 3 deletions src/editors/templates/datype-wizards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import { get, translate } from 'lit-translate';
import {
Create,
EditorAction,
getReference,
getValue,
identity,
newActionEvent,
newWizardEvent,
patterns,
SCLTag,
selector,
Wizard,
WizardActor,
Expand Down Expand Up @@ -167,7 +165,6 @@ function addPredefinedDAType(
new: {
parent,
element,
reference: getReference(parent, <SCLTag>element.tagName),
},
});

Expand Down
4 changes: 0 additions & 4 deletions src/editors/templates/dotype-wizards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import {
Create,
createElement,
EditorAction,
getReference,
getValue,
identity,
isPublic,
newActionEvent,
newWizardEvent,
SCLTag,
selector,
Wizard,
WizardActor,
Expand Down Expand Up @@ -74,7 +72,6 @@ function createSDoAction(parent: Element): WizardActor {
new: {
parent,
element,
reference: getReference(parent, <SCLTag>element.tagName),
},
});

Expand Down Expand Up @@ -216,7 +213,6 @@ function addPredefinedDOType(
new: {
parent,
element,
reference: getReference(parent, <SCLTag>element.tagName),
},
});

Expand Down
4 changes: 0 additions & 4 deletions src/editors/templates/enumtype-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import {
cloneElement,
createElement,
EditorAction,
getReference,
getValue,
identity,
isPublic,
newActionEvent,
newWizardEvent,
patterns,
SCLTag,
selector,
Wizard,
WizardActor,
Expand Down Expand Up @@ -57,7 +55,6 @@ function createEnumValAction(parent: Element): WizardActor {
new: {
parent,
element,
reference: getReference(parent, 'EnumVal'),
},
};

Expand Down Expand Up @@ -195,7 +192,6 @@ function createAction(parent: Element, templates: XMLDocument): WizardActor {
new: {
parent,
element,
reference: getReference(parent, <SCLTag>element.tagName),
},
};

Expand Down
3 changes: 0 additions & 3 deletions src/editors/templates/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import {
cloneElement,
Create,
EditorAction,
getReference,
getValue,
isPublic,
SCLTag,
WizardActor,
WizardInput,
} from '../../foundation.js';
Expand Down Expand Up @@ -98,7 +96,6 @@ export function addReferencedDataTypes(
new: {
parent,
element: <Element>adjacent.cloneNode(true),
reference: getReference(parent, <SCLTag>adjacent.tagName),
},
});
});
Expand Down
6 changes: 0 additions & 6 deletions src/editors/templates/lnodetype-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import {
createElement,
EditorAction,
getChildElementsByTagName,
getReference,
getValue,
identity,
isPublic,
newActionEvent,
newWizardEvent,
patterns,
SCLTag,
selector,
Wizard,
WizardActor,
Expand Down Expand Up @@ -97,7 +95,6 @@ function createDoAction(parent: Element): WizardActor {
new: {
parent,
element,
reference: getReference(parent, <SCLTag>element.tagName),
},
});

Expand Down Expand Up @@ -271,7 +268,6 @@ function createNewLNodeType(parent: Element, element: Element): WizardActor {
new: {
parent: element,
element: DO,
reference: getReference(element, <SCLTag>DO.tagName),
},
});
});
Expand All @@ -280,7 +276,6 @@ function createNewLNodeType(parent: Element, element: Element): WizardActor {
new: {
parent,
element,
reference: getReference(parent, <SCLTag>element.tagName),
},
});

Expand Down Expand Up @@ -358,7 +353,6 @@ function addPredefinedLNodeType(
new: {
parent,
element: newLNodeType,
reference: getReference(parent, 'LNodeType'),
},
});

Expand Down
16 changes: 6 additions & 10 deletions src/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ export type ComplexAction = {
export type EditorAction = SimpleAction | ComplexAction;
/** Inserts `new.element` to `new.parent` before `new.reference`. */
export interface Create {
new: { parent: Element; element: Element; reference: Node | null };
new: { parent: Element; element: Element; reference?: Node | null };
derived?: boolean;
checkValidity?: () => boolean;
}
/** Removes `old.element` from `old.parent` before `old.reference`. */
export interface Delete {
old: { parent: Element; element: Element; reference: Node | null };
old: { parent: Element; element: Element; reference?: Node | null };
derived?: boolean;
checkValidity?: () => boolean;
}
/** Reparents of `old.element` to `new.parent` before `new.reference`. */
export interface Move {
old: { parent: Element; element: Element; reference: Node | null };
new: { parent: Element; reference: Node | null };
old: { parent: Element; element: Element; reference?: Node | null };
new: { parent: Element; reference?: Node | null };
derived?: boolean;
checkValidity?: () => boolean;
}
Expand All @@ -48,26 +48,22 @@ export function isCreate(action: EditorAction): action is Create {
return (
(action as Update).old === undefined &&
(action as Create).new?.parent !== undefined &&
(action as Create).new?.element !== undefined &&
(action as Create).new?.reference !== undefined
(action as Create).new?.element !== undefined
);
}
export function isDelete(action: EditorAction): action is Delete {
return (
(action as Delete).old?.parent !== undefined &&
(action as Delete).old?.element !== undefined &&
(action as Delete).old?.reference !== undefined &&
(action as Update).new === undefined
);
}
export function isMove(action: EditorAction): action is Move {
return (
(action as Move).old?.parent !== undefined &&
(action as Move).old?.element !== undefined &&
(action as Move).old?.reference !== undefined &&
(action as Move).new?.parent !== undefined &&
(action as Update).new?.element == undefined &&
(action as Move).new?.reference !== undefined
(action as Update).new?.element == undefined
);
}
export function isUpdate(action: EditorAction): action is Update {
Expand Down
Loading

0 comments on commit b4c69c5

Please sign in to comment.