From 9d8f4fc1ef8ca923771e1c298571690bcfb951cd Mon Sep 17 00:00:00 2001 From: AloeSapling Date: Sat, 23 Aug 2025 15:03:09 +0200 Subject: [PATCH] Added a guard against multiple blue marble instances --- src/main.ts | 105 +++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/src/main.ts b/src/main.ts index 9187904..140f5aa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,22 +9,22 @@ import UIManager from "./uiManager.ts"; import "@placecharity/framework-types" const DUMMY_DATA = { - whoami: "BlueMarble", - schemaVersion: "", - templates: [ - { - coords: [0, 1, 2, 3], - enabled: false, - uuid: "BLAJSIAFBNIUBAWIFIOANWA", - }, - { - coords: [0, 1, 2, 3], - name: "Test", - enabled: false, - authorID: 54, - uuid: "BLAJSIAFBNIUBAWIFIOANWA", - } - ] +whoami: "BlueMarble", +schemaVersion: "", +templates: [ + { + coords: [0, 1, 2, 3], + enabled: false, + uuid: "BLAJSIAFBNIUBAWIFIOANWA", + }, + { + coords: [0, 1, 2, 3], + name: "Test", + enabled: false, + authorID: 54, + uuid: "BLAJSIAFBNIUBAWIFIOANWA", + } +] } const storageData: TBlueMarbleJSON | undefined = DUMMY_DATA // data should be data gotten from storage @@ -32,40 +32,45 @@ export const dataManager = new DataManager(storageData); // export const dataManager = new DataManager(EMPTY_BLUE_MARBLE_JSON); export const uiManager = new UIManager(); - -const params = new URLSearchParams(document.location.search); // Gets the url search query -if (params.has("bmShare")) { - try { - const json = JSON.parse(params.get("bmShare")!) - dataManager.appendData(json) - } catch { } -} - -// Make sure the stored object is in Blue Marble's JSON format -dataManager.toBlueMarbleSchema(); -if(dataManager.getType() !== "BM"){ - dataManager.update(EMPTY_BLUE_MARBLE_JSON) -} - -else if((dataManager.get() as TBlueMarbleJSON).links){ - (dataManager.get() as TBlueMarbleJSON).links?.forEach(link => { - // Imports data from every URL in the stored object - importFromURL(link.url); - }) -} - -// Call the initialisation functions of all the windows -initManageTemplates(); -initManageLinks(); -initCreateTemplate(); -initMainOverlay(); - -/** Fetches data from a url and then updates the object stored in dataManager appropriately - * @since 0.1.0-overhaul - */ -function importFromURL(url: string){ +const mainOverlay = document.querySelector("#bm-main-overlay"); +// If main overlay already exists that means another Blue Marble instance is already running +if(!mainOverlay){ - const data = EMPTY_BLUE_MARBLE_JSON; // data should be the data fetched from the URL + // Only run the code if this is the first Blue Marble instance + const params = new URLSearchParams(document.location.search); // Gets the url search query + if (params.has("bmShare")) { + try { + const json = JSON.parse(params.get("bmShare")!) + dataManager.appendData(json) + } catch { } + } - dataManager.appendTemplateDataFromURL(data, url); + // Make sure the stored object is in Blue Marble's JSON format + dataManager.toBlueMarbleSchema(); + if(dataManager.getType() !== "BM"){ + dataManager.update(EMPTY_BLUE_MARBLE_JSON) + } + + else if((dataManager.get() as TBlueMarbleJSON).links){ + (dataManager.get() as TBlueMarbleJSON).links?.forEach(link => { + // Imports data from every URL in the stored object + importFromURL(link.url); + }) + } + + // Call the initialisation functions of all the windows + initManageTemplates(); + initManageLinks(); + initCreateTemplate(); + initMainOverlay(); + + /** Fetches data from a url and then updates the object stored in dataManager appropriately + * @since 0.1.0-overhaul + */ + function importFromURL(url: string){ + + const data = EMPTY_BLUE_MARBLE_JSON; // data should be the data fetched from the URL + + dataManager.appendTemplateDataFromURL(data, url); + } } \ No newline at end of file