From 25470db495f92e93546a28157197557a5ee168d8 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Mon, 28 Oct 2024 03:27:56 -0500 Subject: [PATCH] fix: put highlight on shadow block when a colour field is selected (#79) --- src/line_cursor.ts | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/line_cursor.ts b/src/line_cursor.ts index e8d26f7..32d65f9 100644 --- a/src/line_cursor.ts +++ b/src/line_cursor.ts @@ -13,6 +13,7 @@ * @author aschmiedt@google.com (Abby Schmiedt) */ +import { FieldColour } from '@blockly/field-colour'; import * as Blockly from 'blockly/core'; import {ASTNode, Marker} from 'blockly/core'; @@ -424,15 +425,44 @@ export class LineCursor extends Marker { const oldNode = (this as any).curNode; (this as any).curNode = newNode; const drawer = (this as any).drawer; + if (!drawer) { + console.error('could not find a drawer'); + return; + } + + const newNodeIsFieldColour = newNode?.getType() == ASTNode.types.FIELD && + (newNode.getLocation() as Blockly.Field) instanceof FieldColour; + const oldNodeIsFieldColour = oldNode?.getType() == ASTNode.types.FIELD && + (oldNode.getLocation() as Blockly.Field) instanceof FieldColour + + if (newNode?.getType() == ASTNode.types.BLOCK) { - if (drawer) { - drawer.hide(); - } + drawer.hide(); const block = newNode.getLocation() as Blockly.BlockSvg; Blockly.common.setSelected(block); - } else if (drawer) { + } + else if (newNodeIsFieldColour) { + drawer.hide(); + + if (oldNode?.getType() == ASTNode.types.BLOCK) { + Blockly.common.setSelected(null); + } else if (oldNodeIsFieldColour) { + const field = oldNode.getLocation() as FieldColour; + const block = field.getSourceBlock() as Blockly.BlockSvg; + block.removeSelect(); + } + + const field = newNode.getLocation() as FieldColour; + const block = field.getSourceBlock() as Blockly.BlockSvg; + block.addSelect(); + } + else { if (oldNode?.getType() == ASTNode.types.BLOCK) { Blockly.common.setSelected(null); + } else if (oldNodeIsFieldColour) { + const field = oldNode.getLocation() as FieldColour; + const block = field.getSourceBlock() as Blockly.BlockSvg; + block.removeSelect(); } drawer.draw(oldNode, newNode);