Files
blockly/core/serialization/workspaces.ts
Maribeth Bottorff 037eb59b89 chore: Lint TsDoc. (#6353)
* chore: add linting for tsdoc

* chore: don't require types on return

* chore: remove redundant fileoverview from ts

* chore: change return to returns and add some newlines

* chore: remove license tag

* chore: don't require params/return docs

* chore: remove spurious struct tags

* Revert "chore: change return to returns and add some newlines"

This reverts commit d6d8656a45.

* chore: don't auto-add param names

* chore: disable require-param bc it breaks on this

* return to returns and add line breaks

* chore: configure additional jsdoc rules

* chore: run format

* Revert "chore: remove license tag"

This reverts commit 173455588a.

* chore: allow license tag format

* chore: only require jsdoc on exported items

* chore: add missing jsdoc or silence where needed

* chore: run format

* chore: lint fixes
2022-08-23 14:27:22 -07:00

105 lines
3.3 KiB
TypeScript

/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* 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 type {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 type {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.
* @returns 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);
}