Yb-components Yb-components
首页
开发规范
yb-cli
  • 开发指南
  • 更新日志
  • 组件
关于
首页
开发规范
yb-cli
  • 开发指南
  • 更新日志
  • 组件
关于
  • 开发指南
  • 更新日志
  • 数据展示

    • YbTable 表格
    • YbCardList 卡片列表
    • YbTree 树
    • YbCascaderList 级联列表
    • YbSimpleTablePage 分页查询表格
    • YbSimpleCardPage 分页卡片列表
    • YbBasicProfile 基础详情数据展示
    • YbOutscrollLayout 固定在滚动区域外
    • YbOutscrollTree 固定在滚动区域外的Tree
    • YbInfoLayout 信息页布局
    • YbEditLayout 编辑页布局
  • 数据录入

    • YbRowsPopoverForm 浮层多行表单
    • YbFormily 配置式表单
    • YbForm 表单
    • YbRowsForm 多行表单
    • YbCollapseForm 折叠表单
    • YbQueryFilter 筛选表单
    • YbFilesSelect 文件选择器
    • YbRangeWrapper 范围结构
    • YbRangeDatepicker 日期范围
    • YbRange 范围
    • YbFormulaInput 简单运算公式输入
    • YbCron 表达式生成器
    • YbTreeSelect 树型选择器
    • YbInputgroupWrapper 复合组
    • YbThemeSelect 主题风格选择器
    • YbSelect 选择器
    • YbPaginationSelect 分页选择器
    • YbCodemirror 代码编辑器
    • YbCodemirrorSql SQL编辑器
      • YbCodemirrorJson JSON编辑器
      • YbCodemirrorXml XML编辑器
      • YbCombiDatepicker 组合时间
      • YbQuarterPicker 季度选择器
      • YbRangeQuarterPicker 季度范围选择器
      • YbConditionSelect 条件选择器
      • YbCheckboxGroup 多选框组
      • YbIconSelect 选择器
      • YbTagsInput 标签输入框
    • 其他组件

      • YbBallLoading 加载中
      • YbSymbolIcon 多色小图标
      • YbKeybuttonsPopover
      • YbPercentBattery 电池百分比
      • YbAffix 固钉
      • YbPagination 分页器
      • YbCollapse 折叠面板
      • YbScrollTool 滚动工具
    • 物料

      • YbMainSideMenu 侧栏菜单
      • YbLoadRemote 远程应用加载
      • YbLayoutPro 布局
      • YbMainPro 主页
      • YbAppMain 应用主页
      • YbAppLogin 普通登录页
      • YbAppPortal Portal登录页
    • 指令

      • v-fixed-in-scroller
    • Mixins

      • pageList_mixin
      • getScopedSlot_mixin
      • editFormPage_mixin
      • uploadProgressPage_mixin
      • rowActions_mixin
      • rowDeletes_mixin
      • drawerToRouterTab_mixin
    • 实验室

      • YbSwiper 轮播
    • 组件
    • 数据录入
    liyufeng
    2023-10-12

    YbCodemirrorSql SQL编辑器

    在 yb-codemirror 基础上使用 sql 语言,并且内置格式化 sql 按钮

    lang 属性内部对应 是 @codemirror/lang-sql 中的 dialect:

    {
    SQL: StandardSQL,
    MYSQL: MySQL,
    POSTGRESQL: PostgreSQL,
    STARROCKS: StandardSQL,
    DORIS: StandardSQL,
    CLICKHOUSE: StandardSQL,
    ORACLE: StandardSQL,
    SQLSERVER: StandardSQL,
    DB2: StandardSQL,
    VERTICA: StandardSQL,
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    # 基本用法

    编辑SQL
    <template>
        <yb-codemirror-sql
            v-model="value"
            lang="POSTGRESQL"
            placeholder="请输入SQL语句"
        ></yb-codemirror-sql>
    </template>
    <script>
        export default {
            data() {
                return {
                    value: "select * from system_schema.tables where keyspace_name = 'system_schema' and table_name = 'tables';",
                };
            },
        };
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    显示 复制 复制

    # 固定布局高度

    编辑SQL
    编辑SQL
    <template>
        <yb-codemirror-sql
            style="height:200px"
            v-model="value"
            placeholder="请输入SQL语句"
        ></yb-codemirror-sql>
        <div style="height:300px;margin-top:10px;">
            <yb-codemirror-sql
                style="height:100%"
                v-model="value"
                placeholder="请输入SQL语句"
            ></yb-codemirror-sql>
        </div>
    </template>
    <script>
        // import { sql } from '@codemirror/lang-sql';
        export default {
            data() {
                return {
                    // extensions:[sql()],
                    value: "select * from system_schema.tables where keyspace_name = 'system_schema' and table_name = 'tables';",
                };
            },
        };
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    显示 复制 复制

    # 可自适应文本高度

    通过设置 autosize 属性,指定最小行数和最大行数,可以使得编辑器的高度能够根据内容自动进行调整, 通过 disabled 属性切换编辑器是否可以编辑。

    启用禁用
    编辑SQL
    <template>
        <el-switch
            v-model="disabled"
            active-text="禁用"
            inactive-text="启用"
            style="margin-bottom:10px"
        >
        </el-switch>
        <yb-codemirror-sql
            v-model="value"
            :autosize="{ minRows: 6, maxRows: 12 }"
            placeholder="请输入SQL语句"
            :disabled="disabled"
        >
        </yb-codemirror-sql>
    </template>
    <script>
        export default {
            data() {
                return {
                    disabled: false,
                    value: "select * from system_schema.tables where keyspace_name = 'system_schema' and table_name = 'tables';",
                };
            },
        };
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    显示 复制 复制

    # 在光标处插入内容

    编辑SQL
    <template>
        <div>
            <div style="margin-bottom:10px">
                <el-button size="mini" @click="insertContent"
                    >插入"select * from a"</el-button
                >
                <el-button size="mini" @click="insertSum"
                    >插入"sum()"并将光标聚焦在括号内</el-button
                >
            </div>
            <yb-codemirror-sql
                v-model="value"
                placeholder="请输入内容"
                :disabled="disabled"
                ref="codemirror"
                style="height:200px"
            >
            </yb-codemirror-sql>
        </div>
    </template>
    <script>
        export default {
            data() {
                return {
                    disabled: false,
                    value: "select * from system_schema.tables where keyspace_name = 'system_schema' and table_name = 'tables';",
                };
            },
            methods: {
                insertContent() {
                    this.$refs['codemirror'].insertDocToCursor('\nselect * from a');
                },
                insertSum() {
                    this.$refs['codemirror'].insertDocToCursor(
                        'sum()',
                        ({ view, doc }) => {
                            return {
                                anchor:
                                    view.state.selection.main.from + doc.length - 1,
                                head:
                                    view.state.selection.main.from + doc.length - 1,
                            };
                        }
                    );
                },
            },
        };
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    显示 复制 复制

    通过 this.$refs['codemirror'].getEditorView()获取到编辑器实例以及 extensions 扩展属性,查看codemirror 的文档 (opens new window),实现更多复杂的操作功能

    # 使用 getSqlExtensions 增强编辑器语法提示

    编辑SQL
    <template>
        <yb-codemirror-sql
            v-model="value"
            placeholder="请输入内容"
            :disabled="disabled"
            ref="codemirror"
            :getSqlExtensions="getSqlExtensions"
            style="height:200px"
        >
        </yb-codemirror-sql>
    </template>
    <script>
        export default {
            data() {
                return {
                    disabled: false,
                    value: "select * from system_schema.tables where keyspace_name = 'system_schema' and table_name = 'tables';",
                };
            },
            methods: {
                // 提供编辑器扩展
                getSqlExtensions(dialect) {
                    const extensionCustomCodeTip = dialect.language.data.of({
                        autocomplete: this.sqlCompletions,
                    });
    
                    return [extensionCustomCodeTip];
                },
                sqlCompletions(context) {
                    let word = context.matchBefore(/\w*/);
                    if (word.from == word.to && !context.explicit) return null;
                    return {
                        from: word.from,
                        options: [
                            { label: '广州医博信息技术有限公司', type: 'keyword' },
                            { label: '广州医博', type: 'variable', info: 'yibo' },
                            {
                                label: 'yibo',
                                type: 'text',
                                apply: '广州医博信息技术有限公司',
                                detail: 'macro',
                            },
                        ],
                    };
                },
            },
        };
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    显示 复制 复制

    # YbCodemirrorSql Attributes

    参数 说明 类型 可选值 默认值
    value/v-model 绑定值 string -- --
    lang SQL 语言的类型 string -- SQL
    showFormatBtn 显示格式化按钮 boolean -- true
    showCopyBtn 显示复制内容按钮 boolean -- true
    showLargerBtn 显示放大按钮 boolean -- true
    getSqlExtensions 可以取的 languageDialects 再返回 extensions function(languageDialects){return extensions} -- --
    sqlFormatterConfig sql-formatter (opens new window) 的扩展参数配置 Object -- {}
    sqlConfig @codemirror/lang-sql (opens new window) 的扩展参数配置 Object -- {}
    languageDialects 对应 lang 属性的 Dialects 映射,可以扩展 Object -- { SQL: StandardSQL, MYSQL: MySQL, POSTGRESQL: PostgreSQL, STARROCKS: StandardSQL, DORIS: StandardSQL, CLICKHOUSE: StandardSQL, ORACLE: StandardSQL,SQLSERVER: StandardSQL,DB2: StandardSQL,VERTICA: StandardSQL,}
    其他属性同 YbCodemirror -- --

    # YbCodemirrorSql Slots

    name 说明 参数
    prependBtns 在内置按钮的之前插入内容
    appendBtns 在内置按钮的之后插入内容
    largerArea 放大弹窗内容

    # YbCodemirrorSql Methods

    方法名称 说明 参数 返回值
    updateExtensions 有可能 getSqlExtensions 内的引用参数改变了不会自动触发更新 extensions , 需要调用 updateExtensions --
    其他属性同 YbCodemirror --

    # YbCodemirrorSql Events

    方法名称 说明 参数 返回值
    其他属性同 YbCodemirror -- --
    上次更新: 2025/01/21, 14:27:00
    YbCodemirror 代码编辑器
    YbCodemirrorJson JSON编辑器

    ← YbCodemirror 代码编辑器 YbCodemirrorJson JSON编辑器→

    Theme by Vdoing | Copyright © 2021-2025 YB-GZ | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式