Skip to content

Commit a9ec980

Browse files
authored
Merge branch 'main' into center-text
2 parents e45bd65 + 42863d7 commit a9ec980

File tree

15 files changed

+10475
-67
lines changed

15 files changed

+10475
-67
lines changed

api/modal.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,17 @@
7575

7676
.st-modal button:hover {
7777
top: .4rem !important;
78+
}
79+
80+
.st-modal button.ste-modal-cancel-btn {
81+
background-color: #c9c9c9 !important;
82+
}
83+
84+
.st-modal input {
85+
padding: 0.5rem !important;
86+
outline: none !important;
87+
border-radius: 0.25rem !important;
88+
border: 0px !important;
89+
background-color: #e3e3e3;
90+
margin-right: .5rem;
7891
}

api/modals.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ ScratchTools.modals = {
3838
});
3939

4040
var closeButton = document.createElement("button");
41-
closeButton.textContent = "Close";
41+
closeButton.textContent = data.cancel ? "Cancel" : "Close";
42+
closeButton.className = data.cancel ? "ste-modal-cancel-btn" : ""
4243
closeButton.onclick = function () {
4344
div.remove();
4445
};

extras/popup/popup.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ span.new-feature-tag.beta {
496496
border-inline-end: 1.5px solid var(--feature-input-bg);
497497
}
498498

499-
.option label {
499+
.option label:not(.special-switch) {
500500
margin-right: 1rem;
501501
}
502502

extras/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ body {
875875
border-inline-end: 1.5px solid var(--feature-input-bg);
876876
}
877877

878-
.option label {
878+
.option label:not(.special-switch) {
879879
margin-right: 1rem;
880880
}
881881

features/features.json

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"id": "align-to-center",
55
"versionAdded": "v4.0.0"
66
},
7+
{
8+
"version": 2,
9+
"id": "more-editor-fonts",
10+
"versionAdded": "v4.0.0"
11+
},
712
{
813
"version": 2,
914
"id": "upload-thumbnail",
@@ -515,7 +520,7 @@
515520
{
516521
"title": "Minimized Remix Credits",
517522
"description": "The remix credit boxes for projects take space from the project Instructions, so this makes the box smaller.",
518-
"credits": ["callumjt", "rgantzos"],
523+
"credits": ["cally", "rgantzos"],
519524
"urls": [
520525
"https://scratch.mit.edu/users/callumjt/",
521526
"https://scratch.mit.edu/users/rgantzos/"
@@ -711,18 +716,6 @@
711716
"type": ["Website"],
712717
"dynamic": true
713718
},
714-
{
715-
"title": "More Editor Fonts",
716-
"description": "Adds more fonts to choose from in the paint editor. They look nicer and are more modern.",
717-
"credits": ["rgantzos", "Lasted10"],
718-
"urls": [
719-
"https://scratch.mit.edu/users/rgantzos/",
720-
"https://scratch.mit.edu/users/Lasted10_Forever/"
721-
],
722-
"file": "special-editor-fonts",
723-
"tags": ["Recommended", "Beta"],
724-
"type": ["Editor"]
725-
},
726719
{
727720
"title": "Display Project Tags",
728721
"description": "Lists all of the tags used in the project description right below the project notes and credits.",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"title": "More Paint Editor Fonts",
3+
"description": "Allows you to use dozens of extra fonts in the paint editor.",
4+
"credits": [
5+
{
6+
"username": "Sanjang_Beta",
7+
"url": "https://scratch.mit.edu/users/Sanjang_Beta/"
8+
},
9+
{ "username": "rgantzos", "url": "https://scratch.mit.edu/users/rgantzos/" }
10+
],
11+
"type": ["Editor"],
12+
"tags": ["New", "Featured"],
13+
"scripts": [{ "file": "script.js", "runOn": "/projects/*" }],
14+
"styles": [{ "file": "style.css", "runOn": "/projects/*" }],
15+
"resources": [{ "name": "more-text-icon", "path": "/text.svg" }]
16+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
export default async function ({ feature, console }) {
2+
let { default: openTypeDefault } = await import(
3+
"../../libraries/opentype.js"
4+
);
5+
openTypeDefault();
6+
7+
let fonts = await (await fetch(feature.server.endpoint("/fonts/"))).json();
8+
9+
feature.page.waitForElements(
10+
"div[class^='asset-panel_wrapper_'] div[class^='action-menu_more-buttons_']",
11+
function (menu) {
12+
if (menu.querySelector(".ste-more-fonts")) return;
13+
14+
let div = document.createElement("div");
15+
div.className = "ste-more-fonts";
16+
feature.self.hideOnDisable(div);
17+
18+
let original =
19+
menu.parentElement.previousElementSibling.previousElementSibling;
20+
let id = original.getAttribute("aria-label").replace(/\s+/g, "_");
21+
22+
let button = document.createElement("button");
23+
button.dataset.tip = "Add Font";
24+
button.dataset.for = `ste-${id}-Add Font`;
25+
button.currentitem = false;
26+
button.ariaLabel = "Add Font";
27+
button.className =
28+
"action-menu_button_1qbot action-menu_more-button_1fMGZ ste-more-fonts-btn";
29+
div.appendChild(button);
30+
31+
let img = Object.assign(document.createElement("img"), {
32+
src: feature.self.getResource("more-text-icon"),
33+
draggable: false,
34+
className: "action-menu_more-icon_TJUQ7",
35+
width: 10,
36+
});
37+
button.appendChild(img);
38+
39+
let tooltip = Object.assign(document.createElement("div"), {
40+
className:
41+
"__react_component_tooltip place-right type-dark action-menu_tooltip_3Bkh5",
42+
id: `ste-${id}-Add Font`,
43+
textContent: "Add Font",
44+
});
45+
tooltip.dataset.id = "tooltip";
46+
div.appendChild(tooltip);
47+
48+
menu.prepend(div);
49+
50+
button.addEventListener("click", function () {
51+
let div = document.createElement("div");
52+
div.className = "ste-font-options";
53+
54+
let modal = ScratchTools.modals.create({
55+
title: "Pick Font",
56+
description:
57+
"You can pick a font from the list below to add to the project.",
58+
components: [
59+
{
60+
type: "html",
61+
content: div,
62+
},
63+
],
64+
cancel: true,
65+
});
66+
67+
for (var i in fonts) {
68+
let span = document.createElement("span");
69+
span.className = "ste-font-option";
70+
span.dataset.font = fonts[i];
71+
72+
let img = document.createElement("img");
73+
img.src = feature.server.endpoint(`/font/image/${fonts[i]}/`);
74+
span.appendChild(img);
75+
76+
span.addEventListener("click", function () {
77+
let font = this.dataset.font;
78+
modal.close();
79+
80+
let button = document.createElement("button");
81+
button.textContent = "Continue";
82+
83+
let typeModal = ScratchTools.modals.create({
84+
title: "Type Text",
85+
description: "Type the text you would like to add.",
86+
cancel: true,
87+
components: [
88+
{
89+
type: "html",
90+
content: Object.assign(document.createElement("input"), {
91+
className: "ste-font-input",
92+
}),
93+
},
94+
{
95+
type: "html",
96+
content: button,
97+
},
98+
{
99+
type: "html",
100+
content: document.createElement("br"),
101+
},
102+
],
103+
});
104+
105+
button.addEventListener("click", function () {
106+
let text = document.querySelector(".ste-font-input").value;
107+
typeModal.close();
108+
109+
setFont(font, text);
110+
});
111+
});
112+
113+
div.appendChild(span);
114+
}
115+
});
116+
117+
let observer = new MutationObserver(doresize);
118+
observer.observe(menu, { attributes: true, subtree: true });
119+
120+
function doresize() {
121+
let rect = div.getBoundingClientRect();
122+
tooltip.style.top = rect.top + 2 + "px";
123+
tooltip.style.left = rect.left + rect.width + "px";
124+
}
125+
}
126+
);
127+
128+
function setFont(font, text) {
129+
async function fetchFont(url) {
130+
const response = await fetch(url);
131+
if (!response.ok) throw new Error("Failed to fetch font");
132+
const arrayBuffer = await response.arrayBuffer();
133+
return arrayBuffer;
134+
}
135+
136+
function createSVGFromText(font, text) {
137+
let width = font.getAdvanceWidth(text, 72);
138+
const path = font.getPath(text, 0, 150, 72);
139+
const svgPath = path.toSVG();
140+
const svg = `
141+
<svg xmlns="http://www.w3.org/2000/svg" width="${width.toString()}" height="200">
142+
${svgPath}
143+
</svg>
144+
`;
145+
return svg;
146+
}
147+
148+
async function generateSVGText(url, text) {
149+
try {
150+
const fontArrayBuffer = await fetchFont(url);
151+
const font = opentype.parse(fontArrayBuffer);
152+
const svgText = createSVGFromText(font, text);
153+
addCostume(svgText);
154+
} catch (error) {
155+
console.error("Error:" + error);
156+
}
157+
}
158+
159+
generateSVGText(feature.server.endpoint(`/font/${font}.ttf`), text);
160+
}
161+
162+
window.setFont = setFont;
163+
164+
function addCostume(svg) {
165+
let fileInput = document.querySelector("input[type=file]");
166+
167+
const blob = new Blob([svg], { type: "image/svg+xml" });
168+
const url = URL.createObjectURL(blob);
169+
170+
const file = new File([blob], "text.svg", { type: "image/svg+xml" });
171+
172+
const dataTransfer = new DataTransfer();
173+
dataTransfer.items.add(file);
174+
fileInput.files = dataTransfer.files;
175+
176+
fileInput.dispatchEvent(new Event("change", { bubbles: true }));
177+
}
178+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[data-for*="Add Font"]:hover + .__react_component_tooltip {
2+
visibility: visible;
3+
}
4+
5+
.ste-more-fonts-btn + .__react_component_tooltip {
6+
left: auto;
7+
}
8+
9+
span.ste-font-option:nth-child(even) {
10+
background-color: #e9e9e9;
11+
}
12+
13+
span.ste-font-option {
14+
display: block;
15+
padding-left: .5rem;
16+
border-radius: .5rem;
17+
cursor: pointer;
18+
height: 3rem;
19+
margin-bottom: .25rem;
20+
margin-top: .25rem;
21+
}
22+
23+
span.ste-font-option:hover {
24+
background-color: #ff9f00;
25+
}
26+
27+
span.ste-font-option:hover img {
28+
filter: invert(1);
29+
}
30+
31+
span.ste-font-option img {
32+
height: 3rem;
33+
}
34+
35+
.ste-font-options {
36+
height: 11rem;
37+
overflow: auto;
38+
}
Lines changed: 24 additions & 0 deletions
Loading

features/project-bar/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Continue to view the information for a project, even after you scroll down past the notes and credits.",
44
"credits": [
55
{
6-
"username": "callumjt",
6+
"username": "cally",
77
"url": "https://scratch.mit.edu/users/callumjt/"
88
},
99
{

0 commit comments

Comments
 (0)