tags:[CodeEditor,代码编辑器] categories:[技术]

CodeEditor 代码编辑器

基于 prismjs 实现的轻量级代码编辑器,支持自定义引用样式与语法。

特点:

CodeEditor 组件代码编辑

  • 目前 ESLint 有两种流派AirbnbStandard 前者推荐加分号,后者不推荐

  • 注意行号样式匹配问题。JS 代码结尾需要加分号

>{}
<template>
  <div class="test-code-page">
    <CodeEditor
      class="body"
      :lang="the.code.lang"
      :text="the.code.body"
      @onEvent="onCodeEditorEvent"
    >
      <template v-slot:tools>
        <Icon type="icon-shezhi" title="设置" @click="the.show = !the.show" />

        <Select v-model="the.code.lang" @onEvent="onLangEvent">
          <option
            v-for="(key, index) in the.code.langList"
            :key="index"
            :value="key"
          >
            {{ key }}
          </option>
        </Select>
      </template>

      <!-- 模板内容 -->
      let _str = ' 77.88888 '; let _jsonstr = `{id:123}`;
    </CodeEditor>
  </div>
</template>

<script>
export default {
  setup(props, context) {
    const { reactive } = window.$plus.vue;

    const the = reactive({
      code: {
        /** 代码语言类别 */
        lang: 'vue',
        langList: [
          'vue',
          'c#',
          'js',
          'css',
          'html',
          'md',
          'ts',
          'py',
          'sql',
          'xml-doc',
          'json',
          'bash',
          'c',
        ],
        title: '在线帮助',
        path: '/page/help/index',
        // 版本
        version: '0.0.1',
        // 编译后代码
        code: '',
        // 源代码
        body: '',
      },
    });

    const onCodeEditorEvent = (resp) => {
      console.log('onCodeEditorEvent', resp);
      the.code.body = resp.value;
    };

    const onLangEvent = (data) => {
      console.log('onLangEvent', the.code.lang, data);
    };

    return { the, onCodeEditorEvent, onLangEvent };
  },
};
</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
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

CodeEditor props

属性说明类型默认值
named 0.4.2组件命名Stringinput
v-model双向绑定数据String-
disabled设置输入框为只读Booleanfalse
lang代码语言Stringvue
css样式Stringqv-code-dark
focus设置为焦点Booleanfalse
tabSize空格间距Number2
newPaddingChars新节点起始定义Array['{', '>', ':']
selfClosingChars结束节点定义Object{open: ['{', '(', '['],close: ['}', ')', ']'],}

CodeEditor events

事件名说明
onEvent点击触发回调事件
cmd图标触发指令 update 更新值string
named自定义组件命名string
value代码内容string

CodeEditor slot

名称说明
tools工具栏
主体内容

prism 引用高亮与样式

  • index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>QVE</title>

    <!-- 引用样式-->
    <link href="/static/plus/prism/tom/prism.css" rel="stylesheet" />
  </head>

  <body>
    <div id="appTopBody"></div>
    <div id="app"></div>
    <div id="endOfBody"></div>

    <!-- 引用代码的 script 标签加上手动 data-manual 标记-->
    <script src="/static/plus/prism/tom/prism.js" data-manual></script>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

语法高亮配置 PrismJS

版本: 1.22.0 主题:tomorrow 语法:languages=markup+css+clike+javascript+bash+c+csharp+json+json5+plsql+python+sql+typoscript+xml-doc 外挂:line-highlight,line-numbers,highlight-keywords,normalize-whitespace

https://prismjs.com/download.html#themes=prism-tomorrow&languages=markup+css+clike+javascript+bash+c+csharp+json+json5+plsql+python+sql+typoscript+xml-doc&plugins=line-highlight+line-numbers+highlight-keywords+normalize-whitespace

Last Updated: