Attach the svg renderer instead of providing it in order to fix VM tests

This commit is contained in:
DD 2018-05-10 11:00:51 -04:00
parent aae1e083bc
commit 589dd7d5e0
4 changed files with 20 additions and 6 deletions

View file

@ -61,11 +61,11 @@
"scratch-parser": "^4.1.0",
"scratch-render": "latest",
"scratch-storage": "^0.4.0",
"scratch-svg-renderer": "0.1.0-prerelease.20180423193917",
"script-loader": "0.7.2",
"socket.io-client": "2.0.4",
"stats.js": "^0.17.0",
"tap": "^11.0.1",
"text-encoding": "0.6.4",
"tiny-worker": "^2.1.1",
"webpack": "^4.8.0",
"webpack-cli": "^2.0.15",

View file

@ -917,6 +917,14 @@ class Runtime extends EventEmitter {
this.renderer = renderer;
}
/**
* Set the svg adapter, which converts scratch 2 svgs to scratch 3 svgs
* @param {!SvgRenderer} svgAdapter The adapter to attach
*/
attachV2SVGAdapter (svgAdapter) {
this.v2SvgAdapter = svgAdapter;
}
/**
* Attach the storage module
* @param {!ScratchStorage} storage The storage module to attach

View file

@ -1,6 +1,5 @@
const StringUtil = require('../util/string-util');
const log = require('../util/log');
const SvgRenderer = require('scratch-svg-renderer').SVGRenderer;
/**
* Initialize a costume from an asset asynchronously.
@ -34,10 +33,9 @@ const loadCostumeFromAsset = function (costume, costumeAsset, runtime, optVersio
if (costumeAsset.assetType === AssetType.ImageVector) {
let svgString = costumeAsset.decodeText();
// SVG Renderer load fixes "quirks" associated with Scratch 2 projects
if (optVersion && optVersion === 2) {
const svgRenderer = new SvgRenderer();
svgRenderer.loadString(svgString);
svgString = svgRenderer.toString();
if (optVersion && optVersion === 2 && runtime.v2SvgAdapter) {
runtime.v2SvgAdapter.loadString(svgString);
svgString = runtime.v2SvgAdapter.toString();
// Put back into storage
const storage = runtime.storage;
costumeAsset.encodeTextData(svgString, storage.DataFormat.SVG);

View file

@ -828,6 +828,14 @@ class VirtualMachine extends EventEmitter {
this.runtime.attachRenderer(renderer);
}
/**
* Set the svg adapter for the VM/runtime, which converts scratch 2 svgs to scratch 3 svgs
* @param {!SvgRenderer} svgAdapter The adapter to attach
*/
attachV2SVGAdapter (svgAdapter) {
this.runtime.attachV2SVGAdapter(svgAdapter);
}
/**
* Set the storage module for the VM/runtime
* @param {!ScratchStorage} storage The storage module to attach