Skip to content

Commit

Permalink
chore(uip-editor): code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fshovchko committed Dec 18, 2024
1 parent d33cd14 commit d02fc15
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/core/base/model.change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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);
}
}
22 changes: 10 additions & 12 deletions src/core/base/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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));
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/base/state.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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})
Expand Down

0 comments on commit d02fc15

Please sign in to comment.