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 DataVariable from './DataVariable'; |
||||
import EditorModel from '../../editor/model/Editor'; |
|
||||
import { get, stringToPath } from '../../utils/mixins'; |
|
||||
|
|
||||
export default class StyleDataVariable extends Model { |
|
||||
em?: EditorModel; |
|
||||
|
|
||||
|
export default class StyleDataVariable extends DataVariable { |
||||
defaults() { |
defaults() { |
||||
return { |
return { |
||||
type: 'data-variable-css', |
...super.defaults(), |
||||
value: '', |
|
||||
path: '', |
|
||||
}; |
}; |
||||
} |
} |
||||
|
|
||||
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