[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)
@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.
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
@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.
Issue #563
Table <thead> and <tbody> tag gets Omitted
Hello, There is new issue with GrapesJS (maybe). I've created new component using bm.add() in which I've used a table element with <thead>...
Issue #544
Add select with optgroup
Hello @artf I have recently started using grapeJs and found it awesome. Can you please help me on How can I add dropdown(select) with optgr...
Issue #508
If you add a checkbox trait, it does not render correctly in the settings panel unless there is also a text trait
A text trait and a checkbox: Results in: If checkbox only: The checkbox overlaps the label:
Issue #423
Highlighting the component
Hi, Any idea how to highlight the component in the container when hover in the layer manager ? I think this is like reverse of click compon...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.