@protobase/schema 0.124.0 → 0.124.2

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.
@@ -6,6 +6,11 @@ import { Field, FieldMeta } from "./field.js";
6
6
  export declare class FileField<V> extends Field<V> {
7
7
  /** Allowed types, checked against the type detected from the content: `['image/*', 'application/pdf']`. */
8
8
  accept(types: readonly string[]): FileField<V>;
9
+ /**
10
+ * Opens the camera on phones and tablets instead of the file picker, the rear one unless `'user'` asks for the front,
11
+ * and takes only images: the image types of `.accept()`, or any image. Desktop browsers pick an image file.
12
+ */
13
+ capture(camera?: 'environment' | 'user'): FileField<V>;
9
14
  /** The largest upload, as bytes or text such as `10 MB`; default 50 MB. */
10
15
  maxSize(size: number | string): FileField<V>;
11
16
  /** Stored with the `public` provider, at a permanent URL anyone with it may open. */
@@ -1,11 +1,22 @@
1
1
  import { Field } from "./field.js";
2
- import { checkedAccept, checkedProvider, checkedVariant, checkedVariants, parseSize } from "./files/file-options.js";
2
+ import { capturedAccept, checkedAccept, checkedCaptureAccept, checkedProvider, checkedVariant, checkedVariants, parseSize } from "./files/file-options.js";
3
3
  //#region src/file-field.ts
4
4
  /** `f.file()`: a text column holding a file's URI, uploaded through `:upload`. See [Files](docs/website/src/content/docs/reference/files.mdx). */
5
5
  var FileField = class FileField extends Field {
6
6
  /** Allowed types, checked against the type detected from the content: `['image/*', 'application/pdf']`. */
7
7
  accept(types) {
8
- return this.withFile({ accept: checkedAccept(types) });
8
+ const accept = checkedAccept(types);
9
+ return this.withFile({ accept: this.meta.file.capture ? checkedCaptureAccept(accept) : accept });
10
+ }
11
+ /**
12
+ * Opens the camera on phones and tablets instead of the file picker, the rear one unless `'user'` asks for the front,
13
+ * and takes only images: the image types of `.accept()`, or any image. Desktop browsers pick an image file.
14
+ */
15
+ capture(camera = "environment") {
16
+ return this.withFile({
17
+ capture: camera,
18
+ accept: capturedAccept(this.meta.file.accept)
19
+ });
9
20
  }
10
21
  /** The largest upload, as bytes or text such as `10 MB`; default 50 MB. */
11
22
  maxSize(size) {
@@ -70,6 +70,8 @@ export type FileOptions = {
70
70
  original?: ImageVariant;
71
71
  /** Whether a value may also be an https URL of a file elsewhere (`.externalUrls()`). */
72
72
  external?: true;
73
+ /** The camera a phone or tablet opens instead of its file picker, rear or front, as HTML's `capture` names them (`.capture()`). */
74
+ capture?: 'environment' | 'user';
73
75
  };
74
76
  /** Bytes from a number or text such as `10 MB` (decimal units) or `5 MiB` (binary units). */
75
77
  export declare const parseSize: (size: number | string) => number;
@@ -34,6 +34,17 @@ const checkedAccept = (types) => types.map((type) => {
34
34
  if (!typePattern.test(lower)) throw new Error(`"${type}" is not a media type or a type/* wildcard`);
35
35
  return lower;
36
36
  });
37
+ /** The types a field opening the camera keeps: the image types of `accept`, or every image when it names none. */
38
+ const capturedAccept = (accept) => {
39
+ const images = accept.filter((type) => type.startsWith("image/"));
40
+ return images.length > 0 ? images : ["image/*"];
41
+ };
42
+ /** The types of `.accept()` on a field that opens the camera, which takes only images. */
43
+ const checkedCaptureAccept = (types) => {
44
+ const other = types.find((type) => !type.startsWith("image/"));
45
+ if (other) throw new Error(`"${other}" is not an image type, and a field with .capture() takes only images`);
46
+ return [...types];
47
+ };
37
48
  const providerPattern = /^[a-z][a-z0-9-]{0,31}$/;
38
49
  const reservedProviders = /* @__PURE__ */ new Set([
39
50
  "http",
@@ -71,4 +82,4 @@ const checkedVariants = (variants) => Object.fromEntries(Object.entries(variants
71
82
  return [name, checkedVariant(`Image variant "${name}"`, variant)];
72
83
  }));
73
84
  //#endregion
74
- export { checkedAccept, checkedProvider, checkedVariant, checkedVariants, defaultMaxSize, imageFormats, parseSize };
85
+ export { capturedAccept, checkedAccept, checkedCaptureAccept, checkedProvider, checkedVariant, checkedVariants, defaultMaxSize, imageFormats, parseSize };
package/dist/model.d.ts CHANGED
@@ -102,6 +102,8 @@ export type FileFieldModel = {
102
102
  original?: ImageVariant;
103
103
  /** Whether a value may also be an https URL of a file elsewhere, drawn and linked as it is (`.externalUrls()`). */
104
104
  external?: true;
105
+ /** The camera a phone or tablet opens instead of its file picker: `environment` (rear) or `user` (front) (`.capture()`). */
106
+ capture?: 'environment' | 'user';
105
107
  };
106
108
  /** `digitsEnd`: only the end of the value's digits, spaces, `+` and dashes ignored (phone numbers). */
107
109
  export type SearchMatch = 'digitsEnd';
@@ -53,7 +53,8 @@ const fieldModel = (name, field) => {
53
53
  ...meta.file.sizeField !== void 0 && { sizeField: meta.file.sizeField },
54
54
  ...meta.file.variants && Object.keys(meta.file.variants).length > 0 && { variants: structuredClone(meta.file.variants) },
55
55
  ...meta.file.original && { original: { ...meta.file.original } },
56
- ...meta.file.external && { external: true }
56
+ ...meta.file.external && { external: true },
57
+ ...meta.file.capture && { capture: meta.file.capture }
57
58
  } },
58
59
  ...meta.ai && { ai: { prompt: meta.ai.prompt } },
59
60
  ...meta.rollup && { rollup: { ...meta.rollup } },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/schema",
3
- "version": "0.124.0",
3
+ "version": "0.124.2",
4
4
  "description": "Resource, field, view and access definitions for Protobase configs.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {