mirror of
https://github.com/google/blockly.git
synced 2026-01-07 17:10:11 +01:00
* fix(build): Restore erroneously-deleted filter function This was deleted in PR #7406 as it was mainly being used to filter core/ vs. test/mocha/ deps into separate deps files - but it turns out also to be used for filtering error messages too. Oops. * refactor(tests): Migrate advanced compilation test to ES Modules * refactor(build): Migrate main.js to TypeScript This turns out to be pretty straight forward, even if it would cause crashing if one actually tried to import this module instead of just feeding it to Closure Compiler. * chore(build): Remove goog.declareModuleId calls Replace goog.declareModuleId calls with a comment recording the former module ID for posterity (or at least until we decide how to reformat the renamings file. * chore(tests): Delete closure/goog/* For the moment we still need something to serve as base.js for the benefit of closure-make-deps, so we keep a vestigial base.js around, containing only the @provideGoog declaration. * refactor(build): Remove vestigial base.js By changing slightly the command line arguments to closure-make-deps and closure-calculate-chunks the need to have any base.js is eliminated. * chore: Typo fix for PR #7415
96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2021 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
// Former goog.module ID: 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.
|
|
*/
|
|
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.
|
|
*/
|
|
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);
|
|
}
|