Skip to content

Commit a79ee27

Browse files
committed
feat: major editor updates
1 parent 0f74ad6 commit a79ee27

18 files changed

+67
-354
lines changed

bun.lock

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@steeze-ui/tabler-icons": "^3.3.1",
5151
"@sveltejs/adapter-static": "^3.0.9",
5252
"@types/golang-wasm-exec": "^1.15.2",
53+
"@zip.js/zip.js": "^2.8.2",
5354
"codemirror": "^6.0.2",
5455
"deepslate": "^0.24.0",
5556
"es-toolkit": "^1.39.10",

src/lib/MMSProject.svelte.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Go, type FileTreeLike, type MmsSymbol } from '@minecraftmetascript/mms-wasm';
22
import MMSWasm from '@minecraftmetascript/mms-wasm/dist/main.wasm?init';
33
import * as deepslate from 'deepslate';
4-
4+
import * as zip from '@zip.js/zip.js';
55
export class MMSFile {
66
private _content: string;
77
get content() {
@@ -36,6 +36,30 @@ export class MMSFile {
3636

3737
export class MMSProject {
3838
private readonly goInstance: Go;
39+
async download() {
40+
const zipFileWriter = new zip.BlobWriter();
41+
const zipWriter = new zip.ZipWriter(zipFileWriter);
42+
const addLeaves = (root: FileTreeLike, path: string[] = []) => {
43+
if (root.isDir) {
44+
for (const [, child] of Object.entries(root.children ?? {}))
45+
addLeaves(child, [...path, root.name]);
46+
} else {
47+
zipWriter.add([...path, root.name].join('/'), new zip.TextReader(JSON.stringify(root)));
48+
}
49+
};
50+
if (!this._fs) return;
51+
addLeaves(this._fs);
52+
await zipWriter.close();
53+
54+
const zipFileBlob = await zipFileWriter.getData();
55+
const url = URL.createObjectURL(zipFileBlob);
56+
const a = document.createElement('a');
57+
a.href = url;
58+
a.download = 'project.zip';
59+
a.click();
60+
URL.revokeObjectURL(url);
61+
a.remove();
62+
}
3963

4064
async init(signal?: AbortSignal) {
4165
const wasmInstance = await MMSWasm(this.goInstance.importObject);
@@ -45,7 +69,7 @@ export class MMSProject {
4569
signal?.addEventListener('abort', () => {
4670
console.log('Abort Signal Received. Quitting MMS');
4771
this.goInstance.exit(1);
48-
});
72+
});
4973
}
5074

5175
constructor() {

src/lib/deepslate/DeepslateRenderer.svelte

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/lib/deepslate/offload/DeepslateRendererOffloaded.svelte renamed to src/lib/deepslate/DeepslateRendererOffloaded.svelte

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
} from './proto';
1414
import { parse } from 'valibot';
1515
import { debounce, throttle } from 'es-toolkit';
16-
import { useEditorContext } from '../../editor/MMSEditor.svelte';
16+
import { useEditorContext } from '../editor/MMSEditor.svelte';
1717
import { Icon } from '@steeze-ui/svelte-icon';
1818
import {
1919
ArrowDown,
@@ -36,7 +36,6 @@
3636
worker.postMessage(parse(DeepslateRenderWorkerMessageSchema, m), opts);
3737
let parent: HTMLElement = canvas;
3838
while (parent !== document.body && !('previewRoot' in parent.dataset)) {
39-
console.log(parent.dataset);
4039
parent = parent.parentElement!;
4140
}
4241
@@ -140,7 +139,15 @@
140139
};
141140
};
142141
143-
let scale = $state(64);
142+
let scale = $state(8);
143+
144+
const updateScale = throttle((delta: number) => {
145+
let next = scale;
146+
next += delta;
147+
next -= next % 2;
148+
if (next < 2) next = 1;
149+
scale = next;
150+
}, 50);
144151
let originX = $state(0);
145152
let originY = $state(0);
146153
let worldHeight = $state(256);
@@ -223,21 +230,13 @@
223230
</span>
224231
<button
225232
class="bg-slate-600/50 text-slate-50 hover:bg-slate-600"
226-
onclick={(e) => (e.shiftKey ? (scale += 6) : (scale += 2))}
233+
onclick={(e) => updateScale(e.shiftKey ? 6 : 2)}
227234
>
228235
<Icon src={Plus} class="w-6" />
229236
</button>
230237
<button
231238
class="bg-slate-600/50 text-slate-50 hover:bg-slate-600"
232-
onclick={(e) => {
233-
if (e.shiftKey) {
234-
if (scale > 12) scale -= 6;
235-
else if (scale > 6) scale = 6;
236-
else scale = 2;
237-
} else {
238-
if (scale > 2) scale -= 2;
239-
}
240-
}}
239+
onclick={(e) => updateScale(e.shiftKey ? -6 : -2)}
241240
>
242241
<Icon src={Minus} class="w-6" />
243242
</button>
@@ -290,6 +289,12 @@
290289

291290
<canvas
292291
use:ds
292+
onmousewheel={(e: WheelEvent) => {
293+
e.preventDefault();
294+
295+
if (e.deltaY > 0) updateScale(-2);
296+
else updateScale(2);
297+
}}
293298
onmousemove={updateMousePos}
294299
ondblclick={toggleMouseLock}
295300
onmousedown={dragStart}

src/lib/deepslate/deepslate.svelte.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/lib/deepslate/offload/deepslate_render_worker.ts renamed to src/lib/deepslate/deepslate_render_worker.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,15 @@ const main = (e: MessageEvent) => {
173173

174174
if (markerPosition) {
175175
requestAnimationFrame(() => {
176-
requestAnimationFrame(() => {
177-
if (!markerPosition) return;
178-
ctx.fillStyle = 'rgb(255,0,0)';
179-
const pos = coordinateToPixel(
180-
markerPosition,
181-
origin,
182-
{ w: canvas.width, h: canvas.height },
183-
scale
184-
);
185-
console.log(pos.x % scale, pos.y % scale);
186-
ctx.fillRect(pos.x, pos.y, scale, scale);
187-
});
176+
if (!markerPosition) return;
177+
ctx.fillStyle = 'rgb(255,0,0)';
178+
const pos = coordinateToPixel(
179+
markerPosition,
180+
origin,
181+
{ w: canvas.width, h: canvas.height },
182+
scale
183+
);
184+
ctx.fillRect(pos.x, pos.y, scale, scale);
188185
});
189186
}
190187
}

0 commit comments

Comments
 (0)