Browse Source

update tests

pull/6691/head
IhorKaleniuk666 2 weeks ago
parent
commit
5132acd384
  1. 2
      packages/core/src/asset_manager/model/Asset.ts
  2. 19
      packages/core/src/asset_manager/model/Assets.ts
  3. 2
      packages/core/src/block_manager/model/Block.ts
  4. 2
      packages/core/src/commands/view/OpenAssets.ts
  5. 2
      packages/core/src/device_manager/model/Device.ts
  6. 7
      packages/core/src/domain_abstract/model/StyleableModel.ts
  7. 2
      packages/core/src/pages/model/Page.ts
  8. 47
      packages/core/src/patch_manager/CollectionWithPatches.ts
  9. 8
      packages/core/src/patch_manager/index.ts
  10. 2
      packages/core/src/selector_manager/model/Selector.ts
  11. 2
      packages/core/src/trait_manager/model/Trait.ts

2
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'`.

19
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<Asset> {
constructor(models?: any, options?: any) {
super(models, options);
}
}
export default class Assets extends TypeableCollectionExt<Asset> {}
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 = [
{

2
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';

2
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);

2
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 {

7
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<string, string | string[] | DataResolverProps>;
@ -45,7 +45,10 @@ type WithDataResolvers<T> = {
[P in keyof T]?: T[P] | DataResolverProps;
};
export default class StyleableModel<T extends StyleableModelProperties = any> extends ModelWithPatches<T, UpdateStyleOptions> {
export default class StyleableModel<T extends StyleableModelProperties = any> extends ModelWithPatches<
T,
UpdateStyleOptions
> {
em?: EditorModel;
views: StyleableView[] = [];
dataResolverWatchers: ModelDataResolverWatchers<T>;

2
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 {

47
packages/core/src/patch_manager/CollectionWithPatches.ts

@ -32,10 +32,11 @@ export default class CollectionWithPatches<T extends Model = Model> 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<T extends Model = Model> 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<T extends Model = Model> 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<T | {}>, 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<T | {}>, 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<T extends Model = Model> 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<T extends Model = Model> 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<T extends Model = Model> 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;
}

8
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];

2
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;

2
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`.

Loading…
Cancel
Save