From 5132acd384f589aa5a83ceabc5f6d29b79dc31f4 Mon Sep 17 00:00:00 2001 From: IhorKaleniuk666 Date: Tue, 20 Jan 2026 13:28:36 +0200 Subject: [PATCH] update tests --- .../core/src/asset_manager/model/Asset.ts | 2 +- .../core/src/asset_manager/model/Assets.ts | 19 +++++++- .../core/src/block_manager/model/Block.ts | 2 +- packages/core/src/commands/view/OpenAssets.ts | 2 +- .../core/src/device_manager/model/Device.ts | 2 +- .../domain_abstract/model/StyleableModel.ts | 7 ++- packages/core/src/pages/model/Page.ts | 2 +- .../patch_manager/CollectionWithPatches.ts | 47 ++++++++++--------- packages/core/src/patch_manager/index.ts | 8 ++-- .../src/selector_manager/model/Selector.ts | 2 +- .../core/src/trait_manager/model/Trait.ts | 2 +- 11 files changed, 58 insertions(+), 37 deletions(-) diff --git a/packages/core/src/asset_manager/model/Asset.ts b/packages/core/src/asset_manager/model/Asset.ts index d298c513a..01b2eed2a 100644 --- a/packages/core/src/asset_manager/model/Asset.ts +++ b/packages/core/src/asset_manager/model/Asset.ts @@ -1,5 +1,5 @@ import { result } from 'underscore'; -import ModelWithPatches from 'patch_manager/ModelWithPatches'; +import ModelWithPatches from '../../patch_manager/ModelWithPatches'; /** * @property {String} type Asset type, eg. `'image'`. diff --git a/packages/core/src/asset_manager/model/Assets.ts b/packages/core/src/asset_manager/model/Assets.ts index f49fdb4a7..7af0a5649 100644 --- a/packages/core/src/asset_manager/model/Assets.ts +++ b/packages/core/src/asset_manager/model/Assets.ts @@ -4,9 +4,24 @@ import AssetImage from './AssetImage'; import AssetImageView from '../view/AssetImageView'; import TypeableCollection from '../../domain_abstract/model/TypeableCollection'; -const TypeableCollectionExt = CollectionWithPatches.extend(TypeableCollection); +export default class Assets extends CollectionWithPatches { + constructor(models?: any, options?: any) { + super(models, options); + } +} -export default class Assets extends TypeableCollectionExt {} +export interface Assets { + types: any[]; + target?: any; + onSelect?: any; + getTypes(): any[]; + getType(id: string): any; + getBaseType(): any; + recognizeType(value: any): any; + addType(id: string, definition: any): void; +} + +Object.assign(Assets.prototype, TypeableCollection); Assets.prototype.types = [ { diff --git a/packages/core/src/block_manager/model/Block.ts b/packages/core/src/block_manager/model/Block.ts index dcee033a2..72d1de962 100644 --- a/packages/core/src/block_manager/model/Block.ts +++ b/packages/core/src/block_manager/model/Block.ts @@ -1,4 +1,4 @@ -import ModelWithPatches from 'patch_manager/ModelWithPatches'; +import ModelWithPatches from '../../patch_manager/ModelWithPatches'; import { isFunction } from 'underscore'; import Editor from '../../editor'; import Category, { CategoryProperties } from '../../abstract/ModuleCategory'; diff --git a/packages/core/src/commands/view/OpenAssets.ts b/packages/core/src/commands/view/OpenAssets.ts index c76cc4fb1..c7c61fa53 100644 --- a/packages/core/src/commands/view/OpenAssets.ts +++ b/packages/core/src/commands/view/OpenAssets.ts @@ -49,7 +49,7 @@ export default { am.__trgCustom(); } else { if (!this.rendered || types) { - let assets: Asset[] = am.getAll().filter((i: Asset) => i); + let assets: Asset[] = am.getAll().filter((i: Asset) => !!i); if (types && types.length) { assets = assets.filter((a) => types.indexOf(a.get('type')) !== -1); diff --git a/packages/core/src/device_manager/model/Device.ts b/packages/core/src/device_manager/model/Device.ts index 397cd24b8..95059c8ca 100644 --- a/packages/core/src/device_manager/model/Device.ts +++ b/packages/core/src/device_manager/model/Device.ts @@ -1,4 +1,4 @@ -import ModelWithPatches from 'patch_manager/ModelWithPatches'; +import ModelWithPatches from '../../patch_manager/ModelWithPatches'; /** @private */ export interface DeviceProperties { diff --git a/packages/core/src/domain_abstract/model/StyleableModel.ts b/packages/core/src/domain_abstract/model/StyleableModel.ts index 90aaeb8b5..e2b67926d 100644 --- a/packages/core/src/domain_abstract/model/StyleableModel.ts +++ b/packages/core/src/domain_abstract/model/StyleableModel.ts @@ -13,7 +13,7 @@ import { DataCollectionStateMap } from '../../data_sources/model/data_collection import { DataWatchersOptions } from '../../dom_components/model/ModelResolverWatcher'; import { DataResolverProps } from '../../data_sources/types'; import { _StringKey } from 'backbone'; -import ModelWithPatches from 'patch_manager/ModelWithPatches'; +import ModelWithPatches from '../../patch_manager/ModelWithPatches'; export type StyleProps = Record; @@ -45,7 +45,10 @@ type WithDataResolvers = { [P in keyof T]?: T[P] | DataResolverProps; }; -export default class StyleableModel extends ModelWithPatches { +export default class StyleableModel extends ModelWithPatches< + T, + UpdateStyleOptions +> { em?: EditorModel; views: StyleableView[] = []; dataResolverWatchers: ModelDataResolverWatchers; diff --git a/packages/core/src/pages/model/Page.ts b/packages/core/src/pages/model/Page.ts index b16f2b2e5..5db010a0f 100644 --- a/packages/core/src/pages/model/Page.ts +++ b/packages/core/src/pages/model/Page.ts @@ -6,7 +6,7 @@ import ComponentWrapper from '../../dom_components/model/ComponentWrapper'; import EditorModel from '../../editor/model/Editor'; import { CssRuleJSON } from '../../css_composer/model/CssRule'; import { ComponentDefinition } from '../../dom_components/model/types'; -import ModelWithPatches from 'patch_manager/ModelWithPatches'; +import ModelWithPatches from '../../patch_manager/ModelWithPatches'; /** @private */ export interface PageProperties { diff --git a/packages/core/src/patch_manager/CollectionWithPatches.ts b/packages/core/src/patch_manager/CollectionWithPatches.ts index 609b150b6..c0223cb63 100644 --- a/packages/core/src/patch_manager/CollectionWithPatches.ts +++ b/packages/core/src/patch_manager/CollectionWithPatches.ts @@ -32,10 +32,11 @@ export default class CollectionWithPatches extends Coll private isResetting = false; constructor(models?: any, options: CollectionWithPatchesOptions = {}) { - super(models, options); - this.em = options.em; - this.collectionId = options.collectionId; - this.patchObjectType = options.patchObjectType; + const nextOptions = { ...options }; + super(models, nextOptions); + this.em = nextOptions.em; + this.collectionId = nextOptions.collectionId; + this.patchObjectType = nextOptions.patchObjectType; this.on('sort', this.handleSort, this); this.rebuildFractionalMap(false); @@ -48,16 +49,6 @@ export default class CollectionWithPatches extends Coll }); } - // Ensure models created via collection.add/reset get a reference to `em`. - // This is critical for patch tracking and for apply(external) routing. - // @ts-ignore - _prepareModel(attrs: any, options: any) { - const nextOptions = options ? { ...options } : {}; - this.em && nextOptions.em == null && (nextOptions.em = this.em); - // @ts-ignore - return Collection.prototype._prepareModel.call(this, attrs, nextOptions); - } - get patchManager(): PatchManager | undefined { return this.em?.Patches; } @@ -66,14 +57,18 @@ export default class CollectionWithPatches extends Coll this.collectionId = id; } - add(models: any, options?: CollectionWithPatchesOptions) { - const result = super.add(models, options); + add(model: T | {}, options?: CollectionWithPatchesOptions): T; + add(models: Array, options?: CollectionWithPatchesOptions): T[]; + add(models: any, options?: CollectionWithPatchesOptions): any { + const result = super.add(models, this.withEmOptions(options) as any); !this.isResetting && this.assignKeysForMissingModels(); - return result; + return result as any; } - remove(...args: any[]) { - const removed = super.remove(...args); + remove(model: T | {}, options?: any): T; + remove(models: Array, options?: any): T[]; + remove(models: any, options?: any): any { + const removed = super.remove(models, options as any); const removedModels = Array.isArray(removed) ? removed : removed ? [removed] : []; removedModels.forEach((model) => { const id = this.getModelId(model as any); @@ -100,7 +95,7 @@ export default class CollectionWithPatches extends Coll reset(models?: any, options?: CollectionWithPatchesOptions) { this.isResetting = true; try { - const result = super.reset(models, options); + const result = super.reset(models, this.withEmOptions(options) as any); this.fractionalMap = {}; this.pendingRemovals = {}; this.rebuildFractionalMap(); @@ -116,7 +111,15 @@ export default class CollectionWithPatches extends Coll } protected getPatchCollectionId(): string | undefined { - return this.collectionId || this.cid; + return this.collectionId || (this as any).cid; + } + + protected withEmOptions(options?: CollectionWithPatchesOptions) { + const nextOptions = options ? { ...options } : {}; + if (this.em && nextOptions.em == null) { + nextOptions.em = this.em; + } + return nextOptions; } protected rebuildFractionalMap(record: boolean = true) { @@ -262,7 +265,7 @@ export default class CollectionWithPatches extends Coll if (op === 'remove') { delete this.fractionalMap[id]; const model = this.getModelByPatchId(id); - model && Collection.prototype.remove.call(this, model); + model && Collection.prototype.remove.call(this, model as any); return; } diff --git a/packages/core/src/patch_manager/index.ts b/packages/core/src/patch_manager/index.ts index 81854c30b..49728dbbd 100644 --- a/packages/core/src/patch_manager/index.ts +++ b/packages/core/src/patch_manager/index.ts @@ -73,7 +73,7 @@ export default class PatchManager { const type = model.patchObjectType; const idFromGetId = typeof model.getId === 'function' ? model.getId() : undefined; const hasGetId = typeof idFromGetId === 'string' ? idFromGetId !== '' : typeof idFromGetId === 'number'; - const id = hasGetId ? idFromGetId : model.id ?? model.get?.('id') ?? model.cid; + const id = hasGetId ? idFromGetId : (model.id ?? model.get?.('id') ?? model.cid); if (!type || id == null) return; const idStr = String(id); this.trackedModels[type] = this.trackedModels[type] || {}; @@ -85,7 +85,7 @@ export default class PatchManager { const type = model.patchObjectType; const idFromGetId = typeof model.getId === 'function' ? model.getId() : undefined; const hasGetId = typeof idFromGetId === 'string' ? idFromGetId !== '' : typeof idFromGetId === 'number'; - const id = hasGetId ? idFromGetId : model.id ?? model.get?.('id') ?? model.cid; + const id = hasGetId ? idFromGetId : (model.id ?? model.get?.('id') ?? model.cid); if (!type || id == null) return; const idStr = String(id); this.trackedModels[type] && delete this.trackedModels[type][idStr]; @@ -99,7 +99,7 @@ export default class PatchManager { const hasGetterId = typeof idFromGetter === 'string' ? idFromGetter !== '' : typeof idFromGetter === 'number'; const id = hasGetterId ? idFromGetter - : collection.collectionId ?? collection.id ?? collection.get?.('id') ?? collection.cid; + : (collection.collectionId ?? collection.id ?? collection.get?.('id') ?? collection.cid); if (!type || id == null) return; const idStr = String(id); this.trackedCollections[type] = this.trackedCollections[type] || {}; @@ -114,7 +114,7 @@ export default class PatchManager { const hasGetterId = typeof idFromGetter === 'string' ? idFromGetter !== '' : typeof idFromGetter === 'number'; const id = hasGetterId ? idFromGetter - : collection.collectionId ?? collection.id ?? collection.get?.('id') ?? collection.cid; + : (collection.collectionId ?? collection.id ?? collection.get?.('id') ?? collection.cid); if (!type || id == null) return; const idStr = String(id); this.trackedCollections[type] && delete this.trackedCollections[type][idStr]; diff --git a/packages/core/src/selector_manager/model/Selector.ts b/packages/core/src/selector_manager/model/Selector.ts index 6a8cf22a4..cea9b50f4 100644 --- a/packages/core/src/selector_manager/model/Selector.ts +++ b/packages/core/src/selector_manager/model/Selector.ts @@ -2,7 +2,7 @@ import { result, forEach, keys } from 'underscore'; import { Model } from '../../common'; import EditorModel from '../../editor/model/Editor'; import { SelectorManagerConfig } from '../config/config'; -import ModelWithPatches from 'patch_manager/ModelWithPatches'; +import ModelWithPatches from '../../patch_manager/ModelWithPatches'; const TYPE_CLASS = 1; const TYPE_ID = 2; diff --git a/packages/core/src/trait_manager/model/Trait.ts b/packages/core/src/trait_manager/model/Trait.ts index 5c6feb586..074d5b37c 100644 --- a/packages/core/src/trait_manager/model/Trait.ts +++ b/packages/core/src/trait_manager/model/Trait.ts @@ -7,7 +7,7 @@ import { isDef } from '../../utils/mixins'; import TraitsEvents, { TraitGetValueOptions, TraitOption, TraitProperties, TraitSetValueOptions } from '../types'; import TraitView from '../view/TraitView'; import Traits from './Traits'; -import ModelWithPatches from 'patch_manager/ModelWithPatches'; +import ModelWithPatches from '../../patch_manager/ModelWithPatches'; /** * @property {String} id Trait id, eg. `my-trait-id`.