From 9b6b8a7d1bdfa11c12fa3a22bba853d4ac2b3e5c Mon Sep 17 00:00:00 2001 From: mobyw Date: Mon, 14 Oct 2024 15:05:29 +0800 Subject: [PATCH 1/4] :bug: Fix mismatch value of dynamic dropdown fields --- .../app/src/generators/nonebot_alconna.ts | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/app/src/generators/nonebot_alconna.ts b/packages/app/src/generators/nonebot_alconna.ts index 2845928..379cdfd 100644 --- a/packages/app/src/generators/nonebot_alconna.ts +++ b/packages/app/src/generators/nonebot_alconna.ts @@ -80,18 +80,24 @@ forBlock["alconna_arg_get"] = function ( generator: PythonGenerator, ) { // This generator will also update the dropdown list - let name_real = block.getFieldValue("NAME"); + let name = block.getFieldValue("NAME"); const args = getAlconnaArg(block); let options = new Array(); + // If the block is not initialized, it is reloading from saved + // Should rebuild the dropdown list and set the name to the saved name `block.name_` if (!block.isInitialized_ && block.name_ !== "") { - // Read from saved - name_real = block.name_; + name = block.name_; block.isInitialized_ = true; - if (args.indexOf(name_real) !== -1) { - options.push([name_real, name_real]); + // Make sure the selected value is the first one of the dropdown list + // Due to the dynamic dropdowns are not responding to set value calls correctly + // https://github.com/google/blockly/issues/3099 + // This will also cause warnings in console: + // `Cannot set the dropdown's value to an unavailable option.` + if (args.indexOf(name) !== -1) { + options.push([name, name]); } args.forEach((arg) => { - if (arg !== name_real) { + if (arg !== name) { options.push([arg, arg]); } }); @@ -101,9 +107,11 @@ forBlock["alconna_arg_get"] = function ( ?.appendField("获取参数") .appendField(new Blockly.FieldDropdown(options), "NAME"); } else { + // If the block is initialized, update the saved name and rebuild the dropdown list args.forEach((arg) => { options.push([arg, arg]); }); + block.name_ = name; } if (args.length === 0) { this.removeInput("PARAMS"); @@ -113,15 +121,15 @@ forBlock["alconna_arg_get"] = function ( .appendField(new Blockly.FieldDropdown([["-", ""]]), "NAME"); return ["", Order.ATOMIC]; } - if (!args.find((arg) => arg === name_real)) { + if (!args.find((arg) => arg === name)) { this.removeInput("PARAMS"); this.setWarningText(""); this.appendDummyInput("PARAMS") ?.appendField("获取参数") .appendField(new Blockly.FieldDropdown(options), "NAME"); } - if (name_real) { - return [generator.getVariableName("arg_" + name_real), Order.NONE]; + if (name) { + return [generator.getVariableName("arg_" + name), Order.NONE]; } return ["", Order.ATOMIC]; }; From 5f204adda4dfc1bb66d3cd7afb6cc0ff9da57d4f Mon Sep 17 00:00:00 2001 From: mobyw Date: Mon, 14 Oct 2024 23:38:58 +0800 Subject: [PATCH 2/4] :fire: Remove unused files --- packages/app/src/blocks/nonebot_matcher.ts | 29 -------------- packages/app/src/blocks/nonebot_util.ts | 3 -- packages/app/src/blocks/text.ts | 40 ------------------- .../app/src/generators/nonebot_matcher.ts | 22 ---------- 4 files changed, 94 deletions(-) delete mode 100644 packages/app/src/blocks/nonebot_matcher.ts delete mode 100644 packages/app/src/blocks/nonebot_util.ts delete mode 100644 packages/app/src/blocks/text.ts delete mode 100644 packages/app/src/generators/nonebot_matcher.ts diff --git a/packages/app/src/blocks/nonebot_matcher.ts b/packages/app/src/blocks/nonebot_matcher.ts deleted file mode 100644 index b189662..0000000 --- a/packages/app/src/blocks/nonebot_matcher.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { BlockDefinition } from "blockly/core/blocks"; - -export const nonebotMatcher: BlockDefinition[] = [ - { - type: "nonebot_on_command", - message0: "创建指令响应器 %1 %2 与我相关 %3", - args0: [ - { - type: "field_input", - name: "CMD", - text: "指令", - }, - { - type: "input_dummy", - }, - { - type: "field_checkbox", - name: "TO_ME", - checked: false, - }, - ], - inputsInline: true, - output: "Matcher", - colour: 20, - tooltip: - "请填写命令触发词,如“帮助”;若勾选“与我相关”,只有在私聊、群聊中@或回复、消息以机器人名字开始或结束时会触发", - helpUrl: "https://nonebot.dev/docs/next/advanced/matcher#command", - }, -]; diff --git a/packages/app/src/blocks/nonebot_util.ts b/packages/app/src/blocks/nonebot_util.ts deleted file mode 100644 index 67d9648..0000000 --- a/packages/app/src/blocks/nonebot_util.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { BlockDefinition } from "blockly/core/blocks"; - -export const definitions: BlockDefinition[] = []; diff --git a/packages/app/src/blocks/text.ts b/packages/app/src/blocks/text.ts deleted file mode 100644 index 5244861..0000000 --- a/packages/app/src/blocks/text.ts +++ /dev/null @@ -1,40 +0,0 @@ -/** - * @license - * Copyright 2023 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import * as Blockly from "blockly/core"; - -// Create a custom block called 'add_text' that adds -// text to the output div on the sample app. -// This is just an example and you should replace this with your -// own custom blocks. -const addText = { - type: "add_text", - message0: "Add text %1 with color %2", - args0: [ - { - type: "input_value", - name: "TEXT", - check: "String", - }, - { - type: "input_value", - name: "COLOR", - check: "Colour", - }, - ], - previousStatement: null, - nextStatement: null, - colour: 160, - tooltip: "", - helpUrl: "", -}; - -// Create the block definitions for the JSON-only blocks. -// This does not register their definitions with Blockly. -// This file has no side effects! -export const blocks = Blockly.common.createBlockDefinitionsFromJsonArray([ - addText, -]); diff --git a/packages/app/src/generators/nonebot_matcher.ts b/packages/app/src/generators/nonebot_matcher.ts deleted file mode 100644 index 96fc24c..0000000 --- a/packages/app/src/generators/nonebot_matcher.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Order } from "blockly/python"; -import * as Blockly from "blockly/core"; - -export const forBlock = Object.create(null); - -forBlock["nonebot_on_command"] = function ( - block: Blockly.Block, - generator: Blockly.CodeGenerator, -) { - const text_cmd = block.getFieldValue("CMD"); - const checkbox_to_me = block.getFieldValue("TO_ME") === "TRUE"; - let to_me_statement = ""; - generator["definitions_"]["from nonebot.plugin import on_command"] = - "from nonebot.plugin import on_command"; - if (checkbox_to_me) { - generator["definitions_"]["from nonebot.rule import to_me"] = - "from nonebot.rule import to_me"; - to_me_statement = ", rule=to_me()"; - } - let code = `on_command("${text_cmd}"${to_me_statement})`; - return [code, Order.NONE]; -}; From e656de6d9da6204bd3a639c298a7b3b81bedd9d7 Mon Sep 17 00:00:00 2001 From: mobyw Date: Mon, 14 Oct 2024 23:40:31 +0800 Subject: [PATCH 3/4] :sparkles: Supplement the missing request generator --- packages/app/src/blocks/nonebot_request.ts | 4 ++- .../app/src/generators/nonebot_request.ts | 36 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/app/src/blocks/nonebot_request.ts b/packages/app/src/blocks/nonebot_request.ts index 5e17b65..893c0ab 100644 --- a/packages/app/src/blocks/nonebot_request.ts +++ b/packages/app/src/blocks/nonebot_request.ts @@ -34,6 +34,7 @@ export const definitions: BlockDefinition[] = [ name: "TYPE", options: [ ["字典", "dict"], + ["列表", "list"], ["文本", "string"], ["二进制", "binary"], ], @@ -90,7 +91,7 @@ export const definitions: BlockDefinition[] = [ }, { type: "input_value", - name: "HEADER", + name: "HEADERS", align: "RIGHT", check: "dict", }, @@ -99,6 +100,7 @@ export const definitions: BlockDefinition[] = [ name: "TYPE", options: [ ["字典", "dict"], + ["列表", "list"], ["文本", "string"], ["二进制", "binary"], ], diff --git a/packages/app/src/generators/nonebot_request.ts b/packages/app/src/generators/nonebot_request.ts index 3db0bb6..a136410 100644 --- a/packages/app/src/generators/nonebot_request.ts +++ b/packages/app/src/generators/nonebot_request.ts @@ -34,6 +34,9 @@ if not isinstance(driver, HTTPClientMixin): \n\ if (type === "dict") { generator["definitions_"]["import json"] = "import json"; code = `json.loads(${content} or "{}")`; + } else if (type === "list") { + generator["definitions_"]["import json"] = "import json"; + code = `json.loads(${content} or "[]")`; } return [code, Order.ATOMIC]; }; @@ -42,6 +45,37 @@ forBlock["request_post"] = function ( block: Blockly.Block, generator: PythonGenerator, ) { - const code = `\n`; + const url = generator.valueToCode(block, "URL", Order.ATOMIC); + const json = generator.valueToCode(block, "JSON", Order.ATOMIC) || "None"; + const params = generator.valueToCode(block, "PARAMS", Order.ATOMIC) || "None"; + const headers = + generator.valueToCode(block, "HEADERS", Order.ATOMIC) || "None"; + const type = block.getFieldValue("TYPE"); + const timeout = block.getFieldValue("TIMEOUT"); + if (url === "" || url === "None") { + return ["None", Order.ATOMIC]; + } + generator["definitions_"]["from nonebot import get_driver"] = + "from nonebot import get_driver"; + generator["definitions_"]["from nonebot.drivers import Request"] = + "from nonebot.drivers import Request"; + generator["definitions_"]["from nonebot.drivers import HTTPClientMixin"] = + "from nonebot.drivers import HTTPClientMixin"; + generator["definitions_"]["nonebot_request_driver"] = + 'driver = get_driver() \n\ + if not isinstance(driver, HTTPClientMixin): \n\ + raise RuntimeError( \n\ + f"Current driver {driver} does not support http client requests!" \n\ + )'; + const request = `Request("POST", ${url}, json=${json}, params=${params}, headers=${headers}, timeout=${timeout})`; + const content = `(await driver.request(${request})).content`; + let code = content; + if (type === "dict") { + generator["definitions_"]["import json"] = "import json"; + code = `json.loads(${content} or "{}")`; + } else if (type === "list") { + generator["definitions_"]["import json"] = "import json"; + code = `json.loads(${content} or "[]")`; + } return [code, Order.ATOMIC]; }; From 0468c3ea167a1f76eee9382591274c8c602f0f70 Mon Sep 17 00:00:00 2001 From: mobyw Date: Mon, 14 Oct 2024 23:42:43 +0800 Subject: [PATCH 4/4] :sparkles: Improve import and export functions and example --- packages/app/src/App.vue | 8 +- packages/app/src/blocks/nonebot_scheduler.ts | 2 - packages/app/src/components/ButtonPanel.vue | 14 +- packages/app/src/components/ConfigTab.vue | 100 ++- packages/app/src/components/TutorialTab.vue | 42 +- packages/app/src/default.ts | 580 ++++++++++++++---- packages/app/src/generators/helper.ts | 29 + .../app/src/generators/nonebot_alconna.ts | 37 +- packages/app/src/generators/nonebot_basic.ts | 36 +- .../app/src/generators/nonebot_scheduler.ts | 23 +- packages/app/src/workspace.ts | 350 +++++++---- 11 files changed, 888 insertions(+), 333 deletions(-) create mode 100644 packages/app/src/generators/helper.ts diff --git a/packages/app/src/App.vue b/packages/app/src/App.vue index 8625df4..ec39ba0 100644 --- a/packages/app/src/App.vue +++ b/packages/app/src/App.vue @@ -1,5 +1,5 @@