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

    YbCodemirror 代码编辑器

    对@codemirror (opens new window)的二次封装,基于 @codemirror@6 的新版本,用于 vue 的代码编辑器组件。

    # 基本用法

    YbCodemirror 默认不带任何代码语言扩展,如需要请通过安装 @codemirror/lang-xxx 并通过 extensions props 传入。

    <template>
        <yb-codemirror v-model="value" placeholder="请输入SQL语句"></yb-codemirror>
    </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
    显示 复制 复制

    # 固定布局高度

    <template>
        <yb-codemirror
            style="height:200px"
            v-model="value"
            placeholder="请输入SQL语句"
        ></yb-codemirror>
        <div style="height:300px;margin-top:10px;">
            <yb-codemirror
                style="height:100%"
                v-model="value"
                placeholder="请输入SQL语句"
            ></yb-codemirror>
        </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 属性,指定最小行数和最大行数,可以使得编辑器的高度能够根据内容自动进行调整。

    启用禁用
    <template>
        <el-switch
            v-model="disabled"
            active-text="禁用"
            inactive-text="启用"
            style="margin-bottom:10px"
        >
        </el-switch>
        <yb-codemirror
            v-model="value"
            :autosize="{ minRows: 6, maxRows: 12 }"
            placeholder="请输入SQL语句"
            :disabled="disabled"
        >
        </yb-codemirror>
    </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
    显示 复制 复制

    # 在光标处插入内容

    <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
                v-model="value"
                placeholder="请输入内容"
                :disabled="disabled"
                ref="codemirror"
                style="height:200px"
            >
            </yb-codemirror>
        </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),实现更多复杂的操作功能

    # YbCodemirror Attributes

    参数 说明 类型 可选值 默认值
    value/v-model 绑定值 string -- --
    customStyle 自定义样式 object -- --
    autosize 自适应内容高度,如,{ minRows: 2, maxRows: 6 } object -- --
    extensions 编辑器语言扩展,传递给 CodeMirror EditorState.create({ extensions }) array | object -- --
    selection 传递给 CodeMirror EditorState.create({ selection }) 属性 object -- --
    root 传递给 CodeMirror new EditorView({ root }) 属性 object -- --
    tabSize 指定按下 Tab 键时的缩进 number -- 4
    placeholder 内容为空时显示的提示 string -- --
    indentWithTab 绑定键盘 Tab 键事件 boolean -- true
    disabled 禁用 boolean -- false

    # YbCodemirror Slots

    name 说明
    default --

    # YbCodemirror Methods

    方法名称 说明 参数 返回值
    getEditorView 获取 codemirror 的视图实例 - -
    focus 聚焦 - -
    insertDocToCursor 在光标处插入内容 ,参数说明:doc:要插入的内容,getSelection:可以自定义光标位置 insertDocToCursor(doc:string,getSelection:function({view,doc}{return {anchor:Number, head: Number}})) -

    # YbCodemirror Events

    方法名称 说明 参数 返回值
    focus 当 YbCodemirror 聚焦时 function(view){} -
    blur 当 YbCodemirror 失焦时 function(view){} -
    update 当 YbCodemirror 的任何状态发生变化时 function(view){} -
    selection 当文档内容被选中时 function(selectString,view){} -
    change 当文档内容变化时 function(docString,view){} -
    上次更新: 2023/11/01, 12:53:28
    YbPaginationSelect 分页选择器
    YbCodemirrorSql SQL编辑器

    ← YbPaginationSelect 分页选择器 YbCodemirrorSql SQL编辑器→

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