@ninesstudios/whiteboard 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -78,6 +78,13 @@ function App() {
78
78
  </div>
79
79
  );
80
80
  }
81
+
82
+ // Example: read & replace elements
83
+ // const elements = whiteboardRef.current?.getElements();
84
+ // whiteboardRef.current?.setElements(elements);
85
+
86
+ // Example: clear all elements
87
+ // whiteboardRef.current?.clearElements();
81
88
  ```
82
89
 
83
90
  ### Programmatic Updates (applyAction)
@@ -129,7 +136,7 @@ function App() {
129
136
 
130
137
  | Prop | Type | Default | Description |
131
138
  |------|------|---------|-------------|
132
- | `onAction` | `(action: object) => void` | `undefined` | Callback fired whenever a state-changing action occurs (e.g., drawing, moving). |
139
+ | `onAction` | `(action: object) => void` | `undefined` | Callback fired for content-changing actions (e.g., add/remove/move elements, text edits). It does not emit high-frequency UI actions like pan/zoom/selection/tool changes. |
133
140
  | `isLocked` | `boolean` | `false` | When `true`, disables all editing tools and shows a lock indicator. |
134
141
  | `lockText` | `string` | `undefined` | Custom text to display on the lock indicator when `isLocked` is true. |
135
142
  | `onLockChange` | `(isLocked: boolean) => void` | `undefined` | Callback fired when the lock state changes. |
@@ -141,8 +148,107 @@ You can access these methods by passing a `ref` to the `Whiteboard` component.
141
148
  | Method | Arguments | Description |
142
149
  |--------|-----------|-------------|
143
150
  | `applyAction` | `(action: object)` | Dispatches an action to the internal Redux store. Useful for applying remote updates. |
151
+ | `getElems` / `getElements` | `()` | Returns the current `elements` array from the internal store. |
152
+ | `setElems` / `setElements` | `(elements: object[])` | Replaces the entire `elements` array in the internal store. |
153
+ | `clearElems` / `clearElements` | `()` | Clears all elements from the internal store. |
144
154
  | `canvas` | - | Returns the underlying HTMLCanvasElement. |
145
155
 
156
+ ### Redux Actions Helper
157
+
158
+ For convenience (e.g., building your own `applyAction` payloads), the package exports the internal Redux action creators:
159
+
160
+ ```js
161
+ import { whiteboardActions } from 'whiteboard';
162
+
163
+ // Example: build an action object to send/apply
164
+ const action = whiteboardActions.clearElements();
165
+ // action.type === 'whiteboard/clearElements'
166
+ ```
167
+
168
+ #### All available actions
169
+
170
+ | Action creator | `action.type` | Payload |
171
+ |---|---|---|
172
+ | `toggleGrid()` | `whiteboard/toggleGrid` | _none_ |
173
+ | `setGridSize(size)` | `whiteboard/setGridSize` | `number` |
174
+ | `setGridColor(color)` | `whiteboard/setGridColor` | `string` |
175
+ | `setSelectedTool(tool)` | `whiteboard/setSelectedTool` | `string` |
176
+ | `setToolProperty({ tool, property, value })` | `whiteboard/setToolProperty` | `{ tool: string, property: string, value: any }` |
177
+ | `addElement(element)` | `whiteboard/addElement` | `Element` (see “addElement payload” below) |
178
+ | `updateElement({ id, updates })` | `whiteboard/updateElement` | `{ id: string, updates: object }` |
179
+ | `removeElement(id)` | `whiteboard/removeElement` | `string` |
180
+ | `setSelectedElement(idOrNull)` | `whiteboard/setSelectedElement` | `string \| null` |
181
+ | `clearElements()` | `whiteboard/clearElements` | _none_ |
182
+ | `setElements(elements)` | `whiteboard/setElements` | `object[]` |
183
+ | `setPan({ x, y })` | `whiteboard/setPan` | `{ x: number, y: number }` |
184
+ | `setZoom(zoom)` | `whiteboard/setZoom` | `number` |
185
+ | `resetViewport()` | `whiteboard/resetViewport` | _none_ |
186
+ | `fitToView({ canvasWidth, canvasHeight, padding? })` | `whiteboard/fitToView` | `{ canvasWidth: number, canvasHeight: number, padding?: number }` |
187
+ | `bringToFront(id)` | `whiteboard/bringToFront` | `string` |
188
+ | `sendToBack(id)` | `whiteboard/sendToBack` | `string` |
189
+ | `setLocked(isLocked)` | `whiteboard/setLocked` | `boolean` |
190
+ | `setWatermarkEnabled(visible)` | `whiteboard/setWatermarkEnabled` | `boolean` |
191
+ | `setWatermarkText(text)` | `whiteboard/setWatermarkText` | `string` |
192
+
193
+ #### `addElement(element)` payload
194
+
195
+ `addElement` expects a full element object. Coordinates are **world coordinates** (the board handles pan/zoom internally).
196
+
197
+ Common fields (all element types):
198
+
199
+ | Field | Type | Notes |
200
+ |---|---|---|
201
+ | `id` | `string` | Must be unique (commonly `Date.now().toString()`). |
202
+ | `type` | `string` | One of the supported element types below. |
203
+
204
+ Element schemas by `type`:
205
+
206
+ - **rectangle / triangle / diamond / hexagon / octagon**
207
+ - Required: `x: number`, `y: number`, `width: number`, `height: number`
208
+ - Styling: `strokeColor: string`, `fillColor: string`, `lineWidth: number`
209
+
210
+ - **circle**
211
+ - Required: `centerX: number`, `centerY: number`, `radius: number`
212
+ - Styling: `strokeColor: string`, `fillColor: string`, `lineWidth: number`
213
+
214
+ - **pencil**
215
+ - Required: `points: Array<{ x: number, y: number }>`
216
+ - Styling: `strokeColor: string`, `lineWidth: number`
217
+
218
+ - **star / heart**
219
+ - Required: `centerX: number`, `centerY: number`, `size: number`
220
+ - Styling: `strokeColor: string`, `fillColor: string`, `lineWidth: number`
221
+
222
+ - **arrow**
223
+ - Required: `startX: number`, `startY: number`, `endX: number`, `endY: number`
224
+ - Styling: `strokeColor: string`, `fillColor: string`, `lineWidth: number`
225
+
226
+ - **text**
227
+ - Required: `x: number`, `y: number`, `text: string`
228
+ - Styling: `fontSize: number`, `fontColor: string`
229
+
230
+ - **image**
231
+ - Required: `x: number`, `y: number`, `width: number`, `height: number`, `src: string`
232
+ - Notes: `src` is typically a Data URL (from `FileReader.readAsDataURL`).
233
+
234
+ Example (rectangle):
235
+
236
+ ```js
237
+ import { whiteboardActions } from 'whiteboard';
238
+
239
+ const action = whiteboardActions.addElement({
240
+ id: '1',
241
+ type: 'rectangle',
242
+ x: 100,
243
+ y: 80,
244
+ width: 200,
245
+ height: 120,
246
+ strokeColor: '#000000',
247
+ fillColor: '#ffffff',
248
+ lineWidth: 2,
249
+ });
250
+ ```
251
+
146
252
  ## License
147
253
 
148
254
  MIT