merchi_product_editor
Version:
A React component for editing product images using Fabric.js
183 lines (182 loc) • 9.37 kB
JavaScript
;
/**
* If your application has a server component or API, you can implement a more robust
* solution for handling PSD files by converting them on the server.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertPsdWithThirdPartyService = exports.convertPsdOnServer = void 0;
/**
* Example function that could be used to convert a PSD file to PNG using a server-side API
* Note: This is a placeholder that should be implemented based on your actual backend
*/
var convertPsdOnServer = function (options) { return __awaiter(void 0, void 0, void 0, function () {
var psdUrl, _a, format, width, height, _b, quality, apiKey, apiEndpoint, queryParams, headers, response, contentType, data, blob, error_1;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
psdUrl = options.psdUrl, _a = options.format, format = _a === void 0 ? 'png' : _a, width = options.width, height = options.height, _b = options.quality, quality = _b === void 0 ? 90 : _b, apiKey = options.apiKey;
apiEndpoint = process.env.REACT_APP_PSD_CONVERSION_API || '/api/convert-psd';
_c.label = 1;
case 1:
_c.trys.push([1, 7, , 8]);
queryParams = new URLSearchParams();
queryParams.append('url', encodeURIComponent(psdUrl));
queryParams.append('format', format);
if (width)
queryParams.append('width', width.toString());
if (height)
queryParams.append('height', height.toString());
queryParams.append('quality', quality.toString());
headers = {
'Content-Type': 'application/json',
};
if (apiKey) {
headers['Authorization'] = "Bearer ".concat(apiKey);
}
return [4 /*yield*/, fetch("".concat(apiEndpoint, "?").concat(queryParams.toString()), {
method: 'GET',
headers: headers,
})];
case 2:
response = _c.sent();
if (!response.ok) {
throw new Error("Server PSD conversion failed: ".concat(response.status, " ").concat(response.statusText));
}
contentType = response.headers.get('content-type');
if (!(contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/json'))) return [3 /*break*/, 4];
return [4 /*yield*/, response.json()];
case 3:
data = _c.sent();
return [2 /*return*/, data.url || data.imageUrl];
case 4:
if (!(contentType === null || contentType === void 0 ? void 0 : contentType.includes('image/'))) return [3 /*break*/, 6];
return [4 /*yield*/, response.blob()];
case 5:
blob = _c.sent();
return [2 /*return*/, URL.createObjectURL(blob)];
case 6: throw new Error('Unexpected response format from PSD conversion API');
case 7:
error_1 = _c.sent();
console.error('Error converting PSD on server:', error_1);
throw error_1;
case 8: return [2 /*return*/];
}
});
}); };
exports.convertPsdOnServer = convertPsdOnServer;
/**
* Implementation using a third-party API service for PSD conversion
* Note: This is a placeholder that would need to be configured with your own API key
*/
var convertPsdWithThirdPartyService = function (options) { return __awaiter(void 0, void 0, void 0, function () {
var psdUrl, _a, format, width, height, _b, quality, apiKey, serviceUrl, response, data, error_2;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
psdUrl = options.psdUrl, _a = options.format, format = _a === void 0 ? 'png' : _a, width = options.width, height = options.height, _b = options.quality, quality = _b === void 0 ? 90 : _b, apiKey = options.apiKey;
serviceUrl = 'https://api.example-psd-service.com/convert';
if (!apiKey) {
throw new Error('API key is required for third-party PSD conversion');
}
_c.label = 1;
case 1:
_c.trys.push([1, 4, , 5]);
return [4 /*yield*/, fetch(serviceUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer ".concat(apiKey),
},
body: JSON.stringify({
url: psdUrl,
outputFormat: format,
width: width,
height: height,
quality: quality,
}),
})];
case 2:
response = _c.sent();
if (!response.ok) {
throw new Error("PSD conversion service error: ".concat(response.status, " ").concat(response.statusText));
}
return [4 /*yield*/, response.json()];
case 3:
data = _c.sent();
return [2 /*return*/, data.resultUrl];
case 4:
error_2 = _c.sent();
console.error('Error with third-party PSD conversion:', error_2);
throw error_2;
case 5: return [2 /*return*/];
}
});
}); };
exports.convertPsdWithThirdPartyService = convertPsdWithThirdPartyService;
/**
* Configuration instructions for server-side PSD support:
*
* 1. Create a server endpoint that accepts a PSD URL and returns a PNG/JPEG
* - You can use libraries like Sharp or ImageMagick on Node.js
* - For AWS Lambda, consider using a Lambda function with Sharp
* - For other platforms, most have image processing capabilities
*
* 2. Set up environment variables:
* - REACT_APP_PSD_CONVERSION_API: URL of your conversion API
* - REACT_APP_PSD_API_KEY: API key if required
*
* 3. Add these services to your loadTemplateImage function:
*
* if (isPsd) {
* // Try server-side conversion first
* try {
* const convertedUrl = await convertPsdOnServer({
* psdUrl: imageUrl,
* width,
* height,
* apiKey: process.env.REACT_APP_PSD_API_KEY
* });
*
* // Then load the converted image URL
* fabric.Image.fromURL(convertedUrl, ...);
* } catch (error) {
* // Fall back to client-side processing or placeholders
* }
* }
*/