import './style.scss'; import { Component } from '@wordpress/element'; const { navigator } = window; // generate dom tree. function getNodeTree(node) { if (node && node.hasChildNodes()) { const children = []; for (let j = 0; j < node.childNodes.length; j += 1) { children.push(getNodeTree(node.childNodes[j])); } return { classList: node.classList, nodeName: node.nodeName, children, }; } return false; } /** * Component Class */ export default class ClassesTree extends Component { constructor(...args) { super(...args); this.state = { nodes: false, }; this.onFrameLoad = this.onFrameLoad.bind(this); this.maybeFindIframe = this.maybeFindIframe.bind(this); this.updateTreeData = this.updateTreeData.bind(this); } componentDidMount() { this.maybeFindIframe(); } componentDidUpdate() { this.maybeFindIframe(); } componentWillUnmount() { if (!this.iframePreview) { return; } this.iframePreview.removeEventListener('load', this.onFrameLoad); } /** * On frame load event. */ onFrameLoad() { if (!this.iframePreview.contentWindow) { return; } // this.frameWindow = this.iframePreview.contentWindow; this.frameJQuery = this.iframePreview.contentWindow.jQuery; if (this.frameJQuery) { this.$framePortfolio = this.frameJQuery('.vp-portfolio'); } this.updateTreeData(); } maybeFindIframe() { if (this.iframePreview) { return; } const { clientId } = this.props; const iframePreview = document.querySelector( `#block-${clientId} iframe` ); if (iframePreview) { this.iframePreview = iframePreview; this.iframePreview.addEventListener('load', this.onFrameLoad); this.onFrameLoad(); } } updateTreeData() { if (this.$framePortfolio) { this.setState({ nodes: getNodeTree(this.$framePortfolio[0]), }); } } render() { if (!this.iframePreview) { return null; } return (