Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions src/io/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,19 @@ class Mouse {
if (!(data.x > 0 && data.x < data.canvasWidth &&
data.y > 0 && data.y < data.canvasHeight)) return;

// target will not exist if project is still loading
const target = this._pickTarget(data.x, data.y);
const isNewMouseDown = !previousDownState && this._isDown;
const isNewMouseUp = previousDownState && !this._isDown;

// Draggable targets start click hats on mouse up.
// Non-draggable targets start click hats on mouse down.
if (target.draggable && isNewMouseUp) {
this._activateClickHats(target);
} else if (!target.draggable && isNewMouseDown) {
this._activateClickHats(target);
if (target) {
const isNewMouseDown = !previousDownState && this._isDown;
const isNewMouseUp = previousDownState && !this._isDown;

// Draggable targets start click hats on mouse up.
// Non-draggable targets start click hats on mouse down.
if (target.draggable && isNewMouseUp) {
this._activateClickHats(target);
} else if (!target.draggable && isNewMouseDown) {
this._activateClickHats(target);
}
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/virtual-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,22 @@ class VirtualMachine extends EventEmitter {
file.date = date;
}

// Tell JSZip to only compress file formats where there will be a significant gain.
const COMPRESSABLE_FORMATS = [
'.json',
'.svg',
'.wav',
'.ttf',
'.otf'
];
for (const file of Object.values(zip.files)) {
if (COMPRESSABLE_FORMATS.some(ext => file.name.endsWith(ext))) {
file.options.compression = 'DEFLATE';
} else {
file.options.compression = 'STORE';
}
}

return zip;
}

Expand All @@ -546,9 +562,9 @@ class VirtualMachine extends EventEmitter {
*/
saveProjectSb3 (type) {
return this._saveProjectZip().generateAsync({
// Don't configure compression here. _saveProjectZip() will set it for each file.
type: type || 'blob',
mimeType: 'application/x.scratch.sb3',
compression: 'DEFLATE'
mimeType: 'application/x.scratch.sb3'
});
}

Expand Down
Binary file added test/fixtures/tw-mixed-file-formats.sb3
Binary file not shown.
31 changes: 31 additions & 0 deletions test/integration/tw_compression_per_file_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const {test} = require('tap');
const fs = require('fs');
const path = require('path');
const VM = require('../../src/virtual-machine');
const makeTestStorage = require('../fixtures/make-test-storage');
const JSZip = require('@turbowarp/jszip');

test('saveProjectSb3() per-file compression', t => {
const vm = new VM();
vm.attachStorage(makeTestStorage());
const fixture = fs.readFileSync(path.join(__dirname, '../fixtures/tw-mixed-file-formats.sb3'));
vm.loadProject(fixture)
.then(() => vm.saveProjectSb3('arraybuffer'))
.then(buffer => JSZip.loadAsync(buffer))
.then(zip => {
const isCompressed = pathInZip => {
// note: uses JSZip private APIs, not very stable, but it should be okay...
const file = zip.files[pathInZip];
return file._data.compression.magic === '\x08\x00';
};

t.ok(isCompressed('project.json'));
t.ok(isCompressed('5cb46ddd903fc2c9976ff881df9273c9.wav'));
t.ok(isCompressed('cd21514d0531fdffb22204e0ec5ed84a.svg'));

t.notOk(isCompressed('0b2e50ca4107ce57416e2ceb840a6347.jpg'));
t.notOk(isCompressed('5c8826d846c06dddeb77590e8792fb7d.png'));

t.end();
});
});
14 changes: 14 additions & 0 deletions test/unit/tw_mouse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const {test} = require('tap');
const Runtime = require('../../src/engine/runtime');

test('isDown does not error before project loads', t => {
const rt = new Runtime();
rt.ioDevices.mouse.postData({
isDown: true,
x: 20,
y: 20,
canvasWidth: 100,
canvasHeight: 100
});
t.end();
});
Loading