|
| 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 | +} |
0 commit comments