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'; |
|||
export const type = 'data-variable-css'; |
|||
import { Model } from '../../common'; |
|||
import EditorModel from '../../editor/model/Editor'; |
|||
import { get, stringToPath } from '../../utils/mixins'; |
|||
|
|||
interface StyleDataVariableProps extends ObjectAny { |
|||
type: string; |
|||
path: string; |
|||
value: string; |
|||
} |
|||
export default class StyleDataVariable extends Model { |
|||
em?: EditorModel; |
|||
|
|||
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> { |
|||
constructor(props: T, opts = {}) { |
|||
super(props, opts); |
|||
onDataSourceChange(model: any) { |
|||
const { path } = this.attributes; |
|||
const newValue = get(model, stringToPath(path).join('.'), ''); |
|||
this.set({ value: newValue }); |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue