import { action, computed, observable } from "mobx"; import { observer } from "mobx-react"; import 'react-image-lightbox/style.css'; // This only needs to be imported once in your app import { Document } from '../../../fields/Document'; import { Field, FieldWaiting } from '../../../fields/Field'; import { Key } from '../../../fields/Key'; import { KeyStore } from '../../../fields/KeyStore'; import { CompileScript, ToField } from "../../util/Scripting"; import { FieldView, FieldViewProps } from './FieldView'; import "./KeyValueBox.scss"; import { KeyValuePair } from "./KeyValuePair"; import React = require("react"); @observer export class KeyValueBox extends React.Component { private _mainCont = React.createRef(); public static LayoutString(fieldStr: string = "DataKey") { return FieldView.LayoutString(KeyValueBox, fieldStr); } @observable private _keyInput: string = ""; @observable private _valueInput: string = ""; @computed get splitPercentage() { return this.props.Document.GetNumber(KeyStore.SchemaSplitPercentage, 50); } constructor(props: FieldViewProps) { super(props); } @action onEnterKey = (e: React.KeyboardEvent): void => { if (e.key === 'Enter') { if (this._keyInput && this._valueInput) { let doc = this.props.Document.GetT(KeyStore.Data, Document); if (!doc || doc === FieldWaiting) { return; } let realDoc = doc; let script = CompileScript(this._valueInput, { addReturn: true }); if (!script.compiled) { return; } let res = script.run(); if (!res.success) return; const field = res.result; if (field instanceof Field) { realDoc.Set(new Key(this._keyInput), field); } else { let dataField = ToField(field); if (dataField) { realDoc.Set(new Key(this._keyInput), dataField); } } this._keyInput = ""; this._valueInput = ""; } } } onPointerDown = (e: React.PointerEvent): void => { if (e.buttons === 1 && this.props.isSelected()) { e.stopPropagation(); } } onPointerWheel = (e: React.WheelEvent): void => { e.stopPropagation(); } createTable = () => { let doc = this.props.Document.GetT(KeyStore.Data, Document); if (!doc || doc === FieldWaiting) { return Loading...; } let realDoc = doc; let ids: { [key: string]: string } = {}; let protos = doc.GetAllPrototypes(); for (const proto of protos) { proto._proxies.forEach((val: any, key: string) => { if (!(key in ids)) { ids[key] = key; } }); } let rows: JSX.Element[] = []; let i = 0; for (let key in ids) { rows.push(); } return rows; } @action keyChanged = (e: React.ChangeEvent) => { this._keyInput = e.currentTarget.value; } @action valueChanged = (e: React.ChangeEvent) => { this._valueInput = e.currentTarget.value; } newKeyValue = () => ( ) @action onDividerMove = (e: PointerEvent): void => { let nativeWidth = this._mainCont.current!.getBoundingClientRect(); this.props.Document.SetNumber(KeyStore.SchemaSplitPercentage, Math.max(0, 100 - Math.round((e.clientX - nativeWidth.left) / nativeWidth.width * 100))); } @action onDividerUp = (e: PointerEvent): void => { document.removeEventListener("pointermove", this.onDividerMove); document.removeEventListener('pointerup', this.onDividerUp); } onDividerDown = (e: React.PointerEvent) => { e.stopPropagation(); e.preventDefault(); document.addEventListener("pointermove", this.onDividerMove); document.addEventListener('pointerup', this.onDividerUp); } render() { let dividerDragger = this.splitPercentage === 0 ? (null) :
; return (
{this.createTable()} {this.newKeyValue()}
Key Fields
{dividerDragger}
); } }