Skip to content

Commit d2baa33

Browse files
author
yuri-kiss
authored
Merge pull request #18 from TurboWarp/develop
merge upstream
2 parents 0413de0 + f957e1c commit d2baa33

File tree

6 files changed

+82
-18
lines changed

6 files changed

+82
-18
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/io/mouse.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,19 @@ class Mouse {
101101
if (!(data.x > 0 && data.x < data.canvasWidth &&
102102
data.y > 0 && data.y < data.canvasHeight)) return;
103103

104+
// target will not exist if project is still loading
104105
const target = this._pickTarget(data.x, data.y);
105-
const isNewMouseDown = !previousDownState && this._isDown;
106-
const isNewMouseUp = previousDownState && !this._isDown;
107-
108-
// Draggable targets start click hats on mouse up.
109-
// Non-draggable targets start click hats on mouse down.
110-
if (target.draggable && isNewMouseUp) {
111-
this._activateClickHats(target);
112-
} else if (!target.draggable && isNewMouseDown) {
113-
this._activateClickHats(target);
106+
if (target) {
107+
const isNewMouseDown = !previousDownState && this._isDown;
108+
const isNewMouseUp = previousDownState && !this._isDown;
109+
110+
// Draggable targets start click hats on mouse up.
111+
// Non-draggable targets start click hats on mouse down.
112+
if (target.draggable && isNewMouseUp) {
113+
this._activateClickHats(target);
114+
} else if (!target.draggable && isNewMouseDown) {
115+
this._activateClickHats(target);
116+
}
114117
}
115118
}
116119
}

src/virtual-machine.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,22 @@ class VirtualMachine extends EventEmitter {
539539
file.date = date;
540540
}
541541

542+
// Tell JSZip to only compress file formats where there will be a significant gain.
543+
const COMPRESSABLE_FORMATS = [
544+
'.json',
545+
'.svg',
546+
'.wav',
547+
'.ttf',
548+
'.otf'
549+
];
550+
for (const file of Object.values(zip.files)) {
551+
if (COMPRESSABLE_FORMATS.some(ext => file.name.endsWith(ext))) {
552+
file.options.compression = 'DEFLATE';
553+
} else {
554+
file.options.compression = 'STORE';
555+
}
556+
}
557+
542558
return zip;
543559
}
544560

@@ -548,9 +564,9 @@ class VirtualMachine extends EventEmitter {
548564
*/
549565
saveProjectSb3 (type) {
550566
return this._saveProjectZip().generateAsync({
567+
// Don't configure compression here. _saveProjectZip() will set it for each file.
551568
type: type || 'blob',
552-
mimeType: 'application/x.scratch.sb3',
553-
compression: 'DEFLATE'
569+
mimeType: 'application/x.scratch.sb3'
554570
});
555571
}
556572

117 KB
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const {test} = require('tap');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const VM = require('../../src/virtual-machine');
5+
const makeTestStorage = require('../fixtures/make-test-storage');
6+
const JSZip = require('@turbowarp/jszip');
7+
8+
test('saveProjectSb3() per-file compression', t => {
9+
const vm = new VM();
10+
vm.attachStorage(makeTestStorage());
11+
const fixture = fs.readFileSync(path.join(__dirname, '../fixtures/tw-mixed-file-formats.sb3'));
12+
vm.loadProject(fixture)
13+
.then(() => vm.saveProjectSb3('arraybuffer'))
14+
.then(buffer => JSZip.loadAsync(buffer))
15+
.then(zip => {
16+
const isCompressed = pathInZip => {
17+
// note: uses JSZip private APIs, not very stable, but it should be okay...
18+
const file = zip.files[pathInZip];
19+
return file._data.compression.magic === '\x08\x00';
20+
};
21+
22+
t.ok(isCompressed('project.json'));
23+
t.ok(isCompressed('5cb46ddd903fc2c9976ff881df9273c9.wav'));
24+
t.ok(isCompressed('cd21514d0531fdffb22204e0ec5ed84a.svg'));
25+
26+
t.notOk(isCompressed('0b2e50ca4107ce57416e2ceb840a6347.jpg'));
27+
t.notOk(isCompressed('5c8826d846c06dddeb77590e8792fb7d.png'));
28+
29+
t.end();
30+
});
31+
});

test/unit/tw_mouse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const {test} = require('tap');
2+
const Runtime = require('../../src/engine/runtime');
3+
4+
test('isDown does not error before project loads', t => {
5+
const rt = new Runtime();
6+
rt.ioDevices.mouse.postData({
7+
isDown: true,
8+
x: 20,
9+
y: 20,
10+
canvasWidth: 100,
11+
canvasHeight: 100
12+
});
13+
t.end();
14+
});

0 commit comments

Comments
 (0)