refactor: update ComposeCanvas and Editor data handling types

This commit is contained in:
ramvignesh-b
2026-04-14 13:10:18 +05:30
parent bc29306fe2
commit 8f9c0b5430
4 changed files with 41 additions and 12 deletions
+1 -2
View File
@@ -27,7 +27,6 @@ export interface FabricImageJSON extends FabricObjectJSON {
} }
export interface CanvasJSON { export interface CanvasJSON {
version: string;
objects: (FabricObjectJSON | FabricImageJSON)[]; objects: (FabricObjectJSON | FabricImageJSON)[];
canvasWidth?: number; canvasWidth?: number;
canvasHeight?: number; canvasHeight?: number;
@@ -266,7 +265,7 @@ export const ComposeCanvas = forwardRef<
}); });
}, },
getData: () => { getData: () => {
if (!fabricRef.current) return { version: "", objects: [] }; if (!fabricRef.current) return { objects: [] };
const json = fabricRef.current.toJSON() as CanvasJSON; const json = fabricRef.current.toJSON() as CanvasJSON;
json.canvasWidth = BASE_WIDTH; json.canvasWidth = BASE_WIDTH;
json.canvasHeight = json.canvasHeight =
+1 -1
View File
@@ -109,7 +109,7 @@ export default function Editor() {
await cryptoUtils.initialize(); await cryptoUtils.initialize();
try { try {
const canvasData = canvasRef.current?.getData(); const canvasData = canvasRef.current?.getData() || { objects: [] };
const canvasImages = canvasRef.current?.getImages() || []; const canvasImages = canvasRef.current?.getImages() || [];
const encImageFilesMap = await encryptCanvasImages( const encImageFilesMap = await encryptCanvasImages(
+3 -3
View File
@@ -213,7 +213,7 @@ export class CryptoUtils {
} }
public async encryptMetadata( public async encryptMetadata(
metadata: Record<string, unknown>, metadata: Record<string, any>,
masterKey: CryptoKey, masterKey: CryptoKey,
): Promise<EncryptedLetter> { ): Promise<EncryptedLetter> {
const { encryptedContent, encrypted_dek, sharingKey } = const { encryptedContent, encrypted_dek, sharingKey } =
@@ -227,7 +227,7 @@ export class CryptoUtils {
public async decryptMetadata( public async decryptMetadata(
encrypted_metadata: EncryptedLetter, encrypted_metadata: EncryptedLetter,
masterKey: CryptoKey, masterKey: CryptoKey,
): Promise<Record<string, unknown>> { ): Promise<Record<string, any>> {
const bytes = await this.openEnvelope( const bytes = await this.openEnvelope(
encrypted_metadata.encrypted_content, encrypted_metadata.encrypted_content,
encrypted_metadata.encrypted_dek, encrypted_metadata.encrypted_dek,
@@ -239,7 +239,7 @@ export class CryptoUtils {
public async decryptMetadataWithSharingKey( public async decryptMetadataWithSharingKey(
encrypted_content: string, encrypted_content: string,
sharingKey: string, sharingKey: string,
): Promise<Record<string, unknown>> { ): Promise<Record<string, any>> {
const bytes = await this.openEnvelopeWithSharingKey( const bytes = await this.openEnvelopeWithSharingKey(
encrypted_content, encrypted_content,
sharingKey, sharingKey,
+36 -6
View File
@@ -36,8 +36,22 @@ describe("letterLogic image helpers", () => {
it("should not encrypt images whose src already ends with .bin", async () => { it("should not encrypt images whose src already ends with .bin", async () => {
const canvasData = { const canvasData = {
objects: [ objects: [
{ type: "Image", src: "already-encrypted.png.bin" }, {
{ type: "Textbox", text: "hello" }, type: "Image",
src: "already-encrypted.png.bin",
top: 0,
left: 0,
width: 100,
height: 100,
},
{
type: "Textbox",
text: "hello",
top: 0,
left: 0,
width: 100,
height: 100,
},
], ],
}; };
@@ -58,7 +72,16 @@ describe("letterLogic image helpers", () => {
it("should encrypt new blob-backed images and return encrypted uploads", async () => { it("should encrypt new blob-backed images and return encrypted uploads", async () => {
const file = new File(["img"], "photo.png", { type: "image/png" }); const file = new File(["img"], "photo.png", { type: "image/png" });
const canvasData = { const canvasData = {
objects: [{ type: "Image", src: "blob:http://localhost/test-image" }], objects: [
{
type: "Image",
src: "blob:http://localhost/test-image",
top: 0,
left: 0,
width: 100,
height: 100,
},
],
}; };
const canvasImages = [ const canvasImages = [
{ {
@@ -92,7 +115,6 @@ describe("letterLogic image helpers", () => {
describe("decryptCanvasImages", () => { describe("decryptCanvasImages", () => {
it("should decrypt images and replace src with blob URL", async () => { it("should decrypt images and replace src with blob URL", async () => {
const canvasData = { const canvasData = {
version: "5.3.0",
objects: [ objects: [
{ {
type: "Image", type: "Image",
@@ -143,7 +165,6 @@ describe("letterLogic image helpers", () => {
it("should include raw file when includeRawFile is true", async () => { it("should include raw file when includeRawFile is true", async () => {
const canvasData = { const canvasData = {
version: "5.3.0",
objects: [ objects: [
{ {
type: "Image", type: "Image",
@@ -188,7 +209,16 @@ describe("letterLogic image helpers", () => {
describe("decryptCanvasImagesWithSharingKey", () => { describe("decryptCanvasImagesWithSharingKey", () => {
it("should decrypt images using sharing key", async () => { it("should decrypt images using sharing key", async () => {
const canvasData = { const canvasData = {
objects: [{ type: "Image", src: "photo.png.bin" }], objects: [
{
type: "Image",
src: "photo.png.bin",
top: 0,
left: 0,
width: 100,
height: 100,
},
],
}; };
const remoteImages = [ const remoteImages = [
{ file_name: "photo.png.bin", file: "https://remote/photo.png.bin" }, { file_name: "photo.png.bin", file: "https://remote/photo.png.bin" },