Refactor utility functions and IndexedDB handling

Extracted and centralized common utility functions such as createCanvas, normalizeToUint8, and generateUniqueFileName in CommonUtils.js to reduce code duplication. Added createImageFromSource to ImageUtils.js. Refactored db.js to use a helper for IndexedDB requests, simplifying error handling and reducing boilerplate. Updated CanvasIO.js, CanvasLayers.js, and Canvas_view.js to use the new utilities.
This commit is contained in:
Dariusz L
2025-06-26 01:58:18 +02:00
parent 7fe34e940e
commit df2680f2c9
6 changed files with 144 additions and 176 deletions

View File

@@ -1,5 +1,7 @@
import {saveImage, getImage, removeImage} from "./db.js";
import {logger, LogLevel} from "./logger.js";
import {createCanvas, normalizeToUint8} from "./CommonUtils.js";
import {createImageFromSource} from "./ImageUtils.js";
// Inicjalizacja loggera dla modułu CanvasIO
const log = {
@@ -70,15 +72,8 @@ export class CanvasIO {
}
return new Promise((resolve) => {
const tempCanvas = document.createElement('canvas');
const maskCanvas = document.createElement('canvas');
tempCanvas.width = this.canvas.width;
tempCanvas.height = this.canvas.height;
maskCanvas.width = this.canvas.width;
maskCanvas.height = this.canvas.height;
const tempCtx = tempCanvas.getContext('2d');
const maskCtx = maskCanvas.getContext('2d');
const { canvas: tempCanvas, ctx: tempCtx } = createCanvas(this.canvas.width, this.canvas.height);
const { canvas: maskCanvas, ctx: maskCtx } = createCanvas(this.canvas.width, this.canvas.height);
tempCtx.fillStyle = '#ffffff';
tempCtx.fillRect(0, 0, this.canvas.width, this.canvas.height);
@@ -249,10 +244,7 @@ export class CanvasIO {
try {
log.debug("Adding input to canvas:", {inputImage});
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = inputImage.width;
tempCanvas.height = inputImage.height;
const { canvas: tempCanvas, ctx: tempCtx } = createCanvas(inputImage.width, inputImage.height);
const imgData = new ImageData(
inputImage.data,