aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/DocumentIcon.tsx
blob: fb54f18e8a0ecc522eb1ee1b44daabed3daa2d54 (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
import { observer } from "mobx-react";
import * as React from "react";
import { DocumentView } from "./DocumentView";
import { DocumentManager } from "../../util/DocumentManager";
import { Transformer, Scripting, ts } from "../../util/Scripting";
import { Field } from "../../../fields/Doc";

@observer
export class DocumentIcon extends React.Component<{ view: DocumentView, index: number }> {
    render() {
        const view = this.props.view;
        const transform = view.props.ScreenToLocalTransform().scale(view.props.ContentScaling()).inverse();
        const { x, y, width, height } = transform.transformBounds(0, 0, view.props.PanelWidth(), view.props.PanelHeight());

        return (
            <div className="documentIcon-outerDiv" style={{
                position: "absolute",
                transform: `translate(${x + width / 2}px, ${y}px)`,
            }}>
                <p>d{this.props.index}</p>
            </div>
        );
    }
}

@observer
export class DocumentIconContainer extends React.Component {
    public static getTransformer(): Transformer {
        const usedDocuments = new Set<number>();
        return {
            transformer: context => {
                return root => {
                    function visit(node: ts.Node) {
                        node = ts.visitEachChild(node, visit, context);

                        if (ts.isIdentifier(node)) {
                            const isntPropAccess = !ts.isPropertyAccessExpression(node.parent) || node.parent.expression === node;
                            const isntPropAssign = !ts.isPropertyAssignment(node.parent) || node.parent.name !== node;
                            const isntParameter = !ts.isParameter(node.parent);
                            if (isntPropAccess && isntPropAssign && isntParameter && !(node.text in globalThis)) {
                                const match = node.text.match(/d([0-9]+)/);
                                if (match) {
                                    const m = parseInt(match[1]);
                                    usedDocuments.add(m);
                                }
                            }
                        }

                        return node;
                    }
                    return ts.visitNode(root, visit);
                };
            },
            getVars() {
                const docs = DocumentManager.Instance.DocumentViews;
                const capturedVariables: { [name: string]: Field } = {};
                usedDocuments.forEach(index => capturedVariables[`d${index}`] = docs[index].props.Document);
                return { capturedVariables };
            }
        };
    }
    render() {
        return DocumentManager.Instance.DocumentViews.map((dv, i) => <DocumentIcon key={i} index={i} view={dv} />);
    }
}