1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const react_1 = __importDefault(require("react"));
|
7 | const ink_1 = require("ink");
|
8 | const ARROW_UP = '\u001B[A';
|
9 | const ARROW_DOWN = '\u001B[B';
|
10 | class Scroll extends react_1.default.Component {
|
11 | constructor() {
|
12 | super(...arguments);
|
13 |
|
14 | this.window = 5;
|
15 | this.state = {
|
16 | cursor: 0,
|
17 | };
|
18 | this.handleInput = (data) => {
|
19 | const { active, values } = this.props;
|
20 | const { cursor } = this.state;
|
21 |
|
22 | if (!active)
|
23 | return;
|
24 |
|
25 | const char = String(data);
|
26 | switch (char) {
|
27 | case ARROW_UP:
|
28 | if (cursor - 1 >= 0)
|
29 | this.setState({ cursor: cursor - 1 });
|
30 | break;
|
31 | case ARROW_DOWN:
|
32 | if (cursor + 1 < values.length)
|
33 | this.setState({ cursor: cursor + 1 });
|
34 | break;
|
35 | }
|
36 | };
|
37 | |
38 |
|
39 |
|
40 | this.getMask = () => {
|
41 | const values = this.props.values;
|
42 | const cursor = this.state.cursor;
|
43 | const size = this.window;
|
44 |
|
45 | const offset = Math.floor(size / 2);
|
46 |
|
47 | if (values.length <= size)
|
48 | return 0;
|
49 |
|
50 | if (cursor - offset <= 0)
|
51 | return 0;
|
52 |
|
53 | if (cursor + offset >= values.length)
|
54 | return values.length - size;
|
55 |
|
56 | return cursor - offset;
|
57 | };
|
58 | }
|
59 | componentDidMount() {
|
60 | const { stdin, setRawMode } = this.props;
|
61 | if (setRawMode)
|
62 | setRawMode(true);
|
63 | stdin.on('data', this.handleInput);
|
64 | }
|
65 | componentWillUnmount() {
|
66 | const { stdin, setRawMode } = this.props;
|
67 | stdin.removeListener('data', this.handleInput);
|
68 | if (setRawMode)
|
69 | setRawMode(false);
|
70 | }
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | static getDerivedStateFromProps(props, state) {
|
79 | if (props.active === false)
|
80 | return Object.assign({}, state, { cursor: 0 });
|
81 | if (props.values.length < state.cursor) {
|
82 | return Object.assign({}, state, { cursor: props.values.length });
|
83 | }
|
84 | return null;
|
85 | }
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | componentDidUpdate(props, state) {
|
94 | const cursor = this.state.cursor;
|
95 | |
96 |
|
97 |
|
98 | if (cursor === this.props.values.length - 2)
|
99 | this.props.onWillReachEnd();
|
100 | }
|
101 | render() {
|
102 | const mask = this.getMask(), cursor = this.state.cursor, size = this.window, values = this.props.values.slice(mask, mask + size), render = this.props.children;
|
103 | return (react_1.default.createElement(ink_1.Box, { flexDirection: "column" },
|
104 | values.length === 0 && (react_1.default.createElement(ink_1.Text, null,
|
105 | react_1.default.createElement(ink_1.Color, { grey: true }, "Start typing or change query so we can find something!"))),
|
106 | values.map((value, i) => render(Object.assign({}, value, { active: this.props.active && i + mask === cursor })))));
|
107 | }
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 | class ScrollWithStdin extends react_1.default.Component {
|
113 | render() {
|
114 | return (react_1.default.createElement(ink_1.StdinContext.Consumer, null, ({ stdin, setRawMode }) => (react_1.default.createElement(Scroll, Object.assign({}, this.props, { stdin: stdin, setRawMode: setRawMode })))));
|
115 | }
|
116 | }
|
117 | exports.default = ScrollWithStdin;
|
118 |
|
\ | No newline at end of file |