Convert fix-uploaded-svgs addon to a builtin feature

There is no reason to turn this off.
This commit is contained in:
Thomas Weber committed 2021-03-31 18:27:46 -05:00
1 parent 68e0e29d91
commit 786f256bc5
15 files changed
+29 -65

No files matched your search

@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"Korrigiere fehlerhafte SVG-Uploads","fix-uploaded-svgs/@description":"Korrigiert ein Bug im Scratch Editor, wodurch SVG-Dateien von externen Bildeditoren wie Affinity Designer fehlerhaft hochgeladen werden."}
@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"Arreglar archivos SVG subidos","fix-uploaded-svgs/@description":"Arregla un error en el editor de Scratch que provoca que algunos SVG creados con ciertos editores de imagen, como Affinity Designer, no se suban correctamente."}
@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"Règler les exportations échouées des graphismes au format SVG","fix-uploaded-svgs/@description":"Règle un bug dans l'éditeur Scratch qui n'arrivait pas à exporter des fichiers SVG créés avec certains éditeurs d'images correctement, comme Affinity Designer."}
@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"Correggi errore caricamento file SVG","fix-uploaded-svgs/@description":"Risolve un errore nell'editor di Scratch a causa del quale fallisce il caricamento di file SVG creati con alcuni editor di immagini, come ad esempio Affinity Designer."}
@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"SVGアップロードの修正","fix-uploaded-svgs/@description":"SVGアップロード時のバグを修正する。"}
@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"Napraw zepsute przesyłanie plików SVG","fix-uploaded-svgs/@description":"Naprawia błąd w Edytorze Scratcha edytor zawodzi dokładnie przesłać pliki SVG stworzone z niektórymi edytorami zdjęć np. Affinity Designer"}
@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"Consertar uploads de SVG quebrados","fix-uploaded-svgs/@description":"Conserta um bug no editor do Scratch que falha ao fazer upload de arquivos SVG criados com alguns editores de imagem, como o Affinity Designer."}
@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"Repară încărcările SVG stricate","fix-uploaded-svgs/@description":"Repară o eroare în editorul Scratch care eșuează în încărcarea corectă a fișierelor SVG create cu unii editori de imagini, cum ar fi Affinity Designer."}
@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"Popravi nalaganje SVG-jev","fix-uploaded-svgs/@description":"Popravi napako v Scratchevem urejevalniku, ki povzroči, da se datoteke SVG, ustvarjene z nekaterimi urejevalniki, kot je Affinity Designer, ne naložijo pravilno."}
@@ -1 +0,0 @@
{"fix-uploaded-svgs/@name":"Bozuk SVG yüklemelerini düzeltin","fix-uploaded-svgs/@description":"Scratch düzenleyicide Affinity Designer gibi bazı görüntü düzenleyicilerle oluşturulmuş SVG dosyalarını düzgün şekilde yüklemeyen bir hatayı düzeltir."}
-1
View File
@@ -24,7 +24,6 @@
"variable-manager",
"load-extensions",
"tw-interface-customization",
"fix-uploaded-svgs",
"drag-drop",
"cat-blocks"
]
@@ -1,14 +0,0 @@
{
"name": "Fix broken SVG uploads",
"description": "Fixes a bug in the Scratch editor that fails to properly upload SVG files created with some image editors, such as Affinity Designer.",
"userscripts": [
{
"url": "userscript.js",
"matches": ["https://scratch.mit.edu/projects/*"],
"runAtComplete": false
}
],
"tags": ["editor"],
"enabledByDefault": true,
"l10n": true
}
@@ -1,40 +0,0 @@
export default async function ({ addon, global, console }) {
const originalFileReader = window.FileReader;
window.FileReader = function () {
const realFileReader = new originalFileReader();
const readAsArrayBuffer = Symbol();
realFileReader[readAsArrayBuffer] = realFileReader.readAsArrayBuffer;
realFileReader.readAsArrayBuffer = function (file) {
(async () => {
if (file.type === "image/svg+xml") {
try {
let text = await file.text();
const xmlParser = new DOMParser();
const xmlDocument = xmlParser.parseFromString(text, "text/xml");
const svgElement = xmlDocument.children[0];
if (
svgElement.height.baseVal.valueAsString === "100%" &&
svgElement.width.baseVal.valueAsString === "100%"
) {
svgElement.removeAttribute("height");
svgElement.removeAttribute("width");
text = xmlDocument.documentElement.outerHTML;
}
const newFile = new File([text], file.name, {
type: file.type,
lastModified: file.lastModified,
});
realFileReader[readAsArrayBuffer](newFile);
} catch (err) {
console.warn(err);
realFileReader[readAsArrayBuffer](file);
}
} else {
realFileReader[readAsArrayBuffer](file);
}
})();
return undefined;
};
return realFileReader;
};
}
+2
View File
@@ -2,6 +2,7 @@ import {BitmapAdapter} from 'scratch-svg-renderer';
import randomizeSpritePosition from './randomize-sprite-position.js';
import bmpConverter from './bmp-converter';
import gifDecoder from './gif-decoder';
import fixSVG from './tw-svg-fixer';
import twStageSize from './tw-stage-size';
/**
@@ -105,6 +106,7 @@ const costumeUpload = function (fileData, fileType, storage, handleCostume, hand
case 'image/svg+xml': {
costumeFormat = storage.DataFormat.SVG;
assetType = storage.AssetType.ImageVector;
fileData = fixSVG(fileData);
break;
}
case 'image/jpeg': {
+27
View File
@@ -0,0 +1,27 @@
import {TextDecoder, TextEncoder} from './tw-text-encoder';
const fixSVG = fileData => {
// width="100%" and height="100%" on SVGs can break things
// Demo: https://scratch.mit.edu/projects/447085841/
// Based on: https://github.com/ScratchAddons/ScratchAddons/pull/748
try {
const bytes = new Uint8Array(fileData);
const str = new TextDecoder().decode(bytes);
const xmlDocument = new DOMParser().parseFromString(str, 'text/xml');
const svgElement = xmlDocument.children[0];
if (
svgElement.height.baseVal.valueAsString === '100%' &&
svgElement.width.baseVal.valueAsString === '100%'
) {
svgElement.removeAttribute('height');
svgElement.removeAttribute('width');
const fixed = xmlDocument.documentElement.outerHTML;
return new TextEncoder().encode(fixed).buffer;
}
return fileData;
} catch (e) {
return fileData;
}
};
export default fixSVG;