Skip to content

Commit e45bd65

Browse files
committed
A few fixes and improvements and stuff
1 parent 2157d3a commit e45bd65

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

features/align-to-center/data.json

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"title": "Align to Center",
3-
"description": "Use Control + U / Command + U to center text within the instruction box on projects",
3+
"description": "Allows you to align text in Instructions and Notes & Credits boxes to the center of the input.",
44
"credits": [
55
{
66
"username": "Brass_Glass",
@@ -14,5 +14,23 @@
1414
"type": ["Website"],
1515
"tags": ["New", "Recommended"],
1616
"dynamic": true,
17-
"scripts": [{ "file": "script.js", "runOn": "/projects/*" }]
17+
"scripts": [{ "file": "script.js", "runOn": "/projects/*" }],
18+
"styles": [{ "file": "style.css", "runOn": "/projects/*" }],
19+
"resources": [{ "name": "center-align", "path": "/icon.svg" }],
20+
"options": [{ "id": "use-align-hotkey", "name": "Use Hotkey (Ctrl + U)", "type": "boolean" }],
21+
"components": [
22+
{
23+
"type": "info",
24+
"content": "For Mac users, use Command + U when the hotkey option is enabled to center text.",
25+
"if": {
26+
"type": "all",
27+
"conditions": [
28+
{
29+
"type": "os",
30+
"value": "Macintosh"
31+
}
32+
]
33+
}
34+
}
35+
]
1836
}

features/align-to-center/icon.svg

Lines changed: 1 addition & 0 deletions
Loading

features/align-to-center/script.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ export default async function ({ feature, console }) {
3131
textarea.value = uncenteredLines.join("\n");
3232
}
3333

34-
function centerAlignText() {
34+
function centerAlignText(textarea) {
35+
if (!feature.self.enabled) return;
36+
3537
const form = document.querySelector(".project-notes");
3638
if (form) {
37-
const activeElement = document.activeElement;
39+
const activeElement = textarea || document.activeElement;
3840
if (
3941
activeElement.tagName === "TEXTAREA" &&
4042
form.contains(activeElement)
@@ -46,7 +48,8 @@ export default async function ({ feature, console }) {
4648
const centeredLines = lines.map((line) => {
4749
const textWidth = getTextWidth(line);
4850
const totalSpaces = (availableWidth - textWidth) / spaceWidth / 2;
49-
const spaces = " ".repeat(Math.floor(totalSpaces));
51+
const spaces =
52+
totalSpaces > 0 ? " ".repeat(Math.floor(totalSpaces)) : "";
5053
return spaces + line;
5154
});
5255
activeElement.value = centeredLines.join("\n");
@@ -56,7 +59,38 @@ export default async function ({ feature, console }) {
5659

5760
window.addEventListener("keydown", (event) => {
5861
if ((event.ctrlKey || event.metaKey) && event.key === "u") {
59-
centerAlignText();
62+
if (feature.settings.get("use-align-hotkey")) {
63+
centerAlignText();
64+
}
6065
}
6166
});
67+
68+
console.log("hey");
69+
70+
ScratchTools.waitForElements(
71+
".project-notes .project-textlabel",
72+
function (div) {
73+
if (div.querySelector(".ste-align-center")) return;
74+
75+
let textarea = div.parentElement.querySelector("textarea");
76+
77+
let img = document.createElement("img");
78+
img.src = feature.self.getResource("center-align");
79+
img.className = "ste-align-center";
80+
img.addEventListener("click", function () {
81+
centerAlignText(textarea);
82+
});
83+
feature.self.hideOnDisable(img);
84+
85+
div.appendChild(img);
86+
87+
textarea.addEventListener("focusin", function () {
88+
img.classList.add("show");
89+
});
90+
91+
textarea.addEventListener("focusout", function () {
92+
img.classList.remove("show");
93+
});
94+
}
95+
);
6296
}

features/align-to-center/style.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.project-notes .project-textlabel {
2+
position: relative;
3+
width: 100%;
4+
}
5+
6+
.ste-align-center {
7+
position: absolute;
8+
right: 0px;
9+
top: 0px;
10+
height: 100%;
11+
cursor: pointer;
12+
display: none;
13+
}
14+
15+
.ste-align-center.show {
16+
display: block;
17+
}

0 commit comments

Comments
 (0)