测试平台系列(57) 美化代码编辑器.md

2022/6/13 测试平台接口测试FastApiPythonReact

Hello, 大家好~我是米洛,欢注迎我的关公号众米洛的测开日记!

# 回顾

上节我们编写了JSON比对相关方法,虽然还没有运用到断言之中,但丝毫不影响我们插播另一篇文章。

其实我们的AceEditor引入许多主题的时候会有些卡顿,目前还不知道是主题太多呢,还是补全代码太多,所以我们先减少下主题再说。

但由于个人一直没有特别喜欢的主题,所以其实很是苦恼,但我看到leetcode的代码编辑器,又很羡慕,它的monaco-editor支持了material,如图。

leetcode的编辑器如此潇洒

# 调研一番

在经历了各种调研之后,发现了2个极品网站。

http://tmtheme-editor.herokuapp.com/#!/editor/theme/Material%20One%20Dark (opens new window)

  • 一键生成主题文件

    但是主题文件是tmTheme格式的,不是太好用。

最左侧可以选各种主题

我们找到自己最想要的2个: material one dark和atom dark

可以看到主题非常精美,是我想要的

点击Download就可以下载到对应的主题文件啦,其实这文件是用于vscode之中的,但咱们想嵌入WebIDE怎么弄呢?

我们还有下面一个网站:

https://bitwiser.in/monaco-themes/ (opens new window)

这个网站可以把主题文件转为JSON文件,这样就能嵌入到monaco编辑器了!

# 言归正传

不过话说回来,我们因为是要改造AceEditor,所以我们要找找AceEditor怎么添加自定义主题

苦心人,天不负。在Ace的github官网找到了这样的文档:

我们来翻译一下他的步骤:

  1. 把代码拉下来,进入tool文件夹,并安装依赖
  2. 把tmtheme文件放到ththemes目录里面
  3. 更新ththeme.js文件,包含我们的新主题
  4. 执行node tmtheme.js

简单粗暴!!!

# 试验一下

先把下载下来的One Dark文件扔进去

把主题写进去

我这省略了npm install的步骤,我们直接node执行ththeme.js

似乎没看到报错

我们去检查下有没有文件生成:

最终在ace/lib/ace/theme文件夹找到嫌疑人

# 结合react-ace

因为我们用的是React组件,那么我们势必要像之前的主题一样import.

但我们生成了js和css,没事我们来看下比如monokai是怎么玩的:

可以看到他把css直接写入到cssText了

这样就达到了只引入js就能有样式的目的,好家伙!!!

话不多说,我们也来试验一下:

照搬monokai的代码,把它require时候用的相对路径都改为绝对路径,最后把cssText用``括起来写进去。

cssClass等字段名字也要改掉哦~

ace.define("ace/theme/material-one-dark",["require","exports","module","ace/lib/dom"], function(require, exports, module) {

  exports.isDark = false;
  exports.cssClass = "ace-material-one-dark";
  exports.cssText = `
.ace-material-one-dark .ace_gutter {
  background: #272B33;
  color: rgb(103,111,122)
}

.ace-material-one-dark .ace_print-margin {
  // width: 1px;
  background: #e8e8e8
}

.ace-material-one-dark {
  background-color: #272B33;
  color: #A6B2C0
}

.ace-material-one-dark .ace_cursor {
  color: #528BFF
}

.ace-material-one-dark .ace_marker-layer .ace_selection {
  background: #3D4350
}

.ace-material-one-dark.ace_multiselect .ace_selection.ace_start {
  box-shadow: 0 0 3px 0px #272B33;
  border-radius: 2px
}

.ace-material-one-dark .ace_marker-layer .ace_step {
  background: rgb(198, 219, 174)
}

.ace-material-one-dark .ace_marker-layer .ace_bracket {
  margin: -1px 0 0 -1px;
  border: 1px solid #747369
}

.ace-material-one-dark .ace_marker-layer .ace_active-line {
  background: #2B313A
}

.ace-material-one-dark .ace_gutter-active-line {
  background-color: #2B313A
}

.ace-material-one-dark .ace_marker-layer .ace_selected-word {
  border: 1px solid #3D4350
}

.ace-material-one-dark .ace_fold {
  background-color: #61AEEF;
  border-color: #A6B2C0
}

.ace-material-one-dark .ace_keyword {
  color: #C679DD
}

.ace-material-one-dark .ace_keyword.ace_operator {
  color: #A6B2C0
}

.ace-material-one-dark .ace_keyword.ace_other.ace_unit {
  color: #D2945D
}

.ace-material-one-dark .ace_constant {
  color: #D2945D
}

.ace-material-one-dark .ace_constant.ace_numeric {
  color: #D2945D
}

.ace-material-one-dark .ace_constant.ace_character.ace_escape {
  color: #57B6C2
}

.ace-material-one-dark .ace_support.ace_function {
  color: #57B6C2
}

.ace-material-one-dark .ace_support.ace_class {
  color: #E5C17C
}

.ace-material-one-dark .ace_storage {
  color: #C679DD
}

.ace-material-one-dark .ace_invalid.ace_illegal {
  color: #272B33;
  background-color: #f2777a
}

.ace-material-one-dark .ace_invalid.ace_deprecated {
  color: #272B33;
  background-color: #d27b53
}

.ace-material-one-dark .ace_string {
  color: #90C378
}

.ace-material-one-dark .ace_string.ace_regexp {
  color: #57B6C2
}

.ace-material-one-dark .ace_comment {
  font-style: italic;
  color: #59626F
}

.ace-material-one-dark .ace_variable {
  color: #DF6A73
}

.ace-material-one-dark .ace_meta.ace_selector {
  color: #C679DD
}

.ace-material-one-dark .ace_entity.ace_other.ace_attribute-name {
  color: #D2945D
}

.ace-material-one-dark .ace_entity.ace_name.ace_function {
  color: #61AEEF
}

.ace-material-one-dark .ace_entity.ace_name.ace_tag {
  color: #DF6A73
}

.ace-material-one-dark .ace_markup.ace_list {
  color: #DF6A73
}
`;

  var dom = require("ace/lib/dom");
  dom.importCssString(exports.cssText, exports.cssClass);
});                (function() {
  ace.require(["ace/theme/ace-material-one-dark"], function(m) {
    if (typeof module == "object" && typeof exports == "object" && module) {
      module.exports = m;
    }
  });
})();

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

最终文件如上,我们给他取名叫MaterialOneDark.js,然后在编辑器引入:

设置为默认主题

看看效果:

非常非常非常nice

心满意足,打完收工~!

在线体验地址: http://pity.buzz (opens new window)