Files
AFFiNE-Mirror/blocksuite/framework/global/src/utils/xywh.ts
T
2024-12-20 15:38:06 +08:00

30 lines
665 B
TypeScript

/**
* XYWH represents the x, y, width, and height of an element or block.
*/
export type XYWH = [number, number, number, number];
/**
* SerializedXYWH is a string that represents the x, y, width, and height of a block.
*/
export type SerializedXYWH = `[${number},${number},${number},${number}]`;
export function serializeXYWH(
x: number,
y: number,
w: number,
h: number
): SerializedXYWH {
return `[${x},${y},${w},${h}]`;
}
export function deserializeXYWH(xywh: string): XYWH {
try {
return JSON.parse(xywh) as XYWH;
} catch (e) {
console.error('Failed to deserialize xywh', xywh);
console.error(e);
return [0, 0, 0, 0];
}
}