import React from 'react'; import CodeMirror from 'codemirror'; import * as spreadsheet from 'loot-core/src/client/sheetql/spreadsheet'; import { send, init as initConnection } from 'loot-core/src/platform/client/fetch'; import { View, Button, Input, InlineField } from 'loot-design/src/components/common'; import { colors } from 'loot-design/src/style'; require('codemirror/lib/codemirror.css'); require('codemirror/theme/monokai.css'); class Debugger extends React.Component { state = { recording: false, selecting: false, name: '__global!tmp', collapsed: true, node: null }; toggleRecord = () => { if (this.state.recording) { window.__stopProfile(); this.setState({ recording: false }); } else { window.__startProfile(); this.setState({ recording: true }); } }; reloadBackend = async () => { window.Actual.reloadBackend(); initConnection(await global.Actual.getServerSocket()); }; init() { this.mirror = CodeMirror(this.node, { theme: 'monokai' }); this.mirror.setSize('100%', '100%'); // this.mirror.on('change', () => { // const val = this.mirror.getValue(); // const [sheetName, name] = this.state.name.split('!'); // spreadsheet.set(sheetName, name, this.mirror.getValue()); // }); const mouseoverHandler = e => { let node = e.target; let cellname = null; while (!cellname && node) { cellname = node.dataset && node.dataset.cellname; node = node.parentNode; } if (this.state.selecting && cellname) { this.bind(cellname); } }; document.body.addEventListener('mouseover', mouseoverHandler, false); const clickHandler = e => { if (this.state.selecting) { this.setState({ selecting: false }); } }; document.body.addEventListener('click', clickHandler, false); this.removeListeners = () => { document.body.removeEventListener('mouseover', mouseoverHandler); document.body.removeEventListener('click', clickHandler); }; this.bind(this.state.name); } deinit() { if (this.unbind) { this.unbind(); } this.removeListeners(); this.mirror = null; } bind(resolvedName) { if (this.unbind) { this.unbind(); } const [sheetName, name] = resolvedName.split('!'); let currentReq = Math.random(); this.currentReq = currentReq; send('debugCell', { sheetName, name }).then(node => { if (currentReq === this.currentReq) { if (node._run) { this.mirror.setValue(node._run); } this.setState({ name: node.name, node }); this.unbind = spreadsheet.bind(sheetName, { name }, null, node => { if (currentReq !== this.currentReq) { return; } this.setState({ node: { ...this.state.node, value: node.value } }); this.valueNode.style.transition = 'none'; this.valueNode.style.backgroundColor = colors.y9; setTimeout(() => { this.valueNode.style.transition = 'background-color .8s'; this.valueNode.style.backgroundColor = 'rgba(0, 0, 0, 0)'; }, 50); }); } }); } componentWillUnmount() { if (this.unbind) { this.unbind(); this.unbind = null; } } onShow = () => { this.setState({ collapsed: false }, () => { this.init(); }); }; onClose = () => { this.setState({ collapsed: true }, () => { this.deinit(); }); }; onSelect = () => { this.setState({ selecting: true }); }; onNameChange = e => { const name = e.target.value; this.bind(name); this.setState({ name }); }; unselect() { if (this.unbind) { this.unbind(); this.unbind = null; this.setState({ sheetName: null, name: null, node: null }); } } render() { const { children } = this.props; const { name, node, selecting, collapsed, recording } = this.state; return (
{children}
{collapsed ? (
) : (
(this.node = n)} />
                  {node && JSON.stringify(node._dependencies, null, 2)}
                
(this.valueNode = n)} > {node && JSON.stringify(node.value)}
)} ); } } export default Debugger;