import { action, configure, observable } from 'mobx'; import { observer } from 'mobx-react'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { DocServer } from '../client/DocServer'; import { resolvedPorts } from '../client/util/CurrentUserUtils'; import { CompileScript } from '../client/util/Scripting'; import { EditableView } from '../client/views/EditableView'; import CursorField from '../fields/CursorField'; import { DateField } from '../fields/DateField'; import { Doc, Field, FieldResult } from '../fields/Doc'; import { Id } from '../fields/FieldSymbols'; import { List } from '../fields/List'; import { RichTextField } from '../fields/RichTextField'; import { ScriptField } from '../fields/ScriptField'; import { URLField } from '../fields/URLField'; DateField; URLField; ScriptField; CursorField; function applyToDoc(doc: { [index: string]: FieldResult }, key: string, scriptString: string): boolean; function applyToDoc(doc: { [index: number]: FieldResult }, key: number, scriptString: string): boolean; function applyToDoc(doc: any, key: string | number, scriptString: string): boolean { const script = CompileScript(scriptString, { addReturn: true, params: { this: doc instanceof Doc ? Doc.name : List.name } }); if (!script.compiled) { return false; } const res = script.run({ this: doc }); if (!res.success) return false; if (!Field.IsField(res.result, true)) return false; doc[key] = res.result; return true; } configure({ enforceActions: 'observed', }); @observer class ListViewer extends React.Component<{ field: List }> { @observable expanded = false; @action onClick = (e: React.MouseEvent) => { this.expanded = !this.expanded; e.stopPropagation(); }; render() { let content; if (this.expanded) { content = (
{this.props.field.map((field, index) => ( applyToDoc(this.props.field, index, value)} /> ))}
); } else { content = <>[...]; } return (
{content}
); } } @observer class DocumentViewer extends React.Component<{ field: Doc }> { @observable expanded = false; @action onClick = (e: React.MouseEvent) => { this.expanded = !this.expanded; e.stopPropagation(); }; render() { let content; if (this.expanded) { const keys = Object.keys(this.props.field); const fields = keys.map(key => { return (
({key}): applyToDoc(this.props.field, key, value)}>
); }); content = (
Document ({this.props.field[Id]})
{fields}
); } else { content = <>[...] ({this.props.field[Id]}); } return (
{content}
); } } @observer class DebugViewer extends React.Component<{ field: FieldResult; setValue(value: string): boolean }> { render() { let content; const field = this.props.field; if (field instanceof List) { content = ; } else if (field instanceof Doc) { content = ; } else if (typeof field === 'string') { content =

"{field}"

; } else if (typeof field === 'number' || typeof field === 'boolean') { content =

{field}

; } else if (field instanceof RichTextField) { content =

RTF: {field.Data}

; } else if (field instanceof URLField) { content =

{field.url.href}

; } else if (field instanceof Promise) { return

Field loading

; } else { return

Unrecognized field type

; } return Field.toScriptString(field)} SetValue={this.props.setValue} contents={content} />; } } @observer class Viewer extends React.Component { @observable private idToAdd: string = ''; @observable private fields: Field[] = []; @action inputOnChange = (e: React.ChangeEvent) => { this.idToAdd = e.target.value; }; @action onKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { DocServer.GetRefField(this.idToAdd).then( action((field: any) => { if (field !== undefined) { this.fields.push(field); } }) ); this.idToAdd = ''; } }; render() { return ( <>
{this.fields.map((field, index) => ( false}> ))}
); } } (async function () { await DocServer.init(window.location.protocol, window.location.hostname, resolvedPorts.socket, 'viewer'); ReactDOM.render(
, document.getElementById('root') ); })();