From 82dab67366cee50604fb5f457dcfa11634977bf3 Mon Sep 17 00:00:00 2001 From: varshneydevansh Date: Tue, 8 Aug 2023 15:24:53 +0530 Subject: [PATCH 1/4] fix: insertion markers to use JSON deserialization --- core/insertion_marker_manager.ts | 46 ++++++-------------------------- 1 file changed, 8 insertions(+), 38 deletions(-) diff --git a/core/insertion_marker_manager.ts b/core/insertion_marker_manager.ts index 48be15c147d..640f8b94c10 100644 --- a/core/insertion_marker_manager.ts +++ b/core/insertion_marker_manager.ts @@ -232,49 +232,19 @@ export class InsertionMarkerManager { * @returns The insertion marker that represents the given block. */ private createMarkerBlock(sourceBlock: BlockSvg): BlockSvg { - const imType = sourceBlock.type; - eventUtils.disable(); let result: BlockSvg; try { - result = this.workspace.newBlock(imType); - result.setInsertionMarker(true); - if (sourceBlock.saveExtraState) { - const state = sourceBlock.saveExtraState(); - if (state && result.loadExtraState) { - result.loadExtraState(state); - } - } else if (sourceBlock.mutationToDom) { - const oldMutationDom = sourceBlock.mutationToDom(); - if (oldMutationDom && result.domToMutation) { - result.domToMutation(oldMutationDom); - } - } - // Copy field values from the other block. These values may impact the - // rendered size of the insertion marker. Note that we do not care about - // child blocks here. - for (let i = 0; i < sourceBlock.inputList.length; i++) { - const sourceInput = sourceBlock.inputList[i]; - if (sourceInput.name === constants.COLLAPSED_INPUT_NAME) { - continue; // Ignore the collapsed input. - } - const resultInput = result.inputList[i]; - if (!resultInput) { - throw new Error(DUPLICATE_BLOCK_ERROR.replace('%1', 'an input')); - } - for (let j = 0; j < sourceInput.fieldRow.length; j++) { - const sourceField = sourceInput.fieldRow[j]; - const resultField = resultInput.fieldRow[j]; - if (!resultField) { - throw new Error(DUPLICATE_BLOCK_ERROR.replace('%1', 'a field')); - } - resultField.setValue(sourceField.getValue()); - } - } + // 1. Serialize the source block to JSON + const blockJson = Blockly.serialization.blocks.save(sourceBlock); - result.setCollapsed(sourceBlock.isCollapsed()); - result.setInputsInline(sourceBlock.getInputsInline()); + // 2. Deserialize the JSON back to a block in the same workspace + result = Blockly.serialization.blocks.append(blockJson, this.workspace); + + // Setting the result as an insertion marker + result.setInsertionMarker(true); + // Initialize the SVG for rendering result.initSvg(); result.getSvgRoot().setAttribute('visibility', 'hidden'); } finally { From 1a2b932e41c4a51d0f6efc0942d7329324a0761f Mon Sep 17 00:00:00 2001 From: varshneydevansh Date: Tue, 8 Aug 2023 15:38:43 +0530 Subject: [PATCH 2/4] removed the const DUPLICATE_BLOCK_ERROR --- core/insertion_marker_manager.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/core/insertion_marker_manager.ts b/core/insertion_marker_manager.ts index 640f8b94c10..cd7824d1a44 100644 --- a/core/insertion_marker_manager.ts +++ b/core/insertion_marker_manager.ts @@ -18,7 +18,6 @@ import type {BlockSvg} from './block_svg.js'; import * as common from './common.js'; import {ComponentManager} from './component_manager.js'; import {config} from './config.js'; -import * as constants from './constants.js'; import * as eventUtils from './events/utils.js'; import type {IDeleteArea} from './interfaces/i_delete_area.js'; import type {IDragTarget} from './interfaces/i_drag_target.js'; @@ -43,16 +42,6 @@ interface CandidateConnection { radius: number; } -/** - * An error message to throw if the block created by createMarkerBlock_ is - * missing any components. - */ -const DUPLICATE_BLOCK_ERROR = - 'The insertion marker ' + - 'manager tried to create a marker but the result is missing %1. If ' + - 'you are using a mutator, make sure your domToMutation method is ' + - 'properly defined.'; - /** * Class that controls updates to connections during drags. It is primarily * responsible for finding the closest eligible connection and highlighting or From 6e3e33527d3c1340cccc72ab7a5eea8bc38efaa9 Mon Sep 17 00:00:00 2001 From: varshneydevansh Date: Wed, 9 Aug 2023 00:53:46 +0530 Subject: [PATCH 3/4] modified to import module instead of referencing --- core/insertion_marker_manager.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core/insertion_marker_manager.ts b/core/insertion_marker_manager.ts index cd7824d1a44..424b5eb07e4 100644 --- a/core/insertion_marker_manager.ts +++ b/core/insertion_marker_manager.ts @@ -22,6 +22,7 @@ import * as eventUtils from './events/utils.js'; import type {IDeleteArea} from './interfaces/i_delete_area.js'; import type {IDragTarget} from './interfaces/i_drag_target.js'; import type {RenderedConnection} from './rendered_connection.js'; +import * as blocks from './serialization/blocks.js'; import type {Coordinate} from './utils/coordinate.js'; import type {WorkspaceSvg} from './workspace_svg.js'; import * as renderManagement from './render_management.js'; @@ -225,10 +226,12 @@ export class InsertionMarkerManager { let result: BlockSvg; try { // 1. Serialize the source block to JSON - const blockJson = Blockly.serialization.blocks.save(sourceBlock); - + const blockJson = blocks.save(sourceBlock); + if (!blockJson) { + throw new Error('Failed to serialize source block.'); + } // 2. Deserialize the JSON back to a block in the same workspace - result = Blockly.serialization.blocks.append(blockJson, this.workspace); + result = blocks.append(blockJson, this.workspace) as BlockSvg; // Setting the result as an insertion marker result.setInsertionMarker(true); From b5e828fa4d04456e2c312d99cce8d1f87914906e Mon Sep 17 00:00:00 2001 From: varshneydevansh Date: Wed, 9 Aug 2023 09:32:21 +0530 Subject: [PATCH 4/4] removed coments for better readability --- core/insertion_marker_manager.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/insertion_marker_manager.ts b/core/insertion_marker_manager.ts index 424b5eb07e4..35e46dc019a 100644 --- a/core/insertion_marker_manager.ts +++ b/core/insertion_marker_manager.ts @@ -225,18 +225,14 @@ export class InsertionMarkerManager { eventUtils.disable(); let result: BlockSvg; try { - // 1. Serialize the source block to JSON const blockJson = blocks.save(sourceBlock); if (!blockJson) { throw new Error('Failed to serialize source block.'); } - // 2. Deserialize the JSON back to a block in the same workspace result = blocks.append(blockJson, this.workspace) as BlockSvg; - // Setting the result as an insertion marker result.setInsertionMarker(true); - // Initialize the SVG for rendering result.initSvg(); result.getSvgRoot().setAttribute('visibility', 'hidden'); } finally {