hivewrite-sdk 1.1.92 → 1.1.93

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
@@ -291,8 +291,8 @@ mergeTags: [
291
291
  | :----------------- | :------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------ |
292
292
  | `onLoad` | `()` | Editor fully initialized |
293
293
  | `onExport` | `(html: string, user_id?: string)` | Triggered on HTML export |
294
- | `onSave` | `(json: {blocks: any[], canvasSettings: CanvasSettings, name: string, description: string, category: string}, user_id?: string)` | Triggered on design save |
295
- | `onAutoSave` | `(json, user_id?: string)` — same JSON as `onSave` | Fired every `autosave` seconds while there are unsaved changes |
294
+ | `onSave` | `(design: SaveDesign, user_id: string \| null, context: SaveContext) => SaveResult \| boolean \| void \| Promise<...>` | Called on every manual Save; return the backend result |
295
+ | `onAutoSave` | `(design, user_id, context) => SaveResult \| boolean \| void \| Promise<...>` — same contract, `context.source === "autosave"` | Fired every `autosave` seconds while there are unsaved changes |
296
296
  | `onError` | `(err: any, user_id?: string)` | Initialization or processing error |
297
297
  | `onTemplateChange` | `(templateId: string, user_id?: string)` | Template selection changed |
298
298
  | `onImageUpload` | `(file: File, user_id?: string)` | **Async**. Handle image uploads, return URL |
@@ -312,24 +312,116 @@ callbacks: {
312
312
  }
313
313
  ```
314
314
 
315
+ ### Saving & Autosave
316
+
317
+ Both save callbacks receive:
318
+
319
+ 1. `design` — the complete editable design (same shape `loadDesign()` accepts).
320
+ 2. `user_id` — the id passed to `init()`.
321
+ 3. `context` — `{ design, html, source, templateId, version, user_id }`, where `html` is the final generated email HTML and `source` is `"manual"` or `"autosave"`.
322
+
323
+ Return (or resolve with) the result of your backend request. The editor **waits** for it and only shows a successful save when `success` is not `false`. Thrown errors and rejected promises are reported to the user as failures.
324
+
325
+ ```javascript
326
+ callbacks: {
327
+ onSave: async (design, user_id, context) => {
328
+ const res = await fetch("/api/templates", {
329
+ method: "POST",
330
+ headers: { "Content-Type": "application/json" },
331
+ body: JSON.stringify({
332
+ user_id,
333
+ design, // full design to reopen later
334
+ html: context.html, // final email HTML
335
+ templateId: context.templateId,
336
+ version: context.version,
337
+ }),
338
+ });
339
+
340
+ if (!res.ok) {
341
+ return { success: false, error: "Could not save the template." };
342
+ }
343
+
344
+ const saved = await res.json();
345
+ return {
346
+ success: true,
347
+ templateId: saved.id, // stored for the next save
348
+ version: saved.version, // stored for the next save
349
+ };
350
+ },
351
+
352
+ onAutoSave: async (design, user_id, context) => {
353
+ // context.source === "autosave"; same contract as onSave
354
+ return saveDraft(design, context.html, context.templateId, context.version);
355
+ },
356
+ }
357
+ ```
358
+
359
+ A callback result can be:
360
+
361
+ ```typescript
362
+ {
363
+ success: boolean;
364
+ templateId?: string; // optional: backend id
365
+ version?: string | number; // optional: backend version
366
+ message?: string; // optional success message for the toast
367
+ error?: string; // shown to the user when success is false
368
+ }
369
+ ```
370
+
371
+ Returning `undefined`/`void` is treated as success, and a plain boolean is treated as the success flag.
372
+
373
+ `hivewrite.save()` also returns the result, so you can handle it yourself:
374
+
375
+ ```javascript
376
+ const result = await hivewrite.save();
377
+ if (!result.success) {
378
+ console.error(result.code, result.error);
379
+ }
380
+ ```
381
+
382
+ ### Template ID & Version
383
+
384
+ The id/version returned from a successful save are remembered and sent with the next save. You can also set them when opening an existing template:
385
+
386
+ ```javascript
387
+ hivewrite.setTemplateId("tpl_123", 4);
388
+ hivewrite.getTemplateId(); // "tpl_123"
389
+ hivewrite.getTemplateVersion(); // 4
390
+ ```
391
+
392
+ `loadDesign()` also restores `templateId`/`version` when they are present in the stored design.
393
+
315
394
  ### Response Examples
316
395
 
317
396
  #### `hivewrite.save()`
318
397
 
319
- Triggers the `onSave` callback with the current design state.
398
+ Triggers the `onSave` callback with the current design state and generated HTML.
320
399
 
321
400
  ```javascript
322
401
  hivewrite.save();
323
402
 
324
- // Result sent to onSave(json, user_id):
403
+ // onSave(design, user_id, context)
404
+ // design:
325
405
  {
326
406
  "body": {
327
- "blocks": [ ... ] // Array of block objects
407
+ "blocks": [ ... ] // Array of block objects
328
408
  },
329
- "canvasSettings": { ... }, // Canvas styles (width, background, etc)
409
+ "canvasSettings": { ... }, // Canvas styles (width, background, etc)
330
410
  "name": "My Email Template",
331
411
  "description": "Here will be the description",
332
- "category": "marketing"
412
+ "category": "marketing",
413
+ "templateId": "tpl_123",
414
+ "version": 4
415
+ }
416
+
417
+ // context:
418
+ {
419
+ "design": { ... }, // same object as above
420
+ "html": "<!doctype html>…", // final generated email HTML
421
+ "source": "manual", // "manual" or "autosave"
422
+ "templateId": "tpl_123",
423
+ "version": 4,
424
+ "user_id": "user_123"
333
425
  }
334
426
  ```
335
427
 
@@ -349,14 +441,16 @@ onExport: (html, user_id) => {
349
441
 
350
442
  ## 🏗️ Design JSON Format
351
443
 
352
- The `loadDesign(data)` method and `onSave` callback use a specific JSON structure.
444
+ The `loadDesign(data)` method and the `onSave` design payload use the same JSON structure. Saves return the blocks under `body.blocks`; `loadDesign` accepts either `body.blocks` or a top-level `blocks` array.
353
445
 
354
446
  ### `data` Object
355
447
 
356
448
  | Property | Type | Description |
357
449
  | :--------------- | :--------------- | :-------------------------------- |
358
- | `blocks` | `EditorBlock[]` | Array of content blocks |
450
+ | `body.blocks` | `EditorBlock[]` | Array of content blocks (a top-level `blocks` array is also accepted by `loadDesign`) |
359
451
  | `canvasSettings` | `CanvasSettings` | Global editor styles and branding |
452
+ | `templateId` | `string \| null` | Optional backend template id |
453
+ | `version` | `string \| number \| null` | Optional backend template version |
360
454
 
361
455
  ### JSON Example
362
456
 
@@ -480,7 +574,10 @@ Exported HTML is fully compatible with:
480
574
  | `exportHtml(callback)` | Shorthand for HTML export |
481
575
  | `exportJson(callback)` | Shorthand for JSON export |
482
576
  | `exportMjml(callback)` | Shorthand for MJML export |
483
- | `save()` | Triggers `onSave` callback with current JSON |
577
+ | `save()` | `Promise<SaveResult>`; triggers `onSave` with the design, HTML and context |
578
+ | `setTemplateId(id, version?)` | Store the backend template id/version for the next save |
579
+ | `getTemplateId()` | Get the currently stored template id |
580
+ | `getTemplateVersion()` | Get the currently stored template version |
484
581
  | `setTheme(theme)` | Switch theme at runtime |
485
582
  | `setLocale(locale)` | Switch locale at runtime |
486
583
  | `destroy()` | Unmount and cleanup |