2424
2525<script setup lang="ts">
2626
27- import { ref , watch } from ' vue'
27+ import { ref , useTemplateRef , watch } from ' vue'
2828import CodeBlock from ' ./CodeBlock.vue'
2929import { CODE_INDENT_SPACES } from ' @/constants'
3030
@@ -59,7 +59,7 @@ const formatCode = (codeToFormat: string, codeLang: string): string => {
5959}
6060
6161// ref to contentEditable element
62- const editableInput = ref < HTMLDivElement | null >( )
62+ const editableInputRef = useTemplateRef ( ' editableInput ' )
6363
6464// to hold editable code content
6565const editableCode = ref <string >(formatCode (props .code , props .lang ))
@@ -77,7 +77,7 @@ const cursorPosition = ref<number>(0)
7777 * @param eol (boolean, when true cursor is set to the end of the line)
7878 */
7979const setCursorPosition = (customPosition : number , eol : boolean ) => {
80- if (typeof window === ' undefined' || ! editableInput .value || ! editableInput .value .childNodes [0 ]) {
80+ if (typeof window === ' undefined' || ! editableInputRef .value || ! editableInputRef .value .childNodes [0 ]) {
8181 return
8282 }
8383 // select text from a window
@@ -87,24 +87,24 @@ const setCursorPosition = (customPosition: number, eol: boolean) => {
8787 const selectedRange = document .createRange ()
8888 let rangeNode = 0
8989 let rangePos = customPosition
90- for (let i = 0 ; i < editableInput .value .childNodes .length ; i ++ ) {
91- const contentLen = (editableInput .value .childNodes [i ]?.textContent || ' ' ).length
90+ for (let i = 0 ; i < editableInputRef .value .childNodes .length ; i ++ ) {
91+ const contentLen = (editableInputRef .value .childNodes [i ]?.textContent || ' ' ).length
9292 if (rangePos - contentLen <= 0 ) {
9393 break
9494 }
9595 rangeNode ++
9696 rangePos -= contentLen
97- if (editableInput .value .childNodes [i ]?.nodeName === ' BR' ) {
97+ if (editableInputRef .value .childNodes [i ]?.nodeName === ' BR' ) {
9898 rangePos --
9999 }
100100 }
101101
102- if (! editableInput .value .childNodes [rangeNode ]) return
102+ if (! editableInputRef .value .childNodes [rangeNode ]) return
103103
104104 if (eol ) {
105- rangePos = editableInput .value .childNodes [rangeNode ].textContent ?.length || 0
105+ rangePos = editableInputRef .value .childNodes [rangeNode ].textContent ?.length || 0
106106 }
107- selectedRange .setStart (editableInput .value .childNodes [rangeNode ], rangePos )
107+ selectedRange .setStart (editableInputRef .value .childNodes [rangeNode ], rangePos )
108108 // collapse the range at boundaries
109109 selectedRange .collapse (true )
110110
@@ -115,7 +115,7 @@ const setCursorPosition = (customPosition: number, eol: boolean) => {
115115 selection ?.addRange (selectedRange )
116116
117117 // focus the cursor
118- editableInput .value .focus ()
118+ editableInputRef .value .focus ()
119119}
120120
121121
@@ -124,19 +124,19 @@ const setCursorPosition = (customPosition: number, eol: boolean) => {
124124 * get cursor position
125125 */
126126const getCursorPosition = () => {
127- if (typeof window === ' undefined' || ! editableInput .value ) {
127+ if (typeof window === ' undefined' || ! editableInputRef .value ) {
128128 return 0
129129 }
130130 const selection = window .getSelection ()
131131 if (! selection ) {
132132 return 0
133133 }
134- const currentArray = editableInput .value .innerText .split (' ' )
134+ const currentArray = editableInputRef .value .innerText .split (' ' )
135135
136136 const range = selection .rangeCount > 0 ? selection ?.getRangeAt (0 ) : null
137137 if (range ) {
138138 const clonedRange = range ?.cloneRange ()
139- clonedRange ?.selectNodeContents (editableInput .value )
139+ clonedRange ?.selectNodeContents (editableInputRef .value )
140140 clonedRange ?.setEnd (range .endContainer , range ?.endOffset )
141141
142142 // here we have a string but no \n -s , so we would need to count number of \n in this fragment
@@ -202,8 +202,8 @@ const handleInput = (e: Event) => {
202202 // inject indentations
203203
204204 cText = cText .substring (0 , cursorPosition .value ) + Array (paddings + 1 ).join (' ' ) + cText .substring (cursorPosition .value )
205- if (editableInput .value ) {
206- editableInput .value .innerText = cText
205+ if (editableInputRef .value ) {
206+ editableInputRef .value .innerText = cText
207207 }
208208 // force presented code to the resulting one
209209 presentedCode .value = cText
@@ -242,15 +242,15 @@ const handleFocusOut = (e: Event) => {
242242 const cText = (e .target as HTMLElement ).innerText
243243 let resText = formatCode (cText , props .lang )
244244 editableCode .value = resText
245- if (editableInput .value ) {
246- editableInput .value .innerText = editableCode .value
245+ if (editableInputRef .value ) {
246+ editableInputRef .value .innerText = editableCode .value
247247 }
248248 presentedCode .value = resText
249249 emit (' request-body-changed' , resText )
250250}
251251
252252
253- watch (() => ({ code: props .code , lang: props .lang , editableInput: editableInput .value }),
253+ watch (() => ({ code: props .code , lang: props .lang , editableInput: editableInputRef .value }),
254254 ({ code : newCode , lang : newLang , editableInput : newEditableInput }) => {
255255 if (newCode !== newEditableInput ?.innerText ) {
256256 editableCode .value = formatCode (newCode , newLang )
0 commit comments