@@ -16,10 +16,6 @@ import './addons/CompasLayout.js';
1616
1717import '@openscd/open-scd/src/addons/Waiter.js' ;
1818import '@openscd/open-scd/src/addons/Settings.js' ;
19- import {
20- HistoryState ,
21- historyStateEvent ,
22- } from '@openscd/open-scd/src/addons/History.js' ;
2319import {
2420 initializeNsdoc ,
2521 Nsdoc ,
@@ -34,7 +30,7 @@ import { ActionDetail } from '@material/mwc-list';
3430
3531import { officialPlugins as builtinPlugins } from '../public/js/plugins.js' ;
3632import type { PluginSet , Plugin as CorePlugin } from '@openscd/core' ;
37- import { OscdApi } from '@openscd/core' ;
33+ import { OscdApi , XMLEditor } from '@openscd/core' ;
3834import { classMap } from 'lit-html/directives/class-map.js' ;
3935import {
4036 newConfigurePluginEvent ,
@@ -65,15 +61,15 @@ export class OpenSCD extends LitElement {
6561 <oscd-wizards .host=${ this } >
6662 <compas-history
6763 .host=${ this }
68- .editCount =${ this . historyState . editCount }
64+ .editor =${ this . editor }
6965 >
7066 <oscd-editor
7167 .doc=${ this . doc }
7268 .docName=${ this . docName }
7369 .docId=${ this . docId }
7470 .host=${ this }
75- .editCount=${ this . historyState . editCount }
76- .compasApi =${ this . compasApi }
71+ .editCount=${ this . editCount }
72+ .editor =${ this . editor }
7773 >
7874 <compas-layout
7975 @add-external-plugin=${ this . handleAddExternalPlugin }
@@ -83,9 +79,9 @@ export class OpenSCD extends LitElement {
8379 .host=${ this }
8480 .doc=${ this . doc }
8581 .docName=${ this . docName }
86- .editCount=${ this . historyState . editCount }
87- .historyState=${ this . historyState }
82+ .editCount=${ this . editCount }
8883 .plugins=${ this . storedPlugins }
84+ .editor=${ this . editor }
8985 .compasApi=${ this . compasApi }
9086 >
9187 </compas-layout>
@@ -104,12 +100,7 @@ export class OpenSCD extends LitElement {
104100 /** The UUID of the current [[`doc`]] */
105101 @property ( { type : String } ) docId = '' ;
106102
107- @state ( )
108- historyState : HistoryState = {
109- editCount : - 1 ,
110- canRedo : false ,
111- canUndo : false ,
112- } ;
103+ editor = new XMLEditor ( ) ;
113104
114105 /** Object containing all *.nsdoc files and a function extracting element's label form them*/
115106 @property ( { attribute : false } )
@@ -128,6 +119,10 @@ export class OpenSCD extends LitElement {
128119
129120 @state ( ) private storedPlugins : Plugin [ ] = [ ] ;
130121
122+ @state ( ) private editCount = - 1 ;
123+
124+ private unsubscribers : ( ( ) => any ) [ ] = [ ] ;
125+
131126 /** Loads and parses an `XMLDocument` after [[`src`]] has changed. */
132127 private async loadDoc ( src : string ) : Promise < void > {
133128 const response = await fetch ( src ) ;
@@ -216,12 +211,17 @@ export class OpenSCD extends LitElement {
216211 this . checkAppVersion ( ) ;
217212 this . loadPlugins ( ) ;
218213
214+ this . unsubscribers . push (
215+ this . editor . subscribe ( e => this . editCount ++ ) ,
216+ this . editor . subscribeUndoRedo ( e => this . editCount ++ )
217+ ) ;
218+
219219 // TODO: let Lit handle the event listeners, move to render()
220220 this . addEventListener ( 'reset-plugins' , this . resetPlugins ) ;
221- this . addEventListener ( historyStateEvent , ( e : CustomEvent < HistoryState > ) => {
222- this . historyState = e . detail ;
223- this . requestUpdate ( ) ;
224- } ) ;
221+ }
222+
223+ disconnectedCallback ( ) : void {
224+ this . unsubscribers . forEach ( u => u ( ) ) ;
225225 }
226226
227227 /**
@@ -465,14 +465,15 @@ export class OpenSCD extends LitElement {
465465 return staticTagHtml `<${ tag }
466466 .doc=${ this . doc }
467467 .docName=${ this . docName }
468- .editCount=${ this . historyState . editCount }
468+ .editCount=${ this . editCount }
469469 .plugins=${ this . storedPlugins }
470470 .docId=${ this . docId }
471471 .pluginId=${ plugin . src }
472472 .nsdoc=${ this . nsdoc }
473473 .docs=${ this . docs }
474474 .locale=${ this . locale }
475475 .oscdApi=${ new OscdApi ( tag ) }
476+ .editor=${ this . editor }
476477 .compasApi=${ this . compasApi }
477478 class="${ classMap ( {
478479 plugin : true ,
0 commit comments