-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathframe.ts
174 lines (156 loc) · 4.34 KB
/
frame.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2023 Im-Beast. MIT license.
import { Component, ComponentOptions } from "../component.ts";
import { BoxObject } from "../canvas/box.ts";
import { TextObject } from "../canvas/text.ts";
import { Computed, Signal } from "../signals/mod.ts";
import { signalify } from "../utils/signals.ts";
export const FrameUnicodeCharacters = {
sharp: {
topLeft: "┌",
topRight: "┐",
bottomLeft: "└",
bottomRight: "┘",
horizontal: "─",
vertical: "│",
},
rounded: {
topLeft: "╭",
topRight: "╮",
bottomLeft: "╰",
bottomRight: "╯",
horizontal: "─",
vertical: "│",
},
};
export type FrameUnicodeCharactersType = {
[key in keyof typeof FrameUnicodeCharacters["rounded"]]: string;
};
export interface FrameOptions extends ComponentOptions {
charMap: keyof typeof FrameUnicodeCharacters | FrameUnicodeCharactersType;
}
/**
* Component for creating non-interactive frames
*
* @example
* ```ts
* new Frame({
* parent: tui,
* charMap: "rounded",
* theme: {
* base: crayon.bgBlack.white,
* },
* rectangle: {
* column: 1,
* row: 1,
* height: 5,
* width: 10,
* },
* zIndex: 0,
* });
* ```
*
* If you want frame to follow component just link components rectangle as frame's rectangle.
*
* @example
* ```ts
* const box = new Box(...);
*
* new Frame({
* ...,
* rectangle: box.rectangle,
* });
* ```
*/
export class Frame extends Component {
declare drawnObjects: {
top: TextObject;
bottom: TextObject;
left: BoxObject;
right: BoxObject;
};
charMap: Signal<FrameUnicodeCharactersType>;
constructor(options: FrameOptions) {
super(options);
this.charMap = signalify(
typeof options.charMap === "string" ? FrameUnicodeCharacters[options.charMap] : options.charMap,
);
}
draw(): void {
super.draw();
const { canvas } = this.tui;
const topRectangle = { column: 0, row: 0 };
const top = new TextObject({
canvas,
view: this.view,
style: this.style,
zIndex: this.zIndex,
value: new Computed(() => {
const { topLeft, horizontal, topRight } = this.charMap.value;
return topLeft + horizontal.repeat(this.rectangle.value.width) + topRight;
}),
rectangle: new Computed(() => {
const { column, row } = this.rectangle.value;
topRectangle.column = column - 1;
topRectangle.row = row - 1;
return topRectangle;
}),
});
const bottomRectangle = { column: 0, row: 0 };
const bottom = new TextObject({
canvas,
view: this.view,
style: this.style,
zIndex: this.zIndex,
value: new Computed(() => {
const { bottomLeft, horizontal, bottomRight } = this.charMap.value;
return bottomLeft + horizontal.repeat(this.rectangle.value.width) + bottomRight;
}),
rectangle: new Computed(() => {
const { column, row, height } = this.rectangle.value;
bottomRectangle.column = column - 1;
bottomRectangle.row = row + height;
return bottomRectangle;
}),
});
const verticalCharMapSignal = new Computed(() => this.charMap.value.vertical);
const leftRectangle = { column: 0, row: 0, width: 1, height: 0 };
const left = new BoxObject({
canvas,
view: this.view,
style: this.style,
zIndex: this.zIndex,
filler: verticalCharMapSignal,
rectangle: new Computed(() => {
const { column, row, height } = this.rectangle.value;
leftRectangle.column = column - 1;
leftRectangle.row = row;
leftRectangle.height = height;
return leftRectangle;
}),
});
const rightRectangle = { column: 0, row: 0, width: 1, height: 0 };
const right = new BoxObject({
canvas,
view: this.view,
style: this.style,
zIndex: this.zIndex,
filler: verticalCharMapSignal,
rectangle: new Computed(() => {
const { column, row, width, height } = this.rectangle.value;
rightRectangle.column = column + width;
rightRectangle.row = row;
rightRectangle.height = height;
return rightRectangle;
}),
});
const { drawnObjects } = this;
drawnObjects.top = top;
drawnObjects.bottom = bottom;
drawnObjects.left = left;
drawnObjects.right = right;
top.draw();
bottom.draw();
left.draw();
right.draw();
}
}