aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/collectionMulticolumn/MultirowResizer.tsx
blob: 5a9d6a82c2667c9260ba662b3052ddc09ef6a123 (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
import * as React from 'react';
import { observer } from 'mobx-react';
import { observable, action } from 'mobx';
import { Doc } from '../../../../fields/Doc';
import { NumCast, StrCast } from '../../../../fields/Types';
import { DimUnit } from './CollectionMultirowView';
import { UndoManager } from '../../../util/UndoManager';
import { StyleProp } from '../../StyleProvider';
import { StyleProviderFunc } from '../../nodes/DocumentView';

interface ResizerProps {
    height: number;
    styleProvider?: StyleProviderFunc;
    isContentActive?: () => boolean | undefined;
    columnUnitLength(): number | undefined;
    toTop?: Doc;
    toBottom?: Doc;
}

const resizerOpacity = 1;

@observer
export default class ResizeBar extends React.Component<ResizerProps> {
    @observable private isHoverActive = false;
    @observable private isResizingActive = false;
    private _resizeUndo?: UndoManager.Batch;

    @action
    private registerResizing = (e: React.PointerEvent<HTMLDivElement>) => {
        e.stopPropagation();
        e.preventDefault();
        window.removeEventListener('pointermove', this.onPointerMove);
        window.removeEventListener('pointerup', this.onPointerUp);
        window.addEventListener('pointermove', this.onPointerMove);
        window.addEventListener('pointerup', this.onPointerUp);
        this.isResizingActive = true;
        this._resizeUndo = UndoManager.StartBatch('multcol resizing');
    };

    private onPointerMove = ({ movementY }: PointerEvent) => {
        const { toTop: toTop, toBottom: toBottom, columnUnitLength } = this.props;
        const movingDown = movementY > 0;
        const toNarrow = movingDown ? toBottom : toTop;
        const toWiden = movingDown ? toTop : toBottom;
        const unitLength = columnUnitLength();
        if (unitLength) {
            if (toNarrow) {
                const scale = StrCast(toNarrow._dimUnit, '*') === DimUnit.Ratio ? unitLength : 1;
                toNarrow._dimMagnitude = Math.max(0.05, NumCast(toNarrow._dimMagnitude, 1) - Math.abs(movementY) / scale);
            }
            if (toWiden) {
                const scale = StrCast(toWiden._dimUnit, '*') === DimUnit.Ratio ? unitLength : 1;
                toWiden._dimMagnitude = Math.max(0.05, NumCast(toWiden._dimMagnitude, 1) + Math.abs(movementY) / scale);
            }
        }
    };

    private get isActivated() {
        const { toTop, toBottom } = this.props;
        if (toTop && toBottom) {
            if (StrCast(toTop._dimUnit, '*') === DimUnit.Pixel && StrCast(toBottom._dimUnit, '*') === DimUnit.Pixel) {
                return false;
            }
            return true;
        } else if (toTop) {
            if (StrCast(toTop._dimUnit, '*') === DimUnit.Pixel) {
                return false;
            }
            return true;
        } else if (toBottom) {
            if (StrCast(toBottom._dimUnit, '*') === DimUnit.Pixel) {
                return false;
            }
            return true;
        }
        return false;
    }

    @action
    private onPointerUp = () => {
        this.isResizingActive = false;
        this.isHoverActive = false;
        window.removeEventListener('pointermove', this.onPointerMove);
        window.removeEventListener('pointerup', this.onPointerUp);
        this._resizeUndo?.end();
        this._resizeUndo = undefined;
    };

    render() {
        return (
            <div
                className="multiRowResizer"
                style={{
                    pointerEvents: this.props.isContentActive?.() ? 'all' : 'none',
                    height: this.props.height,
                    backgroundColor: !this.props.isContentActive?.() ? '' : this.props.styleProvider?.(undefined, undefined, StyleProp.WidgetColor),
                }}
                onPointerEnter={action(() => (this.isHoverActive = true))}
                onPointerLeave={action(() => !this.isResizingActive && (this.isHoverActive = false))}>
                <div className={'multiRowResizer-hdl'} onPointerDown={e => this.registerResizing(e)} />
            </div>
        );
    }
}