1
1
<template >
2
- <PrismEditor v-model =" stableCode" @update:modelValue =" updatePreview" :highlight =" highlighter" v-bind =" editorProps" />
2
+ <PrismEditor :class =" { 'VueLive-LineNumbers': editorProps.lineNumbers }" v-model =" stableCode"
3
+ @update:modelValue =" updatePreview" :highlight =" highlighter" v-bind =" editorProps" :lineNumbers =" false" />
3
4
</template >
4
5
5
6
<script lang="ts">
6
7
import { defineComponent , type PropType } from " vue" ;
7
8
import { PrismEditor } from " vue-prism-editor" ;
9
+ import makeHighlight , { CONFIGURED_LANGS , type CONFIGURED_LANGS_TYPE } from " vue-inbrowser-prismjs-highlighter" ;
8
10
import debounce from " debounce" ;
9
11
10
12
import " vue-prism-editor/dist/prismeditor.min.css" ;
11
13
12
- import makeHighlight , { CONFIGURED_LANGS , type CONFIGURED_LANGS_TYPE } from " ./utils/highlight" ;
13
14
14
15
const UPDATE_DELAY = 300 ;
15
16
@@ -37,7 +38,7 @@ export default defineComponent({
37
38
prismLang: {
38
39
type: String as PropType <CONFIGURED_LANGS_TYPE >,
39
40
default: " html" ,
40
- validator : (val : string ) => CONFIGURED_LANGS .includes (val as CONFIGURED_LANGS_TYPE ),
41
+ validator : (val : CONFIGURED_LANGS_TYPE ) => CONFIGURED_LANGS .includes (val ),
41
42
},
42
43
jsx: {
43
44
type: Boolean ,
@@ -57,10 +58,7 @@ export default defineComponent({
57
58
* editor repainted every keystroke
58
59
*/
59
60
stableCode: this .code ,
60
- highlight: (() => (code : string ) => code ) as (
61
- lang : CONFIGURED_LANGS_TYPE ,
62
- jsxInExamples : boolean
63
- ) => (code : string , errorLoc : any ) => string ,
61
+ highlight: (() => (code : string ) => code ) as Awaited <ReturnType <typeof makeHighlight >>,
64
62
};
65
63
},
66
64
async beforeMount() {
@@ -69,16 +67,51 @@ export default defineComponent({
69
67
* load javascript first then load jsx
70
68
* order is not guaranteed to work in ESmodules imports
71
69
*/
72
- this .highlight = await makeHighlight ();
70
+ this .highlight = await makeHighlight (' VueLive-squiggles ' );
73
71
},
74
72
methods: {
75
73
highlighter(code : string ) {
76
74
return this .highlight (this .prismLang , this .jsx )(
77
75
code ,
78
- this .squiggles && this .error && this .error . loc
76
+ this .squiggles && this .adaptedErrorLoc ? this .adaptedErrorLoc : undefined
79
77
);
80
78
},
81
79
},
80
+ computed: {
81
+ adaptedErrorLoc() {
82
+ // for now, we only support error in vsg format.
83
+ // TODO: figure out how SourceMaps work and use them to support vue-sfc format
84
+ if (this .prismLang !== ' vsg' ) {
85
+ return undefined
86
+ }
87
+
88
+ const scriptEnd = / \n [\t ] * </ .exec (this .stableCode )
89
+ const scriptCode = scriptEnd ? this .stableCode .slice (0 , scriptEnd .index + 1 ) : ' '
90
+ const linesInScriptCode = (/ ^ [\t ] * </ .test (scriptCode ) ? 0 : (scriptCode .match (/ \n / g )?.length || 0 )) + 1
91
+
92
+ return this .error && this .error .loc ?
93
+ this .error .loc .start ?
94
+ {
95
+ start: {
96
+ ... this .error .loc .start ,
97
+ line: this .error .loc .start .line + linesInScriptCode ,
98
+ },
99
+ end: {
100
+ ... this .error .loc .end ,
101
+ line: this .error .loc .end .line + linesInScriptCode ,
102
+ },
103
+ } : this .error .loc .line ? {
104
+ start: {
105
+ ... this .error .loc ,
106
+ line: this .error .loc .line + linesInScriptCode ,
107
+ },
108
+ end: {
109
+ ... this .error .loc ,
110
+ line: this .error .loc .line + linesInScriptCode ,
111
+ },
112
+ } : undefined : undefined
113
+ },
114
+ },
82
115
watch: {
83
116
code(value ) {
84
117
this .updatePreview (value );
@@ -103,4 +136,31 @@ export default defineComponent({
103
136
.VueLive-squiggles {
104
137
border-bottom : 2px dotted red ;
105
138
}
139
+
140
+ .VueLive-LineNumbers.prism-editor-wrapper pre .prism-editor__editor ,
141
+ .VueLive-LineNumbers.prism-editor-wrapper textarea .prism-editor__textarea {
142
+ padding-left : 2.5rem ;
143
+ }
144
+
145
+
146
+ .VueLive-LineNumbers pre .prism-editor__editor {
147
+ counter-reset : step;
148
+ counter-increment : step 0 ;
149
+ }
150
+
151
+ .VueLive-LineNumbers pre .line {
152
+ position : relative ;
153
+ }
154
+
155
+ .VueLive-LineNumbers pre .line ::before {
156
+ content : counter (step );
157
+ counter-increment : step;
158
+ white-space : nowrap ;
159
+ width : 2rem ;
160
+ position : absolute ;
161
+ left : -2.5rem ;
162
+ display : inline-block ;
163
+ text-align : right ;
164
+ color : rgba (255 , 255 , 255 , .4 )
165
+ }
106
166
</style >
0 commit comments