Skip to content

Commit

Permalink
feat: change fired markup instance
Browse files Browse the repository at this point in the history
  • Loading branch information
nattallius committed Jun 15, 2021
1 parent 48caeeb commit 5c8984f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/core/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {attr, ESLBaseElement} from '@exadel/esl/modules/esl-base-element/core';
import {UIPRoot} from './root';
import {StateModelFiredObj} from './state-model';

/**
* Base class for UI Playground plugins.
Expand Down Expand Up @@ -28,7 +29,7 @@ export abstract class UIPPlugin extends ESLBaseElement {
super.connectedCallback();
this.classList.add('uip-plugin');
this.root = this.closest(`${UIPRoot.is}`) as UIPRoot;
this.root && this._onRootStateChange();
this.root && this._onRootStateChange({markup: this.root.model.html});
}
protected disconnectedCallback() {
this._root?.removeStateListener(this._onRootStateChange);
Expand All @@ -41,5 +42,5 @@ export abstract class UIPPlugin extends ESLBaseElement {
}

/** Handles root state change*/
protected abstract _onRootStateChange(): void;
protected abstract _onRootStateChange(e: StateModelFiredObj): void;
}
6 changes: 5 additions & 1 deletion src/core/state-model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {Observable} from '@exadel/esl';

export interface StateModelFiredObj {
markup: string,
}

export class UIPStateModel extends Observable {
protected root: Element;

Expand All @@ -12,7 +16,7 @@ export class UIPStateModel extends Observable {
const root = new DOMParser().parseFromString(markup, 'text/html').body;
if (root.innerHTML !== this.root.innerHTML) {
this.root = root;
this.fire({markup: this.html});
this.fire({markup: markup});
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {debounce} from '@exadel/esl/modules/esl-utils/async/debounce';
import {jsonAttr} from '@exadel/esl/modules/esl-base-element/core';

import {UIPPlugin} from '../core/plugin';
import {StateModelFiredObj} from '../core/state-model';

interface EditorConfig {
theme: string;
Expand Down Expand Up @@ -44,8 +45,8 @@ export class UIPEditor extends UIPPlugin {
}, 1000);

@bind
protected _onRootStateChange(): void {
const markup = this.root!.model.html;
protected _onRootStateChange(e: StateModelFiredObj): void {
const {markup} = e;
if (this.editor && markup === this.editor.getValue()) return; // check for self triggered changes

const $inner = document.createElement('div');
Expand Down

0 comments on commit 5c8984f

Please sign in to comment.