> ## Documentation Index
> Fetch the complete documentation index at: https://docs.supernote.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lasso

> Workflow, data model, and common APIs for lasso selection (get/update lasso rectangle and elements).

Lasso is a common selection interaction in NOTE/DOC. After the user draws a region on the page, the system computes the selected element set and shows the lasso toolbar.
Plugins can display their registered buttons in the lasso toolbar. After the user taps a plugin button and enters the plugin UI, the plugin can read and modify data based on the current lasso context.

## Workflow

1. User completes a lasso selection on the page
2. The system shows the lasso toolbar (plugin-registered buttons may appear)
3. User taps a plugin button and enters the plugin UI
4. The plugin reads the lasso rectangle and selected elements, then runs business logic (e.g., modify selected TextBox elements, titles, geometries, stars, etc.)
5. The plugin can show/hide/remove the lasso box as needed

## Data Model

The selected element list can be obtained via [`PluginCommAPI.getLassoElements()`](https://docs.supernote.com/en/api-reference/supernote-plugin/plugin-comm-api/get-lasso-data). The return value is [`Element[]`](https://docs.supernote.com/en/api-reference/supernote-plugin/types/trail).

* Use `Element.type` to distinguish element categories (constants: [`ElementType`](https://docs.supernote.com/en/api-reference/supernote-plugin/types/trail#elementtype))
* For large point datasets (e.g., angle points, contour points), `Element` fields are provided as accessors ([`ElementDataAccessor`](https://docs.supernote.com/en/api-reference/supernote-plugin/types/trail-data-accessor)) to avoid transferring large point sets to RN at once

## Common APIs

| Goal | API | Description |
| - | - | - |
| Get lasso rectangle | [`getLassoRect()`](https://docs.supernote.com/en/api-reference/supernote-plugin/plugin-comm-api/get-lasso-rect) | Returns lasso rectangle (`Rect`) |
| Create lasso selection | [`lassoElements(rect)`](https://docs.supernote.com/en/api-reference/supernote-plugin/plugin-comm-api/lasso-elements) | Creates a lasso selection from a rectangle (Android pixel coordinates) |
| Resize lasso rectangle | [`resizeLassoRect(rect, rotationDegree?)`](https://docs.supernote.com/en/api-reference/supernote-plugin/plugin-comm-api/resize-lasso-rect) | Resizes the lasso rectangle and can optionally rotate supported elements |
| Flip selected elements in place | [`flipLassoElements(flipType)`](https://docs.supernote.com/en/api-reference/supernote-plugin/plugin-comm-api/flip-lasso-elements) | Flips elements in the current lasso selection in place (does not create duplicates). `1` is horizontal and `2` is vertical |
| Get selected elements | [`getLassoElements()`](https://docs.supernote.com/en/api-reference/supernote-plugin/plugin-comm-api/get-lasso-data) | Returns `Element[]` (accessor fields are filled on success) |
| Control lasso box | [`setLassoBoxState(state)`](https://docs.supernote.com/en/api-reference/supernote-plugin/plugin-comm-api/set-lasso-box-state) | `0` show, `1` hide, `2` remove completely, `3` fully hide (including lasso elements) |

> [!TIP]
>
> With `sn-plugin-lib` later than `0.1.65`, both the lasso rectangle and element points (stroke sample points) use Android pixel coordinates, based on the actual page size from [`getPageSize`](https://docs.supernote.com/en/api-reference/supernote-plugin/plugin-file-api/get-page-size). You can compare them directly without conversion. See [Coordinate System](https://docs.supernote.com/en/plugin-base/coordinate-system.md).

## Example

The example below shows how to, within a plugin UI:

* Get the lasso rectangle
* Update the lasso rectangle by proportional scaling
* Get selected elements and filter by type
* Show/hide/remove the lasso box

```ts wrap theme={null}
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;
}
```

## Constraints and Recommendations

* `resizeLassoRect` optionally accepts `rotationDegree`: omit it to keep the current rotation; when provided, only strokes and geometries rotate
* During `resizeLassoRect`, only titles, strokes, and geometries support stretch resizing; all other element types support proportional scaling only
* `duplicateFlipLassoElements` first duplicates supported elements, then flips the newly created elements; the originals remain unchanged
* `duplicateFlipLassoElements` only supports strokes and geometries, and currently supports horizontal and vertical flipping only
* `getLassoElements` may return elements with accessor fields: do not assume point sets are fully in JS; use accessors to fetch data on demand
* `setLassoBoxState(2)` removes the lasso state completely: typically used at the end of an operation; after removal, the lasso toolbar will close on the user side
* `setLassoBoxState(3)` fully hides the lasso box (including lasso elements): typically used for custom toolbars/UI so the plugin can control the presentation


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.