Skip to content

Commit 72d0160

Browse files
authored
refactor(logging): add reset kind log event (#247)
* refactor(logging): add reset kind log event * test(open-scd): update snapshot * test(logging): adapt reset unit test * refactor(logging): remove public reset method
1 parent f118779 commit 72d0160

File tree

8 files changed

+38
-20
lines changed

8 files changed

+38
-20
lines changed

__snapshots__/open-scd.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@
392392
on=""
393393
>
394394
</mwc-icon-button-toggle>
395+
<mwc-icon-button-toggle
396+
id="resetfilter"
397+
on=""
398+
>
399+
</mwc-icon-button-toggle>
395400
<mwc-list
396401
id="content"
397402
wrapfocus=""

src/Logging.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const icons = {
2323
warning: 'warning',
2424
error: 'report',
2525
action: 'history',
26+
reset: 'none',
2627
};
2728

2829
/**
@@ -33,8 +34,6 @@ const icons = {
3334
* the committed action, allowing the user to go to `previousAction` with
3435
* `undo()` if `canUndo` and to go to `nextAction` with `redo()` if `canRedo`.
3536
*
36-
* Also provides a `reset()` method resetting the history and `currentAction`.
37-
*
3837
* Renders the `history` to `logUI` and the latest `'error'` [[`LogEntry`]] to
3938
* `messageUI`.
4039
*/
@@ -95,19 +94,19 @@ export function Logging<TBase extends LitElementConstructor>(Base: TBase) {
9594
return true;
9695
}
9796

98-
/** Resets the history to an empty state. */
99-
reset(): void {
100-
this.history = [];
101-
this.currentAction = -1;
102-
}
103-
10497
private onLog(le: LogEvent): void {
98+
if (le.detail.kind === 'reset') {
99+
this.history = [];
100+
this.currentAction = -1;
101+
return;
102+
}
103+
105104
const entry: LogEntry = {
106105
time: new Date(),
107106
...le.detail,
108107
};
109108

110-
if (entry.kind == 'action') {
109+
if (entry.kind === 'action') {
111110
if (entry.action.derived) return;
112111
entry.action.derived = true;
113112
if (this.nextAction !== -1) this.history.splice(this.nextAction);
@@ -145,11 +144,9 @@ export function Logging<TBase extends LitElementConstructor>(Base: TBase) {
145144

146145
this.undo = this.undo.bind(this);
147146
this.redo = this.redo.bind(this);
148-
this.reset = this.reset.bind(this);
149147

150148
this.onLog = this.onLog.bind(this);
151149
this.addEventListener('log', this.onLog);
152-
this.addEventListener('open-doc', this.reset);
153150
}
154151

155152
renderLogEntry(

src/foundation.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export function newWizardEvent(
222222

223223
type InfoEntryKind = 'info' | 'warning' | 'error';
224224

225-
export type LogEntryType = 'info' | 'warning' | 'error' | 'action';
225+
export type LogEntryType = 'info' | 'warning' | 'error' | 'action' | 'reset';
226226

227227
/** The basic information contained in each [[`LogEntry`]]. */
228228
interface LogDetailBase {
@@ -240,7 +240,11 @@ export interface InfoDetail extends LogDetailBase {
240240
cause?: LogEntry;
241241
}
242242

243-
export type LogDetail = InfoDetail | CommitDetail;
243+
export interface ResetDetail {
244+
kind: 'reset';
245+
}
246+
247+
export type LogDetail = InfoDetail | CommitDetail | ResetDetail;
244248
export type LogEvent = CustomEvent<LogDetail>;
245249
export function newLogEvent(
246250
detail: LogDetail,

src/icons.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ export const iconColors = {
5454
};
5555

5656
export function getFilterIcon(
57-
type: 'action' | 'info' | 'warning' | 'error',
57+
type: 'action' | 'info' | 'warning' | 'error' | 'reset',
5858
state: boolean
5959
): TemplateResult {
60+
if (type === 'reset') return html``;
6061
return html`<svg
6162
slot="${state ? 'onIcon' : 'offIcon'}"
6263
style="color:var(${state ? iconColors[type] : '--mwc-theme-on-background'})"

src/menu/NewProject.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ListItemBase } from '@material/mwc-list/mwc-list-item-base';
55

66
import {
77
EditorAction,
8+
newLogEvent,
89
newOpenDocEvent,
910
newWizardEvent,
1011
Wizard,
@@ -25,6 +26,9 @@ export default class NewProjectPlugin extends LitElement {
2526
.value
2627
);
2728

29+
document
30+
.querySelector('open-scd')
31+
?.dispatchEvent(newLogEvent({ kind: 'reset' }));
2832
document
2933
.querySelector('open-scd')
3034
?.dispatchEvent(

src/menu/OpenProject.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { css, html, LitElement, query, TemplateResult } from 'lit-element';
22

3-
import { newOpenDocEvent } from '../foundation.js';
3+
import { newLogEvent, newOpenDocEvent } from '../foundation.js';
44

55
export default class OpenProjectPlugin extends LitElement {
66
@query('#open-plugin-input') pluginFileUI!: HTMLInputElement;
@@ -14,6 +14,9 @@ export default class OpenProjectPlugin extends LitElement {
1414
const docName = file.name;
1515
const doc = new DOMParser().parseFromString(text, 'application/xml');
1616

17+
document
18+
.querySelector('open-scd')!
19+
.dispatchEvent(newLogEvent({ kind: 'reset' }));
1720
document
1821
.querySelector('open-scd')!
1922
.dispatchEvent(newOpenDocEvent(doc, docName));

src/open-scd.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,16 @@ export class OpenSCD extends Hosting(
8686
if (ctrlAnd('l')) this.logUI.open ? this.logUI.close() : this.logUI.show();
8787
if (ctrlAnd('m')) this.menuUI.open = !this.menuUI.open;
8888
if (ctrlAnd('o'))
89-
this.menuUI.querySelector<ListItem>('mwc-list-item.loader')?.click();
89+
this.menuUI
90+
.querySelector<ListItem>('mwc-list-item[iconid="folder_open"]')
91+
?.click();
92+
if (ctrlAnd('O'))
93+
this.menuUI
94+
.querySelector<ListItem>('mwc-list-item[iconid="create_new_folder"]')
95+
?.click();
9096
if (ctrlAnd('s'))
91-
this.menuUI.querySelector<ListItem>('mwc-list-item.saver')?.click();
92-
if (ctrlAnd('S'))
9397
this.menuUI
94-
.querySelector<ListItem>('mwc-list-item.saver + mwc-list-item.saver')
98+
.querySelector<ListItem>('mwc-list-item[iconid="save"]')
9599
?.click();
96100
if (ctrlAnd('P')) this.pluginUI.show();
97101

test/unit/Logging.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('LoggingElement', () => {
114114
});
115115

116116
it('can reset its history', () => {
117-
element.reset();
117+
element.dispatchEvent(newLogEvent({ kind: 'reset' }));
118118
expect(element).property('history').to.be.empty;
119119
expect(element).to.have.property('currentAction', -1);
120120
});

0 commit comments

Comments
 (0)