Skip to content

Commit

Permalink
feat: Trigger validate after edit
Browse files Browse the repository at this point in the history
  • Loading branch information
clepski committed Nov 13, 2024
1 parent 305109b commit c23c849
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/core-api/edit-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Open SCD offers an API for editing the scd document which can be used with [Html

The edits to the `doc` will be done in place, e.g. the `doc` changes but will keep the same reference. If your plugin needs to react to changes in the doc, you should listen to changes in the `editCount` property.

### Event factory
## Event factory

Open SCD core exports a factory function for edit events, so you do not have to build them manually.

Expand Down
14 changes: 8 additions & 6 deletions packages/openscd/src/addons/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
isNamespaced,
isRemove,
isUpdate,
OpenEvent as OpenEventV2,
Remove,
Update,
} from '@openscd/core';
Expand Down Expand Up @@ -72,10 +71,10 @@ export class OscdEditor extends LitElement {
}

private onAction(event: EditorActionEvent<EditorAction>) {
const editV2 = convertEditV1toV2(event.detail.action);
const edit = convertEditV1toV2(event.detail.action);
const initiator = event.detail.initiator;

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

/**
Expand Down Expand Up @@ -117,17 +116,17 @@ export class OscdEditor extends LitElement {
return html`<slot></slot>`;
}

handleEditEvent(event: EditEvent) {
async handleEditEvent(event: EditEvent) {
const edit = event.detail.edit;
const undoEdit = handleEdit(edit);

this.dispatchEvent(
newEditCompletedEvent(event.detail.edit, event.detail.initiator)
);

const isUserEvent = event.detail.initiator === 'user';
const shouldCreateHistoryEntry = event.detail.initiator !== 'redo' && event.detail.initiator !== 'undo';

if (isUserEvent) {
if (shouldCreateHistoryEntry) {
const { title, message } = this.getLogText(edit);

this.dispatchEvent(newLogEvent({
Expand All @@ -138,6 +137,9 @@ export class OscdEditor extends LitElement {
undo: undoEdit,
}));
}

await this.updateComplete;
this.dispatchEvent(newValidateEvent());
}
}

Expand Down
26 changes: 24 additions & 2 deletions packages/openscd/test/unit/Editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,39 @@ describe('OSCD-Editor', () => {
expect(logEntry.redo).to.deep.equal(remove);
});

it('should not log edit for undo, redo or system event', () => {
it('should not log edit for undo or redo event', () => {
const remove: Remove = {
node: bay2,
};

host.dispatchEvent(newEditEvent(remove, 'redo'));
host.dispatchEvent(newEditEvent(remove, 'undo'));
host.dispatchEvent(newEditEvent(remove, 'system'));

expect(log).to.have.lengthOf(0);
});

describe('validate after edit', () => {
let hasTriggeredValidate = false;
beforeEach(() => {
hasTriggeredValidate = false;

element.addEventListener('validate', () => {
hasTriggeredValidate = true;
});
});

it('should dispatch validate event after edit', async () => {
const remove: Remove = {
node: bay2,
};

host.dispatchEvent(newEditEvent(remove));

await element.updateComplete;

expect(hasTriggeredValidate).to.be.true;
});
});
});
});
});
Expand Down

0 comments on commit c23c849

Please sign in to comment.