From f24940e7816891ee78add455d37edbdf7f17ab23 Mon Sep 17 00:00:00 2001 From: Maribeth Moffatt Date: Mon, 4 Aug 2025 16:14:44 -0400 Subject: [PATCH] fix: dont add comments to full block fields (#9263) * fix: dont add comments to full block fields * chore: remove some nonnull assertions --- core/block.ts | 2 +- core/contextmenu_items.ts | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/core/block.ts b/core/block.ts index 9f7c11d4f..8ba61b1ca 100644 --- a/core/block.ts +++ b/core/block.ts @@ -1118,7 +1118,7 @@ export class Block { * * @yields A generator that can be used to iterate the fields on the block. */ - *getFields(): Generator { + *getFields(): Generator { for (const input of this.inputList) { for (const field of input.fieldRow) { yield field; diff --git a/core/contextmenu_items.ts b/core/contextmenu_items.ts index 774bfdde2..8bb71775f 100644 --- a/core/contextmenu_items.ts +++ b/core/contextmenu_items.ts @@ -25,6 +25,12 @@ import {StatementInput} from './renderers/zelos/zelos.js'; import {Coordinate} from './utils/coordinate.js'; import type {WorkspaceSvg} from './workspace_svg.js'; +function isFullBlockField(block?: BlockSvg) { + if (!block || !block.isSimpleReporter()) return false; + const firstField = block.getFields().next().value; + return firstField?.isFullBlockField(); +} + /** * Option to undo previous action. */ @@ -362,10 +368,15 @@ export function registerComment() { preconditionFn(scope: Scope) { const block = scope.block; if ( - !block!.isInFlyout && - block!.workspace.options.comments && - !block!.isCollapsed() && - block!.isEditable() + block && + !block.isInFlyout && + block.workspace.options.comments && + !block.isCollapsed() && + block.isEditable() && + // Either block already has a comment so let us remove it, + // or the block isn't just one full-block field block, which + // shouldn't be allowed to have comments as there's no way to read them. + (block.hasIcon(CommentIcon.TYPE) || !isFullBlockField(block)) ) { return 'enabled'; } @@ -373,8 +384,8 @@ export function registerComment() { }, callback(scope: Scope) { const block = scope.block; - if (block!.hasIcon(CommentIcon.TYPE)) { - block!.setCommentText(null); + if (block && block.hasIcon(CommentIcon.TYPE)) { + block.setCommentText(null); } else { block!.setCommentText(''); }