@modelform/react-native 0.1.1 → 0.3.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.
@@ -1,11 +1,12 @@
1
1
  /**
2
- * The layout editor's model. modelform lays panels out in reading order, packed into lines by
3
- * width (see layout.ts), so editing a layout is editing an order plus sizes: moving a panel
4
- * changes where it falls in the order, and every gridPos x/y is then rewritten from that
5
- * order so the file (and Grafana) agree with what the app shows.
2
+ * The layout editor's model. Wide screens show panels where Grafana does: at their gridPos,
3
+ * floated up (see compactGrid in layout.ts). So editing the grid is placing and sizing panels,
4
+ * and after every change gridPos is rewritten to the floated-up layout, so the file (and
5
+ * Grafana) say what the app shows. Phones show panels in reading order, packed into lines;
6
+ * moving a panel there moves it in that order, which is still a place on the grid.
6
7
  *
7
8
  * Panels are identified by their index in `dashboard.panels`, which edits keep stable (apart
8
- * from removing one), so the JSON text can be patched in place.
9
+ * from removing some), so the JSON text can be patched in place.
9
10
  */
10
11
  import { type JsonEdit } from './jsonEdit';
11
12
  import { type LayoutMode } from './layout';
@@ -13,8 +14,11 @@ import type { Panel } from './schema';
13
14
  /** Top-level panel indices (rows included) in the order the app shows them; ties keep file order. */
14
15
  export declare function readingOrder(panels: Panel[]): number[];
15
16
  /**
16
- * Rewrites gridPos x/y so that sorting by position gives `order`, packing each line by width
17
- * the way the app does. Sizes are kept. Returns new panels; untouched ones are the same objects.
17
+ * Rewrites gridPos x/y so that sorting by position gives `order`, packing each line by width.
18
+ * Sizes are kept. Returns new panels; untouched ones are the same objects.
19
+ *
20
+ * @deprecated Wide screens now show panels where their gridPos puts them, so packing into
21
+ * lines throws placement away. Use `settleLayout` to tidy a layout, `placePanel` to move one.
18
22
  */
19
23
  export declare function repack(panels: Panel[], order: number[]): Panel[];
20
24
  export interface CanvasItem {
@@ -39,11 +43,17 @@ export interface Canvas {
39
43
  }
40
44
  export declare const CANVAS_GAP = 8;
41
45
  export declare const CANVAS_ROW_HEADER = 32;
42
- /** A size to show for one panel while it's being resized, in the units of the current mode. */
46
+ /**
47
+ * One panel shown as it's being changed: a size while it's resized, in the units of the current
48
+ * mode, or (on the grid) a place while it's dragged. The other panels make way as they will.
49
+ */
43
50
  export interface SizeOverride {
44
51
  index: number;
45
- span: number;
46
- h: number;
52
+ span?: number;
53
+ h?: number;
54
+ /** Grid only: column and line of the top left. */
55
+ x?: number;
56
+ y?: number;
47
57
  }
48
58
  /** Sizes in the current mode: grid uses gridPos; phone uses `mobile`, falling back like the app does. */
49
59
  export declare function panelSize(panel: Panel, mode: LayoutMode): {
@@ -58,30 +68,63 @@ export declare function layoutCanvas(panels: Panel[], order: number[], mode: Lay
58
68
  * the pointer is over (left/right for panels, top/bottom for rows).
59
69
  */
60
70
  export declare function dropTarget(canvas: Canvas, x: number, y: number, dragging: number): number | null;
61
- /** Moves `index` to just before `before` (or to the end) and repacks. */
71
+ /**
72
+ * Moves `index` to just before `before` in reading order (or to the end), for phones, where
73
+ * order is all there is: it takes `before`'s place on the grid, which moves down to make way.
74
+ * The grid stays the source of truth, so panels beside the moved one can float up past
75
+ * `before` too; the moved panel always lands right before it.
76
+ */
62
77
  export declare function movePanel(panels: Panel[], index: number, before: number | null): Panel[];
63
- /** Resizes in the units of `mode`: gridPos w/h on the grid, mobile span/h on phones. */
78
+ /** Resizes in the units of `mode`: gridPos w/h on the grid (panels in the way move down), mobile span/h on phones. */
64
79
  export declare function resizePanel(panels: Panel[], index: number, mode: LayoutMode, span: number, h: number): Panel[];
65
80
  /** Shows or hides a panel on phones. */
66
81
  export declare function setHiddenOnPhones(panels: Panel[], index: number, hidden: boolean): Panel[];
67
82
  /** One more than the largest panel id anywhere, collapsed rows included. */
68
83
  export declare function nextPanelId(panels: Panel[]): number;
69
- /** Adds a panel after `after` (or at the end), with a fresh id. Returns the panels and the new one's index. */
84
+ /**
85
+ * Adds a panel with a fresh id: beside `after` if it fits on the same line, else under it, or
86
+ * at the bottom without one. Panels in the way move down. Returns the panels and the new one's index.
87
+ */
70
88
  export declare function insertPanel(panels: Panel[], panel: Panel, after: number | null): {
71
89
  panels: Panel[];
72
90
  index: number;
73
91
  };
74
- /** A copy of a panel right after it. */
92
+ /** A copy of a panel beside or under it. */
75
93
  export declare function duplicatePanel(panels: Panel[], index: number): {
76
94
  panels: Panel[];
77
95
  index: number;
78
96
  };
79
- /** Removes a panel. A collapsed row takes the panels inside it along. */
97
+ /** Removes a panel; the panels below float up. A collapsed row takes the panels inside it along. */
80
98
  export declare function removePanel(panels: Panel[], index: number): Panel[];
81
- /** Expands a collapsed row: its panels move out to the top level, right after it. */
99
+ /**
100
+ * Expands a collapsed row: its panels move out to the top level, under it, as they were laid
101
+ * out inside it, and everything below moves down to make room. They're added at the end of
102
+ * the panels array so the rest of the file stays as it is.
103
+ */
82
104
  export declare function expandRow(panels: Panel[], index: number): Panel[];
105
+ /**
106
+ * Collapses a row: the panels under it, down to the next row, move inside it, and the panels
107
+ * below float up. Returns the panels and the indices that left the top level, for panelEdits.
108
+ */
109
+ export declare function collapseRow(panels: Panel[], index: number): {
110
+ panels: Panel[];
111
+ removed: number[];
112
+ };
113
+ /**
114
+ * Floats every panel up the way the app shows them, so the file says what the app shows. With
115
+ * `fixed`, that panel keeps its place (just moved or resized) and any it now covers move down
116
+ * first. Panels already in place are left as the same objects.
117
+ */
118
+ export declare function settleLayout(panels: Panel[], fixed?: number): Panel[];
119
+ /** Puts a panel with its top left at column x, line y; panels in the way move down, then everything floats up. */
120
+ export declare function placePanel(panels: Panel[], index: number, x: number, y: number): Panel[];
121
+ /** The grid cell a panel `w` columns wide would take with its top left at pixel (x, y) on a canvas `width` wide. */
122
+ export declare function snapToGrid(width: number, x: number, y: number, w: number): {
123
+ x: number;
124
+ y: number;
125
+ };
83
126
  /**
84
127
  * JSON edits that turn `before` into `after` (panel arrays from the ops above), with
85
128
  * `panels` patched element by element so the file keeps its formatting.
86
129
  */
87
- export declare function panelEdits(before: Panel[], after: Panel[], removed?: number): JsonEdit[];
130
+ export declare function panelEdits(before: Panel[], after: Panel[], removed?: number | number[]): JsonEdit[];
@@ -96,10 +96,23 @@ export declare function validateBundle(bundle: ConfigBundle, file?: string): Con
96
96
  * React Native's URL polyfill doesn't support relative resolution.
97
97
  */
98
98
  export declare function resolveUrl(base: string, rel: string): string;
99
- /** `accept` replaces the default Accept header, e.g. to ask for a bundle. */
99
+ /** `accept` replaces the default Accept header, e.g. to ask for a bundle. `signal` cancels the request. */
100
100
  export type FetchJson = (url: string, options?: {
101
101
  accept?: string;
102
+ signal?: AbortSignal;
102
103
  }) => Promise<unknown>;
104
+ export interface HttpFetchJsonOptions {
105
+ /** Extra request headers, e.g. Authorization. */
106
+ headers?: () => Promise<Record<string, string>> | Record<string, string>;
107
+ /** The error message for a failed response. Defaults to `HTTP <status>`. */
108
+ httpError?: (status: number) => string;
109
+ }
110
+ /**
111
+ * A FetchJson over fetch(). It remembers each response's ETag and sends it back as
112
+ * If-None-Match, so a file that hasn't changed comes back as a 304 and is answered from
113
+ * memory. On the web that only happens where the server exposes ETag to scripts.
114
+ */
115
+ export declare function httpFetchJson(options?: HttpFetchJsonOptions): FetchJson;
103
116
  export declare const defaultFetchJson: FetchJson;
104
117
  export interface LoadResult {
105
118
  /** Present when there are no errors. */
@@ -107,6 +120,8 @@ export interface LoadResult {
107
120
  issues: ConfigIssue[];
108
121
  /** The config's revision, when the server sent it as one bundle. */
109
122
  revision?: string;
123
+ /** True when the manifest couldn't be fetched at all (no network, an HTTP error, a timeout), as opposed to a config with errors. */
124
+ unreachable?: boolean;
110
125
  }
111
126
  /** Asked for on the manifest's URL, a server that can returns the whole config in one response. */
112
127
  export declare const BUNDLE_TYPE = "application/vnd.modelform.bundle+json";
@@ -133,7 +148,7 @@ export declare function bundleRoot(manifestUrl: string, manifestPath: string): s
133
148
  * Builds a bundle from a server's bundle response. Documents inside the config come from the
134
149
  * response; one elsewhere (an absolute URL) is still fetched.
135
150
  */
136
- export declare function assembleFromResponse(url: string, response: BundleResponse, fetchJson?: FetchJson): Promise<LoadResult>;
151
+ export declare function assembleFromResponse(url: string, response: BundleResponse, fetchJson?: FetchJson, signal?: AbortSignal): Promise<LoadResult>;
137
152
  /**
138
153
  * Builds a bundle from a parsed manifest, getting each path document with `load`.
139
154
  * Never throws: failures to fetch or parse come back as issues.
@@ -143,7 +158,13 @@ export declare function assembleBundle(manifest: unknown, load: (path: string) =
143
158
  * Fetches a config: in one response from a server that bundles (all of it from the same
144
159
  * moment), or else the manifest and then each document it points to, resolved against its URL.
145
160
  */
146
- export declare function fetchBundle(url: string, fetchJson?: FetchJson): Promise<LoadResult>;
161
+ export declare function fetchBundle(url: string, fetchJson?: FetchJson, signal?: AbortSignal): Promise<LoadResult>;
162
+ /**
163
+ * Checks a bundle that was assembled earlier (a cached copy, say) as if it had just been
164
+ * fetched, since the checks may have changed since. Returns it readied for rendering, or
165
+ * nothing if it no longer loads.
166
+ */
167
+ export declare function revalidateBundle(bundle: ConfigBundle): ConfigBundle | undefined;
147
168
  /** Where the last good config is kept between launches. */
148
169
  export interface ConfigCache {
149
170
  read(): Promise<CachedConfig | null>;
@@ -164,8 +185,14 @@ export interface ConfigState {
164
185
  /** The shown config's revision, when the server gives one. */
165
186
  revision?: string;
166
187
  loading: boolean;
167
- /** A summary ("2 errors") when the last fetch failed; the bundle shown is then the last good one. See `issues`. */
188
+ /**
189
+ * Set when the last fetch failed; the bundle shown is then the last good one. See `issues`.
190
+ * A summary ("2 errors") for a config with errors, or why the server couldn't be reached
191
+ * ("HTTP 503", "no answer in 15s") when `unreachable`.
192
+ */
168
193
  error?: string;
194
+ /** True when the last fetch failed because the config couldn't be fetched at all, not because it has errors. */
195
+ unreachable?: boolean;
169
196
  /** From the last fetch, good or bad. */
170
197
  issues: ConfigIssue[];
171
198
  }
@@ -176,11 +203,14 @@ export interface ConfigLoaderOptions {
176
203
  /** Shown until something better loads, e.g. a config bundled with the app. */
177
204
  fallback?: ConfigBundle;
178
205
  fetchJson?: FetchJson;
206
+ /** How long a fetch may take before it's abandoned. Defaults to 15 seconds. */
207
+ timeoutMs?: number;
179
208
  }
180
209
  /**
181
210
  * Keeps the app's config current. Shows the bundled fallback, then the cached copy,
182
211
  * then each newer fetch that validates. A bad fetch never replaces a good config: it's
183
- * reported in `error`/`issues` and the last good bundle stays.
212
+ * reported in `error`/`issues` and the last good bundle stays. Call `dispose` when done
213
+ * with a loader, so a fetch still in flight can't write the cache afterwards.
184
214
  */
185
215
  export declare class ConfigLoader {
186
216
  private options;
@@ -188,14 +218,18 @@ export declare class ConfigLoader {
188
218
  private snapshot;
189
219
  private lastJson?;
190
220
  private inflight?;
221
+ private controller?;
222
+ private disposed;
191
223
  constructor(options: ConfigLoaderOptions);
192
224
  get state(): ConfigState;
193
225
  subscribe: (listener: () => void) => () => void;
194
226
  getSnapshot: () => ConfigState;
195
- /** Loads the cached copy (if it was fetched from the same URL), then fetches. */
227
+ /** Loads the cached copy (if it was fetched from the same URL and still checks out), then fetches. */
196
228
  start(): Promise<void>;
197
229
  /** Fetches now. Concurrent calls share one fetch. Only notifies listeners when something changed. */
198
230
  refresh(): Promise<void>;
231
+ /** Stops the loader: cancels a fetch in flight, and nothing it does afterwards changes the state or the cache. */
232
+ dispose(): void;
199
233
  /** `notify: false` drops the patch, so the snapshot only changes when listeners hear about it. */
200
234
  private set;
201
235
  }