Skip to content

Commit

Permalink
Chore: Added oscd-edit-completed Event (#1533)
Browse files Browse the repository at this point in the history
* Chore: Added oscd-edit-completed Event

* Added import to initiator

* Added import to initiator

* Updated event to EditCompletedEvent

* Changed DocChangedEvent

* Fixed tests

* Always emit the editCompletedEvent from the Editor

* Fixed tests

* Removed @open-wc from core eslint

* Removed unused files

* Fixed codacy issues

* Fixed codacy issue

* Fixed review comment

* Added import
  • Loading branch information
pascalwilbrink authored Jun 3, 2024
1 parent 38b490d commit b967902
Show file tree
Hide file tree
Showing 15 changed files with 745 additions and 661 deletions.
7 changes: 7 additions & 0 deletions packages/core/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ export { cyrb64 } from './foundation/cyrb64.js';

export { Editing } from './mixins/Editing.js';
export type { Plugin, PluginSet } from './foundation/plugin.js';

export { newEditCompletedEvent } from './foundation/edit-completed-event.js';

export type {
EditCompletedEvent,
EditCompletedDetail,
} from './foundation/edit-completed-event.js';
14 changes: 8 additions & 6 deletions packages/core/foundation/deprecated/editor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Initiator } from '../edit-event.js';

/** Inserts `new.element` to `new.parent` before `new.reference`. */
export interface Create {
new: { parent: Node; element: Node; reference?: Node | null };
Expand Down Expand Up @@ -148,27 +150,27 @@ export function invert(action: EditorAction): EditorAction {
/** Represents some intended modification of a `Document` being edited. */
export interface EditorActionDetail<T extends EditorAction> {
action: T;
initiator?: Initiator;
}
export type EditorActionEvent<T extends EditorAction> = CustomEvent<
EditorActionDetail<T>
>;

export function newActionEvent<T extends EditorAction>(
action: T,
initiator: Initiator = 'user',
eventInitDict?: CustomEventInit<Partial<EditorActionDetail<T>>>
): EditorActionEvent<T> {
return new CustomEvent<EditorActionDetail<T>>('editor-action', {
bubbles: true,
composed: true,
...eventInitDict,
detail: { action, ...eventInitDict?.detail },
detail: { action, initiator, ...eventInitDict?.detail },
});
}


declare global {
interface ElementEventMap {
['editor-action']: EditorActionEvent<EditorAction>;
}
interface ElementEventMap {
['editor-action']: EditorActionEvent<EditorAction>;
}
}
31 changes: 31 additions & 0 deletions packages/core/foundation/edit-completed-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Edit, Initiator } from './edit-event.js';

import { EditorAction } from './deprecated/editor.js';

export type EditCompletedDetail = {
edit: Edit | EditorAction;
initiator: Initiator;
};

/** Represents the intent to open `doc` with filename `docName`. */
export type EditCompletedEvent = CustomEvent<EditCompletedDetail>;

export function newEditCompletedEvent(
edit: Edit | EditorAction,
initiator: Initiator = 'user'
): EditCompletedEvent {
return new CustomEvent<EditCompletedDetail>('oscd-edit-completed', {
bubbles: true,
composed: true,
detail: {
edit: edit,
initiator: initiator,
},
});
}

declare global {
interface ElementEventMap {
['oscd-edit-completed']: EditCompletedEvent;
}
}
21 changes: 17 additions & 4 deletions packages/core/foundation/edit-event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type Initiator = 'user' | 'system' | 'undo' | 'redo' | string;

/** Intent to `parent.insertBefore(node, reference)` */
export type Insert = {
parent: Node;
Expand Down Expand Up @@ -48,13 +50,24 @@ export function isRemove(edit: Edit): edit is Remove {
);
}

export type EditEvent<E extends Edit = Edit> = CustomEvent<E>;
export interface EditEventDetail<E extends Edit = Edit> {
edit: E;
initiator: Initiator;
}

export type EditEvent = CustomEvent<EditEventDetail>;

export function newEditEvent<E extends Edit>(edit: E): EditEvent<E> {
return new CustomEvent<E>('oscd-edit', {
export function newEditEvent<E extends Edit>(
edit: E,
initiator: Initiator = 'user'
): EditEvent {
return new CustomEvent<EditEventDetail>('oscd-edit', {
composed: true,
bubbles: true,
detail: edit,
detail: {
edit: edit,
initiator: initiator,
},
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/mixins/Editing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function Editing<TBase extends LitElementConstructor>(
}

handleEditEvent(event: EditEvent) {
const edit = event.detail;
const edit = event.detail.edit;
this.history.splice(this.editCount);
this.history.push({ undo: handleEdit(edit), redo: edit });
this.editCount += 1;
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
]
},
"extends": [
"@open-wc",
"prettier"
],
"plugins": [
Expand Down
16 changes: 8 additions & 8 deletions packages/openscd/src/addons/Editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenEvent } from '@openscd/core';
import { OpenEvent, newEditCompletedEvent } from '@openscd/core';
import {
property,
LitElement,
Expand All @@ -16,16 +16,13 @@ import {
EditorActionEvent,
SimpleAction,
Replace,
Update
Update,
} from '@openscd/core/foundation/deprecated/editor.js';

import { newLogEvent } from '@openscd/core/foundation/deprecated/history.js';
import { newValidateEvent } from '@openscd/core/foundation/deprecated/validation.js'
import {OpenDocEvent} from '@openscd/core/foundation/deprecated/open-event.js';
import {
getReference,
SCLTag,
} from '../foundation.js';
import { newValidateEvent } from '@openscd/core/foundation/deprecated/validation.js';
import { OpenDocEvent } from '@openscd/core/foundation/deprecated/open-event.js';
import { getReference, SCLTag } from '../foundation.js';
import {
isCreate,
isDelete,
Expand Down Expand Up @@ -439,6 +436,9 @@ export class OscdEditor extends LitElement {

await this.updateComplete;
this.dispatchEvent(newValidateEvent());
this.dispatchEvent(
newEditCompletedEvent(event.detail.action, event.detail.initiator)
);
}

/**
Expand Down
Loading

0 comments on commit b967902

Please sign in to comment.