From 2a2f06c7ff86d0036b3e80c6677ca348da4a5fe1 Mon Sep 17 00:00:00 2001 From: Ronald Moesbergen Date: Fri, 10 May 2024 08:02:39 +0200 Subject: [PATCH] feat: support loading old XML saved workspaces (#58) --- src/lib/components/core/header/Header.svelte | 10 ++++------ src/lib/domain/blockly/blockly.ts | 17 +++++++++++++++-- src/lib/domain/robots.groups.ts | 2 -- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/lib/components/core/header/Header.svelte b/src/lib/components/core/header/Header.svelte index 6a06e93..d8390f1 100644 --- a/src/lib/components/core/header/Header.svelte +++ b/src/lib/components/core/header/Header.svelte @@ -6,6 +6,7 @@ import leaphyLogo from "$assets/leaphy-logo.svg"; import Button from "$components/ui/Button.svelte"; import ContextItem from "$components/ui/ContextItem.svelte"; import Select from "$components/ui/Select.svelte"; +import { loadWorkspaceFromString } from "$domain/blockly/blockly"; import { FileHandle } from "$domain/handles"; import { robots } from "$domain/robots"; import { Screen, Theme, screen, selected, theme } from "$state/app.svelte"; @@ -50,9 +51,9 @@ import { faVolumeHigh, faVolumeXmark, } from "@fortawesome/free-solid-svg-icons"; -import { serialization } from "blockly"; +import { Xml, serialization } from "blockly"; import JSZip from "jszip"; -import { type Writable, get } from "svelte/store"; +import { get } from "svelte/store"; import MicroPythonIO from "../../../micropython"; import About from "../popups/popups/About.svelte"; import Examples from "../popups/popups/Examples.svelte"; @@ -137,10 +138,7 @@ async function openProject() { code.set(await content.text()); } else { if (get(mode) === Mode.BLOCKS) { - serialization.workspaces.load( - JSON.parse(await content.text()), - $workspace, - ); + loadWorkspaceFromString(await content.text(), $workspace); } else { restore.set(JSON.parse(await content.text())); mode.set(Mode.BLOCKS); diff --git a/src/lib/domain/blockly/blockly.ts b/src/lib/domain/blockly/blockly.ts index aeb526b..10730b9 100644 --- a/src/lib/domain/blockly/blockly.ts +++ b/src/lib/domain/blockly/blockly.ts @@ -9,14 +9,14 @@ import { BackpackChange } from "@blockly/workspace-backpack"; import { CATEGORIES, EXTENSIONS, - THEME, blocks, translations, } from "@leaphy-robotics/leaphy-blocks"; +import { type WorkspaceSvg, serialization } from "blockly"; +import type { Workspace } from "blockly"; import type { CategoryInfo, ToolboxDefinition, - ToolboxItemInfo, } from "blockly/core/utils/toolbox"; import { Backpack } from "./backpack"; import { LeaphyCategory } from "./category-ui/category"; @@ -122,6 +122,19 @@ export function setLocale(robot: RobotDevice, locale: string) { } } +// Load a workspace from a JSON or XML string +export function loadWorkspaceFromString(content: string, workspace: Workspace) { + try { + const json = JSON.parse(content); + serialization.workspaces.load(json, workspace); + } catch { + // It's not JSON, maybe it's XML + const xml = Blockly.utils.xml.textToDom(content); + workspace.clear(); + Blockly.Xml.domToWorkspace(xml, workspace); + } +} + export function setupWorkspace( robot: RobotDevice, element: HTMLDivElement, diff --git a/src/lib/domain/robots.groups.ts b/src/lib/domain/robots.groups.ts index af39fa9..4a51948 100644 --- a/src/lib/domain/robots.groups.ts +++ b/src/lib/domain/robots.groups.ts @@ -1,7 +1,5 @@ import { RobotType } from "./robots.types"; -console.log("hi"); - const L_ORIGINAL_NANO_ALL = [ RobotType.L_ORIGINAL_NANO, RobotType.L_ORIGINAL_NANO_ESP32,