Issue #1393Opened August 29, 2018by pnellesen1 reactions

[Question] Add grapesjs color picker to a new trait type?

Question

I'm building a a new trait type, and would like to be able to include the grapesjs color picker inside it - any suggestions on how to do that? Right now I'm including the standard HTML color input in my "inputEl" html (along with a text field and radio button) but I would love to be able to use the one that comes with grapesJS instead, if that's possible.

Example of where I'd like to put the grapesjs picker.

 inputEl =   `<div>SOME HTML HERE</div><div><span>Color: </span><span><input type="color" id="colorpicker" value=""></span></div><div>MORE HTML HERE</div>`

Thanks for any suggestions. Great tool, btw, thank you for creating it.

Answers (3)

pnellesenSeptember 13, 20181 reactions

@nojacko I've modified my code a little, here's basically what I'm doing:

 getInputEl: function() {
      if (!this.inputEl) {
        var input = document.createElement('div');
        input.innerHTML = this.inputHtml
        var inputEl = input;
        var pickerEl = inputEl.querySelector(".myolorPicker");
        pickerEl.id = "ppcp" + this.cid;
        if (!this.model.colorPickerEl) {// add the jQuery spectrum color picker to our trait editor
          this.model.colorPickerEl = editor.TraitManager.getType('color').prototype.getInputEl.apply(this, arguments);
        }
        pickerEl.appendChild(this.model.colorPickerEl)
        
        ... code to handle value changes and update other fields ...

      return this.inputEl;
}

Hope this helps. This is still a work in progress :)

Note that the getInputEl method of the colorPicker appends a spectrum colorpicker to the body of the canvas each time it's called, and it appears to be mapped to the element it returns, which is why I'm only calling it if I don't already have that element as part of my model. I suspect there are probably better ways to handle this, but this is working for my app at the moment.

artfSeptember 2, 20180 reactions

Hi @pnellesen this is actually a really good question, definitely, we should allow doing so but unfortunately, right now it's not so straightforward. We should refactor few modules to make the UI more reusable. BTW try to extend editor.TraitManager.getType('color').prototype.getInputEl inside getInputEl method and see if it might help you

pnellesenSeptember 6, 20180 reactions
Code SnippetTEXT
@artf  - thanks for the response! Your suggestion worked pretty well (and it even works in Internet Explorer, which is better than my html5 color picker :p ). I have some tweaks to do to my styling and my input data, but otherwise it's good.

Thanks again!

For anyone curious how it worked (and I'm sure there are probably better ways):

Just needed to add about 5 lines of code (in bold below) and add a "data-colorp-c" attribute to the element I wanted to attach the picker to:

getInputEl: function() {
      if (!this.inputEl) {
        var input = document.createElement('div');
        var thisModel = this.model;
        var thisTarget = this.target;
        **var that = this
        var thisColorPicker = function() {
          return editor.TraitManager.getType('color').prototype.getInputEl.apply(that, arguments);
        }**
       ... more code...
      **myColorPickerEl.appendChild(thisColorPicker())**
     ... other code ...
}

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.