|
14 | 14 | const { ipcRenderer } = require("electron"); |
15 | 15 | const { ImageLayer } = require("./image_layer.js"); |
16 | 16 | const { ImageMode } = require("../features/image_mode.js"); |
| 17 | +const LAYER_EVENT_CHANNEL = "imgkit-layer-event"; |
17 | 18 |
|
18 | 19 | // ============================================================================ |
19 | 20 | // MAIN CLASS: ImgKitRenderer |
@@ -45,6 +46,7 @@ class ImgKitRenderer { |
45 | 46 | this.setupScrollEvents(); |
46 | 47 | this.setupGlobalDragPrevention(); |
47 | 48 | this.setupGlobalMagnifyShortcut(); // Setup Alt + M globally |
| 49 | + this.setupLayerEventBridge(); |
48 | 50 | } |
49 | 51 | } |
50 | 52 |
|
@@ -188,6 +190,139 @@ class ImgKitRenderer { |
188 | 190 | }); |
189 | 191 | } |
190 | 192 |
|
| 193 | + setupLayerEventBridge() { |
| 194 | + ipcRenderer.on(LAYER_EVENT_CHANNEL, async (event, payload) => { |
| 195 | + await this.handleLayerEvent(payload); |
| 196 | + }); |
| 197 | + } |
| 198 | + |
| 199 | + async handleLayerEvent(payload) { |
| 200 | + if (!payload || !payload.type) return; |
| 201 | + |
| 202 | + switch (payload.type) { |
| 203 | + case "select": |
| 204 | + this.selectLayerById(payload.layerId, payload.ctrlKey); |
| 205 | + break; |
| 206 | + case "delete": |
| 207 | + this.selectLayerById(payload.layerId); |
| 208 | + this.deleteImage(); |
| 209 | + break; |
| 210 | + case "copy": |
| 211 | + this.selectLayerById(payload.layerId); |
| 212 | + await this.copyImage(); |
| 213 | + break; |
| 214 | + case "paste": |
| 215 | + this.selectLayerById(payload.layerId); |
| 216 | + await this.pasteImage(); |
| 217 | + break; |
| 218 | + case "swap": |
| 219 | + this.swapLayersByIds(payload.fromLayerId, payload.toLayerId); |
| 220 | + break; |
| 221 | + case "drop": |
| 222 | + await this.handleDropFiles(payload.layerId, payload.files || []); |
| 223 | + break; |
| 224 | + case "preview-updated": |
| 225 | + this.updateScrollUI(); |
| 226 | + this.scrollToEnd(); |
| 227 | + break; |
| 228 | + case "navigate": |
| 229 | + this.navigateKeyboard(payload.direction, payload.ctrlKey); |
| 230 | + break; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + selectLayerById(layerId, ctrlKey = false) { |
| 235 | + const index = this.getLayerIndexById(layerId); |
| 236 | + if (index !== -1) { |
| 237 | + this.setCurrentLayer(index, ctrlKey); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + getLayerIndexById(layerId) { |
| 242 | + return this.imageLayerQueue.findIndex((layer) => layer.id === layerId); |
| 243 | + } |
| 244 | + |
| 245 | + getLayerById(layerId) { |
| 246 | + const index = this.getLayerIndexById(layerId); |
| 247 | + return index === -1 ? null : this.imageLayerQueue[index]; |
| 248 | + } |
| 249 | + |
| 250 | + swapLayersByIds(fromLayerId, toLayerId) { |
| 251 | + const fromIndex = this.getLayerIndexById(fromLayerId); |
| 252 | + const toIndex = this.getLayerIndexById(toLayerId); |
| 253 | + if (fromIndex === -1 || toIndex === -1 || fromIndex === toIndex) return; |
| 254 | + this.swapLayers(fromIndex, toIndex); |
| 255 | + } |
| 256 | + |
| 257 | + async handleDropFiles(targetLayerId, files) { |
| 258 | + if (!files || files.length === 0) return; |
| 259 | + |
| 260 | + let dropHandled = false; |
| 261 | + |
| 262 | + for (let i = 0; i < files.length; i++) { |
| 263 | + const entry = files[i]; |
| 264 | + if (!entry) continue; |
| 265 | + |
| 266 | + const targetLayer = |
| 267 | + i === 0 ? this.getLayerById(targetLayerId) : this.createImageLayer(false); |
| 268 | + if (!targetLayer) continue; |
| 269 | + |
| 270 | + let success = false; |
| 271 | + if (entry.path) { |
| 272 | + success = await targetLayer.openImage(entry.path); |
| 273 | + } else if (entry.buffer) { |
| 274 | + const buffer = Buffer.isBuffer(entry.buffer) |
| 275 | + ? entry.buffer |
| 276 | + : Buffer.from(entry.buffer); |
| 277 | + success = await targetLayer.openImageBuffer(buffer, entry.name); |
| 278 | + } |
| 279 | + |
| 280 | + if (success) { |
| 281 | + targetLayer.panel.draggable = true; |
| 282 | + this.currentIndex = this.imageLayerQueue.indexOf(targetLayer); |
| 283 | + dropHandled = true; |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + if (dropHandled && this.currentIndex === this.imageLayerQueue.length - 1) { |
| 288 | + this.createDefaultImage(); |
| 289 | + } |
| 290 | + } |
| 291 | + |
| 292 | + scrollToEnd() { |
| 293 | + if (this.scrollContainer) { |
| 294 | + this.scrollContainer.scrollLeft = this.scrollContainer.scrollWidth; |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + navigateKeyboard(direction, ctrlKey) { |
| 299 | + if (!direction) return; |
| 300 | + |
| 301 | + let newIndex = this.currentIndex; |
| 302 | + |
| 303 | + if (direction === "left") { |
| 304 | + if (ctrlKey) { |
| 305 | + newIndex = 0; |
| 306 | + } else if (newIndex > 0) { |
| 307 | + newIndex -= 1; |
| 308 | + } |
| 309 | + } else if (direction === "right") { |
| 310 | + if (ctrlKey) { |
| 311 | + newIndex = this.imageLayerQueue.length - 1; |
| 312 | + } else if (newIndex < this.imageLayerQueue.length - 1) { |
| 313 | + newIndex += 1; |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + if (newIndex !== this.currentIndex) { |
| 318 | + this.setCurrentLayer(newIndex); |
| 319 | + const layer = this.imageLayerQueue[newIndex]; |
| 320 | + if (layer && layer.panel) { |
| 321 | + layer.panel.focus(); |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + |
191 | 326 | /** |
192 | 327 | * Update scroll UI state and panel ordering |
193 | 328 | * - Disables scroll buttons at edges |
@@ -249,7 +384,7 @@ class ImgKitRenderer { |
249 | 384 | * @returns {ImageLayer} Created image layer |
250 | 385 | */ |
251 | 386 | createImageLayer(isDefault = false) { |
252 | | - const layer = new ImageLayer(this, isDefault); |
| 387 | + const layer = new ImageLayer(isDefault); |
253 | 388 | this.imageLayerQueue.push(layer); |
254 | 389 | this.scrollContainer.appendChild(layer.panel); |
255 | 390 | this.updateScrollUI(); |
@@ -296,11 +431,7 @@ class ImgKitRenderer { |
296 | 431 | * @returns {ImageLayer} Created default layer |
297 | 432 | */ |
298 | 433 | createDefaultImage() { |
299 | | - if (this.imageLayerQueue.length > 0) { |
300 | | - this.currentIndex++; |
301 | | - } |
302 | 434 | const layer = this.createImageLayer(true); |
303 | | - this.currentIndex = this.imageLayerQueue.length - 1; |
304 | 435 | layer.openImage("./assets/addImage.png"); |
305 | 436 | return layer; |
306 | 437 | } |
|
0 commit comments