|
| 1 | +import { Controller } from "@hotwired/stimulus" |
| 2 | +import { EditorView, basicSetup } from "codemirror" |
| 3 | +import { EditorState } from "@codemirror/state" |
| 4 | +import { php } from "@codemirror/lang-php" |
| 5 | +import { json } from "@codemirror/lang-json" |
| 6 | +import { xml } from "@codemirror/lang-xml" |
| 7 | +import { flowThemeExtension } from "../codemirror/themes/theme-flow.js" |
| 8 | + |
| 9 | +export default class extends Controller { |
| 10 | + static outlets = ["wasm", "code-editor"] |
| 11 | + static targets = ["tabBar", "codeTab", "previewTab", "previewTabName", "codePanel", "previewPanel", "downloadBtn"] |
| 12 | + static values = { |
| 13 | + activeTab: { type: String, default: 'code' }, |
| 14 | + previewFile: { type: String, default: '' } |
| 15 | + } |
| 16 | + |
| 17 | + #previewEditor = null |
| 18 | + #debug = false |
| 19 | + |
| 20 | + connect() { |
| 21 | + this.#debug = this.application.debug |
| 22 | + this.#log('Connecting tabs controller') |
| 23 | + } |
| 24 | + |
| 25 | + disconnect() { |
| 26 | + if (this.#previewEditor) { |
| 27 | + this.#previewEditor.destroy() |
| 28 | + this.#previewEditor = null |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + switchToCode(event) { |
| 33 | + if (event) { |
| 34 | + event.preventDefault() |
| 35 | + event.stopPropagation() |
| 36 | + } |
| 37 | + |
| 38 | + this.activeTabValue = 'code' |
| 39 | + this.#updateTabUI() |
| 40 | + } |
| 41 | + |
| 42 | + switchToPreview(event) { |
| 43 | + if (event) { |
| 44 | + event.preventDefault() |
| 45 | + event.stopPropagation() |
| 46 | + } |
| 47 | + |
| 48 | + if (!this.previewFileValue) { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + this.activeTabValue = 'preview' |
| 53 | + this.#updateTabUI() |
| 54 | + } |
| 55 | + |
| 56 | + async openFile(event) { |
| 57 | + const filePath = event.currentTarget.dataset.filePath |
| 58 | + if (!filePath) { |
| 59 | + this.#log('No file path found') |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + if (!this.hasWasmOutlet) { |
| 64 | + this.#log('WASM outlet not found') |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + this.#log('Opening file:', filePath) |
| 69 | + |
| 70 | + try { |
| 71 | + const fullPath = `/workspace${filePath}` |
| 72 | + const result = await this.wasmOutlet.readFile(fullPath) |
| 73 | + |
| 74 | + if (!result.success || result.content === null || result.content === undefined) { |
| 75 | + this.#log('Failed to read file:', filePath) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + const content = typeof result.content === 'string' |
| 80 | + ? result.content |
| 81 | + : new TextDecoder().decode(result.content) |
| 82 | + |
| 83 | + this.previewFileValue = filePath |
| 84 | + this.#setPreviewContent(content, filePath) |
| 85 | + this.activeTabValue = 'preview' |
| 86 | + this.#updateTabUI() |
| 87 | + |
| 88 | + this.#log('File opened:', filePath) |
| 89 | + } catch (error) { |
| 90 | + this.#log('Error opening file:', error) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + closePreview(event) { |
| 95 | + if (event) { |
| 96 | + event.preventDefault() |
| 97 | + event.stopPropagation() |
| 98 | + } |
| 99 | + |
| 100 | + this.previewFileValue = '' |
| 101 | + this.activeTabValue = 'code' |
| 102 | + this.#updateTabUI() |
| 103 | + } |
| 104 | + |
| 105 | + downloadPreviewFile(event) { |
| 106 | + if (event) { |
| 107 | + event.preventDefault() |
| 108 | + } |
| 109 | + |
| 110 | + if (!this.previewFileValue || !this.#previewEditor) { |
| 111 | + this.#log('No file to download') |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + const content = this.#previewEditor.state.doc.toString() |
| 117 | + const fileName = this.previewFileValue.split('/').pop() |
| 118 | + |
| 119 | + const blob = new Blob([content], { type: 'text/plain' }) |
| 120 | + const url = URL.createObjectURL(blob) |
| 121 | + const a = document.createElement('a') |
| 122 | + a.href = url |
| 123 | + a.download = fileName |
| 124 | + document.body.appendChild(a) |
| 125 | + a.click() |
| 126 | + document.body.removeChild(a) |
| 127 | + URL.revokeObjectURL(url) |
| 128 | + |
| 129 | + this.#log('Downloaded:', fileName) |
| 130 | + } catch (error) { |
| 131 | + this.#log('Error downloading file:', error) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + #setPreviewContent(content, filePath) { |
| 136 | + if (!this.hasPreviewPanelTarget) { |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + const extension = filePath.split('.').pop().toLowerCase() |
| 141 | + const languageExtension = this.#getLanguageExtension(extension) |
| 142 | + |
| 143 | + if (this.#previewEditor) { |
| 144 | + this.#previewEditor.destroy() |
| 145 | + this.#previewEditor = null |
| 146 | + this.previewPanelTarget.innerHTML = '' |
| 147 | + } |
| 148 | + |
| 149 | + this.#createPreviewEditor(content, languageExtension) |
| 150 | + |
| 151 | + if (this.hasPreviewTabNameTarget) { |
| 152 | + this.previewTabNameTarget.textContent = filePath.split('/').pop() |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + #createPreviewEditor(content, languageExtension) { |
| 157 | + const extensions = [ |
| 158 | + basicSetup, |
| 159 | + languageExtension || [], |
| 160 | + flowThemeExtension, |
| 161 | + EditorState.readOnly.of(true), |
| 162 | + EditorView.editable.of(false) |
| 163 | + ].flat() |
| 164 | + |
| 165 | + this.#previewEditor = new EditorView({ |
| 166 | + state: EditorState.create({ |
| 167 | + doc: content, |
| 168 | + extensions: extensions |
| 169 | + }), |
| 170 | + parent: this.previewPanelTarget |
| 171 | + }) |
| 172 | + } |
| 173 | + |
| 174 | + #getLanguageExtension(extension) { |
| 175 | + const languageMap = { |
| 176 | + 'php': php(), |
| 177 | + 'phar': php(), |
| 178 | + 'json': json(), |
| 179 | + 'xml': xml() |
| 180 | + } |
| 181 | + |
| 182 | + return languageMap[extension] || null |
| 183 | + } |
| 184 | + |
| 185 | + #updateTabUI() { |
| 186 | + const isCode = this.activeTabValue === 'code' |
| 187 | + const hasPreview = !!this.previewFileValue |
| 188 | + |
| 189 | + if (this.hasCodeTabTarget) { |
| 190 | + this.codeTabTarget.classList.toggle('active', isCode) |
| 191 | + } |
| 192 | + |
| 193 | + if (this.hasPreviewTabTarget) { |
| 194 | + this.previewTabTarget.classList.toggle('active', !isCode && hasPreview) |
| 195 | + this.previewTabTarget.style.display = hasPreview ? 'flex' : 'none' |
| 196 | + } |
| 197 | + |
| 198 | + if (this.hasDownloadBtnTarget) { |
| 199 | + this.downloadBtnTarget.style.display = hasPreview && !isCode ? 'block' : 'none' |
| 200 | + } |
| 201 | + |
| 202 | + if (this.hasCodePanelTarget) { |
| 203 | + this.codePanelTarget.style.display = isCode ? 'block' : 'none' |
| 204 | + } |
| 205 | + |
| 206 | + if (this.hasPreviewPanelTarget) { |
| 207 | + this.previewPanelTarget.style.display = isCode ? 'none' : 'block' |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + #log(...args) { |
| 212 | + if (this.#debug) { |
| 213 | + console.log('[PlaygroundTabs]', ...args) |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments