mirror of https://github.com/artf/grapesjs.git
11 changed files with 76 additions and 89 deletions
@ -1,42 +1,9 @@ |
|||
import { Model } from '../../common'; |
|||
import EditorModel from '../../editor/model/Editor'; |
|||
import { get, stringToPath } from '../../utils/mixins'; |
|||
|
|||
export default class StyleDataVariable extends Model { |
|||
em?: EditorModel; |
|||
import DataVariable from './DataVariable'; |
|||
|
|||
export default class StyleDataVariable extends DataVariable { |
|||
defaults() { |
|||
return { |
|||
type: 'data-variable-css', |
|||
value: '', |
|||
path: '', |
|||
...super.defaults(), |
|||
}; |
|||
} |
|||
|
|||
initialize(attrs: any, options: any) { |
|||
super.initialize(attrs, options); |
|||
this.em = options.em; |
|||
this.listenToDataSource(); |
|||
|
|||
return this; |
|||
} |
|||
|
|||
listenToDataSource() { |
|||
const { path } = this.attributes; |
|||
const resolvedPath = stringToPath(path).join('.'); |
|||
|
|||
if (this.em) { |
|||
this.listenTo(this.em.DataSources, `change:${resolvedPath}`, this.onDataSourceChange); |
|||
} |
|||
} |
|||
|
|||
onDataSourceChange() { |
|||
const { path } = this.attributes; |
|||
const [dsId, drId, key] = stringToPath(path); |
|||
const ds = this?.em?.DataSources.get(dsId); |
|||
const dr = ds && ds.getRecord(drId); |
|||
const newValue = dr?.get(key); |
|||
|
|||
this.set({ value: newValue }); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,18 @@ |
|||
import DataVariable from './DataVariable'; |
|||
import Trait from '../../trait_manager/model/Trait'; |
|||
|
|||
export default class TraitDataVariable extends DataVariable { |
|||
trait?: Trait; |
|||
|
|||
initialize(attrs: any, options: any) { |
|||
super.initialize(attrs, options); |
|||
this.trait = options.trait; |
|||
|
|||
return this; |
|||
} |
|||
|
|||
onDataSourceChange() { |
|||
const newValue = this.getDataValue(); |
|||
this.trait?.setTargetValue(newValue); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue