sql-editor-with-variables.mdx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { SourceCode } from '@theme';
  2. import { BasicStory } from 'components/form-materials/components/sql-editor-with-variables';
  3. # SQLEditorWithVariables
  4. SQLEditorWithVariables 是一个增强版的 SQL 编辑器,支持在 SQL 中插入变量引用。它基于 SQLCodeEditor 构建,集成了变量选择器和变量标签注入功能,使用户能够在 SQL 字符串中使用 `{{variable}}` 语法引用变量。
  5. ## 案例演示
  6. ### 基本使用
  7. <BasicStory />
  8. ```tsx pure title="form-meta.tsx"
  9. import { SQLEditorWithVariables } from '@flowgram.ai/form-materials';
  10. const formMeta = {
  11. render: () => (
  12. <>
  13. <FormHeader />
  14. <Field<string | undefined>
  15. name="sql_editor_with_variables"
  16. defaultValue="SELECT * FROM users WHERE user_id = {{start_0.str}}"
  17. >
  18. {({ field }) => (
  19. <SQLEditorWithVariables
  20. value={field.value}
  21. onChange={(value) => field.onChange(value)}
  22. />
  23. )}
  24. </Field>
  25. </>
  26. ),
  27. }
  28. ```
  29. ### 带变量的 SQL 示例
  30. ```sql
  31. SELECT
  32. id, name, email
  33. FROM users
  34. WHERE id = {{start_0.user_id}}
  35. AND email LIKE '%{{start_0.domain}}'
  36. LIMIT 10;
  37. ```
  38. ### 变量插入
  39. 在编辑器中输入 `@`, `{` 字符可以触发变量选择器。
  40. 输入 `@`, `{` 后会显示可用的变量列表,选择变量后会自动插入为 `{{variable.name}}` 格式。
  41. ## API 参考
  42. ### SQLEditorWithVariables Props
  43. | 属性名 | 类型 | 默认值 | 描述 |
  44. |--------|------|--------|------|
  45. | `value` | `string` | - | SQL 字符串内容 |
  46. | `onChange` | `(value: string) => void` | - | 内容变化时的回调函数 |
  47. | `theme` | `'dark' \| 'light'` | `'light'` | 编辑器主题 |
  48. | `placeholder` | `string` | - | 占位符文本 |
  49. | `activeLinePlaceholder` | `string` | `'按 @ 选择变量'` | 当前行的占位提示 |
  50. | `readonly` | `boolean` | `false` | 是否为只读模式 |
  51. | `options` | `Options` | - | CodeMirror 配置选项 |
  52. ### 变量语法
  53. 在 SQL 字符串中使用双花括号语法引用变量:
  54. ```sql
  55. SELECT * FROM orders WHERE user_id = {{variable.path}}
  56. ```
  57. 支持的变量格式:
  58. - `{{start_0.name}}` - 简单变量
  59. - `{{start_0.address.city}}` - 嵌套属性
  60. ## 源码导读
  61. <SourceCode
  62. href="https://github.com/bytedance/flowgram.ai/tree/main/packages/materials/form-materials/src/components/sql-editor-with-variables"
  63. />
  64. 使用 CLI 命令可以复制源代码到本地:
  65. ```bash
  66. npx @flowgram.ai/cli@latest materials components/sql-editor-with-variables
  67. ```
  68. ### 目录结构讲解
  69. ```
  70. sql-editor-with-variables/
  71. ├── index.tsx # 懒加载导出文件
  72. ├── editor.tsx # 主组件实现
  73. └── README.md # 组件说明文档
  74. ```
  75. ### 核心实现说明
  76. #### 变量选择器集成
  77. 集成 `EditorVariableTree` 和 `EditorVariableTagInject` 扩展:
  78. ```typescript
  79. <EditorVariableTree />
  80. <EditorVariableTagInject />
  81. ```
  82. #### 触发字符
  83. 默认使用 `@` 作为变量选择的触发字符。若需要自定义触发字符,可使用底层 `SQLCodeEditor` 进行扩展:
  84. ```typescript
  85. import { SQLCodeEditor } from '@flowgram.ai/form-materials';
  86. <SQLCodeEditor>
  87. <EditorVariableTree triggerCharacters={["@", "#", "$"]} />
  88. <EditorVariableTagInject />
  89. </SQLCodeEditor>
  90. ```
  91. ### 使用到的 flowgram API
  92. #### @flowgram.ai/coze-editor
  93. - `SQLCodeEditor`: SQL 代码编辑器
  94. - `EditorVariableTree`: 变量树选择器
  95. - `EditorVariableTagInject`: 变量标签注入器
  96. #### @flowgram.ai/i18n
  97. - `I18n`: 国际化支持
  98. #### coze-editor-extensions 物料
  99. - `EditorVariableTree`: 变量树选择触发
  100. - `EditorVariableTagInject`: 变量标签展示
  101. ### 整体流程
  102. ```mermaid
  103. graph TD
  104. A[SQLEditorWithVariables 组件] --> B[渲染 SQLCodeEditor]
  105. B --> C[加载 SQL 语法高亮]
  106. B --> D[集成变量扩展]
  107. D --> E[EditorVariableTree]
  108. D --> F[EditorVariableTagInject]
  109. E --> G[监听 @ 触发]
  110. F --> H[处理变量标签]
  111. G --> I[显示变量列表]
  112. I --> J[选择并插入变量]
  113. H --> L[渲染变量标签样式]
  114. J --> M[更新编辑器内容]
  115. ```