Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support edit api v2 #1581

Merged
merged 36 commits into from
Nov 14, 2024
Merged
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ccdfa48
WIP: Add edit api v2 support
clepski Oct 4, 2024
44255c3
feat: WIP Convert v1 to v2 events
clepski Oct 10, 2024
e2060cc
feat: Dispatch edit completed
clepski Oct 11, 2024
1cae3d5
feat: WIP Handle undo and redo events
clepski Oct 14, 2024
a2f6552
feat: Add log text and remove old code
clepski Oct 17, 2024
5b8b62c
feat: Cleanup and fix complex event
clepski Oct 17, 2024
4f93c8e
feat: Clean up code
clepski Oct 18, 2024
0ef22da
feat: Add history state
clepski Oct 21, 2024
782ba0e
feat: Remove editing mixin
clepski Oct 22, 2024
65fcef4
feat: Move edit event converter to openscd package
clepski Oct 22, 2024
64a0505
feat: Remove editing mixin
clepski Oct 22, 2024
18a2061
test: Add tests
clepski Oct 23, 2024
e606c8b
chore: Remove outdated export
clepski Oct 23, 2024
d54b30b
test: Fix tests
clepski Oct 24, 2024
20b16c9
test: Fix history tests
clepski Oct 25, 2024
50f26fc
test: Remove editing tests
clepski Oct 25, 2024
c8b0652
test: Disable broken test
clepski Oct 25, 2024
78b922f
Revert "test: Disable broken test"
clepski Oct 25, 2024
146de19
Merge branch 'main' into feat/edit-api-v2
clepski Oct 25, 2024
faa1511
feat: Fix convert move ref
clepski Oct 25, 2024
a36755c
feat: Adjust converter to edit api v1 behavior
clepski Oct 25, 2024
5393f00
test: Fix test
clepski Oct 25, 2024
9359d83
Merge branch 'main' into feat/edit-api-v2
clepski Oct 28, 2024
e370f96
test: Adjust mock wizard editor
clepski Oct 28, 2024
41cf660
test: Fix tests
clepski Oct 28, 2024
70ddafb
feat: Fix self reference bug
clepski Oct 28, 2024
2772a04
test: Fix tests
clepski Oct 30, 2024
7ee2e37
test: Skip broken tests
clepski Oct 30, 2024
f50c71c
test: Add Editor tests
clepski Oct 30, 2024
6b2ceb9
test: Add Editor tests
clepski Oct 31, 2024
c4b774d
doc: Add edit api doc
clepski Nov 4, 2024
83e68a6
doc: Add editor action API doc
clepski Nov 4, 2024
305109b
doc: Add link to edit event doc
clepski Nov 4, 2024
c23c849
feat: Trigger validate after edit
clepski Nov 13, 2024
26a01eb
fix: Add review suggestions
clepski Nov 13, 2024
f455522
Merge branch 'main' into feat/edit-api-v2
clepski Nov 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: WIP Convert v1 to v2 events
clepski committed Oct 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 44255c36a7aa43afe21096f86ff01ca210afff5a
3 changes: 3 additions & 0 deletions packages/core/foundation.ts
Original file line number Diff line number Diff line change
@@ -23,6 +23,9 @@ export type {
Update,
Remove,
} from './foundation/edit-event.js';
export {
convertEditV1toV2
} from './foundation/edit-v1-to-v2-converter.js';

export { cyrb64 } from './foundation/cyrb64.js';

96 changes: 96 additions & 0 deletions packages/core/foundation/edit-v1-to-v2-converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Create, Delete, EditorAction, isCreate, isDelete, isMove, isReplace, isSimple, isUpdate, Move, Replace, SimpleAction, Update } from './deprecated/editor.js';
import { Edit, Insert, Remove, Update as UpdateV2 } from './edit-event.js';
import { getReference, SCLTag } from '@openscd/open-scd/src/foundation.js';


export function convertEditV1toV2(action: EditorAction): Edit {
if (isSimple(action)) {
return convertSimpleAction(action);
} else {
return action.actions.map(convertSimpleAction);
}
}

function convertSimpleAction(action: SimpleAction): Edit {
if (isCreate(action)) {
return convertCreate(action);
} else if (isDelete(action)) {
return convertDelete(action);
} else if (isUpdate(action)) {
return convertUpdate(action);
} else if (isMove(action)) {
return convertMove(action);
} else if (isReplace(action)) {
return convertReplace(action);
}

throw new Error('Unknown action type');
}

function convertCreate(action: Create): Insert {
let reference: Node | null = null;
if (
action.new.reference === undefined &&
action.new.element instanceof Element &&
action.new.parent instanceof Element
) {
reference = getReference(
action.new.parent,
<SCLTag>action.new.element.tagName
);
} else {
reference = action.new.reference ?? null;
}

return {
parent: action.new.parent,
node: action.new.element,
reference
};
}

function convertDelete(action: Delete): Remove {
return {
node: action.old.element
};
}

function convertUpdate(action: Update): UpdateV2 {
return {
element: action.element,
attributes: action.newAttributes
};
}

function convertMove(action: Move): Insert {
return {
parent: action.new.parent,
node: action.old.element,
reference: action.new.reference ?? null
}
}

function convertReplace(action: Replace): Edit {
const oldChildren = action.old.element.children;
const newNode = action.new.element.cloneNode() as Element;
newNode.append(...Array.from(oldChildren));
const parent = action.old.element.parentElement;

if (!parent) {
throw new Error('Replace action called without parent in old element');
}

const reference = action.old.element.nextSibling;

const remove: Remove = { node: action.old.element };
const insert: Insert = {
parent,
node: newNode,
reference
};

return [
remove,
insert
];
}
21 changes: 18 additions & 3 deletions packages/openscd/src/addons/Editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenEvent, newEditCompletedEvent } from '@openscd/core';
import { OpenEvent, newEditCompletedEvent, newEditEvent } from '@openscd/core';
import {
property,
LitElement,
@@ -48,6 +48,8 @@ import {
Update as UpdateV2,
} from '@openscd/core';

import { convertEditV1toV2 } from '@openscd/core';

@customElement('oscd-editor')
export class OscdEditor extends LitElement {
/** The `XMLDocument` to be edited */
@@ -431,7 +433,18 @@ export class OscdEditor extends LitElement {
else if (isUpdate(action)) this.logUpdate(action as Update);
}

private async onAction(event: EditorActionEvent<EditorAction>) {
private onAction(event: EditorActionEvent<EditorAction>) {
console.log('old event', event);

const editV2 = convertEditV1toV2(event.detail.action);
const initiator = event.detail.initiator;

console.log('dispatching new event', editV2);

this.host.dispatchEvent(newEditEvent(editV2, initiator));
}

private async onActionOld(event: EditorActionEvent<EditorAction>) {
if (isSimple(event.detail.action)) {
if (this.onSimpleAction(event.detail.action))
this.logSimpleAction(event.detail.action);
@@ -491,7 +504,7 @@ export class OscdEditor extends LitElement {
this.host.addEventListener('oscd-open', this.handleOpenDoc);

// TODO: Test v2 API
this.addEventListener('oscd-edit', event => this.handleEditEventV2(event));
this.host.addEventListener('oscd-edit', event => this.handleEditEventV2(event));
}

render(): TemplateResult {
@@ -528,6 +541,8 @@ export class OscdEditor extends LitElement {
}

function handleEditV2(edit: EditV2): EditV2 {
console.log('Edit V2 event', edit);

if (isInsertV2(edit)) return handleInsertV2(edit);
if (isUpdateV2(edit)) return handleUpdateV2(edit);
if (isRemoveV2(edit)) return handleRemoveV2(edit);