* 创建单个规则项 * @param rule 规则对象 * @param index 规则索引
(rule: RegexRule, index: number)
| 74 | * @param index 规则索引 |
| 75 | */ |
| 76 | private createRuleItem(rule: RegexRule, index: number) { |
| 77 | const ruleContainer = this.rulesContainer.createDiv({ cls: 'regex-rule-item' }); |
| 78 | |
| 79 | // 名称输入框 |
| 80 | const nameInput = new TextComponent(ruleContainer); |
| 81 | nameInput.setPlaceholder(t('Rule name')); |
| 82 | nameInput.setValue(rule.name); |
| 83 | nameInput.onChange(value => { |
| 84 | rule.name = value; |
| 85 | void this.saveRules(); |
| 86 | }); |
| 87 | |
| 88 | // 正则表达式输入框 |
| 89 | const patternInput = new TextComponent(ruleContainer); |
| 90 | patternInput.setPlaceholder(t('Regular expression with capture groups')); |
| 91 | patternInput.setValue(rule.pattern); |
| 92 | patternInput.onChange(value => { |
| 93 | rule.pattern = value; |
| 94 | void this.saveRules(); |
| 95 | }); |
| 96 | |
| 97 | // 颜色文本输入框 |
| 98 | const colorContainer = ruleContainer.createDiv(); |
| 99 | const colorInput = new TextComponent(colorContainer); |
| 100 | colorInput.setPlaceholder('#ffeb3b'); |
| 101 | colorInput.setValue(rule.color); |
| 102 | colorInput.inputEl.addClass('color-input'); // 使用CSS类替代内联样式 |
| 103 | colorInput.onChange(value => { |
| 104 | // 确保颜色值有效 |
| 105 | const colorValue = value.trim(); |
| 106 | if (colorValue && (colorValue.startsWith('#') || colorValue.startsWith('rgb') || colorValue.startsWith('rgba'))) { |
| 107 | rule.color = colorValue; |
| 108 | void this.saveRules(); |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | // 删除图标 |
| 113 | const deleteContainer = ruleContainer.createDiv({ cls: 'regex-rule-delete' }); |
| 114 | setIcon(deleteContainer, 'trash-2'); // 使用 Obsidian 的 trash-2 图标 |
| 115 | deleteContainer.setAttr('aria-label', t('Delete rule')); |
| 116 | deleteContainer.addEventListener('click', () => { |
| 117 | this.rules.splice(index, 1); |
| 118 | void this.saveRules(); |
| 119 | this.display(); // 重新渲染整个列表 |
| 120 | }); |
| 121 | |
| 122 | // 启用/禁用开关 - 直接添加到规则容器中,不使用额外的div |
| 123 | const toggle = new ToggleComponent(ruleContainer); |
| 124 | toggle.setValue(rule.enabled); |
| 125 | toggle.onChange(value => { |
| 126 | rule.enabled = value; |
| 127 | void this.saveRules(); |
| 128 | }); |
| 129 | // 为开关添加类名,便于CSS选择器定位 |
| 130 | toggle.toggleEl.addClass('regex-rule-toggle'); |
| 131 | } |
| 132 | |
| 133 | /** |