🎨 Updated

This commit is contained in:
2025-06-05 02:11:36 +08:00
parent d9745ac7d1
commit 3e20f47b8e
17 changed files with 354 additions and 1053 deletions

View File

@@ -299,16 +299,6 @@ export class EditingConfig {
*/
"autoSaveDelay": number;
/**
* 变更字符阈值
*/
"changeThreshold": number;
/**
* 最小保存间隔(毫秒)
*/
"minSaveInterval": number;
/** Creates a new EditingConfig instance. */
constructor($$source: Partial<EditingConfig> = {}) {
if (!("fontSize" in $$source)) {
@@ -335,12 +325,6 @@ export class EditingConfig {
if (!("autoSaveDelay" in $$source)) {
this["autoSaveDelay"] = 0;
}
if (!("changeThreshold" in $$source)) {
this["changeThreshold"] = 0;
}
if (!("minSaveInterval" in $$source)) {
this["minSaveInterval"] = 0;
}
Object.assign(this, $$source);
}

View File

@@ -0,0 +1,19 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
/**
* DialogService 对话框服务,处理文件选择等对话框操作
* @module
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import {Call as $Call, Create as $Create} from "@wailsio/runtime";
/**
* SelectDirectory 打开目录选择对话框
*/
export function SelectDirectory(): Promise<string> & { cancel(): void } {
let $resultPromise = $Call.ByID(2249533621) as any;
return $resultPromise;
}

View File

@@ -14,10 +14,6 @@ import {Call as $Call, Create as $Create} from "@wailsio/runtime";
// @ts-ignore: Unused imports
import * as models$0 from "../models/models.js";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import * as $models from "./models.js";
/**
* ForceSave 强制保存当前文档
*/
@@ -46,30 +42,6 @@ export function GetActiveDocumentContent(): Promise<string> & { cancel(): void }
return $resultPromise;
}
/**
* GetDiffInfo 获取两个文本之间的详细差异信息
*/
export function GetDiffInfo(oldText: string, newText: string): Promise<$models.DiffResult> & { cancel(): void } {
let $resultPromise = $Call.ByID(2490726526, oldText, newText) as any;
let $typingPromise = $resultPromise.then(($result: any) => {
return $$createType2($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
return $typingPromise;
}
/**
* GetSaveSettings 获取文档保存设置
*/
export function GetSaveSettings(): Promise<models$0.EditingConfig | null> & { cancel(): void } {
let $resultPromise = $Call.ByID(4257471801) as any;
let $typingPromise = $resultPromise.then(($result: any) => {
return $$createType4($result);
}) as any;
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
return $typingPromise;
}
/**
* Initialize 初始化文档服务
*/
@@ -79,15 +51,7 @@ export function Initialize(): Promise<void> & { cancel(): void } {
}
/**
* LoadDefaultDocument 加载默认文档
*/
export function LoadDefaultDocument(): Promise<void> & { cancel(): void } {
let $resultPromise = $Call.ByID(2343023569) as any;
return $resultPromise;
}
/**
* SaveDocumentSync 同步保存文档内容 (用于页面关闭前同步保存)
* SaveDocumentSync 同步保存文档内容
*/
export function SaveDocumentSync(content: string): Promise<void> & { cancel(): void } {
let $resultPromise = $Call.ByID(3770207288, content) as any;
@@ -95,29 +59,13 @@ export function SaveDocumentSync(content: string): Promise<void> & { cancel(): v
}
/**
* ServiceShutdown 实现应用程序关闭时的服务关闭逻辑
* ServiceShutdown 服务关闭
*/
export function ServiceShutdown(): Promise<void> & { cancel(): void } {
let $resultPromise = $Call.ByID(638578044) as any;
return $resultPromise;
}
/**
* SetSaveCallback 设置保存回调函数
*/
export function SetSaveCallback(callback: any): Promise<void> & { cancel(): void } {
let $resultPromise = $Call.ByID(675315211, callback) as any;
return $resultPromise;
}
/**
* Shutdown 关闭文档服务,确保所有数据保存
*/
export function Shutdown(): Promise<void> & { cancel(): void } {
let $resultPromise = $Call.ByID(3444504909) as any;
return $resultPromise;
}
/**
* UpdateActiveDocumentContent 更新当前活动文档内容
*/
@@ -126,17 +74,6 @@ export function UpdateActiveDocumentContent(content: string): Promise<void> & {
return $resultPromise;
}
/**
* UpdateSaveSettings 更新文档保存设置
*/
export function UpdateSaveSettings(docConfig: models$0.EditingConfig): Promise<void> & { cancel(): void } {
let $resultPromise = $Call.ByID(1245479534, docConfig) as any;
return $resultPromise;
}
// Private type creation functions
const $$createType0 = models$0.Document.createFrom;
const $$createType1 = $Create.Nullable($$createType0);
const $$createType2 = $models.DiffResult.createFrom;
const $$createType3 = models$0.EditingConfig.createFrom;
const $$createType4 = $Create.Nullable($$createType3);

View File

@@ -2,11 +2,13 @@
// This file is automatically generated. DO NOT EDIT
import * as ConfigService from "./configservice.js";
import * as DialogService from "./dialogservice.js";
import * as DocumentService from "./documentservice.js";
import * as HotkeyService from "./hotkeyservice.js";
import * as SystemService from "./systemservice.js";
export {
ConfigService,
DialogService,
DocumentService,
HotkeyService,
SystemService

View File

@@ -5,137 +5,6 @@
// @ts-ignore: Unused imports
import {Create as $Create} from "@wailsio/runtime";
/**
* DiffResult 包含差异比较的结果信息
*/
export class DiffResult {
/**
* 编辑操作列表
*/
"Edits": Edit[];
/**
* 插入的字符数
*/
"InsertCount": number;
/**
* 删除的字符数
*/
"DeleteCount": number;
/**
* 变更的行数
*/
"ChangedLines": number;
/**
* 总变更字符数(插入+删除)
*/
"TotalChanges": number;
/**
* 变更的token数如单词、标识符等
*/
"ChangedTokens": number;
/** Creates a new DiffResult instance. */
constructor($$source: Partial<DiffResult> = {}) {
if (!("Edits" in $$source)) {
this["Edits"] = [];
}
if (!("InsertCount" in $$source)) {
this["InsertCount"] = 0;
}
if (!("DeleteCount" in $$source)) {
this["DeleteCount"] = 0;
}
if (!("ChangedLines" in $$source)) {
this["ChangedLines"] = 0;
}
if (!("TotalChanges" in $$source)) {
this["TotalChanges"] = 0;
}
if (!("ChangedTokens" in $$source)) {
this["ChangedTokens"] = 0;
}
Object.assign(this, $$source);
}
/**
* Creates a new DiffResult instance from a string or object.
*/
static createFrom($$source: any = {}): DiffResult {
const $$createField0_0 = $$createType1;
let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
if ("Edits" in $$parsedSource) {
$$parsedSource["Edits"] = $$createField0_0($$parsedSource["Edits"]);
}
return new DiffResult($$parsedSource as Partial<DiffResult>);
}
}
/**
* Edit 表示单个编辑操作
*/
export class Edit {
/**
* 操作类型
*/
"Type": EditType;
/**
* 操作内容
*/
"Content": string;
/** Creates a new Edit instance. */
constructor($$source: Partial<Edit> = {}) {
if (!("Type" in $$source)) {
this["Type"] = (0 as EditType);
}
if (!("Content" in $$source)) {
this["Content"] = "";
}
Object.assign(this, $$source);
}
/**
* Creates a new Edit instance from a string or object.
*/
static createFrom($$source: any = {}): Edit {
let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
return new Edit($$parsedSource as Partial<Edit>);
}
}
/**
* Edit 表示编辑操作类型
*/
export enum EditType {
/**
* The Go zero value for the underlying type of the enum.
*/
$zero = 0,
/**
* EditInsert 插入操作
*/
EditInsert = 0,
/**
* EditDelete 删除操作
*/
EditDelete = 1,
/**
* EditEqual 相等部分
*/
EditEqual = 2,
};
/**
* MemoryStats 内存统计信息
*/
@@ -202,7 +71,3 @@ export class MemoryStats {
return new MemoryStats($$parsedSource as Partial<MemoryStats>);
}
}
// Private type creation functions
const $$createType0 = Edit.createFrom;
const $$createType1 = $Create.Array($$createType0);

View File

@@ -73,10 +73,9 @@ export default {
selectDirectory: 'Select Directory',
selectDataDirectory: 'Select Data Storage Directory',
defaultDataPath: 'Default data storage path',
enterCustomPath: 'Enter custom data storage path',
pathHint: 'Example: C:\\MyData or /home/user/data or relative path ./data',
bufferFiles: 'Buffer Files Path',
useCustomLocation: 'Use custom location for buffer files',
customDataPath: 'Custom Data Storage Path',
fontSize: 'Font Size',
fontSizeDescription: 'Editor font size',
fontSettings: 'Font Settings',
@@ -96,7 +95,6 @@ export default {
restartRequired: '(Restart required)',
saveOptions: 'Save Options',
autoSaveDelay: 'Auto Save Delay (ms)',
changeThreshold: 'Change Threshold',
minSaveInterval: 'Min Save Interval (ms)'
selectDirectoryFailed: 'Failed to select directory'
}
};

View File

@@ -73,10 +73,9 @@ export default {
selectDirectory: '选择目录',
selectDataDirectory: '选择数据存储目录',
defaultDataPath: '默认数据存储路径',
enterCustomPath: '请输入自定义数据存储路径',
pathHint: '例如: C:\\MyData 或 /home/user/data 或相对路径 ./data',
bufferFiles: '缓冲文件路径',
useCustomLocation: '使用自定义位置存储缓冲文件',
customDataPath: '自定义数据存储路径',
fontSize: '字体大小',
fontSizeDescription: '编辑器字体大小',
fontSettings: '字体设置',
@@ -96,7 +95,6 @@ export default {
restartRequired: '(需要重启)',
saveOptions: '保存选项',
autoSaveDelay: '自动保存延迟(毫秒)',
changeThreshold: '变更字符阈值',
minSaveInterval: '最小保存间隔(毫秒)'
selectDirectoryFailed: '选择目录失败'
}
};

View File

@@ -62,9 +62,7 @@ const EDITING_CONFIG_KEY_MAP: EditingConfigKeyMap = {
enableTabIndent: 'editing.enable_tab_indent',
tabSize: 'editing.tab_size',
tabType: 'editing.tab_type',
autoSaveDelay: 'editing.auto_save_delay',
changeThreshold: 'editing.change_threshold',
minSaveInterval: 'editing.min_save_interval'
autoSaveDelay: 'editing.auto_save_delay'
} as const;
const APPEARANCE_CONFIG_KEY_MAP: AppearanceConfigKeyMap = {
@@ -137,9 +135,7 @@ const DEFAULT_CONFIG: AppConfig = {
enableTabIndent: true,
tabSize: CONFIG_LIMITS.tabSize.default,
tabType: CONFIG_LIMITS.tabType.default,
autoSaveDelay: 5000,
changeThreshold: 500,
minSaveInterval: 1000
autoSaveDelay: 5000
},
appearance: {
language: LanguageType.LangZhCN
@@ -338,9 +334,7 @@ export const useConfigStore = defineStore('config', () => {
fontFamily: (value: string) => safeCall(() => updateEditingConfig('fontFamily', value), 'config.saveFailed', 'config.saveSuccess'),
fontWeight: (value: string) => safeCall(() => updateEditingConfig('fontWeight', value), 'config.saveFailed', 'config.saveSuccess'),
defaultDataPath: (value: string) => safeCall(() => updateGeneralConfig('defaultDataPath', value), 'config.saveFailed', 'config.saveSuccess'),
autoSaveDelay: (value: number) => safeCall(() => updateEditingConfig('autoSaveDelay', value), 'config.saveFailed', 'config.saveSuccess'),
changeThreshold: (value: number) => safeCall(() => updateEditingConfig('changeThreshold', value), 'config.saveFailed', 'config.saveSuccess'),
minSaveInterval: (value: number) => safeCall(() => updateEditingConfig('minSaveInterval', value), 'config.saveFailed', 'config.saveSuccess')
autoSaveDelay: (value: number) => safeCall(() => updateEditingConfig('autoSaveDelay', value), 'config.saveFailed', 'config.saveSuccess')
};
return {
@@ -394,8 +388,6 @@ export const useConfigStore = defineStore('config', () => {
// 保存配置相关方法
setAutoSaveDelay: setters.autoSaveDelay,
setChangeThreshold: setters.changeThreshold,
setMinSaveInterval: setters.minSaveInterval,
// 热键配置相关方法
setEnableGlobalHotkey: (value: boolean) => safeCall(() => updateGeneralConfig('enableGlobalHotkey', value), 'config.saveFailed', 'config.saveSuccess'),

View File

@@ -12,6 +12,12 @@ export interface AutoSaveOptions {
/**
* 创建自动保存插件
*
* 新的简化保存策略:
* - 前端只负责将内容变更传递给后端
* - 后端使用定时保存机制,每隔配置的时间间隔自动保存(仅在有变更时)
* - 移除了复杂的阈值保存和最小间隔控制
*
* @param options 配置选项
* @returns EditorView.Plugin
*/

View File

@@ -138,27 +138,31 @@ const handleAutoSaveDelayChange = async (event: Event) => {
}
};
const handleChangeThresholdChange = async (event: Event) => {
const target = event.target as HTMLInputElement;
const value = parseInt(target.value, 10);
if (!isNaN(value) && value >= 10 && value <= 10000) {
await safeCall(
() => configStore.setChangeThreshold(value),
'config.changeThresholdUpdateFailed'
);
}
};
// 动态字体预览文本
const fontPreviewText = computed(() => {
const currentFont = configStore.config.editing.fontFamily;
const handleMinSaveIntervalChange = async (event: Event) => {
const target = event.target as HTMLInputElement;
const value = parseInt(target.value, 10);
if (!isNaN(value) && value >= 100 && value <= 10000) {
await safeCall(
() => configStore.setMinSaveInterval(value),
'config.minSaveIntervalUpdateFailed'
);
// 根据字体类型返回不同的预览文本
if (currentFont.includes('HarmonyOS')) {
return '鸿蒙字体测试';
} else if (currentFont.includes('Microsoft YaHei')) {
return '微软雅黑测试';
} else if (currentFont.includes('PingFang')) {
return '苹方字体测试';
} else if (currentFont.includes('JetBrains')) {
return 'JetBrains Mono';
} else if (currentFont.includes('Fira Code')) {
return 'Fira Code Test';
} else if (currentFont.includes('Source Code')) {
return 'Source Code Pro';
} else if (currentFont.includes('Cascadia')) {
return 'Cascadia Code';
} else if (currentFont.includes('SF Mono') || currentFont.includes('Monaco')) {
return 'System Monospace';
} else {
return 'Font Preview';
}
};
});
</script>
<template>
@@ -234,7 +238,7 @@ const handleMinSaveIntervalChange = async (event: Event) => {
<div class="preview-text">
<span>function example() {</span>
<span class="indent">console.log("Hello, 世界!");</span>
<span class="indent">const message = "鸿蒙字体测试";</span>
<span class="indent">const message = "{{ fontPreviewText }}";</span>
<span>}</span>
</div>
</div>
@@ -276,7 +280,7 @@ const handleMinSaveIntervalChange = async (event: Event) => {
</SettingSection>
<SettingSection :title="t('settings.saveOptions')">
<SettingItem :title="t('settings.autoSaveDelay')" :description="'单位:毫秒'">
<SettingItem :title="t('settings.autoSaveDelay')" :description="'定时保存间隔,每隔指定时间自动保存(仅在有变更时)'">
<input
type="number"
class="number-input"
@@ -284,24 +288,6 @@ const handleMinSaveIntervalChange = async (event: Event) => {
@change="handleAutoSaveDelayChange"
/>
</SettingItem>
<SettingItem :title="t('settings.changeThreshold')" :description="'变更字符超过此阈值时触发保存'">
<input
type="number"
class="number-input"
:value="configStore.config.editing.changeThreshold"
@change="handleChangeThresholdChange"
/>
</SettingItem>
<SettingItem :title="t('settings.minSaveInterval')" :description="'单位:毫秒'">
<input
type="number"
class="number-input"
:value="configStore.config.editing.minSaveInterval"
@change="handleMinSaveIntervalChange"
/>
</SettingItem>
</SettingSection>
</div>
</template>

View File

@@ -1,11 +1,12 @@
<script setup lang="ts">
import { useConfigStore } from '@/stores/configStore';
import { useI18n } from 'vue-i18n';
import { computed, ref, watch } from 'vue';
import { computed} from 'vue';
import SettingSection from '../components/SettingSection.vue';
import SettingItem from '../components/SettingItem.vue';
import ToggleSwitch from '../components/ToggleSwitch.vue';
import { useErrorHandler } from '@/utils/errorHandler';
import { DialogService } from '@/../bindings/voidraft/internal/services';
import * as runtime from '@wailsio/runtime';
const { t } = useI18n();
@@ -92,27 +93,28 @@ const useCustomDataPath = computed({
set: (value: boolean) => configStore.setUseCustomDataPath(value)
});
// 自定义路径输入
const customPathInput = ref('');
// 自定义路径显示
const customPathDisplay = computed(() => {
return configStore.config.general.customDataPath || '';
});
// 监听自定义路径的变化,同步到配置
const updateCustomPath = () => {
configStore.setCustomDataPath(customPathInput.value.trim());
};
const selectDirectory = async () => {
// 只有开启自定义路径时才能选择
if (!useCustomDataPath.value) return;
// 当启用自定义路径时,初始化输入框的值
const initCustomPath = () => {
if (useCustomDataPath.value) {
customPathInput.value = configStore.config.general.customDataPath || '';
try {
const selectedPath = await DialogService.SelectDirectory();
if (selectedPath && selectedPath.trim()) {
await configStore.setCustomDataPath(selectedPath.trim());
}
} catch (error) {
// 可以添加用户友好的错误提示
await safeCall(async () => {
throw error;
}, 'settings.selectDirectoryFailed');
}
};
// 监听useCustomDataPath的变化
watch(() => useCustomDataPath.value, (newVal) => {
if (newVal) {
initCustomPath();
}
}, { immediate: true });
</script>
<template>
@@ -163,20 +165,25 @@ watch(() => useCustomDataPath.value, (newVal) => {
<SettingItem :title="t('settings.useCustomDataPath')">
<ToggleSwitch v-model="useCustomDataPath" />
</SettingItem>
<div class="path-input-section" v-if="useCustomDataPath">
<!-- 路径显示区域 -->
<div class="path-section">
<div class="path-input-container">
<input
type="text"
v-model="customPathInput"
@blur="updateCustomPath"
@keyup.enter="updateCustomPath"
:placeholder="t('settings.enterCustomPath')"
class="path-input"
:value="useCustomDataPath ? customPathDisplay : configStore.config.general.defaultDataPath"
readonly
:placeholder="useCustomDataPath ? t('settings.selectDataDirectory') : t('settings.defaultDataPath')"
:class="[
'path-display-input',
{ 'clickable': useCustomDataPath, 'disabled': !useCustomDataPath }
]"
@click="selectDirectory"
/>
<div class="path-hint">{{ t('settings.pathHint') }}</div>
<div class="path-label">
{{ useCustomDataPath ? t('settings.customDataPath') : t('settings.defaultDataPath') }}
</div>
</div>
<div class="default-path-info" v-else>
<div class="path-display default">{{ configStore.config.general.defaultDataPath }}</div>
<span class="path-label">{{ t('settings.defaultDataPath') }}</span>
</div>
</SettingSection>
@@ -305,12 +312,12 @@ watch(() => useCustomDataPath.value, (newVal) => {
}
}
.path-input-section {
.path-section {
margin-top: 10px;
padding-left: 20px;
padding-right: 20px;
padding: 0 20px;
.path-input {
.path-input-container {
.path-display-input {
width: 100%;
max-width: 100%;
box-sizing: border-box;
@@ -322,53 +329,39 @@ watch(() => useCustomDataPath.value, (newVal) => {
font-size: 13px;
transition: all 0.2s ease;
&.disabled {
opacity: 0.6;
cursor: not-allowed;
background-color: #2a2a2a;
border-color: #444444;
}
&.clickable {
cursor: pointer;
&:hover {
border-color: #4a9eff;
background-color: #404040;
}
&:focus {
outline: none;
border-color: #4a9eff;
background-color: #404040;
}
}
&::placeholder {
color: #888888;
}
}
.path-hint {
margin-top: 5px;
color: #888888;
font-size: 12px;
line-height: 1.4;
}
}
.default-path-info {
margin-top: 10px;
padding-left: 20px;
padding-right: 20px;
.path-display {
padding: 8px 12px;
background-color: #2a2a2a;
border: 1px solid #444444;
border-radius: 4px;
color: #888888;
font-size: 13px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
box-sizing: border-box;
&.default {
font-style: italic;
}
}
.path-label {
display: block;
margin-top: 4px;
color: #666666;
color: #888888;
font-size: 12px;
}
}
}
.danger-zone {

View File

@@ -64,8 +64,6 @@ type EditingConfig struct {
// 保存选项
AutoSaveDelay int `json:"autoSaveDelay" yaml:"auto_save_delay" mapstructure:"auto_save_delay"` // 自动保存延迟(毫秒)
ChangeThreshold int `json:"changeThreshold" yaml:"change_threshold" mapstructure:"change_threshold"` // 变更字符阈值
MinSaveInterval int `json:"minSaveInterval" yaml:"min_save_interval" mapstructure:"min_save_interval"` // 最小保存间隔(毫秒)
}
// AppearanceConfig 外观设置配置
@@ -137,8 +135,6 @@ func NewDefaultAppConfig() *AppConfig {
TabType: TabTypeSpaces,
// 保存选项
AutoSaveDelay: 5000, // 5秒后自动保存
ChangeThreshold: 500, // 500个字符变更触发保存
MinSaveInterval: 1000, // 最小间隔1000毫秒
},
Appearance: AppearanceConfig{
Language: LangZhCN,

View File

@@ -124,8 +124,6 @@ func setDefaults(v *viper.Viper) {
v.SetDefault("editing.tab_size", defaultConfig.Editing.TabSize)
v.SetDefault("editing.tab_type", defaultConfig.Editing.TabType)
v.SetDefault("editing.auto_save_delay", defaultConfig.Editing.AutoSaveDelay)
v.SetDefault("editing.change_threshold", defaultConfig.Editing.ChangeThreshold)
v.SetDefault("editing.min_save_interval", defaultConfig.Editing.MinSaveInterval)
// 外观设置默认值
v.SetDefault("appearance.language", defaultConfig.Appearance.Language)

View File

@@ -0,0 +1,66 @@
package services
import (
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/services/log"
)
// DialogService 对话框服务,处理文件选择等对话框操作
type DialogService struct {
logger *log.LoggerService
}
// NewDialogService 创建新的对话框服务实例
func NewDialogService(logger *log.LoggerService) *DialogService {
if logger == nil {
logger = log.New()
}
return &DialogService{
logger: logger,
}
}
// SelectDirectory 打开目录选择对话框
func (ds *DialogService) SelectDirectory() (string, error) {
dialog := application.OpenFileDialogWithOptions(&application.OpenFileDialogOptions{
// 目录选择配置
CanChooseDirectories: true, // 允许选择目录
CanChooseFiles: false, // 不允许选择文件
CanCreateDirectories: true, // 允许创建新目录
AllowsMultipleSelection: false, // 单选模式
// 显示配置
ShowHiddenFiles: false, // 不显示隐藏文件
HideExtension: false, // 不隐藏扩展名
CanSelectHiddenExtension: false, // 不允许选择隐藏扩展名
TreatsFilePackagesAsDirectories: false, // 不将文件包当作目录处理
AllowsOtherFileTypes: false, // 不允许其他文件类型
// 系统配置
ResolvesAliases: true, // 解析别名/快捷方式
// 对话框文本配置
Title: "选择数据存储目录",
Message: "请选择用于存储应用数据的文件夹",
ButtonText: "选择",
// 不设置过滤器,因为我们选择目录
Filters: nil,
// 不指定默认目录,让系统决定
Directory: "",
// 不绑定到特定窗口,使用默认行为
Window: nil,
})
path, err := dialog.PromptForSingleSelection()
if err != nil {
ds.logger.Error("Failed to select directory", "error", err)
return "", err
}
ds.logger.Info("Directory selected", "path", path)
return path, nil
}

View File

@@ -1,306 +0,0 @@
package services
// Edit 表示编辑操作类型
type EditType int
const (
// EditInsert 插入操作
EditInsert EditType = iota
// EditDelete 删除操作
EditDelete
// EditEqual 相等部分
EditEqual
)
// Edit 表示单个编辑操作
type Edit struct {
Type EditType // 操作类型
Content string // 操作内容
}
// DiffResult 包含差异比较的结果信息
type DiffResult struct {
Edits []Edit // 编辑操作列表
InsertCount int // 插入的字符数
DeleteCount int // 删除的字符数
ChangedLines int // 变更的行数
TotalChanges int // 总变更字符数(插入+删除)
ChangedTokens int // 变更的token数如单词、标识符等
}
// calculateChangesDetailed 使用Myers差分算法计算两个字符串之间的具体变更
func calculateChangesDetailed(oldText, newText string) DiffResult {
// 将文本分割成行
oldLines := splitLines(oldText)
newLines := splitLines(newText)
// 计算行级别的差异
edits := computeLineEdits(oldLines, newLines)
// 计算变更统计
result := DiffResult{
Edits: edits,
}
// 统计变更
for _, edit := range edits {
switch edit.Type {
case EditInsert:
result.InsertCount += len(edit.Content)
result.ChangedLines++
case EditDelete:
result.DeleteCount += len(edit.Content)
result.ChangedLines++
}
}
result.TotalChanges = result.InsertCount + result.DeleteCount
result.ChangedTokens = estimateChangedTokens(edits)
return result
}
// splitLines 将文本分割成行
func splitLines(text string) []string {
var lines []string
var currentLine string
for _, char := range text {
if char == '\n' {
lines = append(lines, currentLine)
currentLine = ""
} else {
currentLine += string(char)
}
}
// 添加最后一行(如果不是以换行符结尾)
if currentLine != "" {
lines = append(lines, currentLine)
}
return lines
}
// computeLineEdits 使用Myers差分算法计算行级别的差异
func computeLineEdits(oldLines, newLines []string) []Edit {
var edits []Edit
// 使用Myers差分算法计算行级别的差异
script := myersDiff(oldLines, newLines)
// 将差异脚本转换为编辑操作
for _, op := range script {
switch op.Type {
case EditEqual:
edits = append(edits, Edit{
Type: EditEqual,
Content: oldLines[op.OldStart],
})
case EditDelete:
edits = append(edits, Edit{
Type: EditDelete,
Content: oldLines[op.OldStart],
})
case EditInsert:
edits = append(edits, Edit{
Type: EditInsert,
Content: newLines[op.NewStart],
})
}
}
return edits
}
// DiffOp 表示差分操作
type DiffOp struct {
Type EditType
OldStart int
OldEnd int
NewStart int
NewEnd int
}
// myersDiff 实现Myers差分算法
func myersDiff(oldLines, newLines []string) []DiffOp {
// 基本思路Myers差分算法通过建立编辑图来寻找最短编辑路径
// 简化版实现
var script []DiffOp
oldLen := len(oldLines)
newLen := len(newLines)
// 使用动态规划找出最长公共子序列(LCS)
lcs := longestCommonSubsequence(oldLines, newLines)
// 根据LCS构建差分脚本
oldIndex, newIndex := 0, 0
for _, entry := range lcs {
// 处理LCS之前的差异
for oldIndex < entry.OldIndex {
script = append(script, DiffOp{
Type: EditDelete,
OldStart: oldIndex,
OldEnd: oldIndex + 1,
NewStart: newIndex,
NewEnd: newIndex,
})
oldIndex++
}
for newIndex < entry.NewIndex {
script = append(script, DiffOp{
Type: EditInsert,
OldStart: oldIndex,
OldEnd: oldIndex,
NewStart: newIndex,
NewEnd: newIndex + 1,
})
newIndex++
}
// 处理相等部分
script = append(script, DiffOp{
Type: EditEqual,
OldStart: oldIndex,
OldEnd: oldIndex + 1,
NewStart: newIndex,
NewEnd: newIndex + 1,
})
oldIndex++
newIndex++
}
// 处理剩余差异
for oldIndex < oldLen {
script = append(script, DiffOp{
Type: EditDelete,
OldStart: oldIndex,
OldEnd: oldIndex + 1,
NewStart: newIndex,
NewEnd: newIndex,
})
oldIndex++
}
for newIndex < newLen {
script = append(script, DiffOp{
Type: EditInsert,
OldStart: oldIndex,
OldEnd: oldIndex,
NewStart: newIndex,
NewEnd: newIndex + 1,
})
newIndex++
}
return script
}
// LCSEntry 表示最长公共子序列中的一个条目
type LCSEntry struct {
OldIndex int
NewIndex int
}
// longestCommonSubsequence 寻找两个字符串数组的最长公共子序列
func longestCommonSubsequence(oldLines, newLines []string) []LCSEntry {
oldLen := len(oldLines)
newLen := len(newLines)
// 创建动态规划表
dp := make([][]int, oldLen+1)
for i := range dp {
dp[i] = make([]int, newLen+1)
}
// 填充DP表
for i := 1; i <= oldLen; i++ {
for j := 1; j <= newLen; j++ {
if oldLines[i-1] == newLines[j-1] {
dp[i][j] = dp[i-1][j-1] + 1
} else {
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
}
}
}
// 回溯找出LCS
var lcs []LCSEntry
i, j := oldLen, newLen
for i > 0 && j > 0 {
if oldLines[i-1] == newLines[j-1] {
lcs = append([]LCSEntry{{OldIndex: i - 1, NewIndex: j - 1}}, lcs...)
i--
j--
} else if dp[i-1][j] > dp[i][j-1] {
i--
} else {
j--
}
}
return lcs
}
// max 返回两个整数中的较大值
func max(a, b int) int {
if a > b {
return a
}
return b
}
// estimateChangedTokens 估计变更的token数量
// 这里使用简单的单词分割来估计
func estimateChangedTokens(edits []Edit) int {
tokenCount := 0
for _, edit := range edits {
switch edit.Type {
case EditInsert, EditDelete:
// 简单地将内容按空白字符分割成单词
words := splitIntoWords(edit.Content)
tokenCount += len(words)
}
}
return tokenCount
}
// splitIntoWords 将文本分割成单词
func splitIntoWords(text string) []string {
var words []string
var currentWord string
// 简单的状态机:
// - 如果是字母、数字或下划线,添加到当前单词
// - 否则,结束当前单词并开始新单词
for _, char := range text {
if isWordChar(char) {
currentWord += string(char)
} else {
if currentWord != "" {
words = append(words, currentWord)
currentWord = ""
}
}
}
// 添加最后一个单词(如果有)
if currentWord != "" {
words = append(words, currentWord)
}
return words
}
// isWordChar 判断字符是否是单词字符(字母、数字或下划线)
func isWordChar(char rune) bool {
return (char >= 'a' && char <= 'z') ||
(char >= 'A' && char <= 'Z') ||
(char >= '0' && char <= '9') ||
char == '_'
}

View File

@@ -11,20 +11,6 @@ import (
"voidraft/internal/models"
)
// SaveTrigger 保存触发器类型
type SaveTrigger int
const (
// SaveTriggerAuto 自动保存
SaveTriggerAuto SaveTrigger = iota
// SaveTriggerManual 手动触发保存
SaveTriggerManual
// SaveTriggerThreshold 超过阈值触发保存
SaveTriggerThreshold
// SaveTriggerShutdown 程序关闭触发保存
SaveTriggerShutdown
)
// DocumentError 文档操作错误
type DocumentError struct {
Operation string // 操作名称
@@ -45,18 +31,18 @@ func (e *DocumentError) Unwrap() error {
type DocumentService struct {
configService *ConfigService
logger *log.LoggerService
activeDoc *models.Document
document *models.Document // 文档实例
docStore *Store[models.Document]
memoryCache *models.Document // 内存缓存,小改动只更新此缓存
lock sync.RWMutex
lastSaveTime time.Time
changeCounter int // 变更计数器,记录自上次保存后的变更数量
saveTimer *time.Timer // 自动保存定时器
pendingSave bool // 是否有等待保存的更
saveChannel chan SaveTrigger // 保存通道,用于接收保存触发信号
shutdownChan chan struct{} // 关闭通道,用于程序退出时通知保存协程
shutdownWg sync.WaitGroup // 等待组,用于确保保存协程正常退出
onSaveCallback func(trigger SaveTrigger) // 保存回调函数
mutex sync.RWMutex // 保护document的读写
// 定时保存
saveTimer *time.Timer
timerMutex sync.Mutex // 保护定时器操作
isDirty bool // 文档是否有保存的
// 服务控制
shutdown chan struct{}
shutdownOnce sync.Once
}
// NewDocumentService 创建新的文档服务实例
@@ -68,9 +54,7 @@ func NewDocumentService(configService *ConfigService, logger *log.LoggerService)
service := &DocumentService{
configService: configService,
logger: logger,
saveChannel: make(chan SaveTrigger, 10),
shutdownChan: make(chan struct{}),
lastSaveTime: time.Now(),
shutdown: make(chan struct{}),
}
return service
@@ -79,203 +63,101 @@ func NewDocumentService(configService *ConfigService, logger *log.LoggerService)
// Initialize 初始化文档服务
func (ds *DocumentService) Initialize() error {
// 确保文档目录存在
err := ds.ensureDocumentsDir()
if err != nil {
if err := ds.ensureDocumentsDir(); err != nil {
ds.logger.Error("Document: Failed to ensure documents directory", "error", err)
return &DocumentError{Operation: "initialize", Err: err}
}
// 初始化文档存储
err = ds.initDocumentStore()
if err != nil {
if err := ds.initDocumentStore(); err != nil {
ds.logger.Error("Document: Failed to initialize document store", "error", err)
return &DocumentError{Operation: "init_store", Err: err}
}
// 加载默认文档
err = ds.LoadDefaultDocument()
if err != nil {
if err := ds.loadDefaultDocument(); err != nil {
ds.logger.Error("Document: Failed to load default document", "error", err)
return &DocumentError{Operation: "load_default", Err: err}
}
// 启动保存处理协程
ds.startSaveProcessor()
// 启动定时保存
ds.startAutoSave()
return nil
}
// startSaveProcessor 启动保存处理协程
func (ds *DocumentService) startSaveProcessor() {
ds.shutdownWg.Add(1)
go func() {
defer ds.shutdownWg.Done()
for {
select {
case trigger := <-ds.saveChannel:
// 接收到保存信号,执行保存
ds.saveToStore(trigger)
case <-ds.shutdownChan:
// 接收到关闭信号,保存并退出
if ds.pendingSave {
ds.saveToStore(SaveTriggerShutdown)
}
return
}
}
}()
}
// scheduleAutoSave 安排自动保存
func (ds *DocumentService) scheduleAutoSave() {
// 获取配置
// startAutoSave 启动定时保存
func (ds *DocumentService) startAutoSave() {
config, err := ds.configService.GetConfig()
if err != nil {
ds.logger.Error("Document: Failed to get config for auto save", "error", err)
// 使用默认值2
ds.scheduleTimerWithDelay(2000)
ds.scheduleNextSave(5 * time.Second) // 默认5
return
}
// 检查配置有效性
if config == nil {
ds.logger.Error("Document: Config is nil, using default delay")
ds.scheduleTimerWithDelay(2000)
return
}
delay := time.Duration(config.Editing.AutoSaveDelay) * time.Millisecond
ds.scheduleNextSave(delay)
}
// 打印保存设置,便于调试
ds.logger.Debug("Document: Auto save settings",
"autoSaveDelay", config.Editing.AutoSaveDelay,
"changeThreshold", config.Editing.ChangeThreshold,
"minSaveInterval", config.Editing.MinSaveInterval)
// scheduleNextSave 安排下次保存
func (ds *DocumentService) scheduleNextSave(delay time.Duration) {
ds.timerMutex.Lock()
defer ds.timerMutex.Unlock()
ds.lock.Lock()
defer ds.lock.Unlock()
// 重置自动保存定时器
// 停止现有定时器
if ds.saveTimer != nil {
ds.saveTimer.Stop()
}
// 创建新的自动保存定时器
autoSaveDelay := config.Editing.AutoSaveDelay
ds.logger.Debug("Document: Scheduling auto save", "delay", autoSaveDelay)
ds.scheduleTimerWithDelay(autoSaveDelay)
}
// scheduleTimerWithDelay 使用指定延迟创建定时器
func (ds *DocumentService) scheduleTimerWithDelay(delayMs int) {
ds.saveTimer = time.AfterFunc(time.Duration(delayMs)*time.Millisecond, func() {
// 只有在有待保存的更改时才触发保存
if ds.pendingSave {
ds.saveChannel <- SaveTriggerAuto
}
// 创建新的定时器
ds.saveTimer = time.AfterFunc(delay, func() {
ds.performAutoSave()
// 安排下次保存
ds.startAutoSave()
})
}
// saveToStore 保存文档到存储
func (ds *DocumentService) saveToStore(trigger SaveTrigger) {
ds.lock.Lock()
defer ds.lock.Unlock()
// performAutoSave 执行自动保存
func (ds *DocumentService) performAutoSave() {
ds.mutex.Lock()
defer ds.mutex.Unlock()
// 如果没有内存缓存或活动文档,直接返回
if ds.memoryCache == nil || ds.activeDoc == nil {
// 如果没有变更,跳过保存
if !ds.isDirty || ds.document == nil {
return
}
// 获取配置
config, err := ds.configService.GetConfig()
if err != nil {
ds.logger.Error("Document: Failed to get config for save", "error", err)
// 继续使用默认值
}
// 更新元数据并保存
ds.document.Meta.LastUpdated = time.Now()
// 设置默认值
minInterval := 500 // 默认500毫秒
ds.logger.Info("Document: Auto saving document",
"id", ds.document.Meta.ID,
"contentLength", len(ds.document.Content))
// 如果成功获取了配置,使用配置值
if err == nil && config != nil {
minInterval = config.Editing.MinSaveInterval
}
// 如果是自动保存,检查最小保存间隔
if trigger == SaveTriggerAuto {
now := time.Now()
elapsed := now.Sub(ds.lastSaveTime).Milliseconds()
// 如果距离上次保存时间太短,重新安排保存
if elapsed < int64(minInterval) {
// 重新安排保存,延迟 = 最小间隔 - 已经过的时间
delayMs := minInterval - int(elapsed)
ds.logger.Debug("Document: Rescheduling save due to min interval",
"minInterval", minInterval,
"elapsed", elapsed,
"delayMs", delayMs)
ds.lock.Unlock() // 解锁后再启动定时器,避免死锁
ds.scheduleTimerWithDelay(delayMs)
ds.lock.Lock() // 恢复锁
return
}
}
// 更新活动文档
ds.activeDoc = ds.memoryCache
ds.activeDoc.Meta.LastUpdated = time.Now()
// 保存到存储
ds.logger.Info("Document: Saving document to disk",
"trigger", trigger,
"id", ds.activeDoc.Meta.ID,
"contentLength", len(ds.activeDoc.Content))
err = ds.docStore.Set(*ds.activeDoc)
if err != nil {
ds.logger.Error("Document: Failed to save document", "trigger", trigger, "error", err)
if err := ds.docStore.Set(*ds.document); err != nil {
ds.logger.Error("Document: Failed to auto save document", "error", err)
return
}
// 强制确保保存到磁盘
err = ds.docStore.Save()
if err != nil {
ds.logger.Error("Document: Failed to force save document", "trigger", trigger, "error", err)
if err := ds.docStore.Save(); err != nil {
ds.logger.Error("Document: Failed to force save document", "error", err)
return
}
// 重置计数器和状态
ds.changeCounter = 0
ds.pendingSave = false
ds.lastSaveTime = time.Now()
// 触发回调
if ds.onSaveCallback != nil {
ds.onSaveCallback(trigger)
}
ds.logger.Info("Document: Saved document", "trigger", trigger, "id", ds.activeDoc.Meta.ID)
// 重置脏标记
ds.isDirty = false
ds.logger.Info("Document: Auto save completed")
}
// Shutdown 关闭文档服务,确保所有数据保存
func (ds *DocumentService) Shutdown() {
// 发送关闭信号
close(ds.shutdownChan)
// stopTimer 停止定时器
func (ds *DocumentService) stopTimer() {
ds.timerMutex.Lock()
defer ds.timerMutex.Unlock()
// 等待保存协程退出
ds.shutdownWg.Wait()
// 停止定时器
if ds.saveTimer != nil {
ds.saveTimer.Stop()
ds.saveTimer = nil
}
ds.logger.Info("Document: Service shutdown completed")
}
// SetSaveCallback 设置保存回调函数
func (ds *DocumentService) SetSaveCallback(callback func(trigger SaveTrigger)) {
ds.onSaveCallback = callback
}
// initDocumentStore 初始化文档存储
@@ -287,10 +169,9 @@ func (ds *DocumentService) initDocumentStore() error {
ds.logger.Info("Document: Initializing document store", "path", docPath)
// 创建文档存储强制保存和Service触发的保存都使用同步保存到磁盘
ds.docStore = NewStore[models.Document](StoreOption{
FilePath: docPath,
AutoSave: true, // 启用自动保存确保Set操作直接写入磁盘
AutoSave: true,
Logger: ds.logger,
})
@@ -304,7 +185,6 @@ func (ds *DocumentService) ensureDocumentsDir() error {
return err
}
// 获取实际的数据路径(如果启用自定义路径则使用自定义路径,否则使用默认路径)
var dataPath string
if config.General.UseCustomDataPath && config.General.CustomDataPath != "" {
dataPath = config.General.CustomDataPath
@@ -312,14 +192,8 @@ func (ds *DocumentService) ensureDocumentsDir() error {
dataPath = config.General.DefaultDataPath
}
// 创建文档目录
docsDir := filepath.Join(dataPath, "docs")
err = os.MkdirAll(docsDir, 0755)
if err != nil {
return err
}
return nil
return os.MkdirAll(docsDir, 0755)
}
// getDocumentsDir 获取文档目录路径
@@ -329,7 +203,6 @@ func (ds *DocumentService) getDocumentsDir() (string, error) {
return "", err
}
// 获取实际的数据路径(如果启用自定义路径则使用自定义路径,否则使用默认路径)
var dataPath string
if config.General.UseCustomDataPath && config.General.CustomDataPath != "" {
dataPath = config.General.CustomDataPath
@@ -349,153 +222,99 @@ func (ds *DocumentService) getDefaultDocumentPath() (string, error) {
return filepath.Join(docsDir, "default.json"), nil
}
// LoadDefaultDocument 加载默认文档
func (ds *DocumentService) LoadDefaultDocument() error {
// 从Store加载文档
// loadDefaultDocument 加载默认文档
func (ds *DocumentService) loadDefaultDocument() error {
doc := ds.docStore.Get()
// 检查文档是否有效
ds.mutex.Lock()
defer ds.mutex.Unlock()
if doc.Meta.ID == "" {
// 创建默认文档
defaultDoc := models.NewDefaultDocument()
ds.lock.Lock()
ds.activeDoc = defaultDoc
ds.memoryCache = defaultDoc // 同时更新内存缓存
ds.lock.Unlock()
ds.document = models.NewDefaultDocument()
// 保存默认文档
err := ds.docStore.Set(*defaultDoc)
if err != nil {
if err := ds.docStore.Set(*ds.document); err != nil {
return &DocumentError{Operation: "save_default", Err: err}
}
ds.logger.Info("Document: Created and saved default document")
return nil
} else {
ds.document = &doc
ds.logger.Info("Document: Loaded default document", "id", doc.Meta.ID)
}
// 设置为活动文档
ds.lock.Lock()
ds.activeDoc = &doc
ds.memoryCache = &doc // 同时更新内存缓存
ds.lock.Unlock()
ds.logger.Info("Document: Loaded default document", "id", doc.Meta.ID)
return nil
}
// GetActiveDocument 获取当前活动文档
func (ds *DocumentService) GetActiveDocument() (*models.Document, error) {
ds.lock.RLock()
defer ds.lock.RUnlock()
ds.mutex.RLock()
defer ds.mutex.RUnlock()
if ds.memoryCache == nil {
if ds.document == nil {
return nil, errors.New("no active document loaded")
}
// 返回内存缓存中的文档,确保获得最新版本
return ds.memoryCache, nil
// 返回副本以防止外部修改
docCopy := *ds.document
return &docCopy, nil
}
// GetActiveDocumentContent 获取当前活动文档内容
func (ds *DocumentService) GetActiveDocumentContent() (string, error) {
ds.lock.RLock()
defer ds.lock.RUnlock()
ds.mutex.RLock()
defer ds.mutex.RUnlock()
if ds.memoryCache == nil {
if ds.document == nil {
return "", errors.New("no active document loaded")
}
return ds.memoryCache.Content, nil
return ds.document.Content, nil
}
// UpdateActiveDocumentContent 更新当前活动文档内容
func (ds *DocumentService) UpdateActiveDocumentContent(content string) error {
// 获取配置
config, err := ds.configService.GetConfig()
if err != nil {
ds.logger.Error("Document: Failed to get config for content update", "error", err)
// 出错时仍继续,使用默认行为
}
ds.mutex.Lock()
defer ds.mutex.Unlock()
// 设置默认配置值
threshold := 100 // 默认值
// 如果成功获取了配置,使用配置值
if err == nil && config != nil {
threshold = config.Editing.ChangeThreshold
}
ds.lock.Lock()
if ds.memoryCache == nil {
ds.lock.Unlock()
if ds.document == nil {
return errors.New("no active document loaded")
}
// 计算变更数量
oldContent := ds.memoryCache.Content
changedChars := calculateChanges(oldContent, content)
ds.changeCounter += changedChars
// 调试信息
ds.logger.Debug("Document: Content updated",
"changedChars", changedChars,
"totalChanges", ds.changeCounter,
"threshold", threshold)
// 更新内存缓存
ds.memoryCache.Content = content
ds.memoryCache.Meta.LastUpdated = time.Now()
ds.pendingSave = true
// 如果变更超过阈值,触发保存
if ds.changeCounter >= threshold {
ds.logger.Info("Document: Change threshold reached, triggering save",
"threshold", threshold,
"changes", ds.changeCounter)
// 提前解锁,避免死锁
ds.lock.Unlock()
ds.saveChannel <- SaveTriggerThreshold
} else {
// 否则安排自动保存
ds.lock.Unlock()
ds.scheduleAutoSave()
}
// 更新文档内容并标记为脏
ds.document.Content = content
ds.document.Meta.LastUpdated = time.Now()
ds.isDirty = true
return nil
}
// SaveDocumentSync 同步保存文档内容 (用于页面关闭前同步保存)
// SaveDocumentSync 同步保存文档内容
func (ds *DocumentService) SaveDocumentSync(content string) error {
ds.lock.Lock()
ds.mutex.Lock()
defer ds.mutex.Unlock()
if ds.memoryCache == nil {
ds.lock.Unlock()
if ds.document == nil {
return errors.New("no active document loaded")
}
// 更新内存缓存
ds.memoryCache.Content = content
ds.memoryCache.Meta.LastUpdated = time.Now()
// 更新内
ds.document.Content = content
ds.document.Meta.LastUpdated = time.Now()
// 直接保存到存储
doc := *ds.memoryCache
ds.lock.Unlock()
err := ds.docStore.Set(doc)
if err != nil {
// 立即保存
if err := ds.docStore.Set(*ds.document); err != nil {
return err
}
// 重置状态
ds.lock.Lock()
ds.pendingSave = false
ds.changeCounter = 0
ds.lastSaveTime = time.Now()
ds.lock.Unlock()
if err := ds.docStore.Save(); err != nil {
return err
}
ds.logger.Info("Document: Synced document save completed")
// 重置脏标记
ds.isDirty = false
ds.logger.Info("Document: Sync save completed")
return nil
}
@@ -503,105 +322,47 @@ func (ds *DocumentService) SaveDocumentSync(content string) error {
func (ds *DocumentService) ForceSave() error {
ds.logger.Info("Document: Force save triggered")
// 获取当前文档内容
ds.lock.RLock()
if ds.memoryCache == nil {
ds.lock.RUnlock()
ds.mutex.RLock()
if ds.document == nil {
ds.mutex.RUnlock()
return errors.New("no active document loaded")
}
content := ds.memoryCache.Content
ds.lock.RUnlock()
content := ds.document.Content
ds.mutex.RUnlock()
// 使用同步方法直接保存到磁盘
if err := ds.SaveDocumentSync(content); err != nil {
ds.logger.Error("Document: Force save failed", "error", err)
return err
}
ds.logger.Info("Document: Force save completed successfully")
return nil
return ds.SaveDocumentSync(content)
}
// calculateChanges 计算两个字符串之间的变更数量
func calculateChanges(old, new string) int {
// 使用详细的差分算法计算变更
result := calculateChangesDetailed(old, new)
// 返回总变更字符数
return result.TotalChanges
}
// GetDiffInfo 获取两个文本之间的详细差异信息
func (ds *DocumentService) GetDiffInfo(oldText, newText string) DiffResult {
return calculateChangesDetailed(oldText, newText)
}
// GetSaveSettings 获取文档保存设置
func (ds *DocumentService) GetSaveSettings() (*models.EditingConfig, error) {
config, err := ds.configService.GetConfig()
if err != nil {
return nil, &DocumentError{Operation: "get_save_settings", Err: err}
}
return &config.Editing, nil
}
// UpdateSaveSettings 更新文档保存设置
func (ds *DocumentService) UpdateSaveSettings(docConfig models.EditingConfig) error {
// 使用配置服务的 Set 方法更新文档配置
if err := ds.configService.Set("editing.auto_save_delay", docConfig.AutoSaveDelay); err != nil {
return &DocumentError{Operation: "update_save_settings_auto_save_delay", Err: err}
}
if err := ds.configService.Set("editing.change_threshold", docConfig.ChangeThreshold); err != nil {
return &DocumentError{Operation: "update_save_settings_change_threshold", Err: err}
}
if err := ds.configService.Set("editing.min_save_interval", docConfig.MinSaveInterval); err != nil {
return &DocumentError{Operation: "update_save_settings_min_save_interval", Err: err}
}
// 安排自动保存
ds.scheduleAutoSave()
ds.logger.Info("Document: Updated save settings")
return nil
}
// ServiceShutdown 实现应用程序关闭时的服务关闭逻辑
// ServiceShutdown 服务关闭
func (ds *DocumentService) ServiceShutdown() error {
ds.logger.Info("Document: Service is shutting down, saving document...")
ds.logger.Info("Document: Service is shutting down...")
// 获取当前活动文档
ds.lock.RLock()
if ds.memoryCache == nil {
ds.lock.RUnlock()
ds.logger.Info("Document: No active document to save on shutdown")
return nil
}
// 确保只执行一次关闭
var shutdownErr error
ds.shutdownOnce.Do(func() {
// 停止定时器
ds.stopTimer()
// 获取要保存的内容
content := ds.memoryCache.Content
ds.lock.RUnlock()
// 同步保存文档内容
err := ds.SaveDocumentSync(content)
if err != nil {
ds.logger.Error("Document: Failed to save document on shutdown", "error", err)
return err
}
// 执行最后一次保存
ds.mutex.RLock()
if ds.document != nil && ds.isDirty {
content := ds.document.Content
ds.mutex.RUnlock()
if err := ds.SaveDocumentSync(content); err != nil {
ds.logger.Error("Document: Failed to save on shutdown", "error", err)
shutdownErr = err
} else {
ds.logger.Info("Document: Document saved successfully on shutdown")
// 关闭通道以通知保存协程退出
close(ds.shutdownChan)
// 等待保存协程退出
ds.shutdownWg.Wait()
// 停止所有计时器
if ds.saveTimer != nil {
ds.saveTimer.Stop()
}
} else {
ds.mutex.RUnlock()
}
return nil
// 关闭服务
close(ds.shutdown)
ds.logger.Info("Document: Service shutdown completed")
})
return shutdownErr
}

View File

@@ -13,6 +13,7 @@ type ServiceManager struct {
documentService *DocumentService
systemService *SystemService
hotkeyService *HotkeyService
dialogService *DialogService
logger *log.LoggerService
}
@@ -33,6 +34,9 @@ func NewServiceManager() *ServiceManager {
// 初始化热键服务
hotkeyService := NewHotkeyService(configService, logger)
// 初始化对话服务
dialogService := NewDialogService(logger)
// 使用新的配置通知系统设置热键配置变更监听
err := configService.SetHotkeyChangeCallback(func(enable bool, hotkey *models.HotkeyCombo) error {
return hotkeyService.UpdateHotkey(enable, hotkey)
@@ -54,6 +58,7 @@ func NewServiceManager() *ServiceManager {
documentService: documentService,
systemService: systemService,
hotkeyService: hotkeyService,
dialogService: dialogService,
logger: logger,
}
}
@@ -65,6 +70,7 @@ func (sm *ServiceManager) GetServices() []application.Service {
application.NewService(sm.documentService),
application.NewService(sm.systemService),
application.NewService(sm.hotkeyService),
application.NewService(sm.dialogService),
}
}