File size: 2,629 Bytes
5193146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { app } from "../../../scripts/app.js";
import { $el } from "../../../scripts/ui.js";
import { ConfigSetting, addJNodesSetting } from "./common/SettingsManager.js"

class CustomizationConfigSetting extends ConfigSetting {
	constructor(settingName, defaultValue) {
        super("Customization." + settingName, defaultValue);
    }
}

export let setting_FontSize = new CustomizationConfigSetting("MultilineText.Font.Size", 80);
export let setting_FontFamily = new CustomizationConfigSetting("MultilineText.Font.Family", 'monospace');

function setTextAreaFontSize(textarea, size) {
	textarea.style.fontSize = size.toString() + "%";
}

function setTextAreaFontFamily(textarea, fontFamily) {
	textarea.style.fontFamily = fontFamily;
}

function setFontOnGivenTextArea(textarea) {
	setTextAreaFontSize(textarea,setting_FontSize.value);
	setTextAreaFontFamily(textarea, setting_FontFamily.value);
}

function setFontOnAllTextAreas() {
	const textareas = document.querySelectorAll('textarea');

	for (const textarea of textareas) {
		setFontOnGivenTextArea(textarea);
	}
}

app.registerExtension({
	name: "JNodes.Customization.MultilineText.Font",
	async loadedGraphNode(node) {

		for (const wid in node.widgets) {
			if (node.widgets[wid]?.element?.type == 'textarea') {
				setFontOnGivenTextArea(node.widgets[wid]?.element);
			}
		}
	},
	async setup() {

		// Font Family Setting
		{
			const supportedTypefaces =
				[
					'monospace', 'Arial, sans-serif', 'Lora',
				];

			const labelWidget = $el("label", {
				textContent: "Multiline Text Font Family:",
			});

			const settingWidget = $el(
				"select",
				{
					oninput: (e) => {
						setting_FontFamily.value = e.target.value;
						setFontOnAllTextAreas();
					},
				},
				supportedTypefaces.map((m) =>
					$el("option", {
						value: m,
						textContent: m,
						selected: setting_FontFamily.value === m,
					})
				)
			);

			const tooltip =
				"The font typeface to use for multiline text boxes";
			addJNodesSetting(labelWidget, settingWidget, tooltip);
		}

		// Font Size Setting
		{
			const labelWidget = $el("label", {
				textContent: "Multiline Text Font Size (%):",
			});

			const settingWidget = $el("input", {
				type: "number",
				min: "1",
				value: setting_FontSize.value,
				oninput: (e) => {
					let value = e.target.valueAsNumber;
					if (value <= 1.0) { value = 1.0; }
					setting_FontSize.value = value;
					setFontOnAllTextAreas();
				},
			});

			const tooltip =
				"The font size (expressed as a percentage) to use for multiline text boxes";
			addJNodesSetting(labelWidget, settingWidget, tooltip);
		}
	}
});