Skip to content

Commit 8421538

Browse files
feat: Insertion marker json deserialization 7316 (#7364)
* fix: insertion markers to use JSON deserialization * removed the const DUPLICATE_BLOCK_ERROR * modified to import module instead of referencing * removed coments for better readability
1 parent a0b5657 commit 8421538

1 file changed

Lines changed: 6 additions & 48 deletions

File tree

core/insertion_marker_manager.ts

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import type {BlockSvg} from './block_svg.js';
1818
import * as common from './common.js';
1919
import {ComponentManager} from './component_manager.js';
2020
import {config} from './config.js';
21-
import * as constants from './constants.js';
2221
import * as eventUtils from './events/utils.js';
2322
import type {IDeleteArea} from './interfaces/i_delete_area.js';
2423
import type {IDragTarget} from './interfaces/i_drag_target.js';
2524
import type {RenderedConnection} from './rendered_connection.js';
25+
import * as blocks from './serialization/blocks.js';
2626
import type {Coordinate} from './utils/coordinate.js';
2727
import type {WorkspaceSvg} from './workspace_svg.js';
2828
import * as renderManagement from './render_management.js';
@@ -43,16 +43,6 @@ interface CandidateConnection {
4343
radius: number;
4444
}
4545

46-
/**
47-
* An error message to throw if the block created by createMarkerBlock_ is
48-
* missing any components.
49-
*/
50-
const DUPLICATE_BLOCK_ERROR =
51-
'The insertion marker ' +
52-
'manager tried to create a marker but the result is missing %1. If ' +
53-
'you are using a mutator, make sure your domToMutation method is ' +
54-
'properly defined.';
55-
5646
/**
5747
* Class that controls updates to connections during drags. It is primarily
5848
* responsible for finding the closest eligible connection and highlighting or
@@ -232,48 +222,16 @@ export class InsertionMarkerManager {
232222
* @returns The insertion marker that represents the given block.
233223
*/
234224
private createMarkerBlock(sourceBlock: BlockSvg): BlockSvg {
235-
const imType = sourceBlock.type;
236-
237225
eventUtils.disable();
238226
let result: BlockSvg;
239227
try {
240-
result = this.workspace.newBlock(imType);
241-
result.setInsertionMarker(true);
242-
if (sourceBlock.saveExtraState) {
243-
const state = sourceBlock.saveExtraState();
244-
if (state && result.loadExtraState) {
245-
result.loadExtraState(state);
246-
}
247-
} else if (sourceBlock.mutationToDom) {
248-
const oldMutationDom = sourceBlock.mutationToDom();
249-
if (oldMutationDom && result.domToMutation) {
250-
result.domToMutation(oldMutationDom);
251-
}
252-
}
253-
// Copy field values from the other block. These values may impact the
254-
// rendered size of the insertion marker. Note that we do not care about
255-
// child blocks here.
256-
for (let i = 0; i < sourceBlock.inputList.length; i++) {
257-
const sourceInput = sourceBlock.inputList[i];
258-
if (sourceInput.name === constants.COLLAPSED_INPUT_NAME) {
259-
continue; // Ignore the collapsed input.
260-
}
261-
const resultInput = result.inputList[i];
262-
if (!resultInput) {
263-
throw new Error(DUPLICATE_BLOCK_ERROR.replace('%1', 'an input'));
264-
}
265-
for (let j = 0; j < sourceInput.fieldRow.length; j++) {
266-
const sourceField = sourceInput.fieldRow[j];
267-
const resultField = resultInput.fieldRow[j];
268-
if (!resultField) {
269-
throw new Error(DUPLICATE_BLOCK_ERROR.replace('%1', 'a field'));
270-
}
271-
resultField.setValue(sourceField.getValue());
272-
}
228+
const blockJson = blocks.save(sourceBlock);
229+
if (!blockJson) {
230+
throw new Error('Failed to serialize source block.');
273231
}
232+
result = blocks.append(blockJson, this.workspace) as BlockSvg;
274233

275-
result.setCollapsed(sourceBlock.isCollapsed());
276-
result.setInputsInline(sourceBlock.getInputsInline());
234+
result.setInsertionMarker(true);
277235

278236
result.initSvg();
279237
result.getSvgRoot().setAttribute('visibility', 'hidden');

0 commit comments

Comments
 (0)