Browse Source

切换tab时,记住滚动条位置

mxd 3 years ago
parent
commit
dffde1c7ab

+ 2 - 1
src/components/common/data/magic-tab.js

@@ -11,7 +11,7 @@ export default {
       type: Boolean
     }
   },
-  emits: ['update:value', 'change', 'close', 'item-contextmenu'],
+  emits: ['update:value', 'change', 'close', 'item-contextmenu', 'before-change'],
   setup(props) {
     // 拖拽中的对象
     const draggableItem = ref({})
@@ -119,6 +119,7 @@ export default {
               id: this.scrollItem === tab || this.scrollItem === tab.id ? this.scrollId : undefined,
               onClick: (e) => {
                 if (this.value !== tab) {
+                  this.$emit('before-change', this.value)
                   this.$emit('update:value', tab)
                   this.$emit('change', tab)
                 }

+ 6 - 0
src/components/common/magic-monaco-editor.vue

@@ -83,6 +83,12 @@ export default {
 		getEditorDom(){
 			return this.$refs.editor
 		},
+		getScrollTop(){
+			return this.instance && this.instance.getScrollTop()
+		},
+		setScrollTop(val){
+			this.instance && this.instance.setScrollTop(val)
+		},
 		match(){
 			if(this.matches){
 				nextTick(() => {

+ 10 - 1
src/components/panel/main/magic-script-editor.vue

@@ -15,7 +15,8 @@
 		</div>
 		<template v-else>
 			<magic-tab v-model:value="selectTab" :tabs="openedScripts" className="magic-script-tab"  ref="tab"
-				:allow-close="true" @close="onClose" @change="tab => bus.$emit('open', tab)"  @item-contextmenu="onContextMenu">
+				:allow-close="true" @close="onClose" @change="tab => bus.$emit('open', tab)"
+				@before-change="beforeChange" @item-contextmenu="onContextMenu">
 				<template v-slot="{ tab }">
 					<magic-text-icon :icon="tab.getIcon(tab.item)"/>{{ tab.item.name }}<span v-if="isUpdated(tab)">*</span>
 					<magic-icon v-if="tab.item.lock === $LOCKED" icon="lock"/>
@@ -194,6 +195,11 @@ bus.$event(Socket.LOGIN_RESPONSE, () => {
 		bus.send(Socket.SET_FILE_ID, selectTab.value.item?.id || '0')
 	}
 })
+const beforeChange = tab => {
+	if(tab && editor.value){
+		tab.scrollTop = editor.value.getScrollTop()
+	}
+}
 // 打开文件
 bus.$on(Message.OPEN, opened => {
 	let find = openedScripts.find(it => it.item === opened.item || (it.item.id && it.item.id === opened.item.id))
@@ -201,6 +207,7 @@ bus.$on(Message.OPEN, opened => {
 	if(find){
 		selectTab.value = find
 		loading.value = false
+		nextTick(() => editor.value.setScrollTop(find.scrollTop || 0))
 	} else {
 		openedScripts.push(opened)
 		selectTab.value = opened
@@ -211,10 +218,12 @@ bus.$on(Message.OPEN, opened => {
 				Object.keys(data).forEach(key => opened.item[key] = data[key])
 				opened.tmpObject = JSON.parse(JSON.stringify(opened.processSave(data)))
 				loading.value = false
+				nextTick(() => editor.value.setScrollTop(0))
 			})
 		} else {
 			opened.tmpObject = JSON.parse(JSON.stringify(opened.processSave(opened.item)))
 			loading.value = false
+			nextTick(() => editor.value.setScrollTop(0))
 		}
 	}
 	if(selectTab.value.decorations && selectTab.value.decorations.length > 0){