Skip to content

Commit

Permalink
feat: support loading old XML saved workspaces (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoesbergen authored May 10, 2024
1 parent 0ea57de commit 2a2f06c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/lib/components/core/header/Header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions src/lib/domain/blockly/blockly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions src/lib/domain/robots.groups.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 2a2f06c

Please sign in to comment.