aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/PDFBox.tsx
blob: 6450cb8260f6943894efd70f2bbd52fe1e9015ca (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { action, computed, IReactionDisposer, observable, reaction, runInAction } from 'mobx';
import { observer } from "mobx-react";
import * as Pdfjs from "pdfjs-dist";
import "pdfjs-dist/web/pdf_viewer.css";
import 'react-image-lightbox/style.css';
import { Doc, WidthSym, Opt } from "../../../new_fields/Doc";
import { makeInterface } from "../../../new_fields/Schema";
import { ScriptField } from '../../../new_fields/ScriptField';
import { BoolCast, Cast, NumCast } from "../../../new_fields/Types";
import { PdfField } from "../../../new_fields/URLField";
import { KeyCodes } from '../../northstar/utils/KeyCodes';
import { CompileScript } from '../../util/Scripting';
import { DocComponent } from "../DocComponent";
import { InkingControl } from "../InkingControl";
import { PDFViewer } from "../pdf/PDFViewer";
import { positionSchema } from "./DocumentView";
import { FieldView, FieldViewProps } from './FieldView';
import { pageSchema } from "./ImageBox";
import "./PDFBox.scss";
import React = require("react");

type PdfDocument = makeInterface<[typeof positionSchema, typeof pageSchema]>;
const PdfDocument = makeInterface(positionSchema, pageSchema);
export const handleBackspace = (e: React.KeyboardEvent) => { if (e.keyCode === KeyCodes.BACKSPACE) e.stopPropagation(); };

@observer
export class PDFBox extends DocComponent<FieldViewProps, PdfDocument>(PdfDocument) {
    public static LayoutString() { return FieldView.LayoutString(PDFBox); }

    @observable private _flyout: boolean = false;
    @observable private _alt = false;
    @observable private _pdf: Opt<Pdfjs.PDFDocumentProxy>;

    @computed get containingCollectionDocument() { return this.props.ContainingCollectionView && this.props.ContainingCollectionView.props.Document; }
    @computed get dataDoc() { return BoolCast(this.props.Document.isTemplate) && this.props.DataDoc ? this.props.DataDoc : this.props.Document; }
    @computed get fieldExtensionDoc() { return Doc.resolvedFieldDataDoc(this.props.DataDoc ? this.props.DataDoc : this.props.Document, this.props.fieldKey, "true"); }

    private _mainCont: React.RefObject<HTMLDivElement> = React.createRef();
    private _reactionDisposer?: IReactionDisposer;
    private _keyValue: string = "";
    private _valueValue: string = "";
    private _scriptValue: string = "";
    private _keyRef: React.RefObject<HTMLInputElement> = React.createRef();
    private _valueRef: React.RefObject<HTMLInputElement> = React.createRef();
    private _scriptRef: React.RefObject<HTMLInputElement> = React.createRef();

    componentDidMount() {
        this.props.setPdfBox && this.props.setPdfBox(this);

        const pdfUrl = Cast(this.props.Document.data, PdfField);
        if (pdfUrl instanceof PdfField) {
            Pdfjs.getDocument(pdfUrl.url.pathname).promise.then(pdf => runInAction(() => this._pdf = pdf));
        }
        this._reactionDisposer = reaction(
            () => this.props.Document.panY,
            () => this._mainCont.current && this._mainCont.current.scrollTo({ top: NumCast(this.Document.panY), behavior: "auto" })
        );
    }

    componentWillUnmount() {
        this._reactionDisposer && this._reactionDisposer();
    }

    public GetPage() {
        return Math.floor(NumCast(this.props.Document.panY) / NumCast(this.dataDoc.nativeHeight)) + 1;
    }

    @action
    public BackPage() {
        let cp = Math.ceil(NumCast(this.props.Document.panY) / NumCast(this.dataDoc.nativeHeight)) + 1;
        cp = cp - 1;
        if (cp > 0) {
            this.props.Document.curPage = cp;
            this.props.Document.panY = (cp - 1) * NumCast(this.dataDoc.nativeHeight);
        }
    }

    @action
    public GotoPage(p: number) {
        if (p > 0 && p <= NumCast(this.props.Document.numPages)) {
            this.props.Document.curPage = p;
            this.props.Document.panY = (p - 1) * NumCast(this.dataDoc.nativeHeight);
        }
    }

    @action
    public ForwardPage() {
        let cp = this.GetPage() + 1;
        if (cp <= NumCast(this.props.Document.numPages)) {
            this.props.Document.curPage = cp;
            this.props.Document.panY = (cp - 1) * NumCast(this.dataDoc.nativeHeight);
        }
    }

    @action
    setPanY = (y: number) => {
        this.containingCollectionDocument && (this.containingCollectionDocument.panY = y);
    }

    @action
    private applyFilter = () => {
        let scriptText = this._scriptValue.length > 0 ? this._scriptValue :
            this._keyValue.length > 0 && this._valueValue.length > 0 ?
                `return this.${this._keyValue} === ${this._valueValue}` : "return true";
        let script = CompileScript(scriptText, { params: { this: Doc.name } });
        script.compiled && (this.props.Document.filterScript = new ScriptField(script));
    }

    scrollTo = (y: number) => {
        this._mainCont.current && this._mainCont.current.scrollTo({ top: Math.max(y - (this._mainCont.current.offsetHeight / 2), 0), behavior: "auto" });
    }

    private resetFilters = () => {
        this._keyValue = this._valueValue = "";
        this._scriptValue = "return true";
        this._keyRef.current && (this._keyRef.current.value = "");
        this._valueRef.current && (this._valueRef.current.value = "");
        this._scriptRef.current && (this._scriptRef.current.value = "");
        this.applyFilter();
    }
    private newKeyChange = (e: React.ChangeEvent<HTMLInputElement>) => this._keyValue = e.currentTarget.value;
    private newValueChange = (e: React.ChangeEvent<HTMLInputElement>) => this._valueValue = e.currentTarget.value;
    private newScriptChange = (e: React.ChangeEvent<HTMLInputElement>) => this._scriptValue = e.currentTarget.value;

    settingsPanel() {
        return !this.props.active() ? (null) :
            (<div className="pdfBox-settingsCont" onPointerDown={(e) => e.stopPropagation()}>
                <button className="pdfBox-settingsButton" onClick={action(() => this._flyout = !this._flyout)} title="Open Annotation Settings"
                    style={{ marginTop: `${this.containingCollectionDocument ? NumCast(this.containingCollectionDocument.panY) : 0}px` }}>
                    <div className="pdfBox-settingsButton-arrow"
                        style={{
                            borderTop: `25px solid ${this._flyout ? "#121721" : "transparent"}`,
                            borderBottom: `25px solid ${this._flyout ? "#121721" : "transparent"}`,
                            borderRight: `25px solid ${this._flyout ? "transparent" : "#121721"}`,
                            transform: `scaleX(${this._flyout ? -1 : 1})`
                        }} />
                    <div className="pdfBox-settingsButton-iconCont">
                        <FontAwesomeIcon style={{ color: "white" }} icon="cog" size="3x" />
                    </div>
                </button>
                <div className="pdfBox-settingsFlyout" style={{ left: `${this._flyout ? -600 : 100}px` }} >
                    <div className="pdfBox-settingsFlyout-title">
                        Annotation View Settings
                    </div>
                    <div className="pdfBox-settingsFlyout-kvpInput">
                        <input placeholder="Key" className="pdfBox-settingsFlyout-input" onKeyDown={handleBackspace} onChange={this.newKeyChange}
                            style={{ gridColumn: 1 }} ref={this._keyRef} />
                        <input placeholder="Value" className="pdfBox-settingsFlyout-input" onKeyDown={handleBackspace} onChange={this.newValueChange}
                            style={{ gridColumn: 3 }} ref={this._valueRef} />
                    </div>
                    <div className="pdfBox-settingsFlyout-kvpInput">
                        <input placeholder="Custom Script" onChange={this.newScriptChange} onKeyDown={handleBackspace} style={{ gridColumn: "1 / 4" }} ref={this._scriptRef} />
                    </div>
                    <div className="pdfBox-settingsFlyout-kvpInput">
                        <button style={{ gridColumn: 1 }} onClick={this.resetFilters}>
                            <FontAwesomeIcon style={{ color: "white" }} icon="trash" size="lg" />
                            &nbsp; Reset Filters
                        </button>
                        <button style={{ gridColumn: 3 }} onClick={this.applyFilter}>
                            <FontAwesomeIcon style={{ color: "white" }} icon="check" size="lg" />
                            &nbsp; Apply
                        </button>
                    </div>
                </div>
            </div>);
    }

    loaded = (nw: number, nh: number, np: number) => {
        this.dataDoc.numPages = np;
        if (!this.dataDoc.nativeWidth || !this.dataDoc.nativeHeight || !this.dataDoc.scrollHeight) {
            let oldaspect = NumCast(this.dataDoc.nativeHeight) / NumCast(this.dataDoc.nativeWidth, 1);
            this.dataDoc.nativeWidth = nw;
            this.dataDoc.nativeHeight = this.dataDoc.nativeHeight ? nw * oldaspect : nh;
            this.dataDoc.height = this.dataDoc[WidthSym]() * (nh / nw);
            this.dataDoc.scrollHeight = np * this.dataDoc.nativeHeight;
        }
    }

    @action
    onScroll = (e: React.UIEvent<HTMLDivElement>) => {
        if (e.currentTarget && this.containingCollectionDocument) {
            this.containingCollectionDocument.panTransformType = "None";
            this.containingCollectionDocument.panY = e.currentTarget.scrollTop;
        }
    }

    render() {
        const pdfUrl = Cast(this.props.Document.data, PdfField);
        let classname = "pdfBox-cont" + (this.props.active() && !InkingControl.Instance.selectedTool && !this._alt ? "-interactive" : "");
        return (!(pdfUrl instanceof PdfField) || !this._pdf ?
            <div>{`pdf, ${this.props.Document.data}, not found`}</div> :
            <div className={classname}
                onScroll={this.onScroll}
                style={{ marginTop: `${this.containingCollectionDocument ? NumCast(this.containingCollectionDocument.panY) : 0}px` }}
                ref={this._mainCont}>
                <div className="pdfBox-scrollHack" style={{ height: NumCast(this.props.Document.scrollHeight) + (NumCast(this.props.Document.nativeHeight) - NumCast(this.props.Document.nativeHeight) / NumCast(this.props.Document.scale, 1)), width: "100%" }} />
                <PDFViewer pdf={this._pdf} url={pdfUrl.url.pathname} active={this.props.active} scrollTo={this.scrollTo} loaded={this.loaded} panY={NumCast(this.props.Document.panY)}
                    Document={this.props.Document} DataDoc={this.props.DataDoc}
                    addDocTab={this.props.addDocTab} setPanY={this.setPanY}
                    addDocument={this.props.addDocument}
                    fieldKey={this.props.fieldKey} fieldExtensionDoc={this.fieldExtensionDoc} />
                {this.settingsPanel()}
            </div>);
    }
}