Skip to content

Commit 245bdcb

Browse files
authored
add delete option to the function parameter edit prompt (microsoft#11076)
1 parent d6e5148 commit 245bdcb

4 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎pxtblocks/builtins/functions.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function initFunctions() {
3030
msg.FUNCTIONS_DEFNORETURN_TITLE = lf("function");
3131
msg.PROCEDURES_HUE = pxt.toolbox.getNamespaceColor("functions");
3232
msg.REPORTERS_HUE = pxt.toolbox.getNamespaceColor("variables");
33+
msg.FUNCTIONS_DELETE_PARAMETER_BUTTON = lf("{id:delete-function-parameter}Delete");
3334

3435
// builtin procedures_defnoreturn
3536
const proceduresDefId = "procedures_defnoreturn";

‎pxtblocks/external.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,19 @@ export function setOnWorkspaceContextMenu(impl: (workspace: Blockly.WorkspaceSvg
5656
_onWorkspaceContextMenu = impl;
5757
}
5858

59+
// from webapp/src/core.ts DialogOptions
5960
export interface PromptOptions {
6061
placeholder: string;
62+
buttons?: PromptButton[];
63+
}
64+
65+
// from webapp/src/sui.ts ModalButton
66+
interface PromptButton {
67+
label?: string;
68+
title?: string;
69+
icon?: string; // defaults to "checkmark"
70+
className?: string; // defaults "positive"
71+
onclick?: () => (Promise<void> | void);
6172
}
6273

6374
let _prompt: (message: string, defaultValue: string, callback: (value: string) => void, options?: PromptOptions) => void;

‎pxtblocks/plugins/functions/fields/fieldArgumentEditor.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as Blockly from "blockly";
22
import { FunctionManager } from "../functionManager";
33
import { ArgumentEditorBlock } from "../blocks/argumentEditorBlocks";
4+
import { prompt } from "../../../external";
5+
import { MsgKey } from "../msg";
46

57
export interface FieldArgumentEditorConfig extends Blockly.FieldTextInputConfig {
68
}
@@ -46,6 +48,50 @@ export class FieldArgumentEditor extends Blockly.FieldTextInput {
4648
}
4749
}
4850

51+
protected override showEditor_(
52+
_e?: Event,
53+
quietInput = false,
54+
manageEphemeralFocus: boolean = true,
55+
) {
56+
this.workspace_ = (this.sourceBlock_ as Blockly.BlockSvg).workspace;
57+
if (
58+
!quietInput &&
59+
this.workspace_.options.modalInputs &&
60+
(Blockly.utils.userAgent.MOBILE || Blockly.utils.userAgent.ANDROID || Blockly.utils.userAgent.IPAD)
61+
) {
62+
this.showPromptEditorModal();
63+
} else {
64+
super.showEditor_(_e, quietInput, manageEphemeralFocus);
65+
}
66+
}
67+
68+
protected showPromptEditorModal() {
69+
prompt(
70+
Blockly.Msg.CHANGE_VALUE_TITLE,
71+
this.getText(),
72+
(text: string) => {
73+
// Text is null if user pressed cancel button.
74+
if (text !== null) {
75+
this.setValue(this.getValueFromEditorText_(text));
76+
}
77+
this.onFinishEditing_(this.value_);
78+
},
79+
{
80+
placeholder: undefined,
81+
buttons: [
82+
{
83+
label: Blockly.Msg[MsgKey.FUNCTIONS_DELETE_PARAMETER_BUTTON],
84+
className: "red",
85+
icon: "trash",
86+
onclick: () => {
87+
this.removeCallback();
88+
}
89+
}
90+
]
91+
}
92+
)
93+
}
94+
4995
protected removeCallback() {
5096
const parent = this.sourceBlock_?.getParent();
5197

‎pxtblocks/plugins/functions/msg.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export enum MsgKey {
1919
FUNCTION_CREATE_NEW = "FUNCTION_CREATE_NEW",
2020
FUNCTION_FLYOUT_LABEL = "FUNCTION_FLYOUT_LABEL",
2121
FUNCTIONS_EDIT_OPTION = "FUNCTIONS_EDIT_OPTION",
22-
FUNCTIONS_CREATE_CALL_OPTION = "FUNCTIONS_CREATE_CALL_OPTION"
22+
FUNCTIONS_CREATE_CALL_OPTION = "FUNCTIONS_CREATE_CALL_OPTION",
23+
FUNCTIONS_DELETE_PARAMETER_BUTTON = "FUNCTIONS_DELETE_PARAMETER_BUTTON"
2324
}
2425

2526
function defineMsg(key: MsgKey, value: string) {
@@ -54,4 +55,6 @@ defineMsg(MsgKey.FUNCTION_FLYOUT_LABEL, "Your Functions");
5455

5556
defineMsg(MsgKey.FUNCTIONS_EDIT_OPTION, "Edit Function");
5657

57-
defineMsg(MsgKey.FUNCTIONS_CREATE_CALL_OPTION, "Create 'call %1'");
58+
defineMsg(MsgKey.FUNCTIONS_CREATE_CALL_OPTION, "Create 'call %1'");
59+
60+
defineMsg(MsgKey.FUNCTIONS_DELETE_PARAMETER_BUTTON, "Delete");

0 commit comments

Comments
 (0)