mirror of
https://github.com/google/blockly.git
synced 2026-05-10 14:10:11 +02:00
b0475b0c68
* fix: Remove spurious blank lines Remove extraneous blank lines introduced by deletion of 'use strict'; pragmas. Also fix the location of the goog.declareModuleId call in core/utils/array.ts. * fix: Add missing double-blank-line before body of modules Our convention is to have two blank lines between the imports (or module ID, if there are no imports) and the beginning of the body of the module. Enforce this. * fix: one addition format error for PR #6243
107 lines
3.4 KiB
TypeScript
107 lines
3.4 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2021 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Contains top-level functions for serializing workspaces to
|
|
* plain JavaScript objects.
|
|
*/
|
|
|
|
/**
|
|
* Contains top-level functions for serializing workspaces to plain JavaScript
|
|
* objects.
|
|
* @namespace Blockly.serialization.workspaces
|
|
*/
|
|
import * as goog from '../../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.serialization.workspaces');
|
|
|
|
import * as eventUtils from '../events/utils.js';
|
|
import {ISerializer} from '../interfaces/i_serializer.js';
|
|
import * as registry from '../registry.js';
|
|
import * as dom from '../utils/dom.js';
|
|
// eslint-disable-next-line no-unused-vars
|
|
import {Workspace} from '../workspace.js';
|
|
import {WorkspaceSvg} from '../workspace_svg.js';
|
|
|
|
|
|
/**
|
|
* Returns the state of the workspace as a plain JavaScript object.
|
|
* @param workspace The workspace to serialize.
|
|
* @return The serialized state of the workspace.
|
|
* @alias Blockly.serialization.workspaces.save
|
|
*/
|
|
export function save(workspace: Workspace):
|
|
{[key: string]: AnyDuringMigration} {
|
|
const state = Object.create(null);
|
|
const serializerMap = registry.getAllItems(registry.Type.SERIALIZER, true);
|
|
for (const key in serializerMap) {
|
|
const save = (serializerMap[key] as ISerializer)?.save(workspace);
|
|
if (save) {
|
|
state[key] = save;
|
|
}
|
|
}
|
|
return state;
|
|
}
|
|
|
|
/**
|
|
* Loads the variable represented by the given state into the given workspace.
|
|
* @param state The state of the workspace to deserialize into the workspace.
|
|
* @param workspace The workspace to add the new state to.
|
|
* @param param1 recordUndo: If true, events triggered by this function will be
|
|
* undo-able by the user. False by default.
|
|
* @alias Blockly.serialization.workspaces.load
|
|
*/
|
|
export function load(
|
|
state: {[key: string]: AnyDuringMigration}, workspace: Workspace,
|
|
{recordUndo = false}: {recordUndo?: boolean} = {}) {
|
|
const serializerMap = registry.getAllItems(registry.Type.SERIALIZER, true);
|
|
if (!serializerMap) {
|
|
return;
|
|
}
|
|
|
|
const deserializers = Object.entries(serializerMap)
|
|
.sort(
|
|
(a, b) => (b[1] as ISerializer)!.priority -
|
|
(a[1] as ISerializer)!.priority);
|
|
|
|
const prevRecordUndo = eventUtils.getRecordUndo();
|
|
eventUtils.setRecordUndo(recordUndo);
|
|
const existingGroup = eventUtils.getGroup();
|
|
if (!existingGroup) {
|
|
eventUtils.setGroup(true);
|
|
}
|
|
|
|
dom.startTextWidthCache();
|
|
if (workspace instanceof WorkspaceSvg) {
|
|
workspace.setResizesEnabled(false);
|
|
}
|
|
|
|
// We want to trigger clearing in reverse priority order so plugins don't end
|
|
// up missing dependencies.
|
|
for (const [, deserializer] of deserializers.reverse()) {
|
|
(deserializer as ISerializer)?.clear(workspace);
|
|
}
|
|
|
|
// reverse() is destructive, so we have to re-reverse to correct the order.
|
|
for (let [name, deserializer] of deserializers.reverse()) {
|
|
name = name;
|
|
const pluginState = state[name];
|
|
if (pluginState) {
|
|
(deserializer as ISerializer)?.load(state[name], workspace);
|
|
}
|
|
}
|
|
|
|
if (workspace instanceof WorkspaceSvg) {
|
|
workspace.setResizesEnabled(true);
|
|
}
|
|
dom.stopTextWidthCache();
|
|
|
|
eventUtils.fire(new (eventUtils.get(eventUtils.FINISHED_LOADING))!
|
|
(workspace));
|
|
|
|
eventUtils.setGroup(existingGroup);
|
|
eventUtils.setRecordUndo(prevRecordUndo);
|
|
}
|