diff --git a/src/core/base/model.change.ts b/src/core/base/model.change.ts index 30d4f575..645c62a2 100644 --- a/src/core/base/model.change.ts +++ b/src/core/base/model.change.ts @@ -5,8 +5,10 @@ import type {UIPRoot} from './root'; import type {UIPStateModel} from './model'; import type {UIPSource} from './source'; +export type UIPModifier = UIPPlugin | UIPRoot | object; + export type UIPChangeInfo = { - modifier: UIPPlugin | UIPRoot; + modifier: UIPModifier; type: UIPSource; force?: boolean; }; @@ -39,7 +41,7 @@ export class UIPChangeEvent extends Event { return this.changes.filter((change) => change.type === 'html'); } - public isOnlyModifier(modifier: UIPPlugin | UIPRoot): boolean { + public isOnlyModifier(modifier: UIPModifier): boolean { return this.changes.every((change) => change.modifier === modifier); } } diff --git a/src/core/base/model.ts b/src/core/base/model.ts index 1d245591..f9d2ae18 100644 --- a/src/core/base/model.ts +++ b/src/core/base/model.ts @@ -10,10 +10,8 @@ import { import {UIPSnippetItem} from './snippet'; -import type {UIPRoot} from './root'; -import type {UIPPlugin} from './plugin'; import type {UIPSnippetTemplate} from './snippet'; -import type {UIPChangeInfo} from './model.change'; +import type {UIPChangeInfo, UIPModifier} from './model.change'; import type {UIPEditableSource} from './source'; /** Type for function to change attribute's current value */ @@ -28,7 +26,7 @@ export type ChangeAttrConfig = { /** Attribute to change */ attribute: string; /** Changes initiator */ - modifier: UIPPlugin | UIPRoot; + modifier: UIPModifier; } & ({ /** New {@link attribute} value */ value: string | boolean; @@ -64,7 +62,7 @@ export class UIPStateModel extends SyntheticEventTarget { * @param js - new state * @param modifier - plugin, that initiates the change */ - public setJS(js: string, modifier: UIPPlugin | UIPRoot): void { + public setJS(js: string, modifier: UIPModifier): void { const script = this.normalizeJS(js); if (this._js === script) return; this._js = script; @@ -81,7 +79,7 @@ export class UIPStateModel extends SyntheticEventTarget { * @param text - new state * @param modifier - plugin, that initiates the change */ - public setNote(text: string, modifier: UIPPlugin | UIPRoot): void { + public setNote(text: string, modifier: UIPModifier): void { const note = UIPNoteNormalizationPreprocessors.preprocess(text); if (this._note === note) return; this._note = note; @@ -95,7 +93,7 @@ export class UIPStateModel extends SyntheticEventTarget { * @param modifier - plugin, that initiates the change * @param force - marker, that indicates if html changes require iframe rerender */ - public setHtml(markup: string, modifier: UIPPlugin | UIPRoot, force: boolean = false): void { + public setHtml(markup: string, modifier: UIPModifier, force: boolean = false): void { const root = this.normalizeHTML(markup); if (root.innerHTML.trim() === this.html.trim()) return; this._html = root; @@ -126,16 +124,16 @@ export class UIPStateModel extends SyntheticEventTarget { return this.normalizeJS(this.activeSnippet.js) !== this.js; } - public reset(source: UIPEditableSource, modifier: UIPPlugin | UIPRoot): void { + public reset(source: UIPEditableSource, modifier: UIPModifier): void { if (source === 'html') this.resetHTML(modifier); if (source === 'js') this.resetJS(modifier); } - protected resetJS(modifier: UIPPlugin | UIPRoot): void { + protected resetJS(modifier: UIPModifier): void { if (this.activeSnippet) this.setJS(this.activeSnippet.js, modifier); } - protected resetHTML(modifier: UIPPlugin | UIPRoot): void { + protected resetHTML(modifier: UIPModifier): void { if (this.activeSnippet) this.setHtml(this.activeSnippet.html, modifier); } @@ -182,7 +180,7 @@ export class UIPStateModel extends SyntheticEventTarget { /** Changes current active snippet */ public applySnippet( snippet: UIPSnippetItem, - modifier: UIPPlugin | UIPRoot + modifier: UIPModifier ): void { if (!snippet) return; this._snippets.forEach((s) => (s.active = s === snippet)); @@ -194,7 +192,7 @@ export class UIPStateModel extends SyntheticEventTarget { ); } /** Applies an active snippet from DOM */ - public applyCurrentSnippet(modifier: UIPPlugin | UIPRoot): void { + public applyCurrentSnippet(modifier: UIPModifier): void { const activeSnippet = this.anchorSnippet || this.activeSnippet || this.snippets[0]; this.applySnippet(activeSnippet, modifier); } diff --git a/src/core/base/state.storage.ts b/src/core/base/state.storage.ts index 1efdfc40..245492be 100644 --- a/src/core/base/state.storage.ts +++ b/src/core/base/state.storage.ts @@ -61,9 +61,9 @@ export class UIPStateStorage { if (!state) return; const stateobj = JSON.parse(state) as UIPStateModelSnippets; - this.model.setHtml(stateobj.html, this as any, true); - this.model.setJS(stateobj.js, this as any); - this.model.setNote(stateobj.note, this as any); + this.model.setHtml(stateobj.html, this, true); + this.model.setJS(stateobj.js, this); + this.model.setNote(stateobj.note, this); } public saveState(): void { @@ -76,7 +76,7 @@ export class UIPStateStorage { const stateKey = this.getStateKey(); stateKey && this.removeEntry(stateKey); - this.model.reset(source, this as any); + this.model.reset(source, this); } @listen({event: 'uip:model:change', target: ($this: UIPStateStorage) => $this.model})