Skip to content

Commit

Permalink
✨ Optimize localstore block and generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mobyw committed Sep 30, 2024
1 parent 309b978 commit 24df0de
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 18 deletions.
47 changes: 44 additions & 3 deletions packages/app/src/blocks/nonebot_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const definitions: BlockDefinition[] = [
type: "store_load_json",
tooltip: "",
helpUrl: "",
message0: "读取文件 %1 %2 到字典 %3",
message0: "读取文件 %1 %2 的字典",
args0: [
{
type: "field_input",
Expand All @@ -42,14 +42,55 @@ export const definitions: BlockDefinition[] = [
type: "input_dummy",
name: "PARAMS",
},
],
output: "DICT",
colour: 150,
inputsInline: true,
},
{
type: "store_save_text",
tooltip: "",
helpUrl: "",
message0: "保存文本 %1 到文件 %2 %3",
args0: [
{
type: "input_value",
name: "DICT",
name: "TEXT",
check: "String",
},
{
type: "field_input",
name: "FILE",
text: "save.txt",
},
{
type: "input_dummy",
name: "PARAMS",
},
],
previousStatement: null,
nextStatement: null,
colour: 150,
colour: 180,
inputsInline: true,
},
{
type: "store_load_text",
tooltip: "",
helpUrl: "",
message0: "读取文件 %1 %2 的文本",
args0: [
{
type: "field_input",
name: "FILE",
text: "save.txt",
},
{
type: "input_dummy",
name: "PARAMS",
},
],
output: "String",
colour: 180,
inputsInline: true,
},
];
22 changes: 9 additions & 13 deletions packages/app/src/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export const startBlocks = {
inputs: {
HANDLE: {
block: {
type: "store_load_json",
id: "d.$llW7Lq)~KG{Z|h1+B",
fields: { FILE: "save.json" },
type: "variables_set",
id: "9_AQ_@)mWU$=2GkYbT-d",
fields: { VAR: { id: "VSvr3AeHEP),5NB5v$+;" } },
inputs: {
DICT: {
VALUE: {
block: {
type: "variables_get",
id: "E,j@$a(@v[(]u_R${B+Z",
fields: { VAR: { id: "VSvr3AeHEP),5NB5v$+;" } },
type: "store_load_json",
id: "3~H`IY?A(%(Bb+8/wL3c",
fields: { FILE: "save.json" },
},
},
},
Expand All @@ -39,9 +39,7 @@ export const startBlocks = {
block: {
type: "variables_get",
id: "[Ws*B$JWgLY]BeT`^_=]",
fields: {
VAR: { id: "VSvr3AeHEP),5NB5v$+;" },
},
fields: { VAR: { id: "VSvr3AeHEP),5NB5v$+;" } },
},
},
},
Expand Down Expand Up @@ -90,9 +88,7 @@ export const startBlocks = {
block: {
type: "variables_get",
id: "7iF8)`YtA.s.-@v_dYNo",
fields: {
VAR: { id: "VSvr3AeHEP),5NB5v$+;" },
},
fields: { VAR: { id: "VSvr3AeHEP),5NB5v$+;" } },
},
},
VALUE: {
Expand Down
27 changes: 25 additions & 2 deletions packages/app/src/generators/nonebot_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,34 @@ forBlock["store_load_json"] = function (
block: Blockly.Block,
generator: PythonGenerator,
) {
const dict = generator.valueToCode(block, "DICT", Order.ATOMIC);
const file = block.getFieldValue("FILE");
generator["definitions_"]["import json"] = "import json";
generator["definitions_"]["import nonebot_plugin_localstore as store"] =
"import nonebot_plugin_localstore as store";
const code = `${dict} = json.loads(store.get_plugin_data_file("${file}").read_text() if store.get_plugin_data_file("${file}").exists() else "{}")\n`;
const code = `json.loads(store.get_plugin_data_file("${file}").read_text() if store.get_plugin_data_file("${file}").exists() else "{}")`;
return [code, Order.ATOMIC];
};

forBlock["store_save_text"] = function (
block: Blockly.Block,
generator: PythonGenerator,
) {
const text = generator.valueToCode(block, "TEXT", Order.ATOMIC);
const file = block.getFieldValue("FILE");
generator["definitions_"]["import nonebot_plugin_localstore as store"] =
"import nonebot_plugin_localstore as store";
const code = `store.get_plugin_data_file("${file}").write_text(${text})\n`;
return code;
};

forBlock["store_load_text"] = function (
block: Blockly.Block,
generator: PythonGenerator,
) {
const file = block.getFieldValue("FILE");
generator["definitions_"]["import json"] = "import json";
generator["definitions_"]["import nonebot_plugin_localstore as store"] =
"import nonebot_plugin_localstore as store";
const code = `store.get_plugin_data_file("${file}").read_text() if store.get_plugin_data_file("${file}").exists() else ""`;
return [code, Order.ATOMIC];
};
16 changes: 16 additions & 0 deletions packages/app/src/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ export const toolbox = {
kind: "BLOCK",
type: "dicts_get",
},
{
kind: "BLOCK",
type: "dicts_get_multi",
},
{
kind: "BLOCK",
type: "dicts_set",
Expand Down Expand Up @@ -440,6 +444,10 @@ export const toolbox = {
kind: "BLOCK",
type: "alconna_arg_get",
},
{
kind: "BLOCK",
type: "nonebot_send",
},
],
name: "跨平台命令处理",
colour: "90",
Expand All @@ -455,6 +463,14 @@ export const toolbox = {
kind: "BLOCK",
type: "store_load_json",
},
{
kind: "BLOCK",
type: "store_save_text",
},
{
kind: "BLOCK",
type: "store_load_text",
},
],
name: "文件存储",
colour: "150",
Expand Down

0 comments on commit 24df0de

Please sign in to comment.