Skip to content

Commit a411ca0

Browse files
committed
half finished bug fixes
1 parent 5616f12 commit a411ca0

File tree

6 files changed

+61
-70
lines changed

6 files changed

+61
-70
lines changed

src/lib/components/text/MiniEditor.svelte

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
} from "$lib/text/general";
2020
import { addTypeSpecificValues } from "$lib/text/nbt_or_json";
2121
import {
22-
ClickEventMark,
2322
FontsExtension,
24-
HoverEventMark,
2523
Obfuscation,
26-
ShadowColorMark,
24+
ShadowColorMark
2725
} from "$lib/tiptap/extensions/index";
2826
import TextStyleButtons from "./TextStyleButtons.svelte";
2927
@@ -45,8 +43,6 @@
4543
Color,
4644
TextStyle,
4745
Obfuscation,
48-
ClickEventMark,
49-
HoverEventMark,
5046
ShadowColorMark,
5147
FontsExtension,
5248
Placeholder.configure({

src/lib/text/nbt.ts

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ export function snbtToDocument(raw: StringyMCText[]): JSONContent {
3030
export function convertToTextOrEmpty(raw: string): StringyMCText[] {
3131
if (raw === "") return [];
3232

33+
// convert unquoted keys to quoted keys and unquoted string values to quoted string values
3334
raw = raw.replace(/([,{]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g, '$1"$2":');
35+
raw = raw.replace(
36+
/(?<=[{,]\s*"[a-zA-Z_$][a-zA-Z0-9_$]*"\s*:\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)/g,
37+
'"$1"',
38+
);
3439

3540
if (raw.match(/^"\w*"/)) {
3641
return [raw.replace(/"/g, "")];
@@ -47,12 +52,12 @@ export function convertToTextOrEmpty(raw: string): StringyMCText[] {
4752
} catch (err) {
4853
return [
4954
"",
50-
{color:"red",text:"An error occurred while parsing the SNBT."},
51-
{color:"yellow",text:" Please send the "},
52-
{color:"gold",text:"following error message"},
53-
{color:"yellow",text:" to Datapack Hub staff:\n"},
54-
{color:"white",bold:true,text:"Error: "},
55-
{color:"gray",text:err!.toString()}
55+
{ color: "red", text: "An error occurred while parsing the SNBT." },
56+
{ color: "yellow", text: " Please send the " },
57+
{ color: "gold", text: "following error message" },
58+
{ color: "yellow", text: " to Datapack Hub staff:\n" },
59+
{ color: "white", bold: true, text: "Error: " },
60+
{ color: "gray", text: err!.toString() },
5661
];
5762
}
5863

@@ -84,53 +89,44 @@ function processTextComponent(text: StringyMCText, baseDocument: JSONContent) {
8489
finalText = applyStyling(text, finalText);
8590

8691
let paragraphContent = baseDocument.content?.at(-1)?.content;
87-
88-
if (!paragraphContent) {
89-
baseDocument.content!.at(-1)!.content = [];
90-
paragraphContent = baseDocument.content!.at(-1)!.content;
91-
}
92-
9392
paragraphContent!.push(finalText);
9493

9594
// Extra property
9695
if (text.extra) {
9796
text.extra!.forEach((txt) => {
98-
if (typeof txt == "object") {
99-
Object.assign(txt, {
100-
bold: txt.bold ?? text.bold,
101-
italic: txt.italic ?? text.italic,
102-
underlined: txt.underlined ?? text.underlined,
103-
obfuscated: txt.obfuscated ?? text.obfuscated,
104-
strikethrough: txt.strikethrough ?? text.strikethrough,
105-
color: txt.color ?? text.color,
106-
shadow_color: txt.shadow_color ?? text.shadow_color,
107-
click_event: txt.click_event ?? text.click_event,
108-
clickEvent: txt.clickEvent ?? text.clickEvent,
109-
hover_event: txt.hover_event ?? text.hover_event,
110-
hoverEvent: txt.hoverEvent ?? text.hoverEvent,
111-
font: txt.font ?? text.font,
112-
});
113-
processTextComponent(txt, baseDocument);
114-
} else {
115-
let newComponent = {
116-
text: txt,
117-
};
118-
Object.assign(newComponent, {
119-
bold: text.bold,
120-
italic: text.italic,
121-
underlined: text.underlined,
122-
obfuscated: text.obfuscated,
123-
strikethrough: text.strikethrough,
124-
color: text.color,
125-
shadow_color: text.shadow_color,
126-
click_event: text.click_event,
127-
clickEvent: text.clickEvent,
128-
hover_event: text.hover_event,
129-
hoverEvent: text.hoverEvent,
130-
font: text.font,
131-
});
132-
processTextComponent(newComponent, baseDocument);
133-
}
97+
const newText = typeof txt === "object" ? { ...txt } : { text: txt };
98+
// Object.assign(newText, {
99+
// bold: text.bold,
100+
// italic: text.italic,
101+
// underlined: text.underlined,
102+
// obfuscated: text.obfuscated,
103+
// strikethrough: text.strikethrough,
104+
// color: text.color,
105+
// shadow_color: text.shadow_color,
106+
// click_event: text.click_event,
107+
// clickEvent: text.clickEvent,
108+
// hover_event: text.hover_event,
109+
// hoverEvent: text.hoverEvent,
110+
// font: text.font,
111+
// });
112+
113+
Object.assign(newText, {
114+
bold: text.bold ?? newText.bold,
115+
italic: text.italic ?? newText.italic,
116+
underlined: text.underlined ?? newText.underlined,
117+
obfuscated: text.obfuscated ?? newText.obfuscated,
118+
strikethrough: text.strikethrough ?? newText.strikethrough,
119+
color: text.color ?? newText.color,
120+
shadow_color: text.shadow_color ?? newText.shadow_color,
121+
click_event: text.click_event ?? newText.click_event,
122+
clickEvent: text.clickEvent ?? newText.clickEvent,
123+
hover_event: text.hover_event ?? newText.hover_event,
124+
hoverEvent: text.hoverEvent ?? newText.hoverEvent,
125+
font: text.font ?? newText.font,
126+
});
127+
128+
console.log(newText);
129+
processTextComponent(newText, baseDocument);
134130
});
135131
}
136132
}
@@ -244,9 +240,9 @@ function applyStyling(
244240
}
245241

246242
if (text.font) {
247-
if (finalText.marks.some((mark) => mark.type === "textStyle")) {
248-
finalText.marks.find((mark) => mark.type === "textStyle")!.attrs!.font =
249-
text.font;
243+
const textStyle = finalText.marks.find((mark) => mark.type === "textStyle");
244+
if (textStyle) {
245+
textStyle.attrs!.font = text.font;
250246
} else {
251247
finalText.marks?.push({
252248
type: "textStyle",

src/lib/text/nbt_or_json.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ export function optimise(arr: StringyMCText[], lore = false): StringyMCText[] {
301301
}
302302
} else {
303303
if (extras[0]) {
304-
merged = { ...sharedAll, ...first, extra: extras };
304+
if (!merged?.extra) merged = { ...sharedAll, ...first, extra: extras };
305+
else merged.extra = [...merged.extra, ...extras];
305306
} else {
306307
merged = { ...sharedAll, ...first };
307308
}

src/lib/tiptap/extensions/marks/HoverEventMark.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const HoverEventMark = Mark.create({
5555
(attributes: HoverEventAttributes) =>
5656
({ chain }) => {
5757
return chain().setMark(this.name, attributes).run();
58-
},
58+
},
5959
unsetHoverEvent:
6060
() =>
6161
({ chain }) => {

src/routes/+page.svelte

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<script lang="ts">
2-
import { convert, translateJSON } from "$lib/text/nbt_or_json";
2+
import { convert } from "$lib/text/nbt_or_json";
33
44
import {
5+
AtlasObjectNode,
56
BlockNBTNode,
67
ClickEventMark,
78
EntityNBTNode,
@@ -10,13 +11,12 @@
1011
HoverEventMark,
1112
KeybindNode,
1213
Obfuscation,
14+
PlayerObjectNode,
1315
ScoreNode,
1416
SelectorNode,
1517
ShadowColorMark,
1618
StorageNBTNode,
17-
TranslateNode,
18-
AtlasObjectNode,
19-
PlayerObjectNode,
19+
TranslateNode
2020
} from "$lib/tiptap/extensions/index";
2121
// Components
2222
import Modal from "$lib/components/Modal.svelte";
@@ -30,7 +30,7 @@
3030
import Placeholder from "@tiptap/extension-placeholder";
3131
import StarterKit from "@tiptap/starter-kit";
3232
import { onDestroy, onMount } from "svelte";
33-
// Icons
33+
// Icons
3434
import IconUndo from "~icons/tabler/arrow-back-up";
3535
import IconRedo from "~icons/tabler/arrow-forward-up";
3636
import IconTick from "~icons/tabler/check";
@@ -55,10 +55,10 @@
5555
5656
import ToolbarButton from "$lib/components/text/ToolbarButton.svelte";
5757
import { openDataStore } from "$lib/db";
58+
import { outputVersion } from "$lib/stores";
5859
import { fontLUT } from "$lib/tiptap/extensions/fonts";
5960
import { tooltip } from "$lib/tooltip";
6061
import { versions, type Version } from "$lib/types";
61-
import { outputVersion } from "$lib/stores";
6262
6363
let tiptapJSON: JSONContent = $state()!;
6464
@@ -94,9 +94,9 @@
9494
let clickEventValue = $state("");
9595
let clickEventDialog: Modal = $state()!;
9696
97-
let hoverEventValue: any = $state();
9897
let hoverEventEditor: MiniEditor = $state()!;
9998
let hoverEventDialog: Modal = $state()!;
99+
let hoverEventValue = $state("");
100100
101101
let fontDialog: Modal = $state()!;
102102
let fontUploadModal: Modal = $state()!;
@@ -548,7 +548,7 @@
548548

549549
<div>
550550
{#if page.url.searchParams.has("dev")}
551-
<code class="inline-block overflow-x-scroll p-3"
551+
<code class="inline-block overflow-x-scroll p-3 text-xs"
552552
>DEV ONLY: {editor
553553
? JSON.stringify(editor.getJSON())
554554
: "Loading..."}</code>
@@ -590,7 +590,7 @@
590590
<div class="relative inline-block">
591591
{#if versionPopup}
592592
<div
593-
class="absolute bottom-full left-1/2 z-10 mb-2 flex w-[400px] -translate-x-1/2 flex-col space-y-1 rounded-md bg-zinc-900 shadow-md shadow-zinc-950">
593+
class="absolute bottom-full left-1/2 z-10 mb-2 flex w-100 -translate-x-1/2 flex-col space-y-1 rounded-md bg-zinc-900 shadow-md shadow-zinc-950">
594594
{#if versionPopupConfirmationVisible}
595595
<div
596596
class="absolute flex h-full w-full flex-col items-center rounded-md bg-zinc-900 px-4 py-4 backdrop-blur-md">

src/tests/unit/snbt_importing.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ it("should apply a hex color to a text node", async () => {
4343
it("should throw an error if the format is invalid", async () => {
4444
const broken = await readTestDataFile("bad/broken.snbt");
4545
const converted = convertToTextOrEmpty(broken);
46-
expect(JSON.stringify(converted)).toContain(
47-
/An error occurred while parsing the SNBT/,
48-
);
46+
expect(JSON.stringify(converted).includes("An error occurred while parsing the SNBT")).toBe(true);
4947
});
5048

5149
it("should return a one length array if passed with a single component", async () => {

0 commit comments

Comments
 (0)