Skip to content

Commit 89f52d1

Browse files
committed
feat(Monaco): height adaptive
1 parent 4f4ac34 commit 89f52d1

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/monaco/Monaco.vue

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { getOrCreateModel } from './utils'
1515
import { loadGrammars, loadTheme } from 'monaco-volar'
1616
import { Store } from '../store'
1717
import type { PreviewMode } from '../editor/types'
18+
import { debounce } from '../utils'
1819
1920
const props = withDefaults(
2021
defineProps<{
@@ -36,6 +37,7 @@ const containerRef = ref<HTMLDivElement>()
3637
const ready = ref(false)
3738
const editor = shallowRef<monaco.editor.IStandaloneCodeEditor>()
3839
const store = inject<Store>('store')!
40+
const editorHeight = ref('0px')
3941
4042
initMonaco(store)
4143
@@ -129,8 +131,20 @@ onMounted(async () => {
129131
// ignore save event
130132
})
131133
134+
const updateEditorHeight = debounce(() => {
135+
const lineCount = editorInstance.getModel()?.getLineCount() || 8
136+
const lineHeight = editorInstance.getOption(
137+
monaco.editor.EditorOption.lineHeight
138+
)
139+
editorHeight.value = (lineCount + 1) * lineHeight + 'px'
140+
})
141+
142+
updateEditorHeight()
143+
132144
editorInstance.onDidChangeModelContent(() => {
133145
emit('change', editorInstance.getValue())
146+
147+
updateEditorHeight()
134148
})
135149
136150
editorInstance.onDidChangeCursorSelection((e) => {
@@ -151,10 +165,10 @@ onBeforeUnmount(() => {
151165
<div class="editor" ref="containerRef"></div>
152166
</template>
153167

154-
<style>
168+
<style scoped>
155169
.editor {
156170
position: relative;
157-
height: 100%;
171+
height: v-bind('editorHeight');
158172
width: 100%;
159173
overflow: hidden;
160174
}

test/VuePreview.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ const isHover = useElementHover(vuePreviewContainerRef)
249249
:deep(.editor) {
250250
box-sizing: border-box;
251251
overflow: hidden;
252-
height: auto;
253252
border-radius: 0 0 var(--vue-preview-radius) var(--vue-preview-radius);
254253
max-height: v-bind('maxHeightForCode');
255254
transition: max-height 0.3s;

0 commit comments

Comments
 (0)