aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/FieldView.tsx
blob: 85dd779fcffc4603bbaf38f3f1ab76b956507f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React = require('react');
import { computed } from 'mobx';
import { observer } from 'mobx-react';
import { DateField } from '../../../fields/DateField';
import { Doc, Field, FieldResult, Opt } from '../../../fields/Doc';
import { List } from '../../../fields/List';
import { ScriptField } from '../../../fields/ScriptField';
import { WebField } from '../../../fields/URLField';
import { DocumentViewSharedProps } from './DocumentView';

//
// these properties get assigned through the render() method of the DocumentView when it creates this node.
// However, that only happens because the properties are "defined" in the markup for the field view.
// See the LayoutString method on each field view :   ImageBox, FormattedTextBox, etc.
//
export interface FieldViewProps extends DocumentViewSharedProps {
    // FieldView specific props that are not part of DocumentView props
    fieldKey: string;
    scrollOverflow?: boolean; // bcz: would like to think this can be avoided -- need to look at further

    select: (isCtrlPressed: boolean) => void;
    isContentActive: (outsideReaction?: boolean) => boolean | undefined;
    isDocumentActive?: () => boolean | undefined;
    isSelected: (outsideReaction?: boolean) => boolean;
    setHeight?: (height: number) => void;
    NativeDimScaling?: () => number; // scaling the DocumentView does to transform its contents into its panel & needed by ScreenToLocal NOTE: Must also be added to DocumentViewInternalsProps
    onBrowseClick?: () => ScriptField | undefined;
    onKey?: (e: React.KeyboardEvent, fieldProps: FieldViewProps) => boolean | undefined;
    pointerEvents?: () => Opt<string>;

    // properties intended to be used from within layout strings (otherwise use the function equivalents that work more efficiently with React)
    // See currentUserUtils headerTemplate for examples of creating text boxes from html which set some of these fields
    // Also, see InkingStroke for examples of creating text boxes from render() methods which set some of these fields
    backgroundColor?: string;
    treeViewDoc?: Doc;
    color?: string;
    fontSize?: number;
    height?: number;
    width?: number;
    noSidebar?: boolean;
    dontScale?: boolean;
    dontSelectOnLoad?: boolean; // suppress selecting (e.g.,. text box) when loaded (and mark as not being associated with scrollTop document field)
}

@observer
export class FieldView extends React.Component<FieldViewProps> {
    public static LayoutString(fieldType: { name: string }, fieldStr: string) {
        return `<${fieldType.name} {...props} fieldKey={'${fieldStr}'}/>`; //e.g., "<ImageBox {...props} fieldKey={"data} />"
    }

    @computed
    get field(): FieldResult {
        const { Document, fieldKey: fieldKey } = this.props;
        return Document[fieldKey];
    }
    render() {
        const field = this.field;
        // prettier-ignore
        if (field instanceof Doc)  return <p> <b>{field.title?.toString()}</b></p>;
        if (field === undefined) return <p>{'<null>'}</p>;
        if (field instanceof DateField) return <p>{field.date.toLocaleString()}</p>;
        if (field instanceof List) return <div> {field.map(f => Field.toString(f)).join(', ')} </div>;
        if (field instanceof WebField) return <p>{Field.toString(field.url.href)}</p>;
        if (!(field instanceof Promise)) return <p>{Field.toString(field)}</p>;
        return <p> {'Waiting for server...'} </p>;
    }
}