mirror of https://github.com/artf/grapesjs.git
3 changed files with 62 additions and 46 deletions
@ -1,14 +1,38 @@ |
|||||
import { Model, ObjectAny } from '../../common'; |
import { Model } from '../../common'; |
||||
export const type = 'data-variable-css'; |
import EditorModel from '../../editor/model/Editor'; |
||||
|
import { get, stringToPath } from '../../utils/mixins'; |
||||
|
|
||||
interface StyleDataVariableProps extends ObjectAny { |
export default class StyleDataVariable extends Model { |
||||
type: string; |
em?: EditorModel; |
||||
path: string; |
|
||||
value: string; |
defaults() { |
||||
} |
return { |
||||
|
type: 'data-variable-css', |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
|
||||
export default class StyleDataVariable<T extends StyleDataVariableProps = StyleDataVariableProps> extends Model<T> { |
onDataSourceChange(model: any) { |
||||
constructor(props: T, opts = {}) { |
const { path } = this.attributes; |
||||
super(props, opts); |
const newValue = get(model, stringToPath(path).join('.'), ''); |
||||
|
this.set({ value: newValue }); |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue