@pixelvault-dev/paste 0.1.0 → 0.1.1

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
@@ -22,6 +22,23 @@ detach();
22
22
  Pasting or dropping an image inserts an `![Uploading…]()` placeholder, uploads
23
23
  the file, then replaces the placeholder with `![name](https://img.pixelvault.dev/…)`.
24
24
 
25
+ ### Click to select
26
+
27
+ A textarea has no built-in "upload" button, so wire `openFilePicker` to your own:
28
+
29
+ ```ts
30
+ import { openFilePicker } from "@pixelvault-dev/paste";
31
+
32
+ button.addEventListener("click", () =>
33
+ openFilePicker(document.querySelector("textarea"), {
34
+ publishableKey: "pv_pub_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
35
+ }),
36
+ );
37
+ ```
38
+
39
+ It opens the OS file dialog and runs the same upload-and-insert flow for the
40
+ chosen image(s).
41
+
25
42
  ### Options
26
43
 
27
44
  | Option | Type | Description |
package/dist/attach.d.ts CHANGED
@@ -24,6 +24,13 @@ export interface PasteOptions {
24
24
  */
25
25
  onError?: (error: Error, file: File) => string | void;
26
26
  }
27
+ /**
28
+ * Open the OS file picker and paste-and-host whatever image(s) the user selects
29
+ * into `target` — the same placeholder → upload → insert flow as paste and drop.
30
+ * Wire it to a button's click handler (a textarea has no built-in "upload"
31
+ * affordance, so the trigger is yours to place).
32
+ */
33
+ export declare function openFilePicker(target: PasteTarget, options: PasteOptions): void;
27
34
  /**
28
35
  * Attach paste-and-host behavior to a textarea or text input: pasting or
29
36
  * dropping an image uploads it to PixelVault and inserts a markdown link at the
package/dist/attach.js CHANGED
@@ -57,6 +57,38 @@ function handleFiles(target, files, options) {
57
57
  for (const file of files)
58
58
  void handleFile(target, file, options);
59
59
  }
60
+ /**
61
+ * Open the OS file picker and paste-and-host whatever image(s) the user selects
62
+ * into `target` — the same placeholder → upload → insert flow as paste and drop.
63
+ * Wire it to a button's click handler (a textarea has no built-in "upload"
64
+ * affordance, so the trigger is yours to place).
65
+ */
66
+ export function openFilePicker(target, options) {
67
+ if (!options.publishableKey) {
68
+ throw new Error("openFilePicker: options.publishableKey is required.");
69
+ }
70
+ const input = document.createElement("input");
71
+ input.type = "file";
72
+ input.accept = "image/*";
73
+ input.multiple = true;
74
+ input.style.display = "none";
75
+ const cleanup = () => input.remove();
76
+ input.addEventListener("change", () => {
77
+ const files = input.files
78
+ ? Array.from(input.files).filter((f) => f.type.startsWith("image/"))
79
+ : [];
80
+ if (files.length)
81
+ handleFiles(target, files, options);
82
+ cleanup();
83
+ }, { once: true });
84
+ // Backstop cleanup if the dialog is cancelled (no change event fires): the
85
+ // window regains focus when the dialog closes. Delayed so a real selection's
86
+ // change handler reads input.files first.
87
+ window.addEventListener("focus", () => window.setTimeout(cleanup, 500), { once: true });
88
+ // Some browsers only open the dialog for an input that's in the document.
89
+ document.body.appendChild(input);
90
+ input.click();
91
+ }
60
92
  /**
61
93
  * Attach paste-and-host behavior to a textarea or text input: pasting or
62
94
  * dropping an image uploads it to PixelVault and inserts a markdown link at the
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { attachPaste } from "./attach.js";
1
+ export { attachPaste, openFilePicker } from "./attach.js";
2
2
  export type { PasteOptions, PasteTarget } from "./attach.js";
3
3
  export { uploadImage, DEFAULT_ENDPOINT } from "./upload.js";
4
4
  export type { UploadResult, UploadOptions } from "./upload.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { attachPaste } from "./attach.js";
1
+ export { attachPaste, openFilePicker } from "./attach.js";
2
2
  export { uploadImage, DEFAULT_ENDPOINT } from "./upload.js";
3
3
  export { PixelVaultPasteError } from "./errors.js";
4
4
  export { buildImageMarkdown, buildPlaceholder, insertText, replaceFirst, } from "./text.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixelvault-dev/paste",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Paste-and-host: drop, paste, or select an image in any textarea and get a hosted CDN URL back. Backed by PixelVault publishable keys.",