Skip to content

Commit 5f5d6e6

Browse files
committed
chore: update documentation and improve types
1 parent b4d3132 commit 5f5d6e6

5 files changed

Lines changed: 81 additions & 32 deletions

File tree

react/README.md

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,31 +122,74 @@ import { EmbedPDF } from '@simplepdf/react-embed-pdf';
122122

123123
### Programmatic Control
124124

125-
_Requires a SimplePDF account_
125+
_Some actions require a SimplePDF account_
126126

127127
Use `const { embedRef, actions } = useEmbed();` to programmatically control the embed editor:
128128

129-
- `actions.submit`: Submit the document (specify or not whether to download a copy of the document on the device of the user)
130-
- `actions.selectTool`: Select a tool to use
129+
| Action | Description |
130+
| ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------- |
131+
| `actions.goTo({ page })` | Navigate to a specific page |
132+
| `actions.selectTool(toolType)` | Select a tool: `'TEXT'`, `'BOXED_TEXT'`, `'CHECKBOX'`, `'PICTURE'`, `'SIGNATURE'`, or `null` to deselect (`CURSOR`) |
133+
| `actions.createField(options)` | Create a field at specified position (see below) |
134+
| `actions.clearFields(options?)` | Clear fields by `fieldIds` or `page`, or all fields if no options |
135+
| `actions.getDocumentContent({ extractionMode })` | Extract document content (`extractionMode: 'auto'` or `'ocr'`) |
136+
| `actions.submit({ downloadCopyOnDevice })` | Submit the document |
131137

132-
```jsx
133-
import { EmbedPDF, useEmbed } from "@simplepdf/react-embed-pdf";
134-
135-
const { embedRef, actions } = useEmbed();
138+
All actions return a `Promise` with a result object: `{ success: true, data: ... }` or `{ success: false, error: { code, message } }`.
136139

137-
return (
138-
<>
139-
<button onClick={() => await actions.submit({ downloadCopyOnDevice: false })}>Submit</button>
140-
<button onClick={() => await actions.selectTool('TEXT')}>Select Text Tool</button>
140+
```jsx
141+
import { EmbedPDF, useEmbed } from '@simplepdf/react-embed-pdf';
142+
143+
const Editor = () => {
144+
const { embedRef, actions } = useEmbed();
145+
146+
const handleSubmit = async () => {
147+
const result = await actions.submit({ downloadCopyOnDevice: false });
148+
if (result.success) {
149+
console.log('Submitted!');
150+
}
151+
};
152+
153+
const handleExtract = async () => {
154+
const result = await actions.getDocumentContent({ extractionMode: 'auto' });
155+
if (result.success) {
156+
console.log('Document name:', result.data.name);
157+
console.log('Pages:', result.data.pages);
158+
}
159+
};
160+
161+
const handleCreateTextField = async () => {
162+
const result = await actions.createField({
163+
type: 'TEXT',
164+
page: 1,
165+
x: 100,
166+
y: 200,
167+
width: 150,
168+
height: 30,
169+
value: 'Hello World',
170+
});
171+
if (result.success) {
172+
console.log('Created field:', result.data.field_id);
173+
}
174+
};
175+
176+
return (
177+
<>
178+
<button onClick={handleSubmit}>Submit</button>
179+
<button onClick={handleExtract}>Extract Content</button>
180+
<button onClick={handleCreateTextField}>Add Text Field</button>
181+
<button onClick={() => actions.selectTool('TEXT')}>Select Text Tool</button>
182+
<button onClick={() => actions.goTo({ page: 2 })}>Go to Page 2</button>
141183
<EmbedPDF
142-
companyIdentifier="yourcompany"
143-
ref={embedRef}
144-
mode="inline"
145-
style={{ width: 900, height: 800 }}
146-
documentURL="https://cdn.simplepdf.com/simple-pdf/assets/sample.pdf"
184+
companyIdentifier="yourcompany"
185+
ref={embedRef}
186+
mode="inline"
187+
style={{ width: 900, height: 800 }}
188+
documentURL="https://cdn.simplepdf.com/simple-pdf/assets/sample.pdf"
147189
/>
148-
</>
149-
);
190+
</>
191+
);
192+
};
150193
```
151194

152195
### <a id="available-props"></a>Available props
@@ -160,19 +203,19 @@ return (
160203
</tr>
161204
<tr>
162205
<td>ref</td>
163-
<td>EmbedRefHandlers</td>
206+
<td>EmbedActions</td>
164207
<td>No</td>
165-
<td>Used for programmatic control of the editor</td>
208+
<td>Used for programmatic control of the editor (see Programmatic Control section)</td>
166209
</tr>
167210
<tr>
168211
<td>mode</td>
169212
<td>"inline" | "modal"</td>
170213
<td>No (defaults to "modal")</td>
171214
<td>Inline the editor or display it inside a modal</td>
172215
</tr>
173-
<tr>
216+
<tr>
174217
<td>locale</td>
175-
<td>"en" | "de" | "es" | "fr" | "it" | "pt"</td>
218+
<td>"en" | "de" | "es" | "fr" | "it" | "nl" | "pt"</td>
176219
<td>No (defaults to "en")</td>
177220
<td>Language to display the editor in (ISO locale)</td>
178221
</tr>
@@ -188,6 +231,12 @@ return (
188231
<td>No</td>
189232
<td><a href="https://simplepdf.com/embed">Allows collecting customers submissions</a></td>
190233
</tr>
234+
<tr>
235+
<td>baseDomain</td>
236+
<td>string</td>
237+
<td>No</td>
238+
<td>Override the base domain for self-hosted deployments (e.g., "yourdomain.com"). Contact sales@simplepdf.com for enterprise self-hosting</td>
239+
</tr>
191240
<tr>
192241
<td>context</td>
193242
<td>Record&lt;string, unknown&gt;</td>
@@ -225,12 +274,12 @@ return (
225274
1. Link the widget
226275

227276
```sh
228-
yarn link
229-
yarn start
277+
npm link
278+
npm start
230279
```
231280

232281
2. Use it in the target application
233282

234283
```sh
235-
yarn link @simplepdf/react-embed-pdf
284+
npm link @simplepdf/react-embed-pdf
236285
```

react/src/hook.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe('useEmbed', () => {
284284

285285
it('getDocumentContent returns error when embedRef not attached', async () => {
286286
const { result } = renderHook(() => useEmbed());
287-
const actionResult = await result.current.actions.getDocumentContent({});
287+
const actionResult = await result.current.actions.getDocumentContent({ extractionMode: 'auto' });
288288
expect(actionResult).toEqual(expectedError);
289289
});
290290

@@ -365,9 +365,9 @@ describe('useEmbed', () => {
365365
const { ref, spies } = createMockEmbedRef();
366366
(result.current.embedRef as React.MutableRefObject<EmbedActions>).current = ref;
367367

368-
const actionResult = await result.current.actions.getDocumentContent({});
368+
const actionResult = await result.current.actions.getDocumentContent({ extractionMode: 'auto' });
369369

370-
expect(spies.getDocumentContent).toHaveBeenCalledWith({});
370+
expect(spies.getDocumentContent).toHaveBeenCalledWith({ extractionMode: 'auto' });
371371
expect(actionResult).toEqual({ success: true });
372372
});
373373

react/src/hook.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export type EmbedActions = {
5555

5656
clearFields: (options?: { fieldIds?: string[]; page?: number }) => Promise<ActionResult<ClearFieldsResult>>;
5757

58-
getDocumentContent: (options?: { extractionMode?: ExtractionMode }) => Promise<ActionResult<DocumentContentResult>>;
58+
getDocumentContent: (options: { extractionMode: ExtractionMode }) => Promise<ActionResult<DocumentContentResult>>;
5959

6060
submit: (options: { downloadCopyOnDevice: boolean }) => Promise<ActionResult>;
6161
};
@@ -175,7 +175,7 @@ export const useEmbed = (): { embedRef: React.RefObject<EmbedActions | null>; ac
175175
);
176176

177177
const handleGetDocumentContent = React.useCallback(
178-
createAction<[{ extractionMode?: ExtractionMode }?], DocumentContentResult>(async (ref, options) => {
178+
createAction<[{ extractionMode: ExtractionMode }], DocumentContentResult>(async (ref, options) => {
179179
return ref.getDocumentContent(options);
180180
}),
181181
[],

react/src/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ describe('EmbedPDF', () => {
508508
throw new Error('Expected ref to be available');
509509
}
510510

511-
const resultPromise = ref.current.getDocumentContent({});
511+
const resultPromise = ref.current.getDocumentContent({ extractionMode: 'auto' });
512512

513513
await waitFor(() => {
514514
expect(mockContentWindow.postMessage).toHaveBeenCalled();

react/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export const EmbedPDF = React.forwardRef<EmbedActions, Props>((props, ref) => {
207207
await ensureEditorReady();
208208
return sendEvent(iframeRef.current, {
209209
type: 'GET_DOCUMENT_CONTENT',
210-
data: { extraction_mode: options?.extractionMode },
210+
data: { extraction_mode: options.extractionMode },
211211
});
212212
}, []);
213213

0 commit comments

Comments
 (0)