🎨 Change configuration structure

This commit is contained in:
2025-06-03 00:19:16 +08:00
parent 77bd15bed7
commit 81868e8d37
12 changed files with 525 additions and 386 deletions

View File

@@ -16,29 +16,6 @@ const (
TabTypeTab TabType = "tab"
)
// DocumentConfig 定义文档配置
type DocumentConfig struct {
// 自动保存延迟(毫秒)- 内容变更后多久自动保存
AutoSaveDelay int `json:"autoSaveDelay" yaml:"auto_save_delay" mapstructure:"auto_save_delay"`
// 变更字符阈值,超过此阈值立即触发保存
ChangeThreshold int `json:"changeThreshold" yaml:"change_threshold" mapstructure:"change_threshold"`
// 最小保存间隔(毫秒)- 两次保存之间的最小时间间隔避免频繁IO
MinSaveInterval int `json:"minSaveInterval" yaml:"min_save_interval" mapstructure:"min_save_interval"`
}
// EditorConfig 定义编辑器配置
type EditorConfig struct {
FontSize int `json:"fontSize" yaml:"font_size" mapstructure:"font_size"` // 字体大小
FontFamily string `json:"fontFamily" yaml:"font_family" mapstructure:"font_family"` // 字体族
FontWeight string `json:"fontWeight" yaml:"font_weight" mapstructure:"font_weight"` // 字体粗细
LineHeight float64 `json:"lineHeight" yaml:"line_height" mapstructure:"line_height"` // 行高
EnableTabIndent bool `json:"enableTabIndent" yaml:"enable_tab_indent" mapstructure:"enable_tab_indent"` // 是否启用Tab缩进
TabSize int `json:"tabSize" yaml:"tab_size" mapstructure:"tab_size"` // Tab大小
TabType TabType `json:"tabType" yaml:"tab_type" mapstructure:"tab_type"` // Tab类型空格或Tab
Language LanguageType `json:"language" yaml:"language" mapstructure:"language"` // 界面语言
AlwaysOnTop bool `json:"alwaysOnTop" yaml:"always_on_top" mapstructure:"always_on_top"` // 窗口是否置顶
}
// LanguageType 语言类型定义
type LanguageType string
@@ -49,17 +26,54 @@ const (
LangEnUS LanguageType = "en-US"
)
// PathsConfig 路径配置集合
type PathsConfig struct {
DataPath string `json:"dataPath" yaml:"data_path" mapstructure:"data_path"` // 数据存储路径
// GeneralConfig 通用设置配置
type GeneralConfig struct {
AlwaysOnTop bool `json:"alwaysOnTop" yaml:"always_on_top" mapstructure:"always_on_top"` // 窗口是否置顶
DataPath string `json:"dataPath" yaml:"data_path" mapstructure:"data_path"` // 数据存储路径
}
// AppConfig 应用配置 - 包含业务配置和路径配置
// EditingConfig 编辑设置配置
type EditingConfig struct {
// 字体设置
FontSize int `json:"fontSize" yaml:"font_size" mapstructure:"font_size"` // 字体大小
FontFamily string `json:"fontFamily" yaml:"font_family" mapstructure:"font_family"` // 字体族
FontWeight string `json:"fontWeight" yaml:"font_weight" mapstructure:"font_weight"` // 字体粗细
LineHeight float64 `json:"lineHeight" yaml:"line_height" mapstructure:"line_height"` // 行高
// Tab设置
EnableTabIndent bool `json:"enableTabIndent" yaml:"enable_tab_indent" mapstructure:"enable_tab_indent"` // 是否启用Tab缩进
TabSize int `json:"tabSize" yaml:"tab_size" mapstructure:"tab_size"` // Tab大小
TabType TabType `json:"tabType" yaml:"tab_type" mapstructure:"tab_type"` // Tab类型空格或Tab
// 保存选项
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 外观设置配置
type AppearanceConfig struct {
Language LanguageType `json:"language" yaml:"language" mapstructure:"language"` // 界面语言
}
// KeyBindingsConfig 快捷键设置配置
type KeyBindingsConfig struct {
// 预留给未来的快捷键配置
}
// UpdatesConfig 更新设置配置
type UpdatesConfig struct {
// 预留给未来的更新配置
}
// AppConfig 应用配置 - 按照前端设置页面分类组织
type AppConfig struct {
Editor EditorConfig `json:"editor" yaml:"editor" mapstructure:"editor"` // 编辑器配
Document DocumentConfig `json:"document" yaml:"document" mapstructure:"document"` // 文档配
Paths PathsConfig `json:"paths" yaml:"paths" mapstructure:"paths"` // 路径配
Metadata ConfigMetadata `json:"metadata" yaml:"metadata" mapstructure:"metadata"` // 配置元数据
General GeneralConfig `json:"general" yaml:"general" mapstructure:"general"` // 通用设
Editing EditingConfig `json:"editing" yaml:"editing" mapstructure:"editing"` // 编辑设
Appearance AppearanceConfig `json:"appearance" yaml:"appearance" mapstructure:"appearance"` // 外观设
KeyBindings KeyBindingsConfig `json:"keyBindings" yaml:"key_bindings" mapstructure:"key_bindings"` // 快捷键设置
Updates UpdatesConfig `json:"updates" yaml:"updates" mapstructure:"updates"` // 更新设置
Metadata ConfigMetadata `json:"metadata" yaml:"metadata" mapstructure:"metadata"` // 配置元数据
}
// ConfigMetadata 配置元数据
@@ -80,24 +94,33 @@ func NewDefaultAppConfig() *AppConfig {
dataDir := filepath.Join(currentDir, "data")
return &AppConfig{
Editor: EditorConfig{
FontSize: 13,
FontFamily: `"HarmonyOS Sans SC", "HarmonyOS Sans", "Microsoft YaHei", "PingFang SC", "Helvetica Neue", Arial, sans-serif`,
FontWeight: "normal",
LineHeight: 1.5,
General: GeneralConfig{
AlwaysOnTop: false,
DataPath: dataDir,
},
Editing: EditingConfig{
// 字体设置
FontSize: 13,
FontFamily: `"HarmonyOS Sans SC", "HarmonyOS Sans", "Microsoft YaHei", "PingFang SC", "Helvetica Neue", Arial, sans-serif`,
FontWeight: "normal",
LineHeight: 1.5,
// Tab设置
EnableTabIndent: true,
TabSize: 4,
TabType: TabTypeSpaces,
Language: LangZhCN,
AlwaysOnTop: false,
},
Document: DocumentConfig{
// 保存选项
AutoSaveDelay: 5000, // 5秒后自动保存
ChangeThreshold: 500, // 500个字符变更触发保存
MinSaveInterval: 1000, // 最小间隔1000毫秒
},
Paths: PathsConfig{
DataPath: dataDir,
Appearance: AppearanceConfig{
Language: LangZhCN,
},
KeyBindings: KeyBindingsConfig{
// 预留给未来的快捷键配置
},
Updates: UpdatesConfig{
// 预留给未来的更新配置
},
Metadata: ConfigMetadata{
Version: "1.0.0",

View File

@@ -68,7 +68,7 @@ func (cs *ConfigService) validateAndFixValue(key string, value interface{}) (int
fixed := false
switch key {
case "editor.font_size":
case "editing.font_size":
if intVal, ok := value.(int); ok {
if intVal < limits.FontSizeMin {
cs.logger.Warning("Config: Font size too small, fixing", "original", intVal, "fixed", limits.FontSizeMin)
@@ -80,7 +80,7 @@ func (cs *ConfigService) validateAndFixValue(key string, value interface{}) (int
}
}
case "editor.tab_size":
case "editing.tab_size":
if intVal, ok := value.(int); ok {
if intVal < limits.TabSizeMin {
cs.logger.Warning("Config: Tab size too small, fixing", "original", intVal, "fixed", limits.TabSizeMin)
@@ -92,7 +92,7 @@ func (cs *ConfigService) validateAndFixValue(key string, value interface{}) (int
}
}
case "editor.tab_type":
case "editing.tab_type":
if strVal, ok := value.(string); ok {
validTypes := []string{string(models.TabTypeSpaces), string(models.TabTypeTab)}
isValid := false
@@ -108,7 +108,7 @@ func (cs *ConfigService) validateAndFixValue(key string, value interface{}) (int
}
}
case "editor.language":
case "appearance.language":
if strVal, ok := value.(string); ok {
validLanguages := []string{string(models.LangZhCN), string(models.LangEnUS)}
isValid := false
@@ -124,7 +124,7 @@ func (cs *ConfigService) validateAndFixValue(key string, value interface{}) (int
}
}
case "document.auto_save_delay":
case "editing.auto_save_delay":
if intVal, ok := value.(int); ok {
if intVal < 1000 {
cs.logger.Warning("Config: Auto save delay too small, fixing", "original", intVal, "fixed", 1000)
@@ -136,7 +136,7 @@ func (cs *ConfigService) validateAndFixValue(key string, value interface{}) (int
}
}
case "document.change_threshold":
case "editing.change_threshold":
if intVal, ok := value.(int); ok {
if intVal < 10 {
cs.logger.Warning("Config: Change threshold too small, fixing", "original", intVal, "fixed", 10)
@@ -148,7 +148,7 @@ func (cs *ConfigService) validateAndFixValue(key string, value interface{}) (int
}
}
case "document.min_save_interval":
case "editing.min_save_interval":
if intVal, ok := value.(int); ok {
if intVal < 100 {
cs.logger.Warning("Config: Min save interval too small, fixing", "original", intVal, "fixed", 100)
@@ -175,39 +175,39 @@ func (cs *ConfigService) validateAllConfig() error {
}
// 验证编辑器配置
if fixedValue, fixed := cs.validateAndFixValue("editor.font_size", config.Editor.FontSize); fixed {
cs.viper.Set("editor.font_size", fixedValue)
if fixedValue, fixed := cs.validateAndFixValue("editing.font_size", config.Editing.FontSize); fixed {
cs.viper.Set("editing.font_size", fixedValue)
hasChanges = true
}
if fixedValue, fixed := cs.validateAndFixValue("editor.tab_size", config.Editor.TabSize); fixed {
cs.viper.Set("editor.tab_size", fixedValue)
if fixedValue, fixed := cs.validateAndFixValue("editing.tab_size", config.Editing.TabSize); fixed {
cs.viper.Set("editing.tab_size", fixedValue)
hasChanges = true
}
if fixedValue, fixed := cs.validateAndFixValue("editor.tab_type", string(config.Editor.TabType)); fixed {
cs.viper.Set("editor.tab_type", fixedValue)
if fixedValue, fixed := cs.validateAndFixValue("editing.tab_type", string(config.Editing.TabType)); fixed {
cs.viper.Set("editing.tab_type", fixedValue)
hasChanges = true
}
if fixedValue, fixed := cs.validateAndFixValue("editor.language", string(config.Editor.Language)); fixed {
cs.viper.Set("editor.language", fixedValue)
if fixedValue, fixed := cs.validateAndFixValue("appearance.language", string(config.Appearance.Language)); fixed {
cs.viper.Set("appearance.language", fixedValue)
hasChanges = true
}
// 验证文档配置
if fixedValue, fixed := cs.validateAndFixValue("document.auto_save_delay", config.Document.AutoSaveDelay); fixed {
cs.viper.Set("document.auto_save_delay", fixedValue)
// 验证保存选项配置
if fixedValue, fixed := cs.validateAndFixValue("editing.auto_save_delay", config.Editing.AutoSaveDelay); fixed {
cs.viper.Set("editing.auto_save_delay", fixedValue)
hasChanges = true
}
if fixedValue, fixed := cs.validateAndFixValue("document.change_threshold", config.Document.ChangeThreshold); fixed {
cs.viper.Set("document.change_threshold", fixedValue)
if fixedValue, fixed := cs.validateAndFixValue("editing.change_threshold", config.Editing.ChangeThreshold); fixed {
cs.viper.Set("editing.change_threshold", fixedValue)
hasChanges = true
}
if fixedValue, fixed := cs.validateAndFixValue("document.min_save_interval", config.Document.MinSaveInterval); fixed {
cs.viper.Set("document.min_save_interval", fixedValue)
if fixedValue, fixed := cs.validateAndFixValue("editing.min_save_interval", config.Editing.MinSaveInterval); fixed {
cs.viper.Set("editing.min_save_interval", fixedValue)
hasChanges = true
}
@@ -280,24 +280,24 @@ func NewConfigService(logger *log.LoggerService) *ConfigService {
func setDefaults(v *viper.Viper) {
defaultConfig := models.NewDefaultAppConfig()
// 编辑器配置默认值
v.SetDefault("editor.font_size", defaultConfig.Editor.FontSize)
v.SetDefault("editor.font_family", defaultConfig.Editor.FontFamily)
v.SetDefault("editor.font_weight", defaultConfig.Editor.FontWeight)
v.SetDefault("editor.line_height", defaultConfig.Editor.LineHeight)
v.SetDefault("editor.enable_tab_indent", defaultConfig.Editor.EnableTabIndent)
v.SetDefault("editor.tab_size", defaultConfig.Editor.TabSize)
v.SetDefault("editor.tab_type", defaultConfig.Editor.TabType)
v.SetDefault("editor.language", defaultConfig.Editor.Language)
v.SetDefault("editor.always_on_top", defaultConfig.Editor.AlwaysOnTop)
// 通用设置默认值
v.SetDefault("general.always_on_top", defaultConfig.General.AlwaysOnTop)
v.SetDefault("general.data_path", defaultConfig.General.DataPath)
// 文档配置默认值
v.SetDefault("document.auto_save_delay", defaultConfig.Document.AutoSaveDelay)
v.SetDefault("document.change_threshold", defaultConfig.Document.ChangeThreshold)
v.SetDefault("document.min_save_interval", defaultConfig.Document.MinSaveInterval)
// 编辑设置默认值
v.SetDefault("editing.font_size", defaultConfig.Editing.FontSize)
v.SetDefault("editing.font_family", defaultConfig.Editing.FontFamily)
v.SetDefault("editing.font_weight", defaultConfig.Editing.FontWeight)
v.SetDefault("editing.line_height", defaultConfig.Editing.LineHeight)
v.SetDefault("editing.enable_tab_indent", defaultConfig.Editing.EnableTabIndent)
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("paths.data_path", defaultConfig.Paths.DataPath)
// 外观设置默认值
v.SetDefault("appearance.language", defaultConfig.Appearance.Language)
// 元数据默认值
v.SetDefault("metadata.version", defaultConfig.Metadata.Version)

View File

@@ -147,9 +147,9 @@ func (ds *DocumentService) scheduleAutoSave() {
// 打印保存设置,便于调试
ds.logger.Debug("Document: Auto save settings",
"autoSaveDelay", config.Document.AutoSaveDelay,
"changeThreshold", config.Document.ChangeThreshold,
"minSaveInterval", config.Document.MinSaveInterval)
"autoSaveDelay", config.Editing.AutoSaveDelay,
"changeThreshold", config.Editing.ChangeThreshold,
"minSaveInterval", config.Editing.MinSaveInterval)
ds.lock.Lock()
defer ds.lock.Unlock()
@@ -160,7 +160,7 @@ func (ds *DocumentService) scheduleAutoSave() {
}
// 创建新的自动保存定时器
autoSaveDelay := config.Document.AutoSaveDelay
autoSaveDelay := config.Editing.AutoSaveDelay
ds.logger.Debug("Document: Scheduling auto save", "delay", autoSaveDelay)
ds.scheduleTimerWithDelay(autoSaveDelay)
}
@@ -197,7 +197,7 @@ func (ds *DocumentService) saveToStore(trigger SaveTrigger) {
// 如果成功获取了配置,使用配置值
if err == nil && config != nil {
minInterval = config.Document.MinSaveInterval
minInterval = config.Editing.MinSaveInterval
}
// 如果是自动保存,检查最小保存间隔
@@ -305,7 +305,7 @@ func (ds *DocumentService) ensureDocumentsDir() error {
}
// 创建文档目录
docsDir := filepath.Join(config.Paths.DataPath, "docs")
docsDir := filepath.Join(config.General.DataPath, "docs")
err = os.MkdirAll(docsDir, 0755)
if err != nil {
return err
@@ -320,7 +320,7 @@ func (ds *DocumentService) getDocumentsDir() (string, error) {
if err != nil {
return "", err
}
return filepath.Join(config.Paths.DataPath, "docs"), nil
return filepath.Join(config.General.DataPath, "docs"), nil
}
// getDefaultDocumentPath 获取默认文档路径
@@ -405,7 +405,7 @@ func (ds *DocumentService) UpdateActiveDocumentContent(content string) error {
// 如果成功获取了配置,使用配置值
if err == nil && config != nil {
threshold = config.Document.ChangeThreshold
threshold = config.Editing.ChangeThreshold
}
ds.lock.Lock()
@@ -520,26 +520,26 @@ func (ds *DocumentService) GetDiffInfo(oldText, newText string) DiffResult {
}
// GetSaveSettings 获取文档保存设置
func (ds *DocumentService) GetSaveSettings() (*models.DocumentConfig, error) {
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.Document, nil
return &config.Editing, nil
}
// UpdateSaveSettings 更新文档保存设置
func (ds *DocumentService) UpdateSaveSettings(docConfig models.DocumentConfig) error {
func (ds *DocumentService) UpdateSaveSettings(docConfig models.EditingConfig) error {
// 使用配置服务的 Set 方法更新文档配置
if err := ds.configService.Set("document.auto_save_delay", docConfig.AutoSaveDelay); err != nil {
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("document.change_threshold", docConfig.ChangeThreshold); err != nil {
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("document.min_save_interval", docConfig.MinSaveInterval); err != nil {
if err := ds.configService.Set("editing.min_save_interval", docConfig.MinSaveInterval); err != nil {
return &DocumentError{Operation: "update_save_settings_min_save_interval", Err: err}
}