• vue3 + vite中按需使用ace-builds实现编辑器


    1.介绍

    本文中只使用ace-builds中src下的个别文件完成功能

    githab官网:https://github.com/ajaxorg/ace-builds/tree/master
    在线demo:https://ace.c9.io/build/kitchen-sink.html

    2.需求描述

    运维人员可以在前台中查看后台接口、实例等的运行日志,希望可以在左侧显示日志的条数。

    3. 使用ace-builds

    1.通过npm

    通过使用npm安装ace-builds发现包过于庞大(打完包后发现ace-builds中占了2mb),很多不需要的主题和代码结构会占包中的很大一部分,但是ace-builds对于包没有提供拆分功能。
    需求中很明白只需要很小一部分功能,所以可以下载到本地按需引入。

    2.通过静态资源引入

    通过下载github上的ace.js,完成编辑器的初始化等(注意:需要下载的是打包后版本的ace.js)
    在这里插入图片描述
    该目录下的ace.js
    在这里插入图片描述
    在需要初始化编辑器的地方引入import "@/assets/js/ace/ace"不需要使用任何表达式接收。
    由于ace.js中使用的是UMD通用模块定义,会将实例自动挂接到当前上下文环境。

    对于主题和代码匹配模式来说也是一样
    在这里插入图片描述
    在需要初始化编辑器的地方引入import "import "@/assets/js/ace/theme-dracula""不需要使用任何表达式接收。

    3.实现效果

    在这里插入图片描述
    实现需求的代码

    <template>
      <div class="app-container">
         <!--   日志内容   -->
         <div id="log-content"></div>
      </div>
    </template>
    <script setup>
    import "@/assets/js/ace/ace"
    import "@/assets/js/ace/theme-dracula"
    
    let editor = null // ace加载实例
    
    onActivated(() => {
      editor = ace.edit("log-content");
      // 使用dracula主题
      editor.setTheme("ace/theme/dracula");
      editor.setOptions({
        // 只读
        readOnly: true,
        // 启用滚动
        autoScrollEditorIntoView: true,
        // 启用换行
        wrap: true,
        fontSize: '15px',
        // 启用自动滚动到底部  0, 0.5, and 1
        scrollPastEnd: 0,
        // 显示参考线
        displayIndentGuides: false
      })
      getList();
      if (document.querySelector('.ace_scrollbar-v')) {
        // 隐藏滚动条
        document.querySelector('.ace_scrollbar-v').className = document.querySelector('.ace_scrollbar-v').className + 'display-scrollbar'
      }
    })
    
    // 查询日志列表
    function getList() {
      editor.setValue(`htto no TOOs
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8] ComoositeCantroller java.updateComposite:37)- 开始更新服务组合
      2021-08-0611:23:352021-08-06 11:23:35 WAR WARN[platform platform undetine undefined undefine nitp-nio-7702-exec-8 http-nio-7702-exec-8 CompositeNodejeva.init:94)(CompositeNodejava.init:94)-服务书点flow-ele-end中服务列表为空服务书点flow-ele-start中服务列表为空
      4 2021-08-0611:23:35 WARN oiatfor htto-n0-7702-ex6c-8 (CompositeManager.javn deleteComoosite:102)-环境中没有服务组合名Add sub 01,不需要进行删的
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8) (CompositeManeger.java.addCompoaite.87)-新增服务组合Add_sub 01完成
      2021-08-06 11:23:35 DNFO piatforrm undefined http-nio-7702-ex0c-81 CompositeManager.jave.addComposite:87)-新增服务相合Add_sub 01完成
      7 2021-08-0611:24:10 INFO pintfom undefinee htto-nio-7702-exac-10 ComoositeControllerjevs.odateComoosite:37)开始面新 组台
      8 2021-08-0611:24:10 INFO platform undefined http-ni0-7702-ecec-10 CompositeCantroller.isva.uodstec Composite:37)-开始更新服务组台
      2021-08
      htto no TOOs
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8] ComoositeCantroller java.updateComposite:37)- 开始更新服务组合
      2021-08-0611:23:352021-08-06 11:23:35 WAR WARN[platform platform undetine undefined undefine nitp-nio-7702-exec-8 http-nio-7702-exec-8 CompositeNodejeva.init:94)(CompositeNodejava.init:94)-服务书点flow-ele-end中服务列表为空服务书点flow-ele-start中服务列表为空
      4 2021-08-0611:23:35 WARN oiatfor htto-n0-7702-ex6c-8 (CompositeManager.javn deleteComoosite:102)-环境中没有服务组合名Add sub 01,不需要进行删的
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8) (CompositeManeger.java.addCompoaite.87)-新增服务组合Add_sub 01完成
      2021-08-06 11:23:35 DNFO piatforrm undefined http-nio-7702-ex0c-81 CompositeManager.jave.addComposite:87)-新增服务相合Add_sub 01完成
      7 2021-08-0611:24:10 INFO pintfom undefinee htto-nio-7702-exac-10 ComoositeControllerjevs.odateComoosite:37)开始面新 组台
      8 2021-08-0611:24:10 INFO platform undefined http-ni0-7702-ecec-10 CompositeCantroller.isva.uodstec Composite:37)-开始更新服务组台
      2021-08
      htto no TOOs
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8] ComoositeCantroller java.updateComposite:37)- 开始更新服务组合
      2021-08-0611:23:352021-08-06 11:23:35 WAR WARN[platform platform undetine undefined undefine nitp-nio-7702-exec-8 http-nio-7702-exec-8 CompositeNodejeva.init:94)(CompositeNodejava.init:94)-服务书点flow-ele-end中服务列表为空服务书点flow-ele-start中服务列表为空
      4 2021-08-0611:23:35 WARN oiatfor htto-n0-7702-ex6c-8 (CompositeManager.javn deleteComoosite:102)-环境中没有服务组合名Add sub 01,不需要进行删的
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8) (CompositeManeger.java.addCompoaite.87)-新增服务组合Add_sub 01完成
      2021-08-06 11:23:35 DNFO piatforrm undefined http-nio-7702-ex0c-81 CompositeManager.jave.addComposite:87)-新增服务相合Add_sub 01完成
      7 2021-08-0611:24:10 INFO pintfom undefinee htto-nio-7702-exac-10 ComoositeControllerjevs.odateComoosite:37)开始面新 组台
      8 2021-08-0611:24:10 INFO platform undefined http-ni0-7702-ecec-10 CompositeCantroller.isva.uodstec Composite:37)-开始更新服务组台
      2021-08htto no TOOs
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8] ComoositeCantroller java.updateComposite:37)- 开始更新服务组合
      2021-08-0611:23:352021-08-06 11:23:35 WAR WARN[platform platform undetine undefined undefine nitp-nio-7702-exec-8 http-nio-7702-exec-8 CompositeNodejeva.init:94)(CompositeNodejava.init:94)-服务书点flow-ele-end中服务列表为空服务书点flow-ele-start中服务列表为空
      4 2021-08-0611:23:35 WARN oiatfor htto-n0-7702-ex6c-8 (CompositeManager.javn deleteComoosite:102)-环境中没有服务组合名Add sub 01,不需要进行删的
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8) (CompositeManeger.java.addCompoaite.87)-新增服务组合Add_sub 01完成
      2021-08-06 11:23:35 DNFO piatforrm undefined http-nio-7702-ex0c-81 CompositeManager.jave.addComposite:87)-新增服务相合Add_sub 01完成
      7 2021-08-0611:24:10 INFO pintfom undefinee htto-nio-7702-exac-10 ComoositeControllerjevs.odateComoosite:37)开始面新 组台
      8 2021-08-0611:24:10 INFO platform undefined http-ni0-7702-ecec-10 CompositeCantroller.isva.uodstec Composite:37)-开始更新服务组台
      2021-08
      htto no TOOs
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8] ComoositeCantroller java.updateComposite:37)- 开始更新服务组合
      2021-08-0611:23:352021-08-06 11:23:35 WAR WARN[platform platform undetine undefined undefine nitp-nio-7702-exec-8 http-nio-7702-exec-8 CompositeNodejeva.init:94)(CompositeNodejava.init:94)-服务书点flow-ele-end中服务列表为空服务书点flow-ele-start中服务列表为空
      4 2021-08-0611:23:35 WARN oiatfor htto-n0-7702-ex6c-8 (CompositeManager.javn deleteComoosite:102)-环境中没有服务组合名Add sub 01,不需要进行删的
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8) (CompositeManeger.java.addCompoaite.87)-新增服务组合Add_sub 01完成
      2021-08-06 11:23:35 DNFO piatforrm undefined http-nio-7702-ex0c-81 CompositeManager.jave.addComposite:87)-新增服务相合Add_sub 01完成
      7 2021-08-0611:24:10 INFO pintfom undefinee htto-nio-7702-exac-10 ComoositeControllerjevs.odateComoosite:37)开始面新 组台
      8 2021-08-0611:24:10 INFO platform undefined http-ni0-7702-ecec-10 CompositeCantroller.isva.uodstec Composite:37)-开始更新服务组台
      2021-08
      htto no TOOs
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8] ComoositeCantroller java.updateComposite:37)- 开始更新服务组合
      2021-08-0611:23:352021-08-06 11:23:35 WAR WARN[platform platform undetine undefined undefine nitp-nio-7702-exec-8 http-nio-7702-exec-8 CompositeNodejeva.init:94)(CompositeNodejava.init:94)-服务书点flow-ele-end中服务列表为空服务书点flow-ele-start中服务列表为空
      4 2021-08-0611:23:35 WARN oiatfor htto-n0-7702-ex6c-8 (CompositeManager.javn deleteComoosite:102)-环境中没有服务组合名Add sub 01,不需要进行删的
      2021-08-0611:23:35 INFO platform undefined http-nio-7702-exec-8) (CompositeManeger.java.addCompoaite.87)-新增服务组合Add_sub 01完成
      2021-08-06 11:23:35 DNFO piatforrm undefined http-nio-7702-ex0c-81 CompositeManager.jave.addComposite:87)-新增服务相合Add_sub 01完成
      7 2021-08-0611:24:10 INFO pintfom undefinee htto-nio-7702-exac-10 ComoositeControllerjevs.odateComoosite:37)开始面新 组台
      8 2021-08-0611:24:10 INFO platform undefined http-ni0-7702-ecec-10 CompositeCantroller.isva.uodstec Composite:37)-开始更新服务组台
      2021-08`);
      nextTick(() => {
        // 光标跳转到几行几列  清楚设置内容后自动选中的效果
        editor.gotoLine(editor.session.getLength());
      })
    }
    
    
    
    </script>
    <style lang="scss" scoped>
    .app-container {
      #log-content {
        width: 100%;
        height: 70vh;
        .display-scrollbar {
          display: none;
        }
      }
    }
    </style>
    
    • 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
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    关于ace的setOptions配置https://github.com/ajaxorg/ace/wiki/Configuring-Ace

    中文对照

    选项名值类型默认可选值功能
    selectionStyleStringtextline|text选中样式
    highlightActiveLineBooleantrue-高亮当前行
    highlightSelectedWordBooleantrue-高亮选中文本
    readOnlyBooleanfalse-是否只读
    cursorStyleStringaceace|slim|smooth|wide光标样式
    mergeUndoDeltasString|Booleanfalsealways合并撤销
    behavioursEnabledBooleantrue-启用行为
    wrapBehavioursEnabledBooleantrue-启用换行
    autoScrollEditorIntoViewBooleanfalse-启用滚动
    copyWithEmptySelectionBooleantrue-复制空格
    useSoftTabsBooleanfalse-使用软标签
    navigateWithinSoftTabsBooleanfalse-软标签跳转
    enableMultiselectBooleanfalse-选中多处
    hScrollBarAlwaysVisibleBooleanfalse-纵向滚动条始终可见
    vScrollBarAlwaysVisibleBooleanfalse-横向滚动条始终可见
    highlightGutterLineBooleantrue-高亮边线
    animatedScrollBooleanfalse-滚动动画
    showInvisiblesBooleanfalse-显示不可见字符
    showPrintMarginBooleanfalse-显示打印边距
    printMarginColumnNumber80-设置页边距
    printMarginBoolean|Numberfalse-显示并设置页边距
    fadeFoldWidgetsBooleanfalse-淡入折叠部件
    showFoldWidgetsBooleantrue-显示折叠部件
    showLineNumbersBooleantrue-显示行号
    showGutterBooleantrue-显示行号区域
    displayIndentGuidesBooleantrue-显示参考线
    fontSizeNumber|Stringinherit-设置字号
    fontFamilyStringinherit设置字体
    maxLinesNumber--至多行数
    minLinesNumber--至少行数
    scrollPastEndBoolean|Number0-滚动位置
    fixedWidthGutterBooleanfalse-固定行号区域宽度
    themeString--主题引用路径,例如"ace/theme/textmate"
    scrollSpeedNumber--滚动速度
    dragDelayNumber--拖拽延时
    dragEnabledBooleantrue-是否启用拖动
    focusTimoutNumber--聚焦超时
    tooltipFollowsMouseBooleanfalse-鼠标提示
    firstLineNumberNumber1-起始行号
    overwriteBoolean--重做
    newLineModeStringautoauto|unix|windows新开行模式
    useWorkerBoolean--使用辅助对象
    useSoftTabsBoolean--使用软标签
    tabSizeNumber--标签大小
    wrapBoolean--换行
    foldStyleString-markbegin|markbeginend|manual折叠样式
    modeString--代码匹配模式,例如“ace/mode/text"
    enableBasicAutocompletionBoolean--启用基本自动完成
    enableLiveAutocompletionBoolean--启用实时自动完成
    enableSnippetsBoolean--启用代码段
    enableEmmetBoolean--启用Emmet
    useElasticTabstopsBoolean--使用弹性制表位

    4.ace-editor修改滚动条样式

    直接设置编辑器容器无法改动滚动条样式,深度监听ace生成的ace_scrollbar类名

    // 设置编辑器的滚动条样式
    :deep(.ace_scrollbar)::-webkit-scrollbar {
      height: 7px;
      width: 7px;
    }
    :deep(.ace_scrollbar)::-webkit-scrollbar-track {
      box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
      background-color: #272822; /* Matches ace monokai */
      border-radius: 10px;
    }
    :deep(.ace_scrollbar)::-webkit-scrollbar-thumb {
      background-color: darkgrey;
      outline: 1px solid slategrey;
      border-radius: 10px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    监听编辑器的任何更改可查看https://ajaxorg.github.io/ace-api-docs/classes/Ace.EditSession.html#on

    还有什么疑问请查看aceAPi文档https://ace.c9.io/

  • 相关阅读:
    Webview 和 React Native 中吸顶效果实现
    FastAPI 快速学习之 Flask 框架对比
    A Cooperative Approach to Particle Swarm Optimization
    python练习题集锦之一
    再学二分查找
    值得注意的: c++动态库、静态库、弱符号__attribute__((weak))以及extern之间的关系
    高性能 MySQL - 第六章 查询性能优化
    MySQL基础学习总结(二)
    SpringBoot2.7.9 配置文件加载方式
    计算机毕业设计Java桂林恒保健康防护有限公司官网(源码+系统+mysql数据库+Lw文档)
  • 原文地址:https://blog.csdn.net/weixin_45532305/article/details/126871801