(file: TFile)
| 169 | } |
| 170 | |
| 171 | async loadFile(file: TFile) { |
| 172 | try { |
| 173 | const mkitString = await this.plugin.app.vault.read(file); |
| 174 | this.spaceKit = safelyParseJSON(mkitString) as SpaceKit; |
| 175 | |
| 176 | // Destroy existing root before creating new one |
| 177 | if (this.root) { |
| 178 | try { |
| 179 | this.root.unmount(); |
| 180 | } catch (e) { |
| 181 | console.error("Error unmounting previous root:", e); |
| 182 | } |
| 183 | this.root = null; |
| 184 | } |
| 185 | |
| 186 | // Clear content |
| 187 | this.contentEl.empty(); |
| 188 | |
| 189 | // Create a container div for React |
| 190 | const container = this.contentEl.createDiv("mk-mkit-root"); |
| 191 | |
| 192 | // Check if fullWidth is set and apply CSS variable |
| 193 | if (this.spaceKit?.definition?.fullWidth) { |
| 194 | this.contentEl.style.setProperty("--page-width", "100%"); |
| 195 | } |
| 196 | |
| 197 | // Use the plugin's UI createRoot method if available, otherwise use React's createRoot |
| 198 | if (this.plugin.ui?.createRoot) { |
| 199 | this.root = this.plugin.ui.createRoot(container); |
| 200 | } else { |
| 201 | // Fallback to standard React createRoot |
| 202 | this.root = createRoot(container); |
| 203 | } |
| 204 | |
| 205 | if (this.root) { |
| 206 | this.root.render( |
| 207 | <MKitViewerComponent |
| 208 | plugin={this.plugin} |
| 209 | spaceKit={this.spaceKit} |
| 210 | filePath={file.path} |
| 211 | onInstall={() => { |
| 212 | // Refresh the view after installation |
| 213 | this.loadFile(file); |
| 214 | }} |
| 215 | /> |
| 216 | ); |
| 217 | } |
| 218 | } catch (error) { |
| 219 | console.error("Failed to load MKit file:", error); |
| 220 | this.contentEl.empty(); |
| 221 | this.contentEl.createEl("div", { |
| 222 | cls: "mk-mkit-error", |
| 223 | text: `Failed to load MKit file: ${error.message}`, |
| 224 | }); |
| 225 | } |
| 226 | } |
| 227 | } |
no test coverage detected