From aa5ccf17538bd8ff81a708d4853f6ef0b76e4834 Mon Sep 17 00:00:00 2001 From: John Nesky Date: Fri, 13 Sep 2024 18:57:03 -0700 Subject: [PATCH 1/2] fix: Simplify list and text WHERE validation --- blocks/lists.ts | 148 +++++++++++++++++++++++------------------------- blocks/text.ts | 57 +++++++++---------- 2 files changed, 98 insertions(+), 107 deletions(-) diff --git a/blocks/lists.ts b/blocks/lists.ts index 28ff17b3df..e92a62839f 100644 --- a/blocks/lists.ts +++ b/blocks/lists.ts @@ -412,6 +412,27 @@ const LISTS_GETINDEX = { this.appendDummyInput() .appendField(modeMenu, 'MODE') .appendField('', 'SPACE'); + const menu = fieldRegistry.fromJson({ + type: 'field_dropdown', + options: this.WHERE_OPTIONS, + }) as FieldDropdown; + menu.setValidator( + /** + * @param value The input value. + * @returns Null if the field has been replaced; otherwise undefined. + */ + function (this: FieldDropdown, value: string) { + const oldValue: string | null = this.getValue(); + const oldAt = oldValue === 'FROM_START' || oldValue === 'FROM_END'; + const newAt = value === 'FROM_START' || value === 'FROM_END'; + if (newAt !== oldAt) { + const block = this.getSourceBlock() as GetIndexBlock; + block.updateAt_(newAt); + } + return undefined; + }, + ); + this.appendDummyInput().appendField(menu, 'WHERE'); this.appendDummyInput('AT'); if (Msg['LISTS_GET_INDEX_TAIL']) { this.appendDummyInput('TAIL').appendField(Msg['LISTS_GET_INDEX_TAIL']); @@ -577,31 +598,6 @@ const LISTS_GETINDEX = { } else { this.appendDummyInput('AT'); } - const menu = fieldRegistry.fromJson({ - type: 'field_dropdown', - options: this.WHERE_OPTIONS, - }) as FieldDropdown; - menu.setValidator( - /** - * @param value The input value. - * @returns Null if the field has been replaced; otherwise undefined. - */ - function (this: FieldDropdown, value: string) { - const newAt = value === 'FROM_START' || value === 'FROM_END'; - // The 'isAt' variable is available due to this function being a - // closure. - if (newAt !== isAt) { - const block = this.getSourceBlock() as GetIndexBlock; - block.updateAt_(newAt); - // This menu has been destroyed and replaced. Update the - // replacement. - block.setFieldValue(value, 'WHERE'); - return null; - } - return undefined; - }, - ); - this.getInput('AT')!.appendField(menu, 'WHERE'); if (Msg['LISTS_GET_INDEX_TAIL']) { this.moveInputBefore('TAIL', null); } @@ -644,6 +640,27 @@ const LISTS_SETINDEX = { this.appendDummyInput() .appendField(operationDropdown, 'MODE') .appendField('', 'SPACE'); + const menu = fieldRegistry.fromJson({ + type: 'field_dropdown', + options: this.WHERE_OPTIONS, + }) as FieldDropdown; + menu.setValidator( + /** + * @param value The input value. + * @returns Null if the field has been replaced; otherwise undefined. + */ + function (this: FieldDropdown, value: string) { + const oldValue: string | null = this.getValue(); + const oldAt = oldValue === 'FROM_START' || oldValue === 'FROM_END'; + const newAt = value === 'FROM_START' || value === 'FROM_END'; + if (newAt !== oldAt) { + const block = this.getSourceBlock() as SetIndexBlock; + block.updateAt_(newAt); + } + return undefined; + }, + ); + this.appendDummyInput().appendField(menu, 'WHERE'); this.appendDummyInput('AT'); this.appendValueInput('TO').appendField(Msg['LISTS_SET_INDEX_INPUT_TO']); this.setInputsInline(true); @@ -756,36 +773,10 @@ const LISTS_SETINDEX = { } else { this.appendDummyInput('AT'); } - const menu = fieldRegistry.fromJson({ - type: 'field_dropdown', - options: this.WHERE_OPTIONS, - }) as FieldDropdown; - menu.setValidator( - /** - * @param value The input value. - * @returns Null if the field has been replaced; otherwise undefined. - */ - function (this: FieldDropdown, value: string) { - const newAt = value === 'FROM_START' || value === 'FROM_END'; - // The 'isAt' variable is available due to this function being a - // closure. - if (newAt !== isAt) { - const block = this.getSourceBlock() as SetIndexBlock; - block.updateAt_(newAt); - // This menu has been destroyed and replaced. Update the - // replacement. - block.setFieldValue(value, 'WHERE'); - return null; - } - return undefined; - }, - ); this.moveInputBefore('AT', 'TO'); if (this.getInput('ORDINAL')) { this.moveInputBefore('ORDINAL', 'TO'); } - - this.getInput('AT')!.appendField(menu, 'WHERE'); }, }; blocks['lists_setIndex'] = LISTS_SETINDEX; @@ -818,7 +809,33 @@ const LISTS_GETSUBLIST = { this.appendValueInput('LIST') .setCheck('Array') .appendField(Msg['LISTS_GET_SUBLIST_INPUT_IN_LIST']); + const createMenu = (n: 1 | 2): FieldDropdown => { + const menu = fieldRegistry.fromJson({ + type: 'field_dropdown', + options: + this[('WHERE_OPTIONS_' + n) as 'WHERE_OPTIONS_1' | 'WHERE_OPTIONS_2'], + }) as FieldDropdown; + menu.setValidator( + /** + * @param value The input value. + * @returns Null if the field has been replaced; otherwise undefined. + */ + function (this: FieldDropdown, value: string) { + const oldValue: string | null = this.getValue(); + const oldAt = oldValue === 'FROM_START' || oldValue === 'FROM_END'; + const newAt = value === 'FROM_START' || value === 'FROM_END'; + if (newAt !== oldAt) { + const block = this.getSourceBlock() as GetSublistBlock; + block.updateAt_(n, newAt); + } + return undefined; + }, + ); + return menu; + }; + this.appendDummyInput('WHERE1_INPUT').appendField(createMenu(1), 'WHERE1'); this.appendDummyInput('AT1'); + this.appendDummyInput('WHERE2_INPUT').appendField(createMenu(2), 'WHERE2'); this.appendDummyInput('AT2'); if (Msg['LISTS_GET_SUBLIST_TAIL']) { this.appendDummyInput('TAIL').appendField(Msg['LISTS_GET_SUBLIST_TAIL']); @@ -896,35 +913,10 @@ const LISTS_GETSUBLIST = { } else { this.appendDummyInput('AT' + n); } - const menu = fieldRegistry.fromJson({ - type: 'field_dropdown', - options: - this[('WHERE_OPTIONS_' + n) as 'WHERE_OPTIONS_1' | 'WHERE_OPTIONS_2'], - }) as FieldDropdown; - menu.setValidator( - /** - * @param value The input value. - * @returns Null if the field has been replaced; otherwise undefined. - */ - function (this: FieldDropdown, value: string) { - const newAt = value === 'FROM_START' || value === 'FROM_END'; - // The 'isAt' variable is available due to this function being a - // closure. - if (newAt !== isAt) { - const block = this.getSourceBlock() as GetSublistBlock; - block.updateAt_(n, newAt); - // This menu has been destroyed and replaced. - // Update the replacement. - block.setFieldValue(value, 'WHERE' + n); - return null; - } - }, - ); - this.getInput('AT' + n)!.appendField(menu, 'WHERE' + n); if (n === 1) { - this.moveInputBefore('AT1', 'AT2'); + this.moveInputBefore('AT1', 'WHERE2_INPUT'); if (this.getInput('ORDINAL1')) { - this.moveInputBefore('ORDINAL1', 'AT2'); + this.moveInputBefore('ORDINAL1', 'WHERE2_INPUT'); } } if (Msg['LISTS_GET_SUBLIST_TAIL']) { diff --git a/blocks/text.ts b/blocks/text.ts index 5ab6318364..318c5dd862 100644 --- a/blocks/text.ts +++ b/blocks/text.ts @@ -216,7 +216,33 @@ const GET_SUBSTRING_BLOCK = { this.appendValueInput('STRING') .setCheck('String') .appendField(Msg['TEXT_GET_SUBSTRING_INPUT_IN_TEXT']); + const createMenu = (n: 1 | 2): FieldDropdown => { + const menu = fieldRegistry.fromJson({ + type: 'field_dropdown', + options: + this[('WHERE_OPTIONS_' + n) as 'WHERE_OPTIONS_1' | 'WHERE_OPTIONS_2'], + }) as FieldDropdown; + menu.setValidator( + /** + * @param value The input value. + * @returns Null if the field has been replaced; otherwise undefined. + */ + function (this: FieldDropdown, value: any): null | undefined { + const oldValue: string | null = this.getValue(); + const oldAt = oldValue === 'FROM_START' || oldValue === 'FROM_END'; + const newAt = value === 'FROM_START' || value === 'FROM_END'; + if (newAt !== oldAt) { + const block = this.getSourceBlock() as GetSubstringBlock; + block.updateAt_(n, newAt); + } + return undefined; + }, + ); + return menu; + }; + this.appendDummyInput('WHERE1_INPUT').appendField(createMenu(1), 'WHERE1'); this.appendDummyInput('AT1'); + this.appendDummyInput('WHERE2_INPUT').appendField(createMenu(2), 'WHERE2'); this.appendDummyInput('AT2'); if (Msg['TEXT_GET_SUBSTRING_TAIL']) { this.appendDummyInput('TAIL').appendField(Msg['TEXT_GET_SUBSTRING_TAIL']); @@ -288,37 +314,10 @@ const GET_SUBSTRING_BLOCK = { this.removeInput('TAIL', true); this.appendDummyInput('TAIL').appendField(Msg['TEXT_GET_SUBSTRING_TAIL']); } - const menu = fieldRegistry.fromJson({ - type: 'field_dropdown', - options: - this[('WHERE_OPTIONS_' + n) as 'WHERE_OPTIONS_1' | 'WHERE_OPTIONS_2'], - }) as FieldDropdown; - menu.setValidator( - /** - * @param value The input value. - * @returns Null if the field has been replaced; otherwise undefined. - */ - function (this: FieldDropdown, value: any): null | undefined { - const newAt = value === 'FROM_START' || value === 'FROM_END'; - // The 'isAt' variable is available due to this function being a - // closure. - if (newAt !== isAt) { - const block = this.getSourceBlock() as GetSubstringBlock; - block.updateAt_(n, newAt); - // This menu has been destroyed and replaced. - // Update the replacement. - block.setFieldValue(value, 'WHERE' + n); - return null; - } - return undefined; - }, - ); - - this.getInput('AT' + n)!.appendField(menu, 'WHERE' + n); if (n === 1) { - this.moveInputBefore('AT1', 'AT2'); + this.moveInputBefore('AT1', 'WHERE2_INPUT'); if (this.getInput('ORDINAL1')) { - this.moveInputBefore('ORDINAL1', 'AT2'); + this.moveInputBefore('ORDINAL1', 'WHERE2_INPUT'); } } }, From 50326ad1fff0f4f38c4e13aca7543c9bad27a489 Mon Sep 17 00:00:00 2001 From: John Nesky Date: Fri, 20 Sep 2024 14:43:49 -0700 Subject: [PATCH 2/2] Addressing PR review comments. --- blocks/lists.ts | 15 +++------------ blocks/text.ts | 5 +---- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/blocks/lists.ts b/blocks/lists.ts index e92a62839f..6754b6847d 100644 --- a/blocks/lists.ts +++ b/blocks/lists.ts @@ -417,10 +417,7 @@ const LISTS_GETINDEX = { options: this.WHERE_OPTIONS, }) as FieldDropdown; menu.setValidator( - /** - * @param value The input value. - * @returns Null if the field has been replaced; otherwise undefined. - */ + /** @param value The input value. */ function (this: FieldDropdown, value: string) { const oldValue: string | null = this.getValue(); const oldAt = oldValue === 'FROM_START' || oldValue === 'FROM_END'; @@ -645,10 +642,7 @@ const LISTS_SETINDEX = { options: this.WHERE_OPTIONS, }) as FieldDropdown; menu.setValidator( - /** - * @param value The input value. - * @returns Null if the field has been replaced; otherwise undefined. - */ + /** @param value The input value. */ function (this: FieldDropdown, value: string) { const oldValue: string | null = this.getValue(); const oldAt = oldValue === 'FROM_START' || oldValue === 'FROM_END'; @@ -816,10 +810,7 @@ const LISTS_GETSUBLIST = { this[('WHERE_OPTIONS_' + n) as 'WHERE_OPTIONS_1' | 'WHERE_OPTIONS_2'], }) as FieldDropdown; menu.setValidator( - /** - * @param value The input value. - * @returns Null if the field has been replaced; otherwise undefined. - */ + /** @param value The input value. */ function (this: FieldDropdown, value: string) { const oldValue: string | null = this.getValue(); const oldAt = oldValue === 'FROM_START' || oldValue === 'FROM_END'; diff --git a/blocks/text.ts b/blocks/text.ts index 318c5dd862..a7ad5374ac 100644 --- a/blocks/text.ts +++ b/blocks/text.ts @@ -223,10 +223,7 @@ const GET_SUBSTRING_BLOCK = { this[('WHERE_OPTIONS_' + n) as 'WHERE_OPTIONS_1' | 'WHERE_OPTIONS_2'], }) as FieldDropdown; menu.setValidator( - /** - * @param value The input value. - * @returns Null if the field has been replaced; otherwise undefined. - */ + /** @param value The input value. */ function (this: FieldDropdown, value: any): null | undefined { const oldValue: string | null = this.getValue(); const oldAt = oldValue === 'FROM_START' || oldValue === 'FROM_END';