11<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" />
34</template >
45
56<script lang="ts">
67import { defineComponent , type PropType } from " vue" ;
78import { PrismEditor } from " vue-prism-editor" ;
9+ import makeHighlight , { CONFIGURED_LANGS , type CONFIGURED_LANGS_TYPE } from " vue-inbrowser-prismjs-highlighter" ;
810import debounce from " debounce" ;
911
1012import " vue-prism-editor/dist/prismeditor.min.css" ;
1113
12- import makeHighlight , { CONFIGURED_LANGS , type CONFIGURED_LANGS_TYPE } from " ./utils/highlight" ;
1314
1415const UPDATE_DELAY = 300 ;
1516
@@ -37,7 +38,7 @@ export default defineComponent({
3738 prismLang: {
3839 type: String as PropType <CONFIGURED_LANGS_TYPE >,
3940 default: " html" ,
40- validator : (val : string ) => CONFIGURED_LANGS .includes (val as CONFIGURED_LANGS_TYPE ),
41+ validator : (val : CONFIGURED_LANGS_TYPE ) => CONFIGURED_LANGS .includes (val ),
4142 },
4243 jsx: {
4344 type: Boolean ,
@@ -57,10 +58,7 @@ export default defineComponent({
5758 * editor repainted every keystroke
5859 */
5960 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 >>,
6462 };
6563 },
6664 async beforeMount() {
@@ -69,16 +67,51 @@ export default defineComponent({
6967 * load javascript first then load jsx
7068 * order is not guaranteed to work in ESmodules imports
7169 */
72- this .highlight = await makeHighlight ();
70+ this .highlight = await makeHighlight (' VueLive-squiggles ' );
7371 },
7472 methods: {
7573 highlighter(code : string ) {
7674 return this .highlight (this .prismLang , this .jsx )(
7775 code ,
78- this .squiggles && this .error && this .error . loc
76+ this .squiggles && this .adaptedErrorLoc ? this .adaptedErrorLoc : undefined
7977 );
8078 },
8179 },
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+ },
82115 watch: {
83116 code(value ) {
84117 this .updatePreview (value );
@@ -103,4 +136,31 @@ export default defineComponent({
103136.VueLive-squiggles {
104137 border-bottom : 2px dotted red ;
105138}
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+ }
106166 </style >
0 commit comments