Apri la tua vista in un punto preciso
Un link può portare la persona direttamente in un punto della tua vista come una stanza, un prodotto o un passo di un gioco. L'indirizzo della webapp porta dopo ?gui= un percorso del tuo sito e la webapp carica la tua vista a quel percorso. All'avvio la tua pagina legge il punto dal suo indirizzo e lo controlla con il mondo nel caso che un link vecchio nomini un punto che non esiste più. L'esempio dal vivo si apre nella hall. Per aprirlo nel giardino premi Open sopra l'esempio e aggiungi ?room=garden al suo indirizzo.
import { useWorld } from './useWorld.js';
// The fake museum keeps its rooms in its data.
const museum = {
name: 'Museum',
data: {
rooms: {
hall: 'The hall: tickets, maps and the cloakroom.',
garden: 'The garden: statues around a fountain.',
library: 'The library: old books and globes.',
},
},
};
// The place to open, read from the address of this page. A webapp link ending with
// ?gui=/?room=garden loads this page at https://your.site/?room=garden.
const wanted = new URLSearchParams(location.search).get('room');
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: museum } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
const rooms = world.data.rooms;
// Look for the room among those the world lists, because an old link can name one that is gone.
const isThere = wanted !== null && Object.hasOwn(rooms, wanted);
return (
<>
<h2>{world.name}</h2>
<p>{rooms[isThere ? wanted : 'hall']}</p>
{wanted !== null && !isThere && <p>There is no room "{wanted}": here is the hall.</p>}
</>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// The fake museum keeps its rooms in its data.
const museum = {
name: 'Museum',
data: {
rooms: {
hall: 'The hall: tickets, maps and the cloakroom.',
garden: 'The garden: statues around a fountain.',
library: 'The library: old books and globes.',
},
},
};
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: museum } });
// The place to open, read from the address of this page. A webapp link ending with
// ?gui=/?room=garden loads this page at https://your.site/?room=garden.
const wanted = new URLSearchParams(location.search).get('room');
const title = document.createElement('h2');
const room = document.createElement('p');
const note = document.createElement('p');
document.body.append(title, room, note);
// Called at once, then after every change of the world.
world.subscribe((node) => {
const rooms = node.data.rooms;
// Look for the room among those the world lists, because an old link can name one that is gone.
const isThere = wanted !== null && Object.hasOwn(rooms, wanted);
title.textContent = node.name;
room.textContent = rooms[isThere ? wanted : 'hall'];
note.textContent = wanted !== null && !isThere ? `There is no room "${wanted}": here is the hall.` : '';
});Come funziona
Quando l'indirizzo della webapp contiene ?gui=/?room=garden, la webapp carica la tua vista a https://your.site/?room=garden e la tua pagina legge location.search come qualsiasi pagina web. Il mondo elenca i punti che esistono (qui data.rooms) e l'esempio controlla in subscribe il punto richiesto appena il mondo è disponibile. Per un punto che non c'è più apre un punto che esiste sempre e spiega alla persona il motivo.
rooms[wanted]trova anche i nomi ereditati comeconstructormentreObject.hasOwn(rooms, wanted)cerca solo tra le stanze.- Un percorso come
/rooms/gardenal posto di una query funziona se il tuo server risponde a ognuno di questi percorsi con la pagina della tua vista. - Chiamare
world.setPathogni volta che la persona si sposta tiene il punto nell'indirizzo e produce link del genere (Tieni nell'indirizzo la scheda aperta).