22
schangxiang@126.com
2025-05-21 05c47e0a832bbd2b8bafb732fc55c1ef8e9b97e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!-- 
 
v-model="双向绑定"
 
:modelValue="表达式"
@update:modelValue="(修改后的表达式)=>{}"
 
inputCodeRef.insertCode('在焦点处插入代码')
 
@update:focusWord="(光标处的单词)=>{}"
 
 -->
<template>
  <div class="inputCode">
    <div
      ref="inputEl"
      class="input"
      :contenteditable="(contentEditable as any)"
      spellcheck="false"
      @input="input"
      @click="click"
      .onblur="saveRange"
    ></div>
    <div class="highlight" v-html="codeHighlighted"></div>
  </div>
</template>
<script setup lang="ts">
import { computed, defineEmits, ref, watch } from 'vue'
 
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue', 'update:focusWord'])
 
// code ----------------------------------------
 
let code = ref('')
watch(
  () => props.modelValue,
  () => {
    code.value = props.modelValue || ''
  },
  { immediate: true }
)
 
// inputEl ----------------------------------------
 
let inputEl = ref()
const contentEditable = 'plaintext-only'
 
function updateInputInnerText() {
  if (!inputEl) {
    return
  }
 
  // 避免影响光标
  if (getInnerText() === code.value) return
 
  inputEl.value.innerText = code.value
}
 
function getInnerText() {
  const innerText = inputEl.value?.innerText || ''
  return fixInnerTextLn(innerText)
}
 
// 非 'plaintext-only' innerText \n 会比页面上的多
// 1 1
// 2 3
// 3 5
function fixInnerTextLn(innerText: string) {
  if (inputEl.value?.contentEditable === contentEditable) {
    return innerText
  }
 
  return innerText.replace(/\n+/g, function ($and) {
    const length = $and.split('').length
    const lengthFixed = Math.floor((length + 1) / 2)
 
    return Array(lengthFixed).fill('\n').join('')
  })
}
 
// highlight ----------------------------------------
 
let codeHighlighted = computed(() => {
  updateInputInnerText()
  return highlight(code.value)
})
 
function highlight(value: string) {
  let html = value
 
  html = html
    .replace(/\b(true|false)\b/g, '👾b $& b👾')
    .replace(/\b[\d.]+/gi, '👾n $& n👾') // number
    .replace(/"(\\.|.)*?"/gi, '👾s $& s👾') // string
    .replace(/[!%^&*\-+=|<>/]+/gi, '👾p $& p👾') // +
    .replace(/\b(\w+)\s*(?=\()/gi, '👾f $& f👾') // function()
    .replace(/\[.*?\]/gi, '👾k $& k👾') // [field]
 
  html = html.replace(/</g, '&lt;').replace(/>/g, '&gt;')
 
  html = html
    .replace(/👾b (.*?) b👾/g, '<span style="color:#fe72f3">$1</span>')
    .replace(/👾n (.*?) n👾/g, '<span style="color:#57b6ff">$1</span>')
    .replace(/👾s (.*?) s👾/g, '<span style="color:#ffff66">$1</span>')
    .replace(/👾p (.*?) p👾/g, '<span style="color:#9B9B9B">$1</span>')
    .replace(/👾f (.*?) f👾/g, '<span style="color:#23DBBB">$1</span>')
    .replace(/👾k (.*?) k👾/g, '<span style="color:#febf72">$1</span>')
 
  html = html.replace(/\n/g, '<br />')
 
  return html
}
 
// emit ----------------------------------------
 
function input() {
  code.value = getInnerText()
  emit('update:modelValue', code.value)
  emit('update:focusWord', getFocusWord())
}
 
function click() {
  emit('update:focusWord', getFocusWord())
}
 
// insertCode ----------------------------------------
 
let range: Range | undefined
function saveRange() {
  const selection = document.getSelection()
  range = selection?.getRangeAt(0)
}
 
function insertCode(text: string) {
  if (!inputEl) {
    console.warn('!inputEl')
    return
  }
 
  const selection = document.getSelection()
  if (!selection) return
 
  if (!range) {
    range = new Range()
    range.selectNodeContents(inputEl.value)
    range.collapse()
  }
 
  selection.removeAllRanges()
  selection.addRange(range)
 
  // range.deleteContents()
  // range.insertNode(document.createTextNode(text))
  // range.collapse()
 
  document.execCommand('insertText', false, text)
 
  // fun( | )
  if (/\)$/.test(text)) {
    const rangeCurrent = selection.getRangeAt(0)
    rangeCurrent.setEnd(rangeCurrent.endContainer, rangeCurrent.endOffset - 2)
 
    selection.removeAllRanges()
    selection.addRange(rangeCurrent)
  }
 
  input()
}
 
// focusWord ----------------------------------------
 
function getFocusWord() {
  const range = document.getSelection()?.getRangeAt(0)
  if (!range) return
 
  const node = range.endContainer
  const text = node.nodeValue || ''
  const left = text.slice(0, range.endOffset)
  const right = text.slice(range.endOffset)
  const l = left.match(/\w+$/)?.[0] || ''
  const r = right.match(/^\w+/)?.[0] || ''
 
  return l + r
}
 
defineExpose({
  insertCode,
  getFocusWord,
})
</script>
 
<style lang="scss" scoped>
.inputCode {
  position: relative;
  width: 100%;
  height: 220px;
  min-height: 42px;
  background: #262c33;
  border-radius: 6px 6px 6px 6px;
  color: #f00;
  color: transparent;
  resize: none;
  white-space: pre;
  caret-color: #fff;
  overflow: auto;
  &:hover,
  &:active {
    resize: vertical;
  }
 
  outline: solid 1px transparent;
  outline-offset: -1px;
  transition: 0.5s outline;
  &:focus-within {
    outline-color: #707070;
  }
 
  .input {
    outline: none;
    min-height: 100%;
    padding: 10px;
    &[contenteditable='plaintext-only'] {
      -webkit-user-modify: read-write-plaintext-only;
    }
    &::selection {
      background-color: rgba(255, 255, 255, 0.25);
    }
  }
  .highlight {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    padding: 10px;
    pointer-events: none;
    color: #febf72;
    color: #fff;
    // margin-top: 50px;
  }
}
</style>