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 { CompileScript, ScriptOptions } from "../../util/Scripting"; import { FieldView, FieldViewProps } from './FieldView'; import "./KeyValueBox.scss"; import { KeyValuePair } from "./KeyValuePair"; import React = require("react"); import { NumCast, Cast, FieldValue } from "../../../new_fields/Types"; import { Doc, Field } from "../../../new_fields/Doc"; import { ComputedField } from "../../../fields/ScriptField"; @observer export class KeyValueBox extends React.Component { private _mainCont = React.createRef(); public static LayoutString(fieldStr: string = "data") { return FieldView.LayoutString(KeyValueBox, fieldStr); } @observable private _keyInput: string = ""; @observable private _valueInput: string = ""; @computed get splitPercentage() { return NumCast(this.props.Document.schemaSplitPercentage, 50); } get fieldDocToLayout() { return this.props.fieldKey ? FieldValue(Cast(this.props.Document[this.props.fieldKey], Doc)) : this.props.Document; } constructor(props: FieldViewProps) { super(props); } @action onEnterKey = (e: React.KeyboardEvent): void => { if (e.key === 'Enter') { if (this._keyInput && this._valueInput && this.fieldDocToLayout) { if (KeyValueBox.SetField(this.fieldDocToLayout, this._keyInput, this._valueInput)) { this._keyInput = ""; this._valueInput = ""; } } } } public static SetField(doc: Doc, key: string, value: string) { let eq = value.startsWith("="); value = eq ? value.substr(1) : value; let dubEq = value.startsWith(":="); value = dubEq ? value.substr(2) : value; let options: ScriptOptions = { addReturn: true }; if (dubEq) options.typecheck = false; let script = CompileScript(value, options); if (!script.compiled) { return false; } let field = new ComputedField(script); if (!dubEq) { let res = script.run(); if (!res.success) return false; field = res.result; } if (Field.IsField(field, true)) { let target = eq ? doc : Doc.GetProto(doc); target[key] = field; return true; } return false; } 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.fieldDocToLayout; if (!doc) { return Loading...; } let realDoc = doc; let ids: { [key: string]: string } = {}; let protos = Doc.GetAllPrototypes(doc); for (const proto of protos) { Object.keys(proto).forEach(key => { if (!(key in ids)) { ids[key] = key; } }); } let rows: JSX.Element[] = []; let i = 0; for (let key of Object.keys(ids).sort()) { 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.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}
); } }