fix: 修复编辑器字体大小保存失效的问题

This commit is contained in:
Baobhan Sith
2025-05-24 00:19:50 +08:00
parent 5f7c757249
commit 0f17d51441
3 changed files with 76 additions and 26 deletions
@@ -18,7 +18,7 @@ const sessionStore = useSessionStore(); // +++ 实例化会话 Store +++
const settingsStore = useSettingsStore(); // +++ 实例化设置 Store +++
const appearanceStore = useAppearanceStore(); // +++ 实例化外观 Store +++
const { shareFileEditorTabsBoolean } = storeToRefs(settingsStore); // +++ 获取共享设置 +++
const { currentEditorFontFamily } = storeToRefs(appearanceStore); // +++ 获取编辑器字体家族设置 +++
const { currentEditorFontFamily, currentEditorFontSize } = storeToRefs(appearanceStore);
// --- Props ---
const props = defineProps({
@@ -214,6 +214,11 @@ const handleEditorScroll = ({ scrollTop, scrollLeft }: { scrollTop: number; scro
}
};
// +++ 处理编辑器字体大小更新事件 +++
const handleEditorFontSizeUpdate = (newSize: number) => {
appearanceStore.setEditorFontSize(newSize);
};
// 注意:关闭/最小化按钮现在应该在 WorkspaceView 控制 Pane,而不是这里
// const handleCloseContainer = () => { ... };
@@ -350,9 +355,11 @@ const handleKeyDown = (event: KeyboardEvent) => {
v-model="localEditorContent"
:language="currentTabLanguage"
:font-family="currentEditorFontFamily"
:font-size="currentEditorFontSize"
theme="vs-dark"
class="editor-instance"
@request-save="handleSaveRequest"
@update:fontSize="handleEditorFontSizeUpdate"
:initialScrollTop="activeTab?.scrollTop ?? 0"
:initialScrollLeft="activeTab?.scrollLeft ?? 0"
@update:scrollPosition="handleEditorScroll"
@@ -7,12 +7,14 @@ import FileEditorTabs from './FileEditorTabs.vue';
import { useFileEditorStore, type FileTab } from '../stores/fileEditor.store';
import { useSettingsStore } from '../stores/settings.store';
import { useSessionStore } from '../stores/session.store';
import { useAppearanceStore } from '../stores/appearance.store';
const { t } = useI18n();
const fileEditorStore = useFileEditorStore();
const settingsStore = useSettingsStore();
const sessionStore = useSessionStore();
const appearanceStore = useAppearanceStore();
// --- ---
const isVisible = ref(false);
@@ -34,6 +36,9 @@ const {
// Store ()
const { showPopupFileEditorBoolean, shareFileEditorTabsBoolean } = storeToRefs(settingsStore);
// Appearance Store
const { currentEditorFontSize } = storeToRefs(appearanceStore);
// --- Store ---
// Store Actions ()
const {
@@ -412,6 +417,11 @@ const handleEditorScroll = ({ scrollTop, scrollLeft }: { scrollTop: number; scro
}
};
// +++ +++
const handleEditorFontSizeUpdate = (newSize: number) => {
appearanceStore.setEditorFontSize(newSize);
};
// ()
const handleCloseContainer = () => {
//
@@ -572,7 +582,9 @@ onBeforeUnmount(() => {
:language="currentTabLanguage"
theme="vs-dark"
class="editor-instance"
:font-size="currentEditorFontSize"
@request-save="handleSaveRequest"
@update:fontSize="handleEditorFontSizeUpdate"
:initialScrollTop="activeTab?.scrollTop ?? 0"
:initialScrollLeft="activeTab?.scrollLeft ?? 0"
@update:scrollPosition="handleEditorScroll"
@@ -3,10 +3,10 @@
</template>
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, watch, defineExpose } from 'vue';
import { ref, onMounted, onBeforeUnmount, watch, defineExpose, defineProps, defineEmits } from 'vue';
import * as monaco from 'monaco-editor';
const localFontSize = ref(14); // 14
const FONT_SIZE_STORAGE_KEY = 'monacoEditorFontSize'; // localStorage key
const props = defineProps({
modelValue: {
@@ -29,6 +29,10 @@ const props = defineProps({
type: String,
default: 'Consolas, "Courier New", monospace',
},
fontSize: { // prop
type: Number,
default: 14, //
},
initialScrollTop: {
type: Number,
default: 0,
@@ -39,20 +43,40 @@ const props = defineProps({
},
});
const emit = defineEmits(['update:modelValue', 'request-save', 'update:scrollPosition']);
const emit = defineEmits(['update:modelValue', 'request-save', 'update:scrollPosition', 'update:fontSize']); // 'update:fontSize'
const editorContainer = ref<HTMLElement | null>(null);
let editorInstance: monaco.editor.IStandaloneCodeEditor | null = null;
// ref localStorage props.fontSize
const internalEditorFontSize = ref(props.fontSize);
onMounted(() => {
// 使 props.fontSize internalEditorFontSize
internalEditorFontSize.value = props.fontSize;
// localStorage
const storedUserPreference = localStorage.getItem(FONT_SIZE_STORAGE_KEY);
if (storedUserPreference) {
const parsedSize = parseInt(storedUserPreference, 10);
if (!isNaN(parsedSize) && parsedSize >= 8 && parsedSize <= 40) {
// localStorage internalEditorFontSize ( prop)
// localStorage
if (parsedSize !== internalEditorFontSize.value) {
emit('update:fontSize', parsedSize);
}
internalEditorFontSize.value = parsedSize;
}
}
if (editorContainer.value) {
editorInstance = monaco.editor.create(editorContainer.value, {
value: props.modelValue,
language: props.language,
theme: props.theme,
fontSize: localFontSize.value,
fontSize: internalEditorFontSize.value, // 使 internalEditorFontSize
fontFamily: props.fontFamily, // 使 prop
automaticLayout: true,
readOnly: props.readOnly,
@@ -140,34 +164,32 @@ onMounted(() => {
// --- ---
const editorDomNode = editorInstance?.getDomNode();
if (editorDomNode) {
console.log('[MonacoEditor] Adding wheel event listener with debounce.');
if (editorDomNode && editorInstance) {
// console.log('[MonacoEditor] Adding wheel event listener.');
editorDomNode.addEventListener('wheel', (event: WheelEvent) => {
if (event.ctrlKey) {
if (event.ctrlKey && editorInstance) {
event.preventDefault();
const currentSizeOpt = editorInstance.getOption(monaco.editor.EditorOption.fontSize);
const currentSize = typeof currentSizeOpt === 'number' ? currentSizeOpt : internalEditorFontSize.value;
const currentSize = localFontSize.value; // 使
let newSize: number;
if (event.deltaY < 0) {
newSize = Math.min(currentSize + 1, 40);
newSize = Math.min(currentSize + 1, 40); // 40
} else {
newSize = Math.max(currentSize - 1, 8);
newSize = Math.max(currentSize - 1, 8); // 8
}
// Update visual font size and local state immediately
if (editorInstance && newSize !== currentSize) {
console.log(`[MonacoEditor] Updating local font size to: ${newSize}`);
localFontSize.value = newSize; //
editorInstance.updateOptions({ fontSize: newSize }); //
// --- ---
// debouncedSetEditorFontSize(newSize);
if (newSize !== currentSize) {
// console.log(`[MonacoEditor] Updating font size to: ${newSize}`);
editorInstance.updateOptions({ fontSize: newSize });
localStorage.setItem(FONT_SIZE_STORAGE_KEY, newSize.toString());
internalEditorFontSize.value = newSize; // internal ref
emit('update:fontSize', newSize); // store
}
}
}, { passive: false });
} else {
console.error('[MonacoEditor] editorDomNode is null, cannot add wheel listener.');
// console.error('[MonacoEditor] editorDomNode or editorInstance is null, cannot add wheel listener.');
}
@@ -219,11 +241,20 @@ watch(() => props.readOnly, (newReadOnly) => {
watch(() => props.fontFamily, (newFontFamily) => {
if (editorInstance) {
editorInstance.updateOptions({ fontFamily: newFontFamily });
};
});
// () fontSize
watch(() => props.fontSize, (newGlobalSize) => {
// fontSize ( prop) internalEditorFontSize ()
if (editorInstance && newGlobalSize !== internalEditorFontSize.value) {
// console.log(`[MonacoEditor] Global font size changed to: ${newGlobalSize}, updating editor.`);
editorInstance.updateOptions({ fontSize: newGlobalSize });
localStorage.setItem(FONT_SIZE_STORAGE_KEY, newGlobalSize.toString()); // localStorage
internalEditorFontSize.value = newGlobalSize;
}
});
// --- ---
onBeforeUnmount(() => {
if (editorInstance) {
editorInstance.dispose();