import { PluginCommAPI, PluginNoteAPI } from 'sn-plugin-lib';
type Rect = { left: number; top: number; right: number; bottom: number };
/**
* Get the current lasso rectangle.
*/
export async function fetchLassoRect(): Promise<Rect> {
const res = await PluginCommAPI.getLassoRect();
if (!res?.success || !res.result) {
throw new Error(res?.error?.message ?? 'Failed to get lasso rectangle');
}
return res.result as Rect;
}
/**
* Create a new Rect by scaling around the center point.
*/
export function scaleRectKeepAspect(rect: Rect, scale: number): Rect {
const width = rect.right - rect.left;
const height = rect.bottom - rect.top;
const cx = rect.left + width / 2;
const cy = rect.top + height / 2;
const newWidth = width * scale;
const newHeight = height * scale;
return {
left: cx - newWidth / 2,
top: cy - newHeight / 2,
right: cx + newWidth / 2,
bottom: cy + newHeight / 2,
};
}
/**
* Scale the lasso rectangle proportionally and submit the resize.
*/
export async function resizeLassoRectByScale(scale: number): Promise<boolean> {
const rect = await fetchLassoRect();
const nextRect = scaleRectKeepAspect(rect, scale);
const res = await PluginCommAPI.resizeLassoRect(nextRect);
return !!res?.success && !!res.result;
}
/**
* Fetch lasso elements and dispatch by type (example).
*/
export async function fetchLassoElementsAndDispatch(): Promise<void> {
const res = (await PluginCommAPI.getLassoElements()) as any;
if (!res?.success || !Array.isArray(res.result)) {
throw new Error(res?.error?.message ?? 'Failed to get lasso elements');
}
const elements = res.result as any[];
for (const el of elements) {
if (el.type === 100) {
await PluginNoteAPI.modifyLassoTitle({ style: 1 });
} else if (el.type === 500 || el.type === 501 || el.type === 502) {
const textBox = el.textBox;
if (textBox) {
await PluginNoteAPI.modifyLassoText({ ...textBox, textContentFull: 'Updated by plugin' });
}
} else if (el.type === 700) {
const geometry = el.geometry;
if (geometry) {
await PluginCommAPI.modifyLassoGeometry(geometry);
}
} else if (el.type === 800) {
const fiveStar = el.fiveStar;
if (fiveStar?.points) {
void fiveStar.points;
}
}
}
}
/**
* Control lasso box visibility.
* - 0: show
* - 1: hide
* - 2: remove completely
* - 3: fully hide (including lasso elements)
*/
export async function setLassoBoxState(state: 0 | 1 | 2 | 3): Promise<boolean> {
const res = await PluginCommAPI.setLassoBoxState(state);
return !!res?.success && !!res.result;
}