Skip to content

Commit

Permalink
Tree View Demo
Browse files Browse the repository at this point in the history
Demoing out how I want to display NBT data using a tree view, like how NBTExplorer and other apps commonly show NBT to users for editing.

#4
  • Loading branch information
Offroaders123 committed Aug 1, 2023
1 parent a3cee82 commit 1be0100
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<button id="formatOpener" disabled>Format Options...</button>
</header>

<textarea id="editor" disabled placeholder="NBT data will show here..." wrap="off" spellcheck="false" autocomplete="off" autocapitalize="none" autocorrect="off"></textarea>
<nbt-tree id="editor" disabled placeholder="NBT data will show here..."></nbt-tree>

<dialog id="formatDialog">
<form id="formatForm" method="dialog">
Expand Down
Binary file added public/bigtest.nbt
Binary file not shown.
38 changes: 32 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ document.addEventListener("drop",async event => {
});

saver.addEventListener("click",async () => {
const snbt = editor.value;
const nbt = parse(snbt);
const nbt = editor.value;
const options = saveOptions();
const nbtData = new NBTData(nbt,options);
const file = await writeFile(nbtData);
Expand All @@ -70,13 +69,42 @@ formatOpener.addEventListener("click",() => {
formatDialog.showModal();
});

const demo = fetch("./bigtest.nbt")
.then(response => response.blob())
.then(blob => new File([blob],"bigtest.nbt"));
demo.then(console.log);
demo.then(openFile);

export class NBTTree extends HTMLElement {
#value: NBTData | null = null;

get value(): NBTData | null {
return this.#value;
}

set value(value: NBTData | null) {
console.log(value);
this.#value = value;
if (value !== null){
this.textContent = stringify(value);
}
}
}

window.customElements.define("nbt-tree",NBTTree);

declare global {
interface HTMLElementTagNameMap {
"nbt-tree": NBTTree;
}
}

/**
* Attempts to read an NBT file, then open it in the editor.
*/
export async function openFile(file: File | FileSystemFileHandle | DataTransferFile): Promise<void> {
saver.disabled = true;
formatOpener.disabled = true;
editor.disabled = true;

if (file instanceof DataTransferItem){
const handle = await file.getAsFileSystemHandle?.() ?? null;
Expand All @@ -92,16 +120,14 @@ export async function openFile(file: File | FileSystemFileHandle | DataTransferF
const nbt = await readFile(file);
if (nbt === null) return;

const snbt = stringify(nbt,{ space: 2 });
openOptions(nbt);
name = file.name;

document.title = `Dovetail - ${name}`;

saver.disabled = false;
formatOpener.disabled = false;
editor.value = snbt;
editor.disabled = false;
editor.value = nbt;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { NBTTree } from "./app.js";

declare global {
interface DataTransferFile extends DataTransferItem {
readonly kind: "file";
Expand All @@ -15,7 +17,7 @@ declare global {
var saver: HTMLButtonElement;
var fileOpener: HTMLInputElement;
var formatOpener: HTMLButtonElement;
var editor: HTMLTextAreaElement;
var editor: NBTTree;
var formatDialog: HTMLDialogElement;
var formatForm: HTMLFormElement & {
readonly elements: FormatOptionsCollection;
Expand Down
7 changes: 2 additions & 5 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ input[type="file"] {
display: none;
}

textarea {
margin: 0;
nbt-tree {
padding: 8px;
flex-grow: 1;
font-size: 13px;
Expand All @@ -71,11 +70,9 @@ textarea {
border-top: 1px solid #80808080;
border-bottom-width: env(safe-area-inset-bottom);
border-radius: 0;
outline: none;
resize: none;
}

textarea[disabled] {
nbt-tree[disabled] {
opacity: unset;
}

Expand Down

0 comments on commit 1be0100

Please sign in to comment.