Skip to content

Commit 7866d38

Browse files
committed
高亮支持codemirror
1 parent df4f300 commit 7866d38

File tree

5 files changed

+54
-51
lines changed

5 files changed

+54
-51
lines changed

README.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -241,51 +241,51 @@ type为`code`时content类型为字符串,直接传入文本或代码即可
241241

242242
code类型消息支持 `highlight.js` 高亮显示
243243

244-
首先你需要配置 **Highlight.js**
244+
首先你需要配置 **Highlight.js**,在main.js入口安装,详细配置见[https://www.npmjs.com/package/highlight.js](https://www.npmjs.com/package/highlight.js)
245245

246246
```js
247-
import Hljs from 'highlight.js';
248-
import 'highlight.js/styles/tomorrow-night-bright.css';
249-
250-
let Highlight = {};
251-
Highlight.install = function (Vue) {
252-
Vue.directive('highlight', {
253-
inserted: function (el) {
254-
let blocks = el.querySelectorAll('pre code');
255-
blocks.forEach(block => {
256-
let ul = document.createElement("ul");
257-
let rowCount = block.outerHTML.split('\n').length;
258-
for (let i = 1; i <= rowCount; i++) {
259-
let li = document.createElement("li")
260-
let text = document.createTextNode(i)
261-
li.appendChild(text)
262-
ul.appendChild(li)
263-
}
264-
ul.className = 'pre-numbering'
265-
block.parentNode.appendChild(ul)
266-
Hljs.highlightBlock(block)
267-
})
268-
},
269-
componentUpdated: function (el) {
270-
let blocks = el.querySelectorAll('pre code');
271-
for (let i = 0; i < blocks.length; i++) {
272-
Hljs.highlightBlock(blocks[i]);
273-
}
274-
}
275-
})
276-
};
247+
import Terminal from 'vue-web-terminal'
248+
import hljs from 'highlight.js'
249+
import java from 'highlight.js/lib/languages/java'
250+
import vuePlugin from "@highlightjs/vue-plugin"
251+
import 'highlight.js/styles/tomorrow-night-bright.css'
277252

278-
export default Highlight;
253+
hljs.registerLanguage('java', java)
254+
Vue.use(vuePlugin)
255+
Vue.use(Terminal, {highlight: true})
279256
```
280257

281-
然后在载入 Terminal 的入口修改配置就可以高亮显示了
258+
vue2版本推荐
259+
```json
260+
{
261+
"@highlightjs/vue-plugin": "^1.0.2",
262+
"highlight.js": "^10.7.3"
263+
}
264+
```
282265

283-
```js
284-
import Terminal from 'vue-web-terminal'
285-
import Hljs from '@/Highlight.js'
266+
#### codemirror 代码高亮
286267

287-
Vue.use(Hljs)
288-
Vue.use(Terminal, {highlight: true})
268+
code类型消息也支持 `codemirror` 高亮显示,详细配置见[https://www.npmjs.com/package/vue-codemirror](https://www.npmjs.com/package/vue-codemirror)
269+
270+
同样只需要在main.js入口安装即可,版本推荐:`"vue-codemirror": "^4.0.6"`
271+
```js
272+
import VueCodemirror from 'vue-codemirror'
273+
import 'codemirror/lib/codemirror.css'
274+
import 'codemirror/theme/darcula.css'
275+
import 'codemirror/mode/clike/clike.js'
276+
import 'codemirror/addon/edit/closebrackets.js'
277+
278+
Vue.use(VueCodemirror)
279+
Vue.use(Terminal, {
280+
codemirror: {
281+
tabSize: 4,
282+
mode: 'text/x-java',
283+
theme: "darcula",
284+
lineNumbers: true,
285+
line: true,
286+
smartIndent: true
287+
}
288+
})
289289
```
290290

291291
### table

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-web-terminal",
3-
"version": "1.2.5",
3+
"version": "1.3.0",
44
"private": false,
55
"scripts": {
66
"serve": "vue-cli-service serve",
@@ -28,7 +28,6 @@
2828
"vue-template-compiler": "^2.6.11",
2929
"webpack": "^5.69.0",
3030
"webpack-cli": "^4.9.2",
31-
"highlight.js": "^11.4.0",
3231
"uglifyjs-webpack-plugin": "^2.1.3"
3332
},
3433
"eslintConfig": {

src/Terminal.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@
8181
</option>
8282
</select>
8383
</span>
84-
<div v-else-if="item.type === 'code'"
85-
style="position: relative;max-height: 500px;overflow: auto;font-size: 20px">
86-
<div v-if="$terminalOptions.highlight" v-highlight>
87-
<pre style="margin:0 0 0 30px"><code v-html="item.content"></code></pre>
84+
<div v-else-if="item.type === 'code'" class="t-code">
85+
<div v-if="$terminalOptions.highlight">
86+
<highlightjs ref="highlightjs" autodetect :code="item.content"/>
8887
</div>
89-
<div v-else style="background: rgb(39 50 58)">
88+
<div v-else-if="$terminalOptions.codemirror">
89+
<codemirror ref="codemirror" v-model="item.content" :options="$terminalOptions.codemirror"/>
90+
</div>
91+
<div v-else style="background: rgb(39 50 58);">
9092
<pre style="padding: 1em;margin: 0"><code style="font-size: 15px" v-html="item.content"></code></pre>
9193
</div>
9294
</div>

src/css/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,11 @@ pre {
102102

103103
.t-a:hover {
104104
color: white;
105+
}
106+
107+
.t-code {
108+
position: relative;
109+
max-height: 500px;
110+
overflow: auto;
111+
font-size: 20px;
105112
}

yarn.lock

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4634,16 +4634,11 @@ hex-color-regex@^1.1.0:
46344634
resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e"
46354635
integrity sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==
46364636

4637-
highlight.js@^10.7.1:
4637+
highlight.js@^10.7.1, highlight.js@^10.7.3:
46384638
version "10.7.3"
46394639
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-10.7.3.tgz#697272e3991356e40c3cac566a74eef681756531"
46404640
integrity sha512-tzcUFauisWKNHaRkN4Wjl/ZA07gENAjFl3J/c480dprkGTg5EQstgaNFqBfUqCq54kZRIEcreTsAgF/m2quD7A==
46414641

4642-
highlight.js@^11.4.0:
4643-
version "11.4.0"
4644-
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-11.4.0.tgz#34ceadd49e1596ee5aba3d99346cdfd4845ee05a"
4645-
integrity sha512-nawlpCBCSASs7EdvZOYOYVkJpGmAOKMYZgZtUqSRqodZE0GRVcFKwo1RcpeOemqh9hyttTdd5wDBwHkuSyUfnA==
4646-
46474642
hmac-drbg@^1.0.1:
46484643
version "1.0.1"
46494644
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1"

0 commit comments

Comments
 (0)