|
|
@@ -63,7 +63,14 @@ const { t } = useI18n()
|
|
|
|
|
|
let monacoEditor: monaco.editor.IStandaloneCodeEditor | null = null
|
|
|
let model: editor.ITextModel | null = null
|
|
|
+let layoutFrame = 0
|
|
|
const editContainer = ref<HTMLElement>()
|
|
|
+const bodyHeight = ref(props.height)
|
|
|
+const resizeState = reactive({
|
|
|
+ startY: 0,
|
|
|
+ startHeight: props.height,
|
|
|
+ isDragging: false
|
|
|
+})
|
|
|
|
|
|
const isFullScreen = ref(false)
|
|
|
const { monacoTheme, themeClass, toggleTheme } = useLocalEditorTheme({
|
|
|
@@ -154,6 +161,18 @@ watch(isFullScreen, () => {
|
|
|
})
|
|
|
})
|
|
|
|
|
|
+watch(
|
|
|
+ () => props.height,
|
|
|
+ (height) => {
|
|
|
+ if (bodyHeight.value < height) {
|
|
|
+ bodyHeight.value = height
|
|
|
+ }
|
|
|
+ nextTick(() => {
|
|
|
+ monacoEditor?.layout()
|
|
|
+ })
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
/**
|
|
|
* @description: 发送回调(formatValue:json,需要转换处理)
|
|
|
* @return {*}
|
|
|
@@ -203,7 +222,7 @@ const fullScreenStyle = computed(() => {
|
|
|
if (isFullScreen.value) return {}
|
|
|
|
|
|
return {
|
|
|
- height: `${props.height + 40}px`
|
|
|
+ height: `${bodyHeight.value + 40}px`
|
|
|
}
|
|
|
})
|
|
|
/**
|
|
|
@@ -217,6 +236,45 @@ const onToggleTheme = () => {
|
|
|
toggleTheme()
|
|
|
}
|
|
|
|
|
|
+const syncEditorLayout = () => {
|
|
|
+ if (layoutFrame) {
|
|
|
+ cancelAnimationFrame(layoutFrame)
|
|
|
+ }
|
|
|
+ layoutFrame = requestAnimationFrame(() => {
|
|
|
+ layoutFrame = 0
|
|
|
+ monacoEditor?.layout()
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+const stopResize = () => {
|
|
|
+ if (!resizeState.isDragging) return
|
|
|
+ resizeState.isDragging = false
|
|
|
+ document.body.style.userSelect = ''
|
|
|
+ document.body.style.cursor = ''
|
|
|
+ window.removeEventListener('mousemove', onResize)
|
|
|
+ window.removeEventListener('mouseup', stopResize)
|
|
|
+}
|
|
|
+
|
|
|
+const onResize = (event: MouseEvent) => {
|
|
|
+ if (!resizeState.isDragging || isFullScreen.value) return
|
|
|
+ const nextHeight = resizeState.startHeight + event.clientY - resizeState.startY
|
|
|
+ const targetHeight = Math.max(props.height, nextHeight)
|
|
|
+ if (targetHeight === bodyHeight.value) return
|
|
|
+ bodyHeight.value = targetHeight
|
|
|
+ syncEditorLayout()
|
|
|
+}
|
|
|
+
|
|
|
+const onResizeStart = (event: MouseEvent) => {
|
|
|
+ if (isFullScreen.value) return
|
|
|
+ resizeState.startY = event.clientY
|
|
|
+ resizeState.startHeight = bodyHeight.value
|
|
|
+ resizeState.isDragging = true
|
|
|
+ document.body.style.userSelect = 'none'
|
|
|
+ document.body.style.cursor = 'ns-resize'
|
|
|
+ window.addEventListener('mousemove', onResize)
|
|
|
+ window.addEventListener('mouseup', stopResize)
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* 设置文本
|
|
|
* @param text
|
|
|
@@ -259,6 +317,10 @@ function insertText(text: string) {
|
|
|
|
|
|
// 销毁model
|
|
|
onBeforeUnmount(() => {
|
|
|
+ stopResize()
|
|
|
+ if (layoutFrame) {
|
|
|
+ cancelAnimationFrame(layoutFrame)
|
|
|
+ }
|
|
|
monacoEditor?.dispose()
|
|
|
model?.dispose()
|
|
|
})
|
|
|
@@ -272,7 +334,10 @@ defineExpose({
|
|
|
<Teleport :disabled="!appendTo" :to="appendTo">
|
|
|
<div
|
|
|
class="monacoEditor !m-0"
|
|
|
- :class="[themeClass, { 'is-fullscreen': isFullScreen }]"
|
|
|
+ :class="[
|
|
|
+ themeClass,
|
|
|
+ { 'is-fullscreen': isFullScreen, 'is-resizing': resizeState.isDragging }
|
|
|
+ ]"
|
|
|
:style="fullScreenStyle"
|
|
|
>
|
|
|
<div class="tools h-[33px] flex items-center justify-between gap-2" v-if="props.tools">
|
|
|
@@ -347,6 +412,7 @@ defineExpose({
|
|
|
<!-- 编辑器 -->
|
|
|
<div class="editor-wrapper">
|
|
|
<div ref="editContainer" class="code-editor"></div>
|
|
|
+ <div v-if="!isFullScreen" class="resize-handle" @mousedown.prevent="onResizeStart"></div>
|
|
|
</div>
|
|
|
</div>
|
|
|
</Teleport>
|
|
|
@@ -382,7 +448,9 @@ defineExpose({
|
|
|
</style>
|
|
|
<style lang="less" scoped>
|
|
|
.monacoEditor {
|
|
|
- border: 1px solid #eee;
|
|
|
+ border: 1px solid #dcdfe6;
|
|
|
+ border-radius: 4px;
|
|
|
+ overflow: hidden;
|
|
|
display: flex;
|
|
|
flex-direction: column;
|
|
|
transition:
|
|
|
@@ -390,6 +458,12 @@ defineExpose({
|
|
|
inset 0.25s ease,
|
|
|
background-color 0.2s;
|
|
|
|
|
|
+ &.is-resizing {
|
|
|
+ transition:
|
|
|
+ inset 0.25s ease,
|
|
|
+ background-color 0.2s;
|
|
|
+ }
|
|
|
+
|
|
|
&.is-fullscreen {
|
|
|
position: absolute;
|
|
|
inset: 0;
|
|
|
@@ -414,6 +488,18 @@ defineExpose({
|
|
|
.editor-wrapper {
|
|
|
flex: 1;
|
|
|
overflow: hidden;
|
|
|
+ position: relative;
|
|
|
+ .resize-handle {
|
|
|
+ position: absolute;
|
|
|
+ bottom: 8px;
|
|
|
+ left: 50%;
|
|
|
+ transform: translateX(-50%);
|
|
|
+ width: 18px;
|
|
|
+ height: 6px;
|
|
|
+ background-color: #d0d5dd;
|
|
|
+ border-radius: 8px;
|
|
|
+ cursor: ns-resize;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
.code-editor {
|