-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtext.ts
89 lines (83 loc) · 2.27 KB
/
text.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
// Copyright 2023 Im-Beast. MIT license.
import { TextObject, TextRectangle } from "../canvas/text.ts";
import { Component, ComponentOptions } from "../component.ts";
import { Signal, SignalOfObject } from "../signals/mod.ts";
import { signalify } from "../utils/signals.ts";
export interface TextOptions extends Omit<ComponentOptions, "rectangle"> {
text: string | Signal<string>;
overwriteWidth?: boolean | Signal<boolean>;
multiCodePointSupport?: boolean | Signal<boolean>;
rectangle: TextRectangle | SignalOfObject<TextRectangle>;
}
/**
* Component for creating single-line, non interactive text
*
* @example
* ```ts
* new Text({
* parent: tui,
* text: "Hello there"
* theme: {
* base: crayon.magenta,
* },
* rectangle: {
* column: 1,
* row: 1,
* },
* zIndex: 0,
* });
* ```
*
* If you need to use emojis or other multi codepoint characters set `multiCodePointSupport` property to true.
* @example
* ```ts
* new Text({
* ...,
* text: "🧡",
* multiCodePointCharacter: true,
* });
* ```
* Rectangle properties – `width` and `height` are calculated automatically by default.
* To overwrite that behaviour set `overwriteRectangle` property to true.
*
* @example
* ```ts
* new Text({
* ...,
* text: "1 2 3 cut me",
* overwriteRectangle: true,
* rectangle: {
* column: 1,
* row: 1,
* width: 6,
* height: 1,
* },
* })
* ```
*/
export class Text extends Component {
declare drawnObjects: { text: TextObject };
text: Signal<string>;
overwriteRectangle: Signal<boolean>;
multiCodePointSupport: Signal<boolean>;
constructor(options: TextOptions) {
super(options as unknown as ComponentOptions);
this.text = signalify(options.text);
this.overwriteRectangle = signalify(options.overwriteWidth ?? false);
this.multiCodePointSupport = signalify(options.multiCodePointSupport ?? false);
}
draw(): void {
const text = new TextObject({
canvas: this.tui.canvas,
view: this.view,
value: this.text,
style: this.style,
zIndex: this.zIndex,
rectangle: this.rectangle as unknown as Signal<TextRectangle>,
multiCodePointSupport: this.multiCodePointSupport,
overwriteRectangle: this.overwriteRectangle,
});
this.drawnObjects.text = text;
text.draw();
}
}