|
| 1 | +import { Component } from "obsidian"; |
| 2 | +import TaskProgressBarPlugin from "../index"; |
| 3 | +import { getStatusIcon } from "../icon"; |
| 4 | +import { TaskProgressBarSettings } from "../common/setting-definition"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Manages Task Genius Icons functionality |
| 8 | + * Handles CSS style injection, body class management, and cleanup |
| 9 | + */ |
| 10 | +export class TaskGeniusIconManager extends Component { |
| 11 | + private plugin: TaskProgressBarPlugin; |
| 12 | + private styleElement: HTMLStyleElement | null = null; |
| 13 | + private readonly STYLE_ID = "task-genius-icons-styles"; |
| 14 | + private readonly BODY_CLASS = "task-genius-checkbox"; |
| 15 | + |
| 16 | + constructor(plugin: TaskProgressBarPlugin) { |
| 17 | + super(); |
| 18 | + this.plugin = plugin; |
| 19 | + } |
| 20 | + |
| 21 | + async onload() { |
| 22 | + // Initialize if enabled |
| 23 | + if (this.plugin.settings.enableTaskGeniusIcons) { |
| 24 | + this.enable(); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + onunload() { |
| 29 | + this.disable(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Enable Task Genius Icons functionality |
| 34 | + */ |
| 35 | + enable() { |
| 36 | + try { |
| 37 | + this.addBodyClass(); |
| 38 | + this.injectStyles(); |
| 39 | + } catch (error) { |
| 40 | + console.error("Task Genius: Failed to enable icons:", error); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Disable Task Genius Icons functionality |
| 46 | + */ |
| 47 | + disable() { |
| 48 | + try { |
| 49 | + this.removeBodyClass(); |
| 50 | + this.removeStyles(); |
| 51 | + } catch (error) { |
| 52 | + console.error("Task Genius: Failed to disable icons:", error); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Update functionality when settings change |
| 58 | + */ |
| 59 | + update() { |
| 60 | + if (this.plugin.settings.enableTaskGeniusIcons) { |
| 61 | + this.enable(); |
| 62 | + } else { |
| 63 | + this.disable(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Add task-genius-checkbox class to body |
| 69 | + */ |
| 70 | + private addBodyClass() { |
| 71 | + document.body.classList.add(this.BODY_CLASS); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Remove task-genius-checkbox class from body |
| 76 | + */ |
| 77 | + private removeBodyClass() { |
| 78 | + document.body.classList.remove(this.BODY_CLASS); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Inject CSS styles into head |
| 83 | + */ |
| 84 | + private injectStyles() { |
| 85 | + // Remove existing styles first |
| 86 | + this.removeStyles(); |
| 87 | + |
| 88 | + // Generate CSS content |
| 89 | + const cssContent = this.generateCSS(); |
| 90 | + |
| 91 | + // Create and inject style element |
| 92 | + this.styleElement = document.createElement("style"); |
| 93 | + this.styleElement.id = this.STYLE_ID; |
| 94 | + this.styleElement.textContent = cssContent; |
| 95 | + document.head.appendChild(this.styleElement); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Remove injected CSS styles |
| 100 | + */ |
| 101 | + private removeStyles() { |
| 102 | + if (this.styleElement) { |
| 103 | + this.styleElement.remove(); |
| 104 | + this.styleElement = null; |
| 105 | + } |
| 106 | + |
| 107 | + // Also remove any existing style element with our ID |
| 108 | + const existingStyle = document.getElementById(this.STYLE_ID); |
| 109 | + if (existingStyle) { |
| 110 | + existingStyle.remove(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Generate CSS content based on current settings |
| 116 | + */ |
| 117 | + private generateCSS(): string { |
| 118 | + const settings = this.plugin.settings; |
| 119 | + const statusConfigs = this.parseTaskStatuses(settings); |
| 120 | + |
| 121 | + let css = ""; |
| 122 | + |
| 123 | + for (const config of statusConfigs) { |
| 124 | + const svgIcon = getStatusIcon(config.status); |
| 125 | + const encodedSvg = this.encodeSvgForCSS(svgIcon); |
| 126 | + |
| 127 | + for (const char of config.chars) { |
| 128 | + css += this.generateCSSRuleForChar(char, encodedSvg); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return css; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Parse taskStatuses configuration into structured format |
| 137 | + */ |
| 138 | + private parseTaskStatuses(settings: TaskProgressBarSettings): Array<{ |
| 139 | + status: |
| 140 | + | "notStarted" |
| 141 | + | "inProgress" |
| 142 | + | "completed" |
| 143 | + | "abandoned" |
| 144 | + | "planned"; |
| 145 | + chars: string[]; |
| 146 | + }> { |
| 147 | + const result: Array<{ |
| 148 | + status: |
| 149 | + | "notStarted" |
| 150 | + | "inProgress" |
| 151 | + | "completed" |
| 152 | + | "abandoned" |
| 153 | + | "planned"; |
| 154 | + chars: string[]; |
| 155 | + }> = []; |
| 156 | + |
| 157 | + const statusMap: Record< |
| 158 | + string, |
| 159 | + "notStarted" | "inProgress" | "completed" | "abandoned" | "planned" |
| 160 | + > = { |
| 161 | + notStarted: "notStarted", |
| 162 | + inProgress: "inProgress", |
| 163 | + completed: "completed", |
| 164 | + abandoned: "abandoned", |
| 165 | + planned: "planned", |
| 166 | + }; |
| 167 | + |
| 168 | + for (const [statusKey, charString] of Object.entries( |
| 169 | + settings.taskStatuses |
| 170 | + )) { |
| 171 | + const status = statusMap[statusKey]; |
| 172 | + if (status) { |
| 173 | + const chars = charString.split("|").map((char) => char.trim()); |
| 174 | + result.push({ status, chars }); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return result; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Encode SVG for use in CSS data URI |
| 183 | + */ |
| 184 | + private encodeSvgForCSS(svgString: string): string { |
| 185 | + try { |
| 186 | + // Remove width and height attributes to make it scalable |
| 187 | + const cleanSvg = svgString |
| 188 | + .replace(/width="[^"]*"/g, "") |
| 189 | + .replace(/height="[^"]*"/g, "") |
| 190 | + .replace(/\s+/g, " ") |
| 191 | + .trim(); |
| 192 | + |
| 193 | + // URL encode for data URI |
| 194 | + const encoded = encodeURIComponent(cleanSvg); |
| 195 | + return `data:image/svg+xml,${encoded}`; |
| 196 | + } catch (error) { |
| 197 | + console.error("Task Genius: Failed to encode SVG:", error); |
| 198 | + return ""; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Generate CSS rule for a specific character |
| 204 | + */ |
| 205 | + private generateCSSRuleForChar(char: string, encodedSvg: string): string { |
| 206 | + // Escape special characters for CSS selector |
| 207 | + const escapedChar = this.escapeCSSSelector(char); |
| 208 | + |
| 209 | + return ` |
| 210 | +.${this.BODY_CLASS} [data-task="${escapedChar}"] > input[type=checkbox]:checked:after, |
| 211 | +.${this.BODY_CLASS} [data-task="${escapedChar}"] > p > input[type=checkbox]:checked:after, |
| 212 | +.${this.BODY_CLASS} [data-task="${escapedChar}"][type=checkbox]:checked:after { |
| 213 | + --webkit-mask-image: url("${encodedSvg}"); |
| 214 | + --webkit-mask-size: 20%; |
| 215 | +} |
| 216 | +`; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Escape special characters for CSS selector |
| 221 | + */ |
| 222 | + private escapeCSSSelector(char: string): string { |
| 223 | + // Handle space character specially |
| 224 | + if (char === " ") { |
| 225 | + return " "; |
| 226 | + } |
| 227 | + |
| 228 | + // Escape special CSS characters |
| 229 | + return char.replace(/[!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~]/g, "\\$&"); |
| 230 | + } |
| 231 | +} |
0 commit comments