import { action, IReactionDisposer, observable, reaction, trace, untracked, computed } from 'mobx'; import { observer } from "mobx-react"; import 'react-image-lightbox/style.css'; import { WidthSym, Doc } from "../../../new_fields/Doc"; import { makeInterface } from "../../../new_fields/Schema"; import { Cast, NumCast, BoolCast } from "../../../new_fields/Types"; import { PdfField } from "../../../new_fields/URLField"; //@ts-ignore // import { Document, Page } from "react-pdf"; // import 'react-pdf/dist/Page/AnnotationLayer.css'; import { RouteStore } from "../../../server/RouteStore"; import { DocComponent } from "../DocComponent"; import { InkingControl } from "../InkingControl"; import { FilterBox } from "../search/FilterBox"; import { Annotation } from './Annotation'; 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"); import { CompileScript } from '../../util/Scripting'; import { Flyout, anchorPoints } from '../DocumentDecorations'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { ScriptField } from '../../../new_fields/ScriptField'; import { KeyCodes } from '../../northstar/utils/KeyCodes'; import { Utils } from '../../../Utils'; import { Id } from '../../../new_fields/FieldSymbols'; 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(PdfDocument) { public static LayoutString() { return FieldView.LayoutString(PDFBox); } @observable private _alt = false; @observable private _scrollY: number = 0; @computed get dataDoc() { return BoolCast(this.props.Document.isTemplate) && this.props.DataDoc ? this.props.DataDoc : this.props.Document; } @observable private _flyout: boolean = false; private _mainCont: React.RefObject; private _reactionDisposer?: IReactionDisposer; private _keyValue: string = ""; private _valueValue: string = ""; private _scriptValue: string = ""; private _keyRef: React.RefObject; private _valueRef: React.RefObject; private _scriptRef: React.RefObject; constructor(props: FieldViewProps) { super(props); this._mainCont = React.createRef(); this._reactionDisposer = reaction( () => this.props.Document.scrollY, () => { if (this._mainCont.current) { this._mainCont.current && this._mainCont.current.scrollTo({ top: NumCast(this.Document.scrollY), behavior: "auto" }); } } ); this._keyRef = React.createRef(); this._valueRef = React.createRef(); this._scriptRef = React.createRef(); } componentDidMount() { if (this.props.setPdfBox) this.props.setPdfBox(this); } componentWillUnmount() { this._reactionDisposer && this._reactionDisposer(); } public GetPage() { return Math.floor(NumCast(this.props.Document.scrollY) / NumCast(this.dataDoc.pdfHeight)) + 1; } public BackPage() { let cp = Math.ceil(NumCast(this.props.Document.scrollY) / NumCast(this.dataDoc.pdfHeight)) + 1; cp = cp - 1; if (cp > 0) { this.props.Document.curPage = cp; this.props.Document.scrollY = (cp - 1) * NumCast(this.dataDoc.pdfHeight); } } public GotoPage(p: number) { if (p > 0 && p <= NumCast(this.props.Document.numPages)) { this.props.Document.curPage = p; this.props.Document.scrollY = (p - 1) * NumCast(this.dataDoc.pdfHeight); } } public ForwardPage() { let cp = this.GetPage() + 1; if (cp <= NumCast(this.props.Document.numPages)) { this.props.Document.curPage = cp; this.props.Document.scrollY = (cp - 1) * NumCast(this.dataDoc.pdfHeight); } } private newKeyChange = (e: React.ChangeEvent) => { this._keyValue = e.currentTarget.value; } private newValueChange = (e: React.ChangeEvent) => { this._valueValue = e.currentTarget.value; } @action private newScriptChange = (e: React.ChangeEvent) => { this._scriptValue = e.currentTarget.value; } private applyFilter = () => { let scriptText = ""; if (this._scriptValue.length > 0) { scriptText = this._scriptValue; } else if (this._keyValue.length > 0 && this._valueValue.length > 0) { scriptText = `return this.${this._keyValue} === ${this._valueValue}`; } else { scriptText = "return true"; } let script = CompileScript(scriptText, { params: { this: Doc.name } }); if (script.compiled) { this.props.Document.filterScript = new ScriptField(script); } } @action private toggleFlyout = () => { this._flyout = !this._flyout; } @action private resetFilters = () => { this._keyValue = this._valueValue = ""; this._scriptValue = "return true"; if (this._keyRef.current) { this._keyRef.current.value = ""; } if (this._valueRef.current) { this._valueRef.current.value = ""; } if (this._scriptRef.current) { this._scriptRef.current.value = ""; } this.applyFilter(); } scrollTo(y: number) { if (this._mainCont.current) { this._mainCont.current.scrollTo({ top: Math.max(y - (this._mainCont.current!.offsetHeight / 2), 0), behavior: "auto" }); } } settingsPanel() { return !this.props.active() ? (null) : (
e.stopPropagation()}>
Annotation View Settings
); } loaded = (nw: number, nh: number, np: number) => { if (this.props.Document) { let doc = this.dataDoc; doc.numPages = np; if (doc.nativeWidth && doc.nativeHeight) return; let oldaspect = NumCast(doc.nativeHeight) / NumCast(doc.nativeWidth, 1); doc.nativeWidth = nw; if (doc.nativeHeight) doc.nativeHeight = nw * oldaspect; else doc.nativeHeight = nh; let ccv = this.props.ContainingCollectionView; if (ccv) { ccv.props.Document.pdfHeight = nh; } doc.height = nh * (doc[WidthSym]() / nw); } } @action onScroll = (e: React.UIEvent) => { if (e.currentTarget) { this._scrollY = e.currentTarget.scrollTop; let ccv = this.props.ContainingCollectionView; if (ccv) { ccv.props.Document.panTransformType = "None"; ccv.props.Document.scrollY = this._scrollY; } } } @computed get fieldExtensionDoc() { return Doc.resolvedFieldDataDoc(this.props.DataDoc ? this.props.DataDoc : this.props.Document, this.props.fieldKey, "true"); } render() { // uses mozilla pdf as default const pdfUrl = Cast(this.props.Document.data, PdfField); if (!(pdfUrl instanceof PdfField)) return
{`pdf, ${this.props.Document.data}, not found`}
; let classname = "pdfBox-cont" + (this.props.active() && !InkingControl.Instance.selectedTool && !this._alt ? "-interactive" : ""); return (
{ e.stopPropagation(); }}> {/*
*/} {this.settingsPanel()}
); } }