Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to add a new property type(integer) to StyleManager? #2360

Closed
MartinPutz opened this issue Oct 30, 2019 · 2 comments
Closed

Comments

@MartinPutz
Copy link

MartinPutz commented Oct 30, 2019

Hi,
I am trying to create a custom StyleManager property with 'integer' type.

My ultimate goal is to 're-define' how certain units are displayed. For example, the user use unit 'X' and then the actual value would be different in pixels.(One of the reasons is the new dragmode since it is using pixels, so I want to keep all values in pixels but would allow the user to use other units).

I've checked the available functions and by extending the 'integer' number input I should be able to achieve that. But I get an error when I try to extend the 'integer' type.

The code I tried:


    const sm = editor.StyleManager;
    const propModel = sm.getType('integer').model;
    sm.addType('converted_integer', {
      model: propModel.extend({
        defaults: {
          ...propModel.prototype.defaults,
          units: [],
          unit: '',
          step: 1,
          min: '',
          max: ''
        },
        init() {
          this.listenTo(this, 'change', this.valueChanged);

        },
        valueChanged(value) {
          console.log("value changed",this);
        },
      }),
      view: sm.getType('integer').view.extend({

      }), 
      isType: (value) => {
        if (value && value.type == 'converted_integer') {
          return value;
        }
      },
     });


This gives an error: "Cannot set property 'ppfx' of undefined"

If I don't extend the view property it falls back to the default type without any issues. If I extend the 'stack' input type there are no issues at all.

What is required for a custom integer property like that?(GrapesJs version:0.15.8)

Thanks for your help.

@artf
Copy link
Member

artf commented Nov 2, 2019

Actually, the integer property has a wrong implementation of init (I'll fix it in the next release).
For now, update your init method with the code below and try if it works

init() {
	propModel.prototype.init.apply(this, arguments);
	this.listenTo(this, 'change', this.valueChanged);
},

@MartinPutz
Copy link
Author

You were right, it works now.
Thanks a lot. Great project!

@artf artf closed this as completed in 2828da8 Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants