/
monaco-singleline.vue
278 lines (259 loc) · 7.51 KB
/
monaco-singleline.vue
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<template>
<div class="monaco-singleline">
<!-- v-show="!showPlaceholder" -->
<monaco-editor
v-model="valueLocal"
v-bind="$attrs"
language="mySpecialLanguage"
theme="myCoolTheme"
:height="height"
:diffEditor="diffEditor"
:options="opts"
:editorBeforeMount="onEditorBeforeMount"
:editorMounted="onEditorMounted"
:class="[
showPlaceholder ? 'editor-with-placeholder' : '',
readOnly ? 'editor-with-read-only' : '',
]"
/>
<div
v-show="showPlaceholder"
class="placeholder"
:style="{ fontSize: opts.fontSize }"
@click="onPlaceholderClick"
>
{{ placeholder }}
</div>
</div>
</template>
<script>
import MonacoEditor from 'monaco-editor-vue'
import '@/styles/index.less'
// all options: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.istandaloneeditorconstructionoptions.html
const DefaultOptions = {
fontSize: '14px',
fontWeight: 'normal',
wordWrap: 'off',
lineNumbers: 'off',
lineNumbersMinChars: 0,
overviewRulerLanes: 0,
overviewRulerBorder: false,
hideCursorInOverviewRuler: true,
lineDecorationsWidth: 0,
glyphMargin: false,
folding: false,
scrollBeyondLastColumn: 0,
scrollbar: {
horizontal: 'hidden',
vertical: 'hidden',
// avoid can not scroll page when hover monaco
alwaysConsumeMouseWheel: false,
},
// disable `Find`
find: {
addExtraSpaceOnTop: false,
autoFindInSelection: 'never',
seedSearchStringFromSelection: false,
},
minimap: { enabled: false },
// see: https://github.com/microsoft/monaco-editor/issues/1746
wordBasedSuggestions: false,
// avoid links underline
links: false,
// avoid highlight hover word
occurrencesHighlight: false,
cursorStyle: 'line-thin',
// hide current row highlight grey border
// see: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.ieditoroptions.html#renderlinehighlight
renderLineHighlight: 'none',
contextmenu: false,
// default selection is rounded
roundedSelection: false,
hover: {
// unit: ms
// default: 300
delay: 100,
},
acceptSuggestionOnEnter: 'on',
// auto adjust width and height to parent
// see: https://github.com/Microsoft/monaco-editor/issues/543#issuecomment-321767059
automaticLayout: true,
// if monaco is inside a table, hover tips or completion may casue table body scroll
fixedOverflowWidgets: true,
}
function noop() {}
export default {
name: 'monaco-singleline',
components: {
MonacoEditor,
},
model: {
prop: 'value',
event: 'change',
},
props: {
value: { type: [String, Number], default: '' },
placeholder: { type: [String, Number], default: '' },
readOnly: { type: Boolean, default: false },
height: { type: [String, Number], default: '21' },
diffEditor: { type: Boolean, default: false },
options: {
type: Object,
default() {
return {}
},
},
editorMounted: { type: Function, default: noop },
editorBeforeMount: { type: Function, default: noop },
onEnter: { type: Function, default: noop },
},
data() {
return {
editor: '',
optionsLocal: {},
}
},
computed: {
valueLocal: {
get() {
return this.value
},
set(value) {
this.$emit('change', value)
},
},
showPlaceholder() {
return !this.diffEditor && !Boolean(`${this.valueLocal}`)
},
opts() {
return {
...DefaultOptions,
...{ readOnly: this.readOnly },
...this.options,
}
},
},
methods: {
focus() {
this.editor.focus()
},
onEditorBeforeMount(monaco) {
this.editorBeforeMount(monaco)
},
onEditorMounted(editor, monaco) {
this.editor = editor
// disable `Find` widget
// see: https://github.com/microsoft/monaco-editor/issues/287#issuecomment-328371787
// eslint-disable-next-line no-bitwise
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_F, () => {})
// disable press `Enter` in case of producing line breaks
editor.addCommand(monaco.KeyCode.Enter, () => {
// State: https://github.com/microsoft/vscode/blob/1.56.0/src/vs/editor/contrib/suggest/suggestWidget.ts#L50
const StateOpen = 3
if (
editor._contentWidgets['editor.widget.suggestWidget'].widget.state !==
StateOpen
) {
this.onEnter(editor.getValue())
} else {
/**
* Origin purpose: disable line breaks
* Side Effect: If defining completions, will prevent `Enter` confirm selection
* Side Effect Solution: always accept selected suggestion when `Enter`
*
* But it is hard to find out the name `acceptSelectedSuggestion` to trigger.
*
* Where to find the `acceptSelectedSuggestion` at monaco official documents ?
* Below is some refs:
* - https://stackoverflow.com/questions/64430041/get-a-list-of-monaco-commands-actions-ids
* - command from: https://github.com/microsoft/vscode/blob/e216a598d3e02401f26459fb63a4f1b6365ec4ec/src/vs/editor/contrib/suggest/suggestController.ts#L632-L638
* - https://github.com/microsoft/vscode/search?q=registerEditorCommand
* - real list: https://github.com/microsoft/vscode/blob/e216a598d3e02401f26459fb63a4f1b6365ec4ec/src/vs/editor/browser/editorExtensions.ts#L611
*
*
* Finally, `acceptSelectedSuggestion` appears here:
* - `editorExtensions.js` Line 288
*/
editor.trigger('', 'acceptSelectedSuggestion')
}
})
// deal with user paste
// see: https://github.com/microsoft/monaco-editor/issues/2009#issue-63987720
editor.onDidPaste((e) => {
// multiple rows will be merged to single row
if (e.endLineNumber <= 1) {
return
}
let newContent = ''
const textModel = editor.getModel()
const lineCount = textModel.getLineCount()
// remove all line breaks
for (let i = 0; i < lineCount; i += 1) {
newContent += textModel.getLineContent(i + 1)
}
textModel.setValue(newContent)
editor.setPosition({ column: newContent.length + 1, lineNumber: 1 })
})
// disable `F1` command palette
editor.addCommand(monaco.KeyCode.F1, () => {})
// --------
this.editorMounted(editor, monaco)
},
onPlaceholderClick() {
// const vm = this
// this.$nextTick(() => {
// // see: https://github.com/PolymerVis/monaco-editor/issues/1#issuecomment-357378349
// vm.editor.layout()
// vm.editor.focus()
// })
this.editor.layout()
this.editor.focus()
},
},
}
</script>
<style lang="less" scoped>
.monaco-singleline {
display: flex;
align-items: center;
position: relative;
padding: 8px 8px;
border: 1px solid #d9d9d9;
&:hover {
border-color: #000;
}
::v-deep {
.decorationsOverviewRuler {
display: none;
}
}
}
.placeholder {
position: absolute;
left: 0;
right: 0;
overflow: hidden;
padding-left: inherit;
padding-right: inherit;
cursor: text;
user-select: none;
font-size: 14px;
// height: 20px;
color: #999;
font-style: italic;
}
.editor-with-placeholder {
::v-deep .view-lines {
opacity: 0;
}
}
.editor-with-read-only {
::v-deep {
.cursors-layer > .cursor {
// display: none !important;
// no use of `!important`, easy for user to modify style
opacity: 0;
}
}
}
</style>