@studio8collective/patent-render 1.0.0 → 1.1.0

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/docs/nextjs.md CHANGED
@@ -16,6 +16,7 @@ If you only want the shortest path, follow sections 1 to 7 and you have a workin
16
16
  8. [Choosing the LLM](#8-choosing-the-llm)
17
17
  9. [Protecting the route and the action](#9-protecting-the-route-and-the-action)
18
18
  10. [Saving and reopening figures](#10-saving-and-reopening-figures)
19
+ - [10.4 Refreshes and interrupted drawings](#104-refreshes-and-interrupted-drawings)
19
20
  11. [Handling the embed result](#11-handling-the-embed-result)
20
21
  12. [Patent context and numeral consistency](#12-patent-context-and-numeral-consistency)
21
22
  13. [Using the core in server code](#13-using-the-core-in-server-code)
@@ -401,19 +402,23 @@ The `PatentDocument` is plain JSON. It contains the drawing, layers, numerals, s
401
402
 
402
403
  ### 10.1 Autosave with `onChange`
403
404
 
404
- `onChange` fires on **every** change, including each frame of a drag. Debounce it before writing to your database:
405
+ `onChange(doc, info)` fires on every drawing change, including each frame of a drag, and at every point where a drawing run could be cut off. Debounce ordinary edits and save the rest at once:
405
406
 
406
407
  ```tsx
407
408
  "use client";
408
- import { useMemo } from "react";
409
- import { PatentRender, type PatentDocument } from "@studio8collective/patent-render";
409
+ import { useEffect, useMemo } from "react";
410
+ import { PatentRender, type PatentDocument, type SaveInfo } from "@studio8collective/patent-render";
410
411
  import { saveFigureDraft } from "./actions";
411
412
 
412
- function debounce<T extends (...a: never[]) => void>(fn: T, ms: number) {
413
- let t: ReturnType<typeof setTimeout> | undefined;
414
- return (...args: Parameters<T>) => {
415
- clearTimeout(t);
416
- t = setTimeout(() => fn(...args), ms);
413
+ function autosaver(save: (doc: PatentDocument) => void, ms: number) {
414
+ let timer: ReturnType<typeof setTimeout> | undefined;
415
+ return {
416
+ change(doc: PatentDocument, { reason }: SaveInfo) {
417
+ clearTimeout(timer);
418
+ if (reason === "edit") timer = setTimeout(() => save(doc), ms);
419
+ else save(doc); // "run" and "close" must not wait
420
+ },
421
+ cancel() { clearTimeout(timer); },
417
422
  };
418
423
  }
419
424
 
@@ -422,7 +427,8 @@ export function FigureEditor({ figureId, initial, exportPng }: {
422
427
  initial?: PatentDocument;
423
428
  exportPng: (d: PatentDocument) => Promise<{ dataUrl: string }>;
424
429
  }) {
425
- const autosave = useMemo(() => debounce((doc: PatentDocument) => void saveFigureDraft(figureId, doc), 1500), [figureId]);
430
+ const autosave = useMemo(() => autosaver((doc) => void saveFigureDraft(figureId, doc), 1500), [figureId]);
431
+ useEffect(() => () => autosave.cancel(), [autosave]);
426
432
 
427
433
  return (
428
434
  <div className="h-[calc(100vh-4rem)]">
@@ -431,7 +437,7 @@ export function FigureEditor({ figureId, initial, exportPng }: {
431
437
  document={initial}
432
438
  figureNumber="2"
433
439
  exportPng={exportPng}
434
- onChange={autosave}
440
+ onChange={autosave.change}
435
441
  onEmbed={() => {}}
436
442
  className="h-full"
437
443
  />
@@ -440,6 +446,14 @@ export function FigureEditor({ figureId, initial, exportPng }: {
440
446
  }
441
447
  ```
442
448
 
449
+ | `info.reason` | Meaning |
450
+ |---|---|
451
+ | `"edit"` | The drawing changed. Safe to debounce. |
452
+ | `"run"` | An assistant run started, reached a new step, or ended. Save now. |
453
+ | `"close"` | The page is being closed or hidden. Save now or lose it. |
454
+
455
+ A request started while the page unloads may be cancelled by the browser, so treat `"close"` as best effort. The `"run"` saves are what recovery relies on.
456
+
443
457
  ### 10.2 The save action: validate on the server
444
458
 
445
459
  ```ts
@@ -479,6 +493,40 @@ export default async function Page({ params }: { params: Promise<{ id: string }>
479
493
 
480
494
  When you pass `document`, `figureNumber`, `jurisdiction` and `sheetLabel` are taken from the document; those props only apply to new figures.
481
495
 
496
+ ### 10.4 Refreshes and interrupted drawings
497
+
498
+ A drawing run stops when the page is refreshed or closed, or the connection drops. The editor handles it in two ways:
499
+
500
+ 1. **Before:** while the assistant works, the browser asks before a refresh or close (its wording can't be changed), and the editor's Cancel button asks too.
501
+ 2. **After:** the next time the figure is opened, a card in the chat offers to recover.
502
+
503
+ | Interrupted during | Choices |
504
+ |---|---|
505
+ | Planning (nothing drawn) | Try again, Edit request, Dismiss |
506
+ | Drawing a new figure | Continue drawing, Start over, Keep as is |
507
+ | A change to an existing figure | Continue change, Undo these edits, Keep as is |
508
+
509
+ Continuing starts a new model call with the saved plan and the current drawing, and draws only what is missing. Nothing restarts by itself. Start over and Undo these edits can be undone.
510
+
511
+ Recovery needs two things from your app:
512
+
513
+ - **Save what `onChange` gives you**, with `"run"` saves sent at once (10.1). The unfinished run travels inside the document as `document.run`.
514
+ - **Reopen with the saved document** (10.3). After a refresh your page loads first, so the card appears when the user opens the figure again.
515
+
516
+ For a new figure, create the database row before opening the editor, or on the first `onChange`, so there is something to reopen.
517
+
518
+ If your own dialog can close in other ways (Escape, a click outside), confirm first while the assistant is busy:
519
+
520
+ ```tsx
521
+ const [busy, setBusy] = useState(false);
522
+
523
+ <Dialog onOpenChange={(open) => { if (!open && busy && !confirm("Stop drawing and close?")) return; setOpen(open); }}>
524
+ <PatentRender onBusyChange={setBusy} {...props} />
525
+ </Dialog>
526
+ ```
527
+
528
+ If the same figure is open in two tabs while one is drawing, the other shows it as interrupted.
529
+
482
530
  ## 11. Handling the embed result
483
531
 
484
532
  The **Embed** button is only enabled once the compliance check reports no errors (warnings are allowed). `onEmbed` then receives:
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "@studio8collective/patent-render",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "AI-assisted, editable, compliance-checked patent drawings for React and Next.js.",
5
5
  "author": "Sachin Bhatnagar",
6
6
  "license": "UNLICENSED",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/sachinbhatnagar/patent-render.git",
10
+ "directory": "packages/patent-render"
11
+ },
7
12
  "type": "module",
8
13
  "sideEffects": [
9
14
  "*.css"