/** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; import { PanelBody, Panel, ColorPicker, RangeControl, ToggleControl, TabPanel, TextControl, Button, Popover, TextareaControl } from '@wordpress/components'; import {useState} from '@wordpress/element'; /** * Internal dependencies */ import './editor.scss'; import LCPDataSelector from '../../../components/LCPDataSelector'; import BarGraph from './components/BarGraph'; import LCPDimensionControl from '../../../components/LCPDimensionControl'; import ColorControls from './components/ColorControls'; export default function Edit({ attributes, setAttributes }) { const { chartHeight, chartWidth, barColor, barOpacity, backgroundColor, showGridY, yGridColor, xGridColor, xGridWidth, yGridWidth, chartData, dataSource, displayChartTitle, chartTitle, allowDownload, downloadMaxWidth, showSorting, showFiltering, showBarValues, showGridX, xAxisLabel, yAxisLabel, chartColorSource, chartCustomColors, enableHierarchicalView, selectedParentId } = attributes; const blockProps = useBlockProps(); const handleDataChange = (newData) => { console.log('New data:', newData); // Debug log setAttributes({ chartData: newData }); }; const handleDataSourceChange = (newSource) => { setAttributes({ dataSource: newSource, chartData: {} // Reset data when source changes }); }; const handleCustomColorChange = (dataset, label, color) => { const existingColors = [...chartCustomColors]; const existingIndex = existingColors.findIndex( c => c.dataset === dataset && c.label === label ); if (existingIndex >= 0) { existingColors[existingIndex].color = color; } else { existingColors.push({ dataset, label, color }); } setAttributes({ chartCustomColors: existingColors }); }; const handleDownload = () => { // Get the SVG element const svg = document.querySelector('.bar-graph svg'); if (!svg) return; // Create a canvas const canvas = document.createElement('canvas'); const svgData = new XMLSerializer().serializeToString(svg); const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' }); const DOMURL = window.URL || window.webkitURL || window; const url = DOMURL.createObjectURL(svgBlob); const img = new Image(); img.onload = () => { // Set canvas dimensions canvas.width = svg.width.baseVal.value; canvas.height = svg.height.baseVal.value; // Draw background const ctx = canvas.getContext('2d'); ctx.fillStyle = backgroundColor; ctx.fillRect(0, 0, canvas.width, canvas.height); // Draw the image ctx.drawImage(img, 0, 0); // Convert to blob and download canvas.toBlob((blob) => { const downloadUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = downloadUrl; a.download = 'bar-graph.png'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(downloadUrl); DOMURL.revokeObjectURL(url); }, 'image/png'); }; img.src = url; }; // Grid Color Popovers const [isYGridColorPopoverOpen, setIsYGridColorPopoverOpen] = useState(false); const [isXGridColorPopoverOpen, setIsXGridColorPopoverOpen] = useState(false); // Toggle the popover visibility const toggleYGridColorPopover = () => { setIsYGridColorPopoverOpen(!isYGridColorPopoverOpen); }; const toggleXGridColorPopover = () => { setIsXGridColorPopoverOpen(!isXGridColorPopoverOpen); }; // Handle color changes const onYGridColorChange = (newColor) => { setAttributes({ yGridColor: newColor }); }; const onXGridColorChange = (newColor) => { setAttributes({ xGridColor: newColor }); }; // Background Color Popover const [isBackgroundColorPopoverOpen, setIsBackgroundColorPopoverOpen] = useState(false); // Toggle the background popover visibility const toggleBackgroundColorPopover = () => { setIsBackgroundColorPopoverOpen(!isBackgroundColorPopoverOpen); }; // Handle color change const onBackgroundColorChange = (newColor) => { setAttributes({ backgroundColor: newColor }); }; // Debug log to check chartData console.log('Chart data:', chartData); // Ensure chartData is an object if (!attributes.chartData || typeof attributes.chartData !== 'object') { setAttributes({ chartData: {} }); } const updateChartData = (value) => { try { // Attempt to parse the JSON input const parsedData = JSON.parse(value); // Validate the structure if (typeof parsedData === 'object' && parsedData !== null) { // Check if the data follows our expected format let isValid = true; Object.entries(parsedData).forEach(([dataset, items]) => { if (!Array.isArray(items)) { isValid = false; } else { items.forEach(item => { if (!item.id || typeof item.id !== 'string') { isValid = false; } }); } }); if (isValid) { setAttributes({ chartData: parsedData }); } else { console.error('Invalid data structure'); } } } catch (e) { console.error('Invalid JSON:', e); } }; return (