-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor Traits Collection * Fix css prefix * Fix trait undo and add test for it --------- Co-authored-by: Artur Arseniev <artur.catch@hotmail.it>
- Loading branch information
Showing
9 changed files
with
125 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
import { TraitManagerConfig } from '../config/config'; | ||
import { TraitProperties } from './Trait'; | ||
import { isString } from 'underscore'; | ||
import Trait, { TraitProperties } from './Trait'; | ||
import EditorModel from '../../editor/model/Editor'; | ||
|
||
export default class TraitFactory { | ||
config: Partial<TraitManagerConfig>; | ||
|
||
constructor(config: Partial<TraitManagerConfig> = {}) { | ||
this.config = config; | ||
} | ||
|
||
export default (config: Partial<TraitManagerConfig> = {}) => ({ | ||
/** | ||
* Build props object by their name | ||
* @param {Array<string>|string} props Array of properties name | ||
* @return {Array<Object>} | ||
*/ | ||
build(props: string | string[]) { | ||
const objs = []; | ||
|
||
if (typeof props === 'string') props = [props]; | ||
build(prop: string | TraitProperties, em: EditorModel): Trait { | ||
return isString(prop) ? this.buildFromString(prop, em) : new Trait(prop, em); | ||
} | ||
|
||
for (let i = 0; i < props.length; i++) { | ||
const prop = props[i]; | ||
const obj: TraitProperties = { | ||
name: prop, | ||
type: 'text', | ||
}; | ||
private buildFromString(name: string, em: EditorModel): Trait { | ||
const obj: TraitProperties = { | ||
name: name, | ||
type: 'text', | ||
}; | ||
|
||
switch (prop) { | ||
case 'target': | ||
obj.type = 'select'; | ||
obj.default = false; | ||
obj.options = config.optionsTarget; | ||
break; | ||
} | ||
|
||
objs.push(obj); | ||
switch (name) { | ||
case 'target': | ||
obj.type = 'select'; | ||
obj.default = false; | ||
obj.options = this.config.optionsTarget; | ||
break; | ||
} | ||
|
||
return objs; | ||
}, | ||
}); | ||
return new Trait(obj, em); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,44 @@ | ||
import Trait from '../../../../src/trait_manager/model/Trait'; | ||
import Traits from '../../../../src/trait_manager/model/Traits'; | ||
import Component from '../../../../src/dom_components/model/Component'; | ||
import Editor from '../../../../src/editor'; | ||
import EditorModel from '../../../../src/editor/model/Editor'; | ||
|
||
describe('TraitModels', () => { | ||
var trait: Trait; | ||
var target: Component; | ||
var modelName = 'title'; | ||
var em: EditorModel; | ||
|
||
beforeEach(() => { | ||
target = new Component(); | ||
trait = new Trait({ | ||
name: modelName, | ||
target, | ||
}); | ||
em = new Editor().getModel(); | ||
target = new Component({}, { em }); | ||
trait = new Trait( | ||
{ | ||
name: modelName, | ||
target, | ||
}, | ||
em | ||
); | ||
}); | ||
|
||
afterEach(() => {}); | ||
|
||
test('Object exists', () => { | ||
expect(trait).toBeTruthy(); | ||
}); | ||
test('Traits undo property', () => { | ||
em.loadOnStart(); | ||
const wrapper = em.Components.getWrapper(); | ||
wrapper!.append(target); | ||
const traits = new Traits([], { em }); | ||
traits.add(modelName); | ||
traits.setTarget(target); | ||
const trait = traits.models[0]; | ||
trait.setTargetValue('TitleValue'); | ||
|
||
expect(target.getAttributes()[modelName]).toBe('TitleValue'); | ||
em.UndoManager.undo(); | ||
expect(target.getAttributes()[modelName]).toBeUndefined; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters