import { SharedTree, TreeViewConfiguration, SchemaFactory, Tree } from "fluid-framework";
import { TinyliciousClient } from "@fluidframework/tinylicious-client";
const client = new TinyliciousClient();
const containerSchema = {
initialObjects: { diceTree: SharedTree },
};
const root = document.getElementById("content");
const sf = new SchemaFactory("fluidHelloWorldSample");
class Dice extends sf.object("Dice", {
value: sf.number,
}) {}
const treeViewConfiguration = new TreeViewConfiguration({ schema: Dice });
const createNewDice = async () => {
const { container } = await client.createContainer(containerSchema, "2");
const dice = container.initialObjects.diceTree.viewWith(treeViewConfiguration);
dice.initialize(new Dice({ value: 1 }));
const id = await container.attach();
renderDiceRoller(dice.root, root);
return id;
};
const loadExistingDice = async (id) => {
const { container } = await client.getContainer(id, containerSchema, "2");
const dice = container.initialObjects.diceTree.viewWith(treeViewConfiguration);
renderDiceRoller(dice.root, root);
};
async function start() {
if (location.hash) {
await loadExistingDice(location.hash.substring(1));
} else {
const id = await createNewDice();
location.hash = id;
}
}
start().catch((error) => console.error(error));
const template = document.createElement("template");
template.innerHTML = `
<style>
.wrapper { display: flex; flex-direction: column; align-items: center; }
.dice { width: 200px; }
.rollButton { width: 118px; height: 48px; background: #0078D4; border-style: none; border-radius: 8px; }
.rollText { font-size: 20px; color: #FFFFFF; }
</style>
<div class="wrapper">
<img class="dice"/>
<button class="rollButton"><span class="rollText">Roll</span></button>
</div>
`;
const renderDiceRoller = (dice, elem) => {
elem.appendChild(template.content.cloneNode(true));
const rollButton = elem.querySelector(".rollButton");
const diceElem = elem.querySelector(".dice");
rollButton.onclick = () => {
dice.value = Math.floor(Math.random() * 6) + 1;
};
const updateDice = () => {
const diceValue = dice.value;
diceElem.src = `/images/dice-${diceValue}.png`;
diceElem.alt = diceValue.toString();
};
updateDice();
Tree.on(dice, "nodeChanged", updateDice);
window.fluidStarted = true;
};