Skip to content

Commit 218ba9a

Browse files
committed
version selector now works as expected
1 parent 3d1df47 commit 218ba9a

File tree

6 files changed

+107
-56
lines changed

6 files changed

+107
-56
lines changed

src/lib/components/modals/CustomSourceModal.svelte

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<script lang="ts">
22
import Modal from "$lib/components/Modal.svelte";
33
import type { ExternalSources } from "$lib/types";
4+
45
import IconScore from "~icons/tabler/123";
56
import IconSelector from "~icons/tabler/at";
67
import IconNBT from "~icons/tabler/braces";
78
import IconKeybind from "~icons/tabler/keyboard";
89
import IconTranslate from "~icons/tabler/language";
10+
import IconObject from "~icons/tabler/box";
11+
912
import CheckBox from "../CheckBox.svelte";
1013
import MiniEditor from "../text/MiniEditor.svelte";
1114
import Combobox from "../Combobox.svelte";
15+
import { outputVersion } from "$lib/stores";
1216
1317
let {
1418
customDialog = $bindable(),
@@ -106,14 +110,16 @@
106110
<IconKeybind class="text-2xl" />
107111
<span>Keybind</span>
108112
</button>
113+
{#if $outputVersion.index >= 2}
109114
<button
110115
class="flex h-full w-full flex-col items-center space-y-2 rounded-md bg-zinc-900 p-3 hover:bg-black/50"
111116
onclick={() => {
112117
customType = "object";
113118
}}>
114-
<IconKeybind class="text-2xl" />
115-
<span>Object (1.22+)</span>
119+
<IconObject class="text-2xl" />
120+
<span>Object</span>
116121
</button>
122+
{/if}
117123
</div>
118124
{:else}
119125
<select bind:value={customType} class="rounded-md bg-zinc-900 p-2">
@@ -122,7 +128,7 @@
122128
<option value="nbt">NBT Value</option>
123129
<option value="selector">Selector</option>
124130
<option value="keybind">Keybind</option>
125-
<option value="object">Object (1.22+)</option>
131+
<option value="object">Object</option>
126132
</select>
127133
{/if}
128134

src/lib/components/modals/ExportModal.svelte

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import { convert, translateJSON } from "$lib/text/nbt_or_json";
55
import IconCopy from "~icons/tabler/copy";
66
import CheckBox from "../CheckBox.svelte";
7+
import { outputVersion } from "$lib/stores";
78
let {
89
outputDialog = $bindable(),
9-
outputVersion = $bindable(),
1010
editor,
1111
recentlyCopied,
1212
shouldOptimise = true,
@@ -16,21 +16,16 @@
1616
</script>
1717

1818
<Modal title="More output formats" bind:this={outputDialog} big key="E">
19-
<p>Select a Minecraft version:</p>
20-
<select bind:value={outputVersion} class="w-fit rounded-md bg-zinc-900 p-2">
21-
<option value="new">1.21.5+</option>
22-
<option value="old">Before 1.21.5</option>
23-
</select>
2419
<div class="flex w-full flex-col">
25-
{#if outputVersion == "new"}
20+
{#if $outputVersion.index > 0}
2621
<div class="mt-1 flex items-center space-x-2">
2722
<CheckBox bind:value={exportAsJSON} label="json" />
2823
<span>Toggle JSON mode (for use in json files)</span>
2924
</div>
3025
{/if}
3126

3227
<p class="mt-2">
33-
As {outputVersion == "new" ? " " : "JSON "}text components:
28+
As {$outputVersion.index > 0 ? " " : "JSON "}text components:
3429
</p>
3530
<div class="flex items-start space-x-3 rounded-lg bg-zinc-950 p-3">
3631
<button
@@ -40,7 +35,6 @@
4035
convert(
4136
editor.getJSON(),
4237
"standard",
43-
outputVersion,
4438
shouldOptimise,
4539
exportAsJSON,
4640
),
@@ -55,7 +49,6 @@
5549
? convert(
5650
editor.getJSON(),
5751
"standard",
58-
outputVersion,
5952
shouldOptimise,
6053
exportAsJSON,
6154
)
@@ -69,7 +62,7 @@
6962
class="rounded-md p-1 text-lg font-medium hover:bg-zinc-900 active:bg-white/10"
7063
onclick={() => {
7164
navigator.clipboard.writeText(
72-
`[lore=${convert(editor.getJSON(), "item_lore", outputVersion, shouldOptimise, exportAsJSON)}]`,
65+
`[lore=${convert(editor.getJSON(), "item_lore", shouldOptimise, exportAsJSON)}]`,
7366
);
7467
recentlyCopied = true;
7568
setTimeout(() => (recentlyCopied = false), 2000);
@@ -81,7 +74,6 @@
8174
? convert(
8275
editor.getJSON(),
8376
"item_lore",
84-
outputVersion,
8577
shouldOptimise,
8678
exportAsJSON,
8779
)

src/lib/stores.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { writable } from "svelte/store";
2+
import { versions } from "./types";
23

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-
})
4+
export const outputVersion = writable(versions[versions.length - 1])

src/lib/text/nbt_or_json.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export function addTypeSpecificValues(
4646
c: JSONContent,
4747
includeInteractivity = true,
4848
) {
49+
exportVersion = get(outputVersion)
50+
4951
switch (c.type) {
5052
case "text":
5153
current.text = unescapeUnicode(c.text!);
@@ -78,8 +80,15 @@ export function addTypeSpecificValues(
7880
current.keybind = c.attrs?.key;
7981
break;
8082
case "object":
81-
current.atlas = c.attrs?.atlas;
82-
current.sprite = c.attrs?.sprite;
83+
if (exportVersion.index >= 2) {
84+
current.atlas = c.attrs?.atlas;
85+
current.sprite = c.attrs?.sprite;
86+
87+
current.bold = undefined;
88+
current.italic = undefined;
89+
} else {
90+
current.text = ""
91+
}
8392
break;
8493
case "selector":
8594
current.selector = c.attrs?.selector;
@@ -325,7 +334,9 @@ export function convert(
325334
optimise: boolean,
326335
force_json: boolean = false,
327336
): string {
337+
exportVersion = get(outputVersion)
328338
let out = translateJSON(jsonContent, { exportType, optimise });
339+
console.log(exportVersion)
329340
if (exportVersion.index >= 1 && !force_json) {
330341
// only remove strings
331342
out = out.replace(/(?<=[{,]\s*)"[^"]*"\s*:/g, (match) =>

src/lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const versions = [
1515
index: 1
1616
},
1717
{
18-
friendly: "1.21.9+",
18+
friendly: "1.21.9*",
1919
description: "'object' type added, allowing you to use non-character sprites",
2020
index: 2
2121
},

src/routes/+page.svelte

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,51 @@
325325
editor!.commands.unsetAllMarks();
326326
}
327327
}
328+
329+
function removeAllNodes(editor: Editor | undefined, type: string) {
330+
if (!editor) { return; }
331+
editor.commands.command(({ tr, state }) => {
332+
const nodeType = state.schema.nodes[type];
333+
if (!nodeType) {
334+
console.warn(`Node type "${type}" not found in schema.`);
335+
return false;
336+
}
337+
338+
state.doc.descendants((node, pos) => {
339+
if (node.type === nodeType) {
340+
tr.delete(pos, pos + node.nodeSize);
341+
}
342+
return true;
343+
});
344+
345+
editor.view.dispatch(tr);
346+
return true;
347+
});
348+
}
349+
350+
let versionPopupConfirmationVisible = $state(false);
351+
let temporaryVersionConfirmation: Version | undefined = $state()
352+
353+
function changeOuptutVersion(version: Version | undefined, confirm = false) {
354+
if (!version) { return; }
355+
356+
if ($outputVersion.index > version.index && confirm == false) {
357+
versionPopupConfirmationVisible = true;
358+
temporaryVersionConfirmation = version
359+
return;
360+
}
361+
362+
outputVersion.set(version)
363+
versionPopup = false;
364+
versionPopupConfirmationVisible = false;
365+
temporaryVersionConfirmation = undefined;
366+
367+
if (version.index < 2) { // remove object keys
368+
removeAllNodes(editor, "object")
369+
}
370+
371+
tiptapJSON = editor!.getJSON();
372+
}
328373
</script>
329374

330375
<svelte:window onkeydown={clearMarksHandler} />
@@ -543,49 +588,50 @@
543588
</div>
544589
{#if doesContentExist}
545590
<div class="mt-2 flex items-center space-x-2 select-none">
546-
<!-- <p class="font-lexend nomob text-xs text-white/60">
547-
{editor
548-
? convert(tiptapJSON!, "standard", outputVersion, shouldOptimise)
549-
.length
550-
: 0} characters
551-
</p>
552-
<p class="font-lexend nomob text-xs text-white/60">
553-
{getTextComponentCount()} components
554-
</p>
555-
556-
<p class="font-lexend nomob text-xs text-white/60">•</p> -->
557-
558591
<p class="font-lexend text-xs text-white/60">
559592
click to change output settings:
560593
</p>
561594

562595
<div class="relative inline-block">
563596
{#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>
597+
<div class="absolute bottom-full mb-2 left-1/2 -translate-x-1/2 bg-zinc-900 w-[400px] shadow-md shadow-zinc-950 rounded-md z-10 flex flex-col space-y-1">
598+
{#if versionPopupConfirmationVisible}
599+
<div class="absolute w-full h-full bg-zinc-900 rounded-md px-4 py-4 backdrop-blur-md flex flex-col items-center">
600+
<div class="m-auto flex flex-col">
601+
<b>Warning:</b>
602+
<span>Changing to an earlier version could remove some elements of your text that are unsupported in this version.</span>
603+
<div class="flex space-x-2 mt-2">
604+
<button class="bg-zinc-800 px-2 py-1 rounded-md hover:bg-zinc-700" onclick={() => changeOuptutVersion(temporaryVersionConfirmation, true)}>Change version</button>
605+
<button class="bg-zinc-800 px-2 py-1 rounded-md hover:bg-zinc-700" onclick={() => {
606+
versionPopupConfirmationVisible = false;
607+
temporaryVersionConfirmation = undefined;
608+
}}>Cancel</button>
609+
</div>
610+
</div>
611+
</div>
612+
{/if}
613+
<div class="px-2 py-2 space-y-1">
614+
<div class="flex items-center ml-[0.3rem]">
615+
<span class="w-1/4 text-sm">version</span>
616+
<span class="w-3/4 text-sm">description</span>
617+
</div>
618+
{#each versions as v}
619+
<button
620+
class="rounded-md bg-zinc-800 p-2 select-none hover:bg-zinc-700 text-left flex items-center w-full"
621+
onclick={() => changeOuptutVersion(v)}>
622+
<b class="w-1/4">{v.friendly}</b>
623+
<span class="w-3/4 text-xs">{v.description}</span>
624+
</button>
625+
{/each}
626+
<span class="ml-[0.3rem] text-xs text-zinc-400">* unreleased minecraft version</span>
568627
</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}
580628
</div>
581629
{/if}
582630

583631
<button
584632
class="ml-1 rounded-md bg-zinc-800 px-1 font-mono select-none hover:bg-zinc-700"
585633
aria-label="Click to toggle the output version."
586-
onclick={() => {
587-
versionPopup = !versionPopup;
588-
}}>{$outputVersion.friendly}</button>
634+
onclick={() => {versionPopup = !versionPopup}}>{$outputVersion.friendly}</button>
589635
</div>
590636

591637
<button
@@ -725,13 +771,12 @@
725771
</div>
726772
</Modal>
727773

728-
<!-- {#await import("$lib/components/modals/ExportModal.svelte") then modal}
774+
{#await import("$lib/components/modals/ExportModal.svelte") then modal}
729775
<modal.default
730776
bind:outputDialog
731-
bind:outputVersion
732777
{editor}
733778
{recentlyCopied} />
734-
{/await} -->
779+
{/await}
735780

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

0 commit comments

Comments
 (0)