report-studio 0.0.1 → 1.0.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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +238 -4
  3. package/dist/designer.js +3209 -0
  4. package/dist/index.js +2 -4
  5. package/dist/viewer-D-c0lKzQ.js +1576 -0
  6. package/package.json +66 -33
  7. package/src/asset/images/icon.png +0 -0
  8. package/src/asset/images/logo.png +0 -0
  9. package/src/cli/index.js +282 -0
  10. package/src/cli/serve.js +135 -0
  11. package/src/designer/blank.js +53 -0
  12. package/src/designer/canvas.js +518 -0
  13. package/src/designer/designer.js +1900 -0
  14. package/src/designer/dialog.js +708 -0
  15. package/src/designer/fields.js +584 -0
  16. package/src/designer/geometry.js +154 -0
  17. package/src/designer/guides.js +370 -0
  18. package/src/designer/history.js +113 -0
  19. package/src/designer/icons.js +118 -0
  20. package/src/designer/index.html +28 -0
  21. package/src/designer/main.js +39 -0
  22. package/src/designer/menu.js +126 -0
  23. package/src/designer/panel.js +478 -0
  24. package/src/designer/preview.js +95 -0
  25. package/src/designer/rulers.js +231 -0
  26. package/src/designer/sample-data.js +240 -0
  27. package/src/designer/select.js +699 -0
  28. package/src/designer/structure.js +604 -0
  29. package/src/designer/styles.css +1616 -0
  30. package/src/designer/toast.js +62 -0
  31. package/src/designer/tokens.js +284 -0
  32. package/src/engine/csv.js +315 -0
  33. package/src/engine/group.js +304 -0
  34. package/src/engine/index.js +34 -0
  35. package/src/engine/measure.js +390 -0
  36. package/src/engine/paginate.js +861 -0
  37. package/src/engine/regions.js +175 -0
  38. package/src/engine/resolve.js +171 -0
  39. package/src/engine/search.js +129 -0
  40. package/src/engine/text-metrics.js +206 -0
  41. package/src/engine/validate.js +328 -0
  42. package/src/index.html +79 -0
  43. package/src/index.js +50 -0
  44. package/src/preview/chrome.css +505 -0
  45. package/src/preview/chrome.js +147 -0
  46. package/src/preview/index.html +28 -0
  47. package/src/preview/main.js +66 -0
  48. package/src/preview/styles.css +16 -0
  49. package/src/preview/viewer.js +745 -0
  50. package/src/render/items.js +425 -0
  51. package/src/render/render.js +98 -0
  52. package/src/render/report.css +202 -0
  53. package/src/server/routes.js +331 -0
  54. package/src/shared/brand.css +29 -0
  55. package/src/shared/dropdown.css +136 -0
  56. package/src/shared/dropdown.js +166 -0
  57. package/src/shared/modal.css +125 -0
  58. package/src/shared/prefix.js +13 -0
  59. package/src/shared/store.js +145 -0
  60. package/src/shared/theme.css +72 -0
  61. package/src/shared/window.js +138 -0
  62. package/src/vite/index.js +53 -0
  63. package/dist/react.js +0 -4
@@ -0,0 +1,145 @@
1
+ /**
2
+ * store.js is the browser's half of reading and writing report files.
3
+ *
4
+ * Four fetches against the four routes in src/server/routes.js. It knows
5
+ * nothing about who answers them - the CLI's http server during
6
+ * `npx report-studio design`, or the Vite plugin for a project already running
7
+ * Vite. That is the whole point of the seam: the designer talks to URLs, and
8
+ * swapping what serves them changes nothing here.
9
+ *
10
+ * Every failure is turned into an Error carrying what the server actually said,
11
+ * because "failed to fetch" in a toast is how a user concludes the tool is
12
+ * broken when the real answer was "there is no report called invoice".
13
+ */
14
+
15
+ import { DEFAULT_PREFIX } from './prefix.js';
16
+
17
+
18
+ /**
19
+ * @param {object} [options]
20
+ * @param {string} [options.prefix] where the routes are mounted
21
+ * @param {typeof fetch} [options.fetch] injectable, so the specs need no server
22
+ * @returns {object} list, load, save, remove
23
+ */
24
+ export function createStore({
25
+ prefix = DEFAULT_PREFIX,
26
+ fetch: doFetch = (...args) => globalThis.fetch(...args)
27
+ } = {}) {
28
+
29
+ async function call(path, init) {
30
+ let response;
31
+
32
+ try {
33
+ response = await doFetch(`${prefix}${path}`, init);
34
+ } catch (cause) {
35
+ /**
36
+ * The routes only exist while a dev server is running. Someone who
37
+ * opened the designer as a plain file, or built it into their own
38
+ * page, gets this - and it should say what to do about it.
39
+ */
40
+ throw new StoreError(
41
+ 'Cannot reach the report server. Start it with ' +
42
+ '`npx report-studio design`.',
43
+ { cause, offline: true }
44
+ );
45
+ }
46
+
47
+ const body = await response.json().catch(() => null);
48
+
49
+ if (!response.ok) {
50
+ throw new StoreError(
51
+ body?.error ?? `the server answered ${response.status}`,
52
+ { status: response.status }
53
+ );
54
+ }
55
+
56
+ return body;
57
+ }
58
+
59
+ return {
60
+ /** @returns {Promise<object[]>} `{ id, name, updatedAt, readable }` */
61
+ async list() {
62
+ const body = await call('', { method: 'GET' });
63
+ return Array.isArray(body?.reports) ? body.reports : [];
64
+ },
65
+
66
+ /** @returns {Promise<object>} the layout */
67
+ load(id) {
68
+ return call(`/${encodeURIComponent(id)}`, { method: 'GET' });
69
+ },
70
+
71
+ /** @returns {Promise<object>} `{ id, name, updatedAt }` */
72
+ save(id, layout) {
73
+ return call(`/${encodeURIComponent(id)}`, {
74
+ method: 'PUT',
75
+ headers: { 'Content-Type': 'application/json' },
76
+ body: JSON.stringify(layout)
77
+ });
78
+ },
79
+
80
+ remove(id) {
81
+ return call(`/${encodeURIComponent(id)}`, { method: 'DELETE' });
82
+ },
83
+
84
+ /**
85
+ * The report's data payload, kept beside it as `<id>.data.json`.
86
+ *
87
+ * @returns {Promise<object|null>} null when there is not one yet, which
88
+ * is an ordinary state for a report nobody has supplied data for -
89
+ * not a failure to report to the user
90
+ */
91
+ async loadData(id) {
92
+ try {
93
+ return await call(`/${encodeURIComponent(id)}/data`, { method: 'GET' });
94
+ } catch (error) {
95
+ if (error.status === 404) return null;
96
+ throw error;
97
+ }
98
+ },
99
+
100
+ saveData(id, data) {
101
+ return call(`/${encodeURIComponent(id)}/data`, {
102
+ method: 'PUT',
103
+ headers: { 'Content-Type': 'application/json' },
104
+ body: JSON.stringify(data, null, 2)
105
+ });
106
+ }
107
+ };
108
+ }
109
+
110
+
111
+ /** carries the server's own words, and whether there was a server at all */
112
+ export class StoreError extends Error {
113
+ constructor(message, { cause, status = null, offline = false } = {}) {
114
+ super(message, { cause });
115
+
116
+ this.name = 'StoreError';
117
+ this.status = status;
118
+ this.offline = offline;
119
+ }
120
+ }
121
+
122
+
123
+ /**
124
+ * The id a report name would be saved as.
125
+ *
126
+ * The filename is the id (see routes.js), and routes.js accepts letters,
127
+ * digits, dashes and underscores - so a name has to be reduced to that before
128
+ * it can become one. Done here rather than on the server so the designer can
129
+ * show what the file will be called before anything is written.
130
+ *
131
+ * @param {string} name
132
+ * @returns {string} a usable id, or '' when nothing usable is left
133
+ */
134
+ export function toId(name) {
135
+ const slug = String(name ?? '')
136
+ .trim()
137
+ .toLowerCase()
138
+ .replace(/[^a-z0-9]+/g, '-')
139
+ .replace(/^-+|-+$/g, '')
140
+ .slice(0, 64)
141
+ .replace(/-+$/, '');
142
+
143
+ /** routes.js requires the first character to be a letter or digit */
144
+ return /^[a-z0-9]/.test(slug) ? slug : '';
145
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * 60 - 30 - 10.
3
+ *
4
+ * 60% --surface #fdfdfe paper, toolbar, dialog: every calm surface
5
+ * 30% --accent #ff9c4b every action, highlight and selected state
6
+ * 10% --border #cdd4db borders and dividers, nothing else
7
+ *
8
+ * The backdrop is neutral rather than accent-tinted, so the accent has to earn
9
+ * its share through the Export button, search hits and selected cards instead
10
+ * of through one large wash. Tint --backdrop toward --accent to give it more.
11
+ *
12
+ * The three shades below are mixes of those, not new colours - each one exists
13
+ * because the base is too light to carry contrast on its own, and each says
14
+ * which ratio it buys. Change the three above and the shades stay in family.
15
+ *
16
+ * The product's two marks come in with it, from brand.css: they are drawn from
17
+ * this palette, so whoever takes the palette should get them as well.
18
+ */
19
+ @import "./brand.css";
20
+
21
+ :root {
22
+ /* the palette */
23
+ --surface: #fdfdfe;
24
+ --accent: #ff9c4b;
25
+ --border: #cdd4db;
26
+
27
+ /* the ground the page sits on - a shade cooler than the paper, so the sheet
28
+ is told apart by its border rather than by a change in temperature */
29
+ --backdrop: #fbfcfd;
30
+ /* accent at 12% over surface - selected rows and cards */
31
+ --accent-soft: #fdf1e9;
32
+ /* accent at 70% over surface - a search hit you can see but are not standing on.
33
+ 1.66:1 against the paper, where the yellow it replaces managed 1.23:1 */
34
+ --accent-tint: #feb981;
35
+ /* accent darkened - 3.5:1 on surface, enough for focus rings and active borders */
36
+ --accent-strong: #cc6f20;
37
+ /* darker still - 5.8:1 both ways, so it can carry text or sit under it */
38
+ --accent-ink: #8f572a;
39
+ /* border at 25% over surface - table headers and other quiet fills */
40
+ --surface-muted: #f1f3f5;
41
+
42
+ /* the one destructive action in the designer, and the tint behind it */
43
+ --danger: #b3261e;
44
+ --danger-soft: #fdecec;
45
+
46
+ /* ink. #ff9c4b is light, so text on the accent is dark, never white: 7.25:1 */
47
+ --ink: #22272c;
48
+ --ink-soft: #667080;
49
+
50
+ --bar-height: 56px;
51
+ --pager-height: 44px;
52
+ /* how far the floating pager sits off the bottom edge */
53
+ --pager-offset: 20px;
54
+ }
55
+
56
+ * {
57
+ box-sizing: border-box;
58
+ margin: 0;
59
+ padding: 0;
60
+ }
61
+
62
+ body {
63
+ font-family: "Courier Prime", monospace;
64
+ font-weight: 400;
65
+ font-style: normal;
66
+ }
67
+
68
+ /*
69
+ * Lifted out of the preview's stylesheet so the designer inherits exactly the
70
+ * same palette. A second copy of these values is a second thing to keep in
71
+ * step, and the one that falls behind is always the one nobody is looking at.
72
+ */
@@ -0,0 +1,138 @@
1
+ /**
2
+ * window.js opens a screen in a window of its own.
3
+ *
4
+ * A designer or a viewer normally takes over an element on the host's page.
5
+ * Sometimes the host would rather it were somewhere else entirely - a second
6
+ * monitor, beside the application rather than inside it - and a report is a
7
+ * page-shaped thing that a browser tab suits better than a panel.
8
+ *
9
+ * Opened blank and built here rather than navigated to a URL: the package is
10
+ * usable with no server at all, and a report being designed has not been saved
11
+ * anywhere a URL could reach. What the new window needs is the host's own
12
+ * stylesheets, which are copied across - so the screen looks the same in its
13
+ * own window as it did in the page, without the host linking anything twice.
14
+ */
15
+
16
+
17
+ /** big enough for an A4 page and the chrome round it, on a laptop screen */
18
+ const DEFAULT_WIDTH = 1280;
19
+ const DEFAULT_HEIGHT = 900;
20
+
21
+
22
+ /**
23
+ * Copies the opener's stylesheets into a new document.
24
+ *
25
+ * Both kinds, because a host may do either: a `<link>` to a file, or a `<style>`
26
+ * a bundler inlined. Links are copied by href rather than cloned wholesale, so
27
+ * the new document fetches them itself and the browser's cache does the work.
28
+ *
29
+ * A cross-origin sheet cannot be read, only linked - which is exactly what
30
+ * happens here, so nothing is lost to it.
31
+ *
32
+ * @param {Document} from
33
+ * @param {Document} to
34
+ */
35
+ function copyStyles(from, to) {
36
+ for (const node of from.querySelectorAll('link[rel="stylesheet"], style')) {
37
+ if (node.tagName === 'LINK') {
38
+ const link = to.createElement('link');
39
+
40
+ link.rel = 'stylesheet';
41
+ link.href = node.href;
42
+ to.head.appendChild(link);
43
+ continue;
44
+ }
45
+
46
+ const style = to.createElement('style');
47
+
48
+ style.textContent = node.textContent;
49
+ to.head.appendChild(style);
50
+ }
51
+ }
52
+
53
+
54
+ /**
55
+ * Opens a blank window and prepares it to be mounted into.
56
+ *
57
+ * Must be called from something the user did - a click, a key - or the browser
58
+ * treats it as a popup and blocks it. A blocked window is null rather than an
59
+ * exception, because a host that cannot open one usually wants to fall back to
60
+ * mounting in the page rather than to fail.
61
+ *
62
+ * @param {object} [options]
63
+ * @param {string} [options.title] the window's title, and its taskbar name
64
+ * @param {number} [options.width]
65
+ * @param {number} [options.height]
66
+ * @param {string} [options.features] passed to window.open as given, for a host
67
+ * that wants something other than a plain resizable window
68
+ * @param {Window} [options.opener] whose stylesheets to copy; this one by default
69
+ * @returns {{window: Window, document: Document, mount: Element, close: Function}|null}
70
+ * null when the browser refused to open it
71
+ */
72
+ export function openWindow({
73
+ title = 'Report Studio',
74
+ width = DEFAULT_WIDTH,
75
+ height = DEFAULT_HEIGHT,
76
+ features,
77
+ opener = globalThis
78
+ } = {}) {
79
+ const spec = features
80
+ ?? `width=${width},height=${height},resizable=yes,scrollbars=yes`;
81
+
82
+ const win = opener.open?.('', '_blank', spec);
83
+
84
+ /** blocked, or opened into something that is not a window we can build in */
85
+ if (!win || !win.document) return null;
86
+
87
+ const doc = win.document;
88
+
89
+ doc.open();
90
+ doc.write('<!doctype html><html><head><meta charset="utf-8">' +
91
+ '</head><body></body></html>');
92
+ doc.close();
93
+
94
+ doc.title = title;
95
+ copyStyles(opener.document, doc);
96
+
97
+ /**
98
+ * The screens take over an element and build the rest inside it, so what
99
+ * they are given is one empty div - the same contract as mounting in a page.
100
+ */
101
+ const mount = doc.createElement('div');
102
+
103
+ mount.id = 'report-studio';
104
+ mount.style.height = '100vh';
105
+ doc.body.style.margin = '0';
106
+ doc.body.appendChild(mount);
107
+
108
+ return {
109
+ window: win,
110
+ document: doc,
111
+ mount,
112
+ close: () => win.close()
113
+ };
114
+ }
115
+
116
+
117
+ /**
118
+ * Closes the window when the page that opened it goes.
119
+ *
120
+ * A window left behind by a navigated-away opener is one nobody can reach the
121
+ * report data for: what it is showing was passed in memory, and the page that
122
+ * held it has gone. Better to take it with us than to leave a dead screen open.
123
+ *
124
+ * @param {Window} opener
125
+ * @param {Window} win
126
+ * @param {Function} [detach] the screen's own teardown
127
+ * @returns {Function} stops watching, without closing anything
128
+ */
129
+ export function closeWithOpener(opener, win, detach = () => { }) {
130
+ const onUnload = () => {
131
+ detach();
132
+ win.close();
133
+ };
134
+
135
+ opener.addEventListener('pagehide', onUnload);
136
+
137
+ return () => opener.removeEventListener('pagehide', onUnload);
138
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The Vite plugin: report file access for a project that already runs Vite.
3
+ *
4
+ * A convenience, not the product. The CLI is the primary way in, because it
5
+ * works for a plain-JS project, a webpack one, or anything else - requiring a
6
+ * build tool the consumer does not otherwise use would be a poor first
7
+ * impression for a package whose engine needs no bundler at all.
8
+ *
9
+ * This is Node code and must never reach a browser bundle, which is why it
10
+ * lives behind its own export path (`report-studio/vite`). Nothing in src/
11
+ * outside this folder and src/server/ imports it.
12
+ */
13
+
14
+ import { createReportRoutes, DEFAULT_PREFIX } from '../server/routes.js';
15
+
16
+
17
+ /**
18
+ * @param {object} [options]
19
+ * @param {string} [options.dir] where report files live, relative to the project
20
+ * @param {string} [options.prefix] the URL the routes are served under
21
+ * @returns {object} a Vite plugin
22
+ */
23
+ export function reportStudio({ dir = 'reports', prefix = DEFAULT_PREFIX } = {}) {
24
+ return {
25
+ name: 'report-studio',
26
+
27
+ /**
28
+ * `configureServer` and not `configurePreviewServer`.
29
+ *
30
+ * The first runs only for `vite` (dev). The second runs for `vite
31
+ * preview`, which serves a production build - mounting a route that
32
+ * writes files there would put a write endpoint in front of built
33
+ * output, which is a hole rather than a feature.
34
+ */
35
+ configureServer(server) {
36
+ const handle = createReportRoutes({ dir, prefix });
37
+
38
+ /**
39
+ * Before Vite's own middleware, so the prefix cannot be shadowed by
40
+ * a file that happens to sit at the same path. The handler calls
41
+ * next() for anything outside the prefix, so nothing else changes.
42
+ */
43
+ server.middlewares.use(handle);
44
+
45
+ server.config.logger.info(
46
+ ` report-studio reports in ${dir}, served at ${prefix}`
47
+ );
48
+ }
49
+ };
50
+ }
51
+
52
+
53
+ export default reportStudio;
package/dist/react.js DELETED
@@ -1,4 +0,0 @@
1
- //#region src/react/index.js
2
- var e = "report-studio-react-0.0.1";
3
- //#endregion
4
- export { e as version };