docx-kit 0.0.0 → 0.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.
@@ -0,0 +1,67 @@
1
+ //#region src/browser/dom.d.ts
2
+ /**
3
+ * Browser DOM utilities — base64 decoding and Blob/image handling.
4
+ *
5
+ * @module browser/dom
6
+ */
7
+ /**
8
+ * Decode a base64 data-URL to raw bytes using the browser `atob` API.
9
+ *
10
+ * Strips the `"data:*;base64,"` prefix, decodes the base64 string
11
+ * via `atob()`, and populates a `Uint8Array` from each character's
12
+ * code point.
13
+ *
14
+ * @param dataUrl - — A base64 data-URI string (e.g. `"data:image/png;base64,iVBO..."`)
15
+ * @returns Raw bytes as `Uint8Array`
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { dataUrlToUint8Array } from 'docx-kit/browser'
20
+ *
21
+ * const bytes = await dataUrlToUint8Array('data:image/png;base64,iVBORw...')
22
+ * ```
23
+ */
24
+ declare function dataUrlToUint8Array(dataUrl: string): Promise<Uint8Array>;
25
+ /**
26
+ * Normalize image data for `ImageRun` — converts `Blob` to `Uint8Array`.
27
+ *
28
+ * In the browser, image data often arrives as a `Blob` (e.g. from
29
+ * `<input type="file">` or `fetch()`). `ImageRun` expects raw bytes,
30
+ * so we convert via `Blob.arrayBuffer()`.
31
+ *
32
+ * @param data - — The raw image data (Blob, Uint8Array, ArrayBuffer, string)
33
+ * @returns Normalized data suitable for `ImageRun`
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { normalizeImageData } from 'docx-kit/browser'
38
+ *
39
+ * const file = document.querySelector('input[type=file]').files[0]
40
+ * const data = await normalizeImageData(file) // Blob → Uint8Array
41
+ * ```
42
+ */
43
+ declare function normalizeImageData(data: unknown): Promise<string | ArrayBuffer | Uint8Array>;
44
+ //#endregion
45
+ //#region src/browser.d.ts
46
+ /**
47
+ * ### ❌ `saveDocument()` — Not available in browsers
48
+ *
49
+ * Browsers do not provide direct filesystem access for security reasons.
50
+ * To trigger a file download in the browser, use `doc.toBlob()` with
51
+ * `URL.createObjectURL()` and a temporary `<a>` element:
52
+ *
53
+ * ```ts
54
+ * const blob = await doc.toBlob()
55
+ * const url = URL.createObjectURL(blob)
56
+ * const a = document.createElement('a')
57
+ * a.href = url
58
+ * a.download = 'output.docx'
59
+ * a.click()
60
+ * URL.revokeObjectURL(url)
61
+ * ```
62
+ *
63
+ * @deprecated This API is not available in browsers. Use `doc.toBlob()` instead.
64
+ */
65
+ declare const saveDocument: never;
66
+ //#endregion
67
+ export { dataUrlToUint8Array, normalizeImageData, saveDocument };
@@ -0,0 +1,54 @@
1
+ //#region src/browser/dom.ts
2
+ /**
3
+ * Browser DOM utilities — base64 decoding and Blob/image handling.
4
+ *
5
+ * @module browser/dom
6
+ */
7
+ /**
8
+ * Decode a base64 data-URL to raw bytes using the browser `atob` API.
9
+ *
10
+ * Strips the `"data:*;base64,"` prefix, decodes the base64 string
11
+ * via `atob()`, and populates a `Uint8Array` from each character's
12
+ * code point.
13
+ *
14
+ * @param dataUrl - — A base64 data-URI string (e.g. `"data:image/png;base64,iVBO..."`)
15
+ * @returns Raw bytes as `Uint8Array`
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { dataUrlToUint8Array } from 'docx-kit/browser'
20
+ *
21
+ * const bytes = await dataUrlToUint8Array('data:image/png;base64,iVBORw...')
22
+ * ```
23
+ */
24
+ async function dataUrlToUint8Array(dataUrl) {
25
+ const base64 = dataUrl.split(",")[1];
26
+ const binary = atob(base64);
27
+ const bytes = new Uint8Array(binary.length);
28
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
29
+ return bytes;
30
+ }
31
+ /**
32
+ * Normalize image data for `ImageRun` — converts `Blob` to `Uint8Array`.
33
+ *
34
+ * In the browser, image data often arrives as a `Blob` (e.g. from
35
+ * `<input type="file">` or `fetch()`). `ImageRun` expects raw bytes,
36
+ * so we convert via `Blob.arrayBuffer()`.
37
+ *
38
+ * @param data - — The raw image data (Blob, Uint8Array, ArrayBuffer, string)
39
+ * @returns Normalized data suitable for `ImageRun`
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import { normalizeImageData } from 'docx-kit/browser'
44
+ *
45
+ * const file = document.querySelector('input[type=file]').files[0]
46
+ * const data = await normalizeImageData(file) // Blob → Uint8Array
47
+ * ```
48
+ */
49
+ async function normalizeImageData(data) {
50
+ if (typeof Blob !== "undefined" && data instanceof Blob) return new Uint8Array(await data.arrayBuffer());
51
+ return data;
52
+ }
53
+ //#endregion
54
+ export { dataUrlToUint8Array, normalizeImageData };