# UseDraw

What [`useDraw`](https://docs.mapcairn.com/api/map/useDraw-function.md) returns.

This is an interface exported by `@mapcairn/map`, checked against version 0.2.0.

## Signature

```ts
import type { UseDraw } from "@mapcairn/map";
 
interface UseDraw
```

## Properties

### addPoint

```ts
readonly addPoint: (at: LatLng) => void;
```

Adds a point, or starts a shape, at a position.

### cancel

```ts
readonly cancel: () => void;
```

Drops the shape being drawn.

### canRedo

```ts
readonly canRedo: boolean;
```

Whether there is an undone change to reapply.

### canUndo

```ts
readonly canUndo: boolean;
```

Whether there is a change to revert.

### clear

```ts
readonly clear: () => void;
```

Removes every shape.

### copyLink

```ts
readonly copyLink: () => Promise<void>;
```

Copies a link with the shapes in it, built from the current address.

### deleteSelected

```ts
readonly deleteSelected: () => void;
```

Removes the selected shape.

### deleteShape

```ts
readonly deleteShape: (shapeId: string) => void;
```

Removes the shape with this id, whether it is selected or not.

### deselect

```ts
readonly deselect: () => void;
```

Clears the selected shape and vertex.

### discardDraft

```ts
readonly discardDraft: () => void;
```

Removes the shape being drawn without a word, when Done closes the tool.

### draft

```ts
readonly draft: DrawShape | null;
```

The shape being drawn, or null.

### exportCsv

```ts
readonly exportCsv: () => Promise<void>;
```

Saves the shapes as a CSV file through the file saver, and sets `feedback` to say how it went.

### exportGeoJson

```ts
readonly exportGeoJson: () => Promise<void>;
```

Saves the shapes as a GeoJSON file through the file saver, and sets `feedback` to say how it went.

### exportWkt

```ts
readonly exportWkt: () => Promise<void>;
```

Saves the shapes as a WKT file through the file saver, and sets `feedback` to say how it went.

### feedback

```ts
readonly feedback: DrawFeedback | null;
```

The outcome of the last export or copy, until the shapes change.

### finish

```ts
readonly finish: () => void;
```

Ends the shape being drawn.

### handles

```ts
readonly handles: readonly MapEditHandle[];
```

The handles to give the map adapter: those of the selected shape while the tool is open.

### hoveredHandle

```ts
readonly hoveredHandle: string | null;
```

The handle a mouse is over, or null.

### importFeedback

```ts
readonly importFeedback: DrawFeedback | null;
```

What the last import added or why it added nothing, until the shapes change again.

### importFile

```ts
readonly importFile: () => Promise<Bounds | null>;
```

Asks the person for a GeoJSON, CSV or WKT file and adds its shapes as one undo
step. Call it straight from a press, so the browser shows the picker.

Returns: The box holding the shapes it added, for the map to fit, or
   null when it added none, including when the host has no file opener.

### insertAtCentre

```ts
readonly insertAtCentre: () => void;
```

Adds a vertex at the map centre after the selected one.

### issue

```ts
readonly issue: DrawIssue | null;
```

Why the selected shape is not valid after an edit, or null.

### issueMessage

```ts
readonly issueMessage: string;
```

What to do about [`UseDraw`](https://docs.mapcairn.com/api/map/UseDraw-interface.md), or an empty string.

### keyCommand

```ts
readonly keyCommand: (event: DrawKeyEvent) => boolean;
```

Runs a key press as a draw command. Returns true when it was one, so the caller can stop the key.

### measure

```ts
readonly measure: string;
```

The measures of the shape being drawn or selected, such as "1.2 ha · 480 m". Empty when none.

### mode

```ts
readonly mode: DrawKind;
```

The kind of shape the next tap starts: point, line, polygon or circle.

### moveSelectedTo

```ts
readonly moveSelectedTo: (at: LatLng) => void;
```

Moves the selected vertex to a position.

### nudge

```ts
readonly nudge: (dx: number, dy: number) => void;
```

Moves the selected vertex by screen pixels (positive x is right, positive y is down).

### onHandleDrag

```ts
readonly onHandleDrag: (drag: MapHandleDrag) => void;
```

A step of a press on an edit handle, from the adapter.

### onHandleHover

```ts
readonly onHandleHover: (id: string | null) => void;
```

Records the edit handle a mouse is over, or null when it leaves, so it can be drawn larger.

### onMapClick

```ts
readonly onMapClick: (event: Pick<MapClickEvent, "latlng" | "point">) => void;
```

A click or tap on the map. Starts, extends or selects a shape.

### placeAtCentre

```ts
readonly placeAtCentre: () => void;
```

Adds a point at the map centre, or moves the selected vertex there.

### redo

```ts
readonly redo: () => void;
```

Reapplies the last undone change. Does nothing when `canRedo` is false.

### removeVertex

```ts
readonly removeVertex: () => void;
```

Removes the selected vertex.

### rereadImport

```ts
readonly rereadImport: (order: WktAxisOrder) => Bounds | null;
```

Reads the file whose import offered [`DrawFeedback`](https://docs.mapcairn.com/api/map/DrawFeedback.md) again in
`order`, and keeps `order` as the WKT axis order.

Returns: The box holding the shapes it added, as [`UseDraw`](https://docs.mapcairn.com/api/map/UseDraw-interface.md)
   does, or null when it added none or there is no such file.

### scaleRadius

```ts
readonly scaleRadius: (factor: number) => void;
```

Multiplies the selected circle's radius.

### select

```ts
readonly select: (shapeId: string, vertex?: number | null) => void;
```

Selects a shape, and a vertex of it when `vertex` is a number. Without
`vertex`, a point or a circle selects its one vertex, so the arrow keys
and Move to map centre work on it; any other shape selects as a whole.

### selected

```ts
readonly selected: DrawShape | null;
```

The selected shape, or null.

### selection

```ts
readonly selection: DrawSelection | null;
```

The selected shape and, when there is one, its selected vertex, or null.

### setMode

```ts
readonly setMode: (kind: DrawKind) => void;
```

Chooses the kind of shape the next tap starts.

### setRadiusToCentre

```ts
readonly setRadiusToCentre: () => void;
```

Sets the selected circle's radius to its distance from the map centre.

### setWktAxisOrder

```ts
readonly setWktAxisOrder: (order: WktAxisOrder) => void;
```

Chooses the order by hand. An offer to read the last file the other way, and the file it kept, are dropped.

### shapes

```ts
readonly shapes: readonly DrawShape[];
```

Every shape, in the order drawn. The last may be unfinished.

### state

```ts
readonly state: DrawState;
```

The draw state as the reducer holds it: the shapes, the mode, the selection and the undo history.

### status

```ts
readonly status: string;
```

A line for the toolbar, such as "Polygon 1, 3 points", or "Nothing drawn yet".

### stepVertex

```ts
readonly stepVertex: (delta: 1 | -1) => void;
```

Moves the selection to the next (`1`) or previous (`-1`) vertex of the selected shape.

### undo

```ts
readonly undo: () => void;
```

Reverts the last change to the shapes. Does nothing when `canUndo` is false.

### wktAxisOrder

```ts
readonly wktAxisOrder: WktAxisOrder;
```

The order of the two numbers in a WKT position that an import reads: longitude first until changed. Kept for the session only.

## See also

- [`LatLng`](https://docs.mapcairn.com/api/core/LatLng.md)
- [`DrawShape`](https://docs.mapcairn.com/api/core/DrawShape.md)
- [`DrawFeedback`](https://docs.mapcairn.com/api/map/DrawFeedback.md)
- [`MapEditHandle`](https://docs.mapcairn.com/api/core/MapEditHandle.md)
- [`Bounds`](https://docs.mapcairn.com/api/core/Bounds.md)
- [`DrawIssue`](https://docs.mapcairn.com/api/core/DrawIssue.md)
- [`DrawKeyEvent`](https://docs.mapcairn.com/api/core/DrawKeyEvent.md)
- [`DrawKind`](https://docs.mapcairn.com/api/core/DrawKind.md)
- [`MapHandleDrag`](https://docs.mapcairn.com/api/core/MapHandleDrag.md)
- [`MapClickEvent`](https://docs.mapcairn.com/api/core/MapClickEvent.md)
- [`WktAxisOrder`](https://docs.mapcairn.com/api/core/WktAxisOrder.md)
- [`DrawSelection`](https://docs.mapcairn.com/api/core/DrawSelection.md)
- [`DrawState`](https://docs.mapcairn.com/api/core/DrawState.md)

Source: [packages/map/src/tools/DrawToolbar/useDraw.ts:133](https://github.com/MapCairn/mapcairn/blob/main/packages/map/src/tools/DrawToolbar/useDraw.ts#L133)
