-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { LitElement } from 'lit'; | ||
|
||
/** Constructor type for defining `LitElement` mixins. */ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type LitElementConstructor = new (...args: any[]) => LitElement; | ||
|
||
export { newOpenDocEvent } from './foundation/open-doc.js'; | ||
export type { OpenDocEvent, OpenDocDetail } from './foundation/open-doc.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export interface OpenDocDetail { | ||
doc: XMLDocument; | ||
docName: string; | ||
} | ||
|
||
/** Represents the intent to open `doc` with filename `docName`. */ | ||
export type OpenDocEvent = CustomEvent<OpenDocDetail>; | ||
|
||
export function newOpenDocEvent( | ||
doc: XMLDocument, | ||
docName: string | ||
): OpenDocEvent { | ||
return new CustomEvent<OpenDocDetail>('open-doc', { | ||
bubbles: true, | ||
composed: true, | ||
detail: { doc, docName }, | ||
}); | ||
} | ||
|
||
declare global { | ||
interface ElementEventMap { | ||
['open-doc']: OpenDocEvent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { html, fixture, expect } from '@open-wc/testing'; | ||
import { LitElement } from 'lit'; | ||
import { customElement } from 'lit/decorators.js'; | ||
|
||
import { Editing } from './Editing.js'; | ||
import { newOpenDocEvent } from '../foundation.js'; | ||
|
||
@customElement('editing-element') | ||
class EditingElement extends Editing(LitElement) {} | ||
|
||
describe('Editing Element', () => { | ||
let editor: EditingElement; | ||
const doc = new DOMParser().parseFromString( | ||
`<?xml version="1.0" encoding="UTF-8"?> | ||
<SCL version="2007" revision="B" xmlns="http://www.iec.ch/61850/2003/SCL"> | ||
</SCL>`, | ||
'application/xml' | ||
); | ||
|
||
beforeEach(async () => { | ||
editor = <EditingElement>( | ||
await fixture(html`<editing-element></editing-element>`) | ||
); | ||
}); | ||
|
||
it('loads a document on OpenDocEvent', async () => { | ||
editor.dispatchEvent(newOpenDocEvent(doc, 'test.scd')); | ||
await editor.updateComplete; | ||
expect(editor.doc).to.equal(doc); | ||
expect(editor.docName).to.equal('test.scd'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { property } from 'lit/decorators.js'; | ||
import { LitElementConstructor, OpenDocEvent } from '../foundation.js'; | ||
|
||
/** A mixin for editing a set of [[docs]] using [[EditorActionEvent]]s */ | ||
export function Editing<TBase extends LitElementConstructor>(Base: TBase) { | ||
class EditingElement extends Base { | ||
/** The `XMLDocument` currently being edited */ | ||
get doc(): XMLDocument { | ||
return this.docs[this.docName]; | ||
} | ||
|
||
/** The set of `XMLDocument`s currently loaded */ | ||
@property({ attribute: false }) | ||
docs: Record<string, XMLDocument> = {}; | ||
|
||
/** The name of the [[`doc`]] currently being edited */ | ||
@property({ type: String }) docName = ''; | ||
|
||
private onOpenDoc(event: OpenDocEvent) { | ||
this.docName = event.detail.docName; | ||
this.docs[this.docName] = event.detail.doc; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
constructor(...args: any[]) { | ||
super(...args); | ||
|
||
this.addEventListener('open-doc', this.onOpenDoc); | ||
} | ||
} | ||
return EditingElement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { LitElement } from 'lit'; | ||
import { Editing } from './mixins/Editing.js'; | ||
|
||
window.customElements.define('open-scd', LitElement); | ||
window.customElements.define('open-scd', Editing(LitElement)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters