Skip to content

Commit befa127

Browse files
committed
add: Support line numbers
1 parent 0cfd876 commit befa127

File tree

4 files changed

+86
-13
lines changed

4 files changed

+86
-13
lines changed

src/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Check out https://vuejs.org/api/sfc-script-setup.html#script-setup
44
import HelloWorld from './components/HelloWorld.vue'
55
import HighCode from './components/HighCode.vue'
6+
// import textloader from './textLoader.js'
67
// import 'vue-highlight-code/dist/style.css'
78
const value = `import { HighCode } from 'vue-highlight-code';
89
import 'vue-highlight-code/dist/style.css';
@@ -14,6 +15,8 @@ export default {
1415
1516
}
1617
}`
18+
// const demoJS = textloader(import.meta.globEager("./demo.js"))
19+
// console.log(demoJS,'demoJS')
1720
const light = 'light'
1821
const vue = 'vue'
1922
const dark = 'dark'

src/components/HighCode.vue

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup>
22
import hljs from 'highlight.js'
33
import CopyCode from './CopyCode.vue'
4-
import { computed } from 'vue'
4+
import { computed, nextTick, ref } from 'vue'
55
import TypeShow from './TypeShow.vue'
66
const props = defineProps({
77
copy: {
@@ -48,16 +48,20 @@ const props = defineProps({
4848
codeValue: {
4949
type: String,
5050
requied: true,
51-
default: ''
51+
default: '',
5252
},
5353
fontSize: {
5454
type: Number,
55-
default: 18
56-
}
55+
default: 18,
56+
},
57+
codeLines: {
58+
type: Boolean,
59+
default: false,
60+
},
5761
})
5862
const langName = props.langName || props.lang
5963
const font_size = props.fontSize
60-
const languageClass = 'hljs language-'+props.lang
64+
const languageClass = 'hljs language-' + props.lang
6165
// const theme = 'dark'
6266
const vHighlight = {
6367
bind(el, binding) {
@@ -73,15 +77,27 @@ const vHighlight = {
7377
el.textContent = binding.value
7478
hljs.highlightElement(el)
7579
},
76-
updated(el, binding) {
77-
// console.log(el, binding, '222')
78-
el.textContent = binding.value
79-
hljs.highlightElement(el)
80-
},
80+
// updated(el, binding) {
81+
// // console.log(el, binding, '222')
82+
// el.textContent = binding.value
83+
// hljs.highlightElement(el)
84+
// },
8185
}
8286
const border_radius = '12px'
8387
const withoutHeader = true
8488
89+
let arr = ref([])
90+
91+
const codeArea = ref(null)
92+
nextTick(() => {
93+
const preCodeHeightDemo = codeArea.value.offsetHeight
94+
console.log(preCodeHeightDemo, 'preCodeHeight')
95+
const count = Math.floor(preCodeHeightDemo / 24)
96+
for (let i = 1; i <= count; i++) {
97+
arr.value.push(i)
98+
}
99+
// console.log(arr,'arr')
100+
})
85101
</script>
86102
<template>
87103
<div
@@ -105,8 +121,24 @@ const withoutHeader = true
105121
<TypeShow v-if="nameShow" :TL="langName"></TypeShow>
106122
<CopyCode v-if="copy" :codeValue="props.codeValue"></CopyCode>
107123
</div>
124+
<!-- <div class="code_area">
125+
<div>1</div>
126+
</div> -->
127+
<div class="code_area_lines">
128+
<div
129+
:class="{
130+
dark: props.theme === 'dark',
131+
light: props.theme === 'light',
132+
}"
133+
v-for="cur in arr"
134+
class="code_area_lines_item"
135+
>
136+
{{ cur }}
137+
</div>
138+
</div>
108139
<div
109140
class="code_area"
141+
ref="codeArea"
110142
:style="{
111143
borderBottomLeftRadius: border_radius,
112144
borderBottomRightRadius: border_radius,
@@ -115,8 +147,7 @@ const withoutHeader = true
115147
}"
116148
:class="{ srollbar_style: props.scrollStyleBool === true }"
117149
>
118-
<pre
119-
>
150+
<pre>
120151
<code
121152
v-highlight="props.codeValue"
122153
:class="languageClass"
@@ -140,8 +171,11 @@ const withoutHeader = true
140171
overflow: hidden;
141172
padding: 20px;
142173
padding-top: 0px;
174+
padding-left: 24px;
143175
overflow: overlay;
144176
border-radius: 5px;
177+
display: flex;
178+
145179
&.srollbar_style::-webkit-scrollbar-track {
146180
background-color: #eee;
147181
// border-radius: 5px;
@@ -168,7 +202,7 @@ const withoutHeader = true
168202
pre code {
169203
// padding: 0px 20px 20px 20px;
170204
font-family: Consolas, Monaco, monospace;
171-
line-height: 1.5;
205+
line-height: 24px;
172206
font-size: 16px;
173207
}
174208
}
@@ -183,6 +217,35 @@ const withoutHeader = true
183217
.wrapper-content::-webkit-scrollbar {
184218
width: 6px;
185219
}
220+
221+
.code_area_lines {
222+
display: flex;
223+
flex-direction: column;
224+
width: 10px;
225+
// justify-content: center;
226+
padding-top: 40px;
227+
align-items: center;
228+
position: absolute;
229+
transform: translateX(6px);
230+
231+
&_item {
232+
height: 24px;
233+
width: 10px;
234+
font-size: 12px;
235+
color: #adb5bd;
236+
text-align: center;
237+
display: flex;
238+
justify-content: center;
239+
align-items: center;
240+
opacity: 0.6;
241+
&.dark {
242+
color: #adb5bd;
243+
}
244+
&.light {
245+
color: #212529;
246+
}
247+
}
248+
}
186249
</style>
187250

188251
<style>

src/demo.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log()
2+

src/textLoader.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = function (content) {
2+
this.cacheable && this.cacheable();
3+
this.value = content;
4+
return "module.exports = " + JSON.stringify(content);
5+
}

0 commit comments

Comments
 (0)