aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/formattedText/DashDocCommentView.tsx
blob: fcd6e0c55819d77b2ee273dec82d729749991983 (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
import { TextSelection } from 'prosemirror-state';
import * as ReactDOM from 'react-dom/client';
import { Doc } from '../../../../fields/Doc';
import { DocServer } from '../../../DocServer';
import React = require('react');

// creates an inline comment in a note when '>>' is typed.
// the comment sits on the right side of the note and vertically aligns with its anchor in the text.
// the comment can be toggled on/off with the '<-' text anchor.
export class DashDocCommentView {
    dom: HTMLDivElement; // container for label and value
    root: any;

    constructor(node: any, view: any, getPos: any) {
        this.dom = document.createElement('div');
        this.dom.style.width = node.attrs.width;
        this.dom.style.height = node.attrs.height;
        this.dom.style.fontWeight = 'bold';
        this.dom.style.position = 'relative';
        this.dom.style.display = 'inline-block';
        this.dom.onkeypress = function (e: any) {
            e.stopPropagation();
        };
        this.dom.onkeydown = function (e: any) {
            e.stopPropagation();
        };
        this.dom.onkeyup = function (e: any) {
            e.stopPropagation();
        };
        this.dom.onmousedown = function (e: any) {
            e.stopPropagation();
        };

        this.root = ReactDOM.createRoot(this.dom);
        this.root.render(<DashDocCommentViewInternal view={view} getPos={getPos} docid={node.attrs.docid} />);
        (this as any).dom = this.dom;
    }

    destroy() {
        this.root.unmount();
    }
    deselectNode() {
        this.dom.classList.remove('ProseMirror-selectednode');
    }
    selectNode() {
        this.dom.classList.add('ProseMirror-selectednode');
    }
}

interface IDashDocCommentViewInternal {
    docid: string;
    view: any;
    getPos: any;
}

export class DashDocCommentViewInternal extends React.Component<IDashDocCommentViewInternal> {
    constructor(props: IDashDocCommentViewInternal) {
        super(props);
        this.onPointerLeaveCollapsed = this.onPointerLeaveCollapsed.bind(this);
        this.onPointerEnterCollapsed = this.onPointerEnterCollapsed.bind(this);
        this.onPointerUpCollapsed = this.onPointerUpCollapsed.bind(this);
        this.onPointerDownCollapsed = this.onPointerDownCollapsed.bind(this);
    }

    onPointerLeaveCollapsed(e: any) {
        DocServer.GetRefField(this.props.docid).then(async dashDoc => dashDoc instanceof Doc && Doc.linkFollowUnhighlight());
        e.preventDefault();
        e.stopPropagation();
    }

    onPointerEnterCollapsed(e: any) {
        DocServer.GetRefField(this.props.docid).then(async dashDoc => dashDoc instanceof Doc && Doc.linkFollowHighlight(dashDoc, false));
        e.preventDefault();
        e.stopPropagation();
    }

    onPointerUpCollapsed(e: any) {
        const target = this.targetNode();

        if (target) {
            const expand = target.hidden;
            const tr = this.props.view.state.tr.setNodeMarkup(target.pos, undefined, { ...target.node.attrs, hidden: target.node.attrs.hidden ? false : true });
            this.props.view.dispatch(tr.setSelection(TextSelection.create(tr.doc, this.props.getPos() + (expand ? 2 : 1)))); // update the attrs
            setTimeout(() => {
                expand && DocServer.GetRefField(this.props.docid).then(async dashDoc => dashDoc instanceof Doc && Doc.linkFollowHighlight(dashDoc));
                try {
                    this.props.view.dispatch(this.props.view.state.tr.setSelection(TextSelection.create(this.props.view.state.tr.doc, this.props.getPos() + (expand ? 2 : 1))));
                } catch (e) {}
            }, 0);
        }
        e.stopPropagation();
    }

    onPointerDownCollapsed(e: any) {
        e.stopPropagation();
    }

    targetNode = () => {
        // search forward in the prosemirror doc for the attached dashDocNode that is the target of the comment anchor
        const state = this.props.view.state;
        for (let i = this.props.getPos() + 1; i < state.doc.content.size; i++) {
            const m = state.doc.nodeAt(i);
            if (m && m.type === state.schema.nodes.dashDoc && m.attrs.docid === this.props.docid) {
                return { node: m, pos: i, hidden: m.attrs.hidden } as { node: any; pos: number; hidden: boolean };
            }
        }

        const dashDoc = state.schema.nodes.dashDoc.create({ width: 75, height: 35, title: 'dashDoc', docid: this.props.docid, float: 'right' });
        this.props.view.dispatch(state.tr.insert(this.props.getPos() + 1, dashDoc));
        setTimeout(() => {
            try {
                this.props.view.dispatch(state.tr.setSelection(TextSelection.create(state.tr.doc, this.props.getPos() + 2)));
            } catch (e) {}
        }, 0);
        return undefined;
    };

    render() {
        return (
            <span
                className="formattedTextBox-inlineComment"
                id={'DashDocCommentView-' + this.props.docid}
                onPointerLeave={this.onPointerLeaveCollapsed}
                onPointerEnter={this.onPointerEnterCollapsed}
                onPointerUp={this.onPointerUpCollapsed}
                onPointerDown={this.onPointerDownCollapsed}></span>
        );
    }
}