[Question] Adding script to content with chart js
Question
Hello I am trying to add a Graph JS block with this code
editor.BlockManager.add('test-chart-block', {
label: 'Test Chart block',
attributes: {class: 'fa fa-text'},
content: {
content: `<canvas id="bar-chart" width="800" height="450"></canvas>`,
script:`new Chart(document.getElementById("bar-chart"), {
type: 'bar',
data: {
labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
datasets: [
{
label: "Population (millions)",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
data: [2478,5267,734,784,433]
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Predicted world population (millions) in 2050'
}
}
});`
}
});
but whenever I would drag the new block I would get an error:

How do I add a script to a content to generate the chart? Also are third party scripts allowed in GrapesJS or are there properties that needs do be defined in initializing the editor?
Thank you!
Answers (1)
I just figured it out, you just need to declare your custom JS in the init method and set allowSripts to true
grapesjs.init({
. . . .
allowScripts: 1,
canvas: {
scripts: [
"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js"
],
styles: [
"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.css"
]
}
. . . .
});
Related Questions and Answers
Continue research with similar issue discussions.
Issue #792
Script property using function don't work with blocks
In Components & JS docs there's this example: This works perfectly. But I couldn't use a function instead of a string in script, as is said...
Issue #1545
[Question] How I Get ID From Block?
Hi, i have my block and I get print in Div id results from my javascript class component... How i get ID from my block ??? editor.BlockMana...
Issue #1584
Not getting the Script of Block from editor.getComponents()
Below is the Component that I'm adding. this.editor.BlockManager.add('dob-mmyy-pii', { id: 'dob-mmyy', label: DOB (MM, YYYY) (PII), categor...
Issue #634
How can I pass content with script?
I made custom component like this way and used script as well then script is working fine but I am not able to select component. Is there a...
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.