import { PluginCommAPI, PluginNoteAPI } from 'sn-plugin-lib';
type Rect = { left: number; top: number; right: number; bottom: number };
/**
* 获取当前套索框矩形。
*/
export async function fetchLassoRect(): Promise<Rect> {
const res = await PluginCommAPI.getLassoRect();
if (!res?.success || !res.result) {
throw new Error(res?.error?.message ?? '获取套索框失败');
}
return res.result as Rect;
}
/**
* 以中心点为基准,按等比例缩放生成新 Rect。
*/
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,
};
}
/**
* 将套索框按等比例缩放并提交调整。
*/
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;
}
/**
* 获取套索元素并示例性地做按类型分流。
*/
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 ?? '获取套索元素失败');
}
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;
}
}
}
}
/**
* 控制套索框显示状态。
* - 0: 显示
* - 1: 隐藏
* - 2: 完全移除
*/
export async function setLassoBoxState(state: 0 | 1 | 2): Promise<boolean> {
const res = await PluginCommAPI.setLassoBoxState(state);
return !!res?.success && !!res.result;
}