Skip to content

Commit 8381483

Browse files
committed
add player object type, and fix functions
1 parent 218ba9a commit 8381483

File tree

7 files changed

+230
-85
lines changed

7 files changed

+230
-85
lines changed

src/lib/components/modals/CustomSourceModal.svelte

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@
4444
selector: "",
4545
},
4646
object: {
47+
object: "",
4748
atlas: "",
4849
sprite: "",
50+
player: {
51+
name: "",
52+
id:""
53+
},
54+
hat: true
4955
},
5056
});
5157
@@ -383,6 +389,16 @@
383389
Add Selector
384390
</button>
385391
{:else if customType === "object"}
392+
<p class="mt-2">Object Type</p>
393+
<select
394+
bind:value={customValues.object.object}
395+
class="rounded-md bg-zinc-900 p-2">
396+
<option value="atlas">Atlas (sprite)</option>
397+
<option value="player">Player Head</option>
398+
</select>
399+
400+
401+
{#if customValues.object.object == "atlas"}
386402
<p class="mt-2">Atlas</p>
387403
<Combobox
388404
items={defaultAtlases}
@@ -394,13 +410,14 @@
394410
type="text"
395411
class="rounded-md bg-zinc-900 p-2"
396412
bind:value={customValues.object.sprite} />
413+
397414
<button
398415
onclick={() => {
399416
customDialog.close();
400417
editor
401418
.chain()
402419
.focus()
403-
.insertObject({
420+
.insertAtlasObject({
404421
atlas: customValues.object.atlas,
405422
sprite: customValues.object.sprite,
406423
})
@@ -409,5 +426,36 @@
409426
class="mt-2 w-fit rounded-md bg-zinc-900 p-2 hover:bg-black/50">
410427
Add Object
411428
</button>
429+
{:else if customValues.object.object == "player"}
430+
<p class="mt-2">Username</p>
431+
<input
432+
type="text"
433+
class="rounded-md bg-zinc-900 p-2"
434+
bind:value={customValues.object.player.name} />
435+
436+
<div class="mt-2 flex items-center space-x-2">
437+
<CheckBox bind:value={customValues.object.hat} label="interpret" />
438+
<label for="interpret"
439+
>Render Hat (2nd skin layer)</label>
440+
</div>
441+
442+
<button
443+
onclick={() => {
444+
customDialog.close();
445+
editor
446+
.chain()
447+
.focus()
448+
.insertPlayerObject({
449+
player: {
450+
name: customValues.object.player.name
451+
},
452+
hat: customValues.object.hat
453+
})
454+
.run();
455+
}}
456+
class="mt-2 w-fit rounded-md bg-zinc-900 p-2 hover:bg-black/50">
457+
Add Object
458+
</button>
459+
{/if}
412460
{/if}
413461
</Modal>

src/lib/text/nbt_or_json.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ export function addTypeSpecificValues(
7979
case "keybind":
8080
current.keybind = c.attrs?.key;
8181
break;
82-
case "object":
82+
case "atlas_object":
8383
if (exportVersion.index >= 2) {
84+
current.object = "atlas"
8485
current.atlas = c.attrs?.atlas;
8586
current.sprite = c.attrs?.sprite;
8687

@@ -90,6 +91,20 @@ export function addTypeSpecificValues(
9091
current.text = ""
9192
}
9293
break;
94+
case "player_object":
95+
if (exportVersion.index >= 2) {
96+
current.object = "player"
97+
current.player = {
98+
name: c.attrs?.player.name
99+
}
100+
current.hat = c.attrs?.hat;
101+
102+
current.bold = undefined;
103+
current.italic = undefined;
104+
} else {
105+
current.text = ""
106+
}
107+
break;
93108
case "selector":
94109
current.selector = c.attrs?.selector;
95110
break;
@@ -336,7 +351,6 @@ export function convert(
336351
): string {
337352
exportVersion = get(outputVersion)
338353
let out = translateJSON(jsonContent, { exportType, optimise });
339-
console.log(exportVersion)
340354
if (exportVersion.index >= 1 && !force_json) {
341355
// only remove strings
342356
out = out.replace(/(?<=[{,]\s*)"[^"]*"\s*:/g, (match) =>

src/lib/tiptap/extensions/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,18 @@ export interface SelectorAttributes {
4343
selector: string;
4444
}
4545

46-
export interface ObjectAttributes {
46+
export interface AtlasObjectAttributes {
4747
atlas: string;
4848
sprite: string;
4949
}
5050

51+
export interface PlayerObjectAttributes {
52+
player: {
53+
name: string;
54+
};
55+
hat?: boolean;
56+
}
57+
5158
declare module "@tiptap/core" {
5259
interface Commands<ReturnType> {
5360
font: {
@@ -92,7 +99,8 @@ declare module "@tiptap/core" {
9299
insertSelector: (attrs: SelectorAttributes) => ReturnType;
93100
};
94101
object: {
95-
insertObject: (attrs: ObjectAttributes) => ReturnType;
102+
insertAtlasObject: (attrs: AtlasObjectAttributes) => ReturnType;
103+
insertPlayerObject: (attrs: PlayerObjectAttributes) => ReturnType;
96104
};
97105
}
98106
}
@@ -113,5 +121,6 @@ export { ScoreNode } from "./nodes/ScoreNode";
113121
export { SelectorNode } from "./nodes/SelectorNode";
114122
export { StorageNBTNode } from "./nodes/StorageNBTNode";
115123
export { TranslateNode } from "./nodes/TranslateNode";
116-
export { ObjectNode } from "./nodes/ObjectNode";
124+
export { AtlasObjectNode } from "./nodes/AtlasObjectNode";
125+
export { PlayerObjectNode } from "./nodes/PlayerObjectNode";
117126
export { FontsExtension } from "./fonts";

src/lib/tiptap/extensions/nodes/ObjectNode.ts renamed to src/lib/tiptap/extensions/nodes/AtlasObjectNode.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Node, mergeAttributes, type CommandProps } from "@tiptap/core";
2-
import type { NodeOptions, ObjectAttributes } from "../index";
2+
import type { NodeOptions, AtlasObjectAttributes } from "../index";
33

4-
export const ObjectNode = Node.create<NodeOptions>({
5-
name: "object",
4+
export const AtlasObjectNode = Node.create<NodeOptions>({
5+
name: "atlas_object",
66

77
inline: true,
88
group: "inline",
@@ -14,7 +14,7 @@ export const ObjectNode = Node.create<NodeOptions>({
1414
};
1515
},
1616

17-
addAttributes(): ObjectAttributes {
17+
addAttributes(): AtlasObjectAttributes {
1818
return {
1919
atlas: "",
2020
sprite: "",
@@ -24,7 +24,7 @@ export const ObjectNode = Node.create<NodeOptions>({
2424
parseHTML() {
2525
return [
2626
{
27-
tag: "span[data-object-node]",
27+
tag: "span[data-atlas-object-node]",
2828
},
2929
];
3030
},
@@ -35,7 +35,7 @@ export const ObjectNode = Node.create<NodeOptions>({
3535
return [
3636
"span",
3737
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
38-
"data-object-node": "true",
38+
"data-atlas-object-node": "true",
3939
contenteditable: "false",
4040
style: `
4141
background-color: #18181b;
@@ -53,7 +53,7 @@ export const ObjectNode = Node.create<NodeOptions>({
5353

5454
addCommands() {
5555
return {
56-
insertObject:
56+
insertAtlasObject:
5757
(attrs) =>
5858
({ commands }: CommandProps) => {
5959
return commands.insertContent({
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Node, mergeAttributes, type CommandProps } from "@tiptap/core";
2+
import type { NodeOptions, PlayerObjectAttributes } from "../index";
3+
4+
export const PlayerObjectNode = Node.create<NodeOptions>({
5+
name: "player_object",
6+
7+
inline: true,
8+
group: "inline",
9+
atom: true,
10+
11+
addOptions() {
12+
return {
13+
HTMLAttributes: {},
14+
};
15+
},
16+
17+
addAttributes(): PlayerObjectAttributes {
18+
return {
19+
player: {
20+
name: ""
21+
},
22+
hat: true
23+
};
24+
},
25+
26+
parseHTML() {
27+
return [
28+
{
29+
tag: "span[data-player-object-node]",
30+
},
31+
];
32+
},
33+
34+
renderHTML({ HTMLAttributes, node }) {
35+
const { player, hat } = node.attrs;
36+
37+
return [
38+
"span",
39+
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
40+
"data-player-object-node": "true",
41+
contenteditable: "false",
42+
style: `
43+
background-color: #18181b;
44+
padding: 0px 5px;
45+
border-radius: 4px;
46+
font-size: 0.9rem;
47+
display: inline-block;
48+
vertical-align: var(--custom-source-align, middle);
49+
text-decoration: inherit;
50+
`,
51+
}),
52+
["span", {}, `OBJECT: [player ${player.name}]`],
53+
];
54+
},
55+
56+
addCommands() {
57+
return {
58+
insertPlayerObject:
59+
(attrs) =>
60+
({ commands }: CommandProps) => {
61+
return commands.insertContent({
62+
type: this.name,
63+
attrs,
64+
});
65+
},
66+
};
67+
},
68+
});

src/lib/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ export type MinecraftText = {
5454
keybind?: string;
5555

5656
selector?: string;
57+
58+
object?: string
5759
atlas?: string;
5860
sprite?: string;
61+
player?: {
62+
name?: string;
63+
};
64+
hat?: string;
5965

6066
color?: string;
6167
shadow_color?: number | number[];
@@ -127,8 +133,14 @@ export type ExternalSources = {
127133
selector: string;
128134
};
129135
object: {
136+
object: string;
130137
atlas: string;
131138
sprite: string;
139+
player: {
140+
name: string;
141+
id: string;
142+
};
143+
hat: boolean;
132144
};
133145
};
134146

0 commit comments

Comments
 (0)