Files
larksuite-cli/shortcuts/base/dashboard_block_create.go
huangxincola e8e0c6fc5a Add +dashboard-arrange command for auto-arranging dashboard blocks layout and introduce text block type with Markdown support for dashboard visualization. (#388)
- Add `+dashboard-arrange` command that triggers server-side smart layout optimization via POST /open-apis/base/v3/bases/{token}/dashboards/{id}/arrange
- Add `text` block type support for dashboard blocks with Markdown syntax (headers, bold, italic, strikethrough, lists)
- Update `validateBlockDataConfig()` to handle text-specific validation rules
- Update documentation (SKILL.md, lark-base-dashboard.md, dashboard-block-data-config.md, lark-base-dashboard-arrange.md)
- Add comprehensive unit tests for new commands and block type
- [x] Unit tests pass (`go test ./shortcuts/base/...`)
- [x] All dashboard-related tests pass including new `TestBaseDashboardExecuteArrange`
- [x] Text block type validation tests pass
- None
2026-04-10 21:05:37 +08:00

87 lines
3.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package base
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/larksuite/cli/shortcuts/common"
)
var BaseDashboardBlockCreate = common.Shortcut{
Service: "base",
Command: "+dashboard-block-create",
Description: "Create a block in a dashboard",
Risk: "write",
Scopes: []string{"base:dashboard:create"},
AuthTypes: authTypes(),
HasFormat: true,
Flags: []common.Flag{
baseTokenFlag(true),
dashboardIDFlag(true),
{Name: "name", Desc: "block name", Required: true},
{Name: "type", Desc: "block type: column(柱状图)|bar(条形图)|line(折线图)|pie(饼图)|ring(环形图)|area(面积图)|combo(组合图)|scatter(散点图)|funnel(漏斗图)|wordCloud(词云)|radar(雷达图)|statistics(指标卡)|text(文本). Read dashboard-block-data-config.md before creating.", Required: true},
{Name: "data-config", Desc: "data config JSON object (table_name, series, count_all, group_by, filter, etc.)"},
{Name: "user-id-type", Desc: "user ID type: open_id / union_id / user_id"},
{Name: "no-validate", Type: "bool", Desc: "skip local data_config validation"},
},
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
pc := newParseCtx(runtime)
if runtime.Bool("no-validate") {
return nil
}
raw := runtime.Str("data-config")
if strings.TrimSpace(raw) == "" {
// text 类型必须提供 data-config含 text 内容)
if strings.ToLower(runtime.Str("type")) == "text" {
return fmt.Errorf("text 类型组件必须提供 data-config包含必填字段 text")
}
return nil
}
cfg, err := parseJSONObject(pc, raw, "data-config")
if err != nil {
return err
}
norm := normalizeDataConfig(cfg)
if errs := validateBlockDataConfig(runtime.Str("type"), norm); len(errs) > 0 {
return formatDataConfigErrors(errs)
}
// 用规范化后的 JSON 覆写 flag确保后续透传一致
b, _ := json.Marshal(norm)
_ = runtime.Cmd.Flags().Set("data-config", string(b))
return nil
},
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
pc := newParseCtx(runtime)
body := map[string]interface{}{}
if name := runtime.Str("name"); name != "" {
body["name"] = name
}
if t := runtime.Str("type"); t != "" {
body["type"] = t
}
if raw := runtime.Str("data-config"); raw != "" {
if parsed, err := parseJSONObject(pc, raw, "data-config"); err == nil {
body["data_config"] = parsed
}
}
params := map[string]interface{}{}
if uid := runtime.Str("user-id-type"); uid != "" {
params["user_id_type"] = uid
}
return common.NewDryRunAPI().
POST("/open-apis/base/v3/bases/:base_token/dashboards/:dashboard_id/blocks").
Params(params).
Body(body).
Set("base_token", runtime.Str("base-token")).
Set("dashboard_id", runtime.Str("dashboard-id"))
},
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
return executeDashboardBlockCreate(runtime)
},
}