Skip to content

Commit 3d1df47

Browse files
committed
INCOMPLETE version selector
the version selector works but we need to rework the converter code (nbt_or_json.ts)
1 parent a3e222d commit 3d1df47

File tree

4 files changed

+79
-49
lines changed

4 files changed

+79
-49
lines changed

src/lib/stores.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { writable } from "svelte/store";
2+
3+
export const outputVersion = writable({
4+
friendly: "1.21.9+",
5+
description: "'object' type added, allowing you to use non-character sprites",
6+
index: 2
7+
})

src/lib/text/nbt_or_json.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import {
1212
trueMarkOrUndefined,
1313
unescapeUnicode,
1414
} from "./general";
15+
import { outputVersion } from "$lib/stores";
16+
import { get } from "svelte/store";
17+
18+
let exportVersion = get(outputVersion)
1519

1620
const styleProps = [
1721
"color",
@@ -34,14 +38,13 @@ const styleProps = [
3438
* @param current current text component
3539
* @param c the current editor JSON
3640
* @param includeInteractivity should it have click and hover events
37-
* @param exportVersion the version to export to
41+
* @param outputVersion the version to export to
3842
* @returns the current component with new properties
3943
*/
4044
export function addTypeSpecificValues(
4145
current: MinecraftText,
4246
c: JSONContent,
4347
includeInteractivity = true,
44-
exportVersion: "new" | "old" = "new",
4548
) {
4649
switch (c.type) {
4750
case "text":
@@ -84,13 +87,10 @@ export function addTypeSpecificValues(
8487
}
8588

8689
if (includeInteractivity) {
87-
switch (exportVersion) {
88-
case "new":
89-
newApplyInteractiveValues(current, c);
90-
break;
91-
case "old":
92-
oldApplyInteractiveValues(current, c);
93-
break;
90+
if (exportVersion.index >= 1) {
91+
newApplyInteractiveValues(current, c);
92+
} else {
93+
oldApplyInteractiveValues(current, c);
9494
}
9595
}
9696

@@ -315,18 +315,18 @@ export function optimise(arr: StringyMCText[], lore = false): StringyMCText[] {
315315
return out;
316316
}
317317

318+
318319
/**
319320
* Converts the JSON content of the editor to an NBT string.
320321
*/
321322
export function convert(
322323
jsonContent: JSONContent,
323324
exportType: "standard" | "item_lore" = "standard",
324-
exportVersion: "new" | "old" = "new",
325325
optimise: boolean,
326326
force_json: boolean = false,
327327
): string {
328-
let out = translateJSON(jsonContent, { exportVersion, exportType, optimise });
329-
if (exportVersion == "new" && !force_json) {
328+
let out = translateJSON(jsonContent, { exportType, optimise });
329+
if (exportVersion.index >= 1 && !force_json) {
330330
// only remove strings
331331
out = out.replace(/(?<=[{,]\s*)"[^"]*"\s*:/g, (match) =>
332332
match.replace(/"/g, ""),
@@ -372,7 +372,6 @@ export function translateJSON(
372372
current,
373373
c,
374374
true,
375-
options.exportVersion,
376375
);
377376
data.push(current);
378377
}
@@ -422,7 +421,6 @@ export function translateJSON(
422421
currentComponent,
423422
c,
424423
false,
425-
options.exportVersion,
426424
);
427425
currentLine.push(currentComponent);
428426
}

src/lib/types.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
export type Version = {
2+
friendly: string,
3+
description: string,
4+
index: number
5+
}
6+
export const versions = [
7+
{
8+
friendly: "pre-1.21.5",
9+
description: "uses json text components",
10+
index: 0
11+
},
12+
{
13+
friendly: "1.21.5-8",
14+
description: "uses nbt as text components, changes to syntax and names",
15+
index: 1
16+
},
17+
{
18+
friendly: "1.21.9+",
19+
description: "'object' type added, allowing you to use non-character sprites",
20+
index: 2
21+
},
22+
]
123
export type BaseMinecraftText = Pick<
224
MinecraftText,
325
| "text"
@@ -114,7 +136,6 @@ export type MCTextKey = keyof MinecraftText;
114136
export type TranslateOptions = Partial<{
115137
indent: boolean;
116138
indentSize: number;
117-
exportVersion: "old" | "new";
118139
optimise: boolean;
119140
exportType: "standard" | "item_lore";
120141
}>;

src/routes/+page.svelte

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import { openDataStore } from "$lib/db";
5757
import { fontLUT } from "$lib/tiptap/extensions/fonts";
5858
import { tooltip } from "$lib/tooltip";
59+
import { versions, type Version } from "$lib/types";
60+
import { outputVersion } from "$lib/stores";
5961
6062
let tiptapJSON: JSONContent = $state()!;
6163
@@ -65,7 +67,7 @@
6567
let colorDialog: Modal = $state()!;
6668
6769
let outputDialog: Modal = $state()!;
68-
let outputVersion: "new" | "old" = $state("new");
70+
let versionPopup: boolean = $state(false);
6971
7072
let doesContentExist: boolean = $state(false);
7173
let shouldOptimise = $state(true);
@@ -323,21 +325,6 @@
323325
editor!.commands.unsetAllMarks();
324326
}
325327
}
326-
327-
function getTextComponentCount() {
328-
const components = JSON.parse(
329-
translateJSON(editor!.getJSON(), {
330-
exportType: "standard",
331-
indent: false,
332-
exportVersion: outputVersion,
333-
optimise: shouldOptimise,
334-
}),
335-
);
336-
if (Array.isArray(components)) {
337-
return components.length;
338-
}
339-
return 1;
340-
}
341328
</script>
342329

343330
<svelte:window onkeydown={clearMarksHandler} />
@@ -533,7 +520,6 @@
533520
convert(
534521
editor!.getJSON(),
535522
"standard",
536-
outputVersion,
537523
shouldOptimise,
538524
),
539525
);
@@ -550,14 +536,14 @@
550536
<code id="outputbox" class="inline break-all">
551537
<!-- {editor ? translateMOTD(tiptapJSON) : "Loading..."} -->
552538
{editor
553-
? convert(tiptapJSON!, "standard", outputVersion, shouldOptimise)
539+
? convert(tiptapJSON!, "standard", shouldOptimise)
554540
: "Loading..."}
555541
</code>
556542
</p>
557543
</div>
558544
{#if doesContentExist}
559545
<div class="mt-2 flex items-center space-x-2 select-none">
560-
<p class="font-lexend nomob text-xs text-white/60">
546+
<!-- <p class="font-lexend nomob text-xs text-white/60">
561547
{editor
562548
? convert(tiptapJSON!, "standard", outputVersion, shouldOptimise)
563549
.length
@@ -567,23 +553,41 @@
567553
{getTextComponentCount()} components
568554
</p>
569555
570-
<p class="font-lexend nomob text-xs text-white/60">•</p>
556+
<p class="font-lexend nomob text-xs text-white/60">•</p> -->
571557

572558
<p class="font-lexend text-xs text-white/60">
573559
click to change output settings:
574560
</p>
575-
<button
576-
{@attach tooltip}
577-
class="ml-1 rounded-md bg-zinc-800 px-1 font-mono select-none hover:bg-zinc-700"
578-
aria-label="Click to toggle the output version. 1.21.5 drastically changed the format of text components, so make sure you select the correct version."
579-
onclick={() => {
580-
const ov = outputVersion;
581-
if (ov == "new") {
582-
outputVersion = "old";
583-
} else {
584-
outputVersion = "new";
585-
}
586-
}}>{outputVersion == "new" ? "1.21.5+" : "pre 1.21.5"}</button>
561+
562+
<div class="relative inline-block">
563+
{#if versionPopup}
564+
<div class="absolute bottom-full mb-2 left-1/2 -translate-x-1/2 bg-zinc-900 w-[400px] shadow-md shadow-zinc-950 px-2 py-2 rounded-md z-10 flex flex-col space-y-1">
565+
<div class="flex items-center ml-[0.3rem]">
566+
<span class="w-1/4 text-sm">version</span>
567+
<span class="w-3/4 text-sm">description</span>
568+
</div>
569+
{#each versions as v}
570+
<button
571+
class="rounded-md bg-zinc-800 p-2 select-none hover:bg-zinc-700 text-left flex items-center w-full"
572+
onclick={() => {
573+
$outputVersion = v;
574+
versionPopup = false;
575+
}}>
576+
<b class="w-1/4">{v.friendly}</b>
577+
<span class="w-3/4 text-xs">{v.description}</span>
578+
</button>
579+
{/each}
580+
</div>
581+
{/if}
582+
583+
<button
584+
class="ml-1 rounded-md bg-zinc-800 px-1 font-mono select-none hover:bg-zinc-700"
585+
aria-label="Click to toggle the output version."
586+
onclick={() => {
587+
versionPopup = !versionPopup;
588+
}}>{$outputVersion.friendly}</button>
589+
</div>
590+
587591
<button
588592
{@attach tooltip}
589593
class="ml-1 rounded-md bg-zinc-800 px-1 font-mono select-none hover:bg-zinc-700"
@@ -721,13 +725,13 @@
721725
</div>
722726
</Modal>
723727

724-
{#await import("$lib/components/modals/ExportModal.svelte") then modal}
728+
<!-- {#await import("$lib/components/modals/ExportModal.svelte") then modal}
725729
<modal.default
726730
bind:outputDialog
727731
bind:outputVersion
728732
{editor}
729733
{recentlyCopied} />
730-
{/await}
734+
{/await} -->
731735

732736
<Modal title="Import from NBT" bind:this={importDialog} key="I">
733737
<div class="flex w-full flex-col space-y-2">

0 commit comments

Comments
 (0)