@thatopen/services 0.35.1 → 0.36.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 (135) hide show
  1. package/README.md +66 -66
  2. package/dist/built-in/index.d.ts +13 -13
  3. package/dist/cli/lib/browser-session.d.ts +17 -0
  4. package/dist/cli/lib/config.d.ts +6 -0
  5. package/dist/cli.js +334 -38
  6. package/dist/cli.js.map +1 -1
  7. package/dist/core/browser-sign-in.d.ts +50 -0
  8. package/dist/core/browser-sign-in.test.d.ts +1 -0
  9. package/dist/core/builtin-recipes/index.d.ts +2 -2
  10. package/dist/core/builtin-recipes/layer-to-native.d.ts +19 -0
  11. package/dist/core/builtin-recipes/records-to-native.d.ts +43 -0
  12. package/dist/core/builtin-recipes/shapes-to-families.d.ts +9 -4
  13. package/dist/core/builtin-recipes/shared.d.ts +8 -11
  14. package/dist/core/channel.d.ts +1 -1
  15. package/dist/core/client.d.ts +46 -2
  16. package/dist/core/frag-items.d.ts +1 -1
  17. package/dist/core/history.d.ts +9 -9
  18. package/dist/core/op-builder.d.ts +2 -2
  19. package/dist/core/platform-client.d.ts +6 -3
  20. package/dist/core/project-recipes.d.ts +1 -1
  21. package/dist/core/recipes.d.ts +31 -5
  22. package/dist/index.cjs.js +23 -20
  23. package/dist/index.cjs.js.map +1 -1
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.es.js +4284 -3455
  26. package/dist/index.es.js.map +1 -1
  27. package/dist/vite-env.d.ts +1 -1
  28. package/docs/ai-quickstart.md +548 -548
  29. package/docs/builtin/paths.json +130 -130
  30. package/docs/cli/create.md +29 -29
  31. package/docs/cli/local-server.md +38 -38
  32. package/docs/cli/login.md +35 -32
  33. package/docs/cli/mcp.md +46 -46
  34. package/docs/cli/publish.md +31 -31
  35. package/docs/cli/run.md +26 -26
  36. package/docs/cli/serve.md +43 -43
  37. package/docs/cli/swap.md +30 -30
  38. package/docs/client/paths.json +2 -2
  39. package/docs/cloud/GaussianSplatConverter.md +43 -43
  40. package/docs/cloud/PointCloudConverter.md +53 -53
  41. package/docs/flow-architecture.md +393 -393
  42. package/docs/flow-plugin-guide.md +354 -354
  43. package/docs/platform-token.md +81 -81
  44. package/docs/rate-limits.md +133 -133
  45. package/docs/recipes-guide.md +243 -243
  46. package/docs/revit-collab-quickstart.md +445 -445
  47. package/docs/rhino-interop-quickstart.md +387 -387
  48. package/docs/sheet-sets-guide.md +69 -69
  49. package/package.json +1 -1
  50. package/resources/AGENTS.md +57 -57
  51. package/src/built-in/CDEManager/example.ts +214 -214
  52. package/src/built-in/CDEManager/examples/FileEditing/example.ts +226 -226
  53. package/src/built-in/CDEManager/examples/FileMoves/example.ts +186 -186
  54. package/src/built-in/CDEManager/examples/FileOperations/example.ts +295 -295
  55. package/src/built-in/CDEManager/examples/FileTransfers/example.ts +210 -210
  56. package/src/built-in/CDEManager/examples/FolderManagement/example.ts +230 -230
  57. package/src/built-in/CDEManager/examples/MetadataSchema/example.ts +230 -230
  58. package/src/built-in/CDEManager/examples/MetadataViews/example.ts +166 -166
  59. package/src/built-in/CDEManager/examples/ProjectConfig/example.ts +177 -177
  60. package/src/built-in/ClashesManager/example.ts +283 -283
  61. package/src/built-in/GISManager/example.ts +355 -355
  62. package/src/built-in/ProjectManager/example.ts +124 -124
  63. package/src/built-in/UIManager/src/app/example.ts +97 -97
  64. package/src/built-in/UIManager/src/app/examples/AppState/example.ts +140 -140
  65. package/src/built-in/UIManager/src/app/examples/Banner/example.ts +159 -159
  66. package/src/built-in/UIManager/src/app/examples/Contexts/example.ts +123 -123
  67. package/src/built-in/UIManager/src/app/examples/FileDrop/example.ts +124 -124
  68. package/src/built-in/UIManager/src/app/examples/FloatingLayout/example.ts +90 -90
  69. package/src/built-in/UIManager/src/app/examples/Modal/example.ts +163 -163
  70. package/src/built-in/UIManager/src/app/examples/Notifications/example.ts +133 -133
  71. package/src/built-in/UIManager/src/app/examples/ProjectFileStorage/example.ts +150 -150
  72. package/src/built-in/UIManager/src/app/examples/Sidebar/example.ts +105 -105
  73. package/src/built-in/UIManager/src/clashes-panel/example.ts +183 -183
  74. package/src/built-in/UIManager/src/gis-panel/example.ts +163 -163
  75. package/src/built-in/UIManager/src/gis-properties-panel/example.ts +168 -168
  76. package/src/built-in/UIManager/src/git-history-panel/example.ts +70 -70
  77. package/src/built-in/UIManager/src/map-panel/example.ts +189 -189
  78. package/src/built-in/UIManager/src/models-list/example.ts +181 -181
  79. package/src/built-in/UIManager/src/settings-panel/example.ts +303 -303
  80. package/src/built-in/UIManager/src/topics-panel/example.ts +170 -170
  81. package/src/built-in/UIManager/src/viewer/example.ts +182 -182
  82. package/src/built-in/UIManager/src/viewer-toolbar/example.ts +376 -376
  83. package/src/built-in/index.ts +2995 -2995
  84. package/src/cli/templates/app/README.md +250 -250
  85. package/src/cli/templates/app/index.html +35 -35
  86. package/src/cli/templates/app/package.json +32 -32
  87. package/src/cli/templates/app/src/app.ts +37 -37
  88. package/src/cli/templates/app/src/assets/tool-placeholder.ts +50 -50
  89. package/src/cli/templates/app/src/bim-components/CloudRunner/index.ts +95 -95
  90. package/src/cli/templates/app/src/bim-components/CloudRunner/src/index.ts +1 -1
  91. package/src/cli/templates/app/src/bim-components/CloudRunner/src/types.ts +5 -5
  92. package/src/cli/templates/app/src/bim-components/index.ts +1 -1
  93. package/src/cli/templates/app/src/globals.ts +1 -1
  94. package/src/cli/templates/app/src/main.history.ts +191 -191
  95. package/src/cli/templates/app/src/main.ts +226 -226
  96. package/src/cli/templates/app/src/setups/channel.ts +76 -76
  97. package/src/cli/templates/app/src/setups/reality-capture/lib/hidden-tiles-plugin.ts +88 -88
  98. package/src/cli/templates/app/src/setups/reality-capture/lib/point-tile-plugin.ts +166 -166
  99. package/src/cli/templates/app/src/setups/reality-capture/lib/render-frame.ts +31 -31
  100. package/src/cli/templates/app/src/setups/reality-capture/lib/splat-tile-plugin.ts +51 -51
  101. package/src/cli/templates/app/src/setups/reality-capture-viewer.ts +1383 -1383
  102. package/src/cli/templates/app/src/ui-components/app-info-section/index.ts +26 -26
  103. package/src/cli/templates/app/src/ui-components/app-info-section/src/index.ts +1 -1
  104. package/src/cli/templates/app/src/ui-components/app-info-section/src/types.ts +15 -15
  105. package/src/cli/templates/app/src/ui-components/cloud-runner-section/index.ts +37 -37
  106. package/src/cli/templates/app/src/ui-components/cloud-runner-section/src/index.ts +1 -1
  107. package/src/cli/templates/app/src/ui-components/cloud-runner-section/src/types.ts +14 -14
  108. package/src/cli/templates/app/src/ui-components/index.ts +2 -2
  109. package/src/cli/templates/app/tsconfig.json +16 -16
  110. package/src/cli/templates/app/vite.config.js +57 -57
  111. package/src/cli/templates/cloud-component/README.md +205 -205
  112. package/src/cli/templates/cloud-component/_thatopen +5 -5
  113. package/src/cli/templates/cloud-component/declarations.json +4 -4
  114. package/src/cli/templates/cloud-component/package.json +21 -21
  115. package/src/cli/templates/cloud-component/src/main.ts +70 -70
  116. package/src/cli/templates/cloud-component/tsconfig.json +16 -16
  117. package/src/cli/templates/cloud-component/vite.config.js +46 -46
  118. package/src/cli/templates/shared/AGENTS.md +8 -8
  119. package/src/cli/templates/shared/CLAUDE.md +1 -1
  120. package/src/cli/templates/shared/_gitignore +5 -5
  121. package/src/core/examples/.env.example +7 -7
  122. package/src/core/examples/apps.ts +67 -67
  123. package/src/core/examples/batch-reads.ts +99 -99
  124. package/src/core/examples/channel.ts +65 -65
  125. package/src/core/examples/components.ts +91 -91
  126. package/src/core/examples/execution.ts +75 -75
  127. package/src/core/examples/file-metadata.ts +94 -94
  128. package/src/core/examples/files.ts +81 -81
  129. package/src/core/examples/folders.ts +82 -82
  130. package/src/core/examples/hidden-files.ts +88 -88
  131. package/src/core/examples/history.ts +103 -103
  132. package/src/core/examples/icons.ts +68 -68
  133. package/src/core/examples/permissions.ts +87 -87
  134. package/dist/core/builtin-recipes/records-to-revit.d.ts +0 -28
  135. package/dist/core/builtin-recipes/rhino-to-revit.d.ts +0 -16
@@ -1,133 +1,133 @@
1
- /* MD
2
- ## Toasts and cross-component notifications
3
- ---
4
- Developers building BIM processing UIs need to surface status updates, errors,
5
- and async feedback to users from different points in the component tree —
6
- but reaching back to the app root to trigger a notification from a deeply
7
- nested component requires threading a reference through every layer in between.
8
-
9
- top-app provides a built-in toast system that any descendant can trigger by
10
- dispatching a standard DOM event. Toasts are auto-dismissed after a type-based
11
- duration and stack at the bottom-left of the screen.
12
-
13
- This tutorial covers showing a toast on app boot via showToast; building a
14
- component that dispatches top:notification to report async progress and
15
- completion without a reference to the root; and using the four toast types
16
- (success, error, warning, info) with their respective durations.
17
-
18
- By the end, you'll have an app with a boot confirmation toast and a panel that
19
- reports async work status without holding any reference to top-app.
20
- */
21
-
22
- import { html, LitElement } from "lit";
23
- import { customElement, state } from "lit/decorators.js";
24
- import * as THREE from "three";
25
- import * as OBC from "@thatopen/components";
26
- import * as OBF from "@thatopen/components-front";
27
- import * as FRAGS from "@thatopen/fragments";
28
- import * as BUI from "@thatopen/ui";
29
- import { PlatformClient, UIManager } from "@thatopen/services";
30
- import type { App, AppReadyEvent } from "../../index";
31
-
32
- // A child component that dispatches top:notification to surface messages
33
- // to the nearest top-app ancestor — no reference to the app element needed.
34
- @customElement("status-reporter")
35
- class StatusReporter extends LitElement {
36
- @state() private _busy = false;
37
-
38
- private _simulateWork() {
39
- this._busy = true;
40
-
41
- // Dispatching top:notification from any descendant of top-app shows a toast.
42
- // bubbles:true + composed:true lets it cross shadow-DOM boundaries.
43
- this.dispatchEvent(
44
- new CustomEvent("top:notification", {
45
- bubbles: true,
46
- composed: true,
47
- detail: { message: "Analysis started", type: "info" },
48
- }),
49
- );
50
-
51
- setTimeout(() => {
52
- this._busy = false;
53
- this.dispatchEvent(
54
- new CustomEvent("top:notification", {
55
- bubbles: true,
56
- composed: true,
57
- detail: {
58
- message: "Analysis complete",
59
- type: "success",
60
- description: "Found 342 elements across 5 categories.",
61
- },
62
- }),
63
- );
64
- }, 2000);
65
- }
66
-
67
- render() {
68
- return html`
69
- <bim-panel label="Reporter">
70
- <bim-panel-section label="Actions" icon="solar:play-bold">
71
- <bim-button
72
- label="Run Analysis"
73
- icon="solar:play-bold"
74
- .loading=${this._busy}
75
- @click=${this._simulateWork}
76
- ></bim-button>
77
- </bim-panel-section>
78
- </bim-panel>
79
- `;
80
- }
81
- }
82
-
83
- // ---- app wiring ----
84
-
85
- const client = PlatformClient.fromPlatformContext();
86
- const { components } = (await client.setup(
87
- { OBC, OBF, BUI, THREE, FRAGS },
88
- { uuid: UIManager.uuid },
89
- )) as { components: OBC.Components };
90
- components.get(UIManager).init();
91
-
92
- const app = document.createElement("top-app") as unknown as App;
93
-
94
- app.setup = (waitUntil) => {
95
- waitUntil(
96
- (async () => {
97
- const fragments = components.get(OBC.FragmentsManager);
98
- const workerUrl = await FRAGS.FragmentsModels.getWorker();
99
- fragments.init(await FRAGS.toClassicWorker(workerUrl), {
100
- classicWorker: true,
101
- });
102
- })(),
103
- "Fragments Core",
104
- );
105
- return { components, client };
106
- };
107
-
108
- app.elements = {
109
- viewer: () =>
110
- html`<top-viewer><top-viewer-tools></top-viewer-tools></top-viewer>`,
111
- panel: () => html`<status-reporter></status-reporter>`,
112
- };
113
-
114
- app.base = "viewer";
115
- app.layouts = {
116
- main: {
117
- label: "Main",
118
- icon: "solar:home-bold",
119
- template: `"panel" 1fr / 22rem`,
120
- },
121
- };
122
- app.layout = "main";
123
-
124
- // top:app-ready fires after all waitUntil promises resolve.
125
- // e.detail.app is a reference to the top-app element itself.
126
- app.addEventListener("top:app-ready", (e) => {
127
- const { app: readyApp } = (e as AppReadyEvent).detail;
128
- readyApp.showToast("App ready", "success");
129
- });
130
-
131
- const container = document.getElementById("that-open-app") ?? document.body;
132
- container.appendChild(app);
133
- document.body.style.margin = "0";
1
+ /* MD
2
+ ## Toasts and cross-component notifications
3
+ ---
4
+ Developers building BIM processing UIs need to surface status updates, errors,
5
+ and async feedback to users from different points in the component tree —
6
+ but reaching back to the app root to trigger a notification from a deeply
7
+ nested component requires threading a reference through every layer in between.
8
+
9
+ top-app provides a built-in toast system that any descendant can trigger by
10
+ dispatching a standard DOM event. Toasts are auto-dismissed after a type-based
11
+ duration and stack at the bottom-left of the screen.
12
+
13
+ This tutorial covers showing a toast on app boot via showToast; building a
14
+ component that dispatches top:notification to report async progress and
15
+ completion without a reference to the root; and using the four toast types
16
+ (success, error, warning, info) with their respective durations.
17
+
18
+ By the end, you'll have an app with a boot confirmation toast and a panel that
19
+ reports async work status without holding any reference to top-app.
20
+ */
21
+
22
+ import { html, LitElement } from "lit";
23
+ import { customElement, state } from "lit/decorators.js";
24
+ import * as THREE from "three";
25
+ import * as OBC from "@thatopen/components";
26
+ import * as OBF from "@thatopen/components-front";
27
+ import * as FRAGS from "@thatopen/fragments";
28
+ import * as BUI from "@thatopen/ui";
29
+ import { PlatformClient, UIManager } from "@thatopen/services";
30
+ import type { App, AppReadyEvent } from "../../index";
31
+
32
+ // A child component that dispatches top:notification to surface messages
33
+ // to the nearest top-app ancestor — no reference to the app element needed.
34
+ @customElement("status-reporter")
35
+ class StatusReporter extends LitElement {
36
+ @state() private _busy = false;
37
+
38
+ private _simulateWork() {
39
+ this._busy = true;
40
+
41
+ // Dispatching top:notification from any descendant of top-app shows a toast.
42
+ // bubbles:true + composed:true lets it cross shadow-DOM boundaries.
43
+ this.dispatchEvent(
44
+ new CustomEvent("top:notification", {
45
+ bubbles: true,
46
+ composed: true,
47
+ detail: { message: "Analysis started", type: "info" },
48
+ }),
49
+ );
50
+
51
+ setTimeout(() => {
52
+ this._busy = false;
53
+ this.dispatchEvent(
54
+ new CustomEvent("top:notification", {
55
+ bubbles: true,
56
+ composed: true,
57
+ detail: {
58
+ message: "Analysis complete",
59
+ type: "success",
60
+ description: "Found 342 elements across 5 categories.",
61
+ },
62
+ }),
63
+ );
64
+ }, 2000);
65
+ }
66
+
67
+ render() {
68
+ return html`
69
+ <bim-panel label="Reporter">
70
+ <bim-panel-section label="Actions" icon="solar:play-bold">
71
+ <bim-button
72
+ label="Run Analysis"
73
+ icon="solar:play-bold"
74
+ .loading=${this._busy}
75
+ @click=${this._simulateWork}
76
+ ></bim-button>
77
+ </bim-panel-section>
78
+ </bim-panel>
79
+ `;
80
+ }
81
+ }
82
+
83
+ // ---- app wiring ----
84
+
85
+ const client = PlatformClient.fromPlatformContext();
86
+ const { components } = (await client.setup(
87
+ { OBC, OBF, BUI, THREE, FRAGS },
88
+ { uuid: UIManager.uuid },
89
+ )) as { components: OBC.Components };
90
+ components.get(UIManager).init();
91
+
92
+ const app = document.createElement("top-app") as unknown as App;
93
+
94
+ app.setup = (waitUntil) => {
95
+ waitUntil(
96
+ (async () => {
97
+ const fragments = components.get(OBC.FragmentsManager);
98
+ const workerUrl = await FRAGS.FragmentsModels.getWorker();
99
+ fragments.init(await FRAGS.toClassicWorker(workerUrl), {
100
+ classicWorker: true,
101
+ });
102
+ })(),
103
+ "Fragments Core",
104
+ );
105
+ return { components, client };
106
+ };
107
+
108
+ app.elements = {
109
+ viewer: () =>
110
+ html`<top-viewer><top-viewer-tools></top-viewer-tools></top-viewer>`,
111
+ panel: () => html`<status-reporter></status-reporter>`,
112
+ };
113
+
114
+ app.base = "viewer";
115
+ app.layouts = {
116
+ main: {
117
+ label: "Main",
118
+ icon: "solar:home-bold",
119
+ template: `"panel" 1fr / 22rem`,
120
+ },
121
+ };
122
+ app.layout = "main";
123
+
124
+ // top:app-ready fires after all waitUntil promises resolve.
125
+ // e.detail.app is a reference to the top-app element itself.
126
+ app.addEventListener("top:app-ready", (e) => {
127
+ const { app: readyApp } = (e as AppReadyEvent).detail;
128
+ readyApp.showToast("App ready", "success");
129
+ });
130
+
131
+ const container = document.getElementById("that-open-app") ?? document.body;
132
+ container.appendChild(app);
133
+ document.body.style.margin = "0";
@@ -1,150 +1,150 @@
1
- /* MD
2
- ## Persisting project-scoped data with projectStorageContext
3
- ---
4
- Most non-trivial apps built on top-app eventually need to persist some of
5
- their own data inside the project — user preferences, cached results,
6
- domain state that isn't a 3D model or a regular CDE file. The naive
7
- approach is for each panel/manager to talk to the CDE client directly:
8
- resolve or create a folder under `__project_data`, resolve or create the
9
- file, decide between uploading a new file and versioning an existing one.
10
- That's the same handful of steps every single consumer ends up
11
- re-implementing.
12
-
13
- top-app centralizes exactly that plumbing behind `projectStorageContext`, a
14
- `@lit/context` value any descendant can `@consume`. It provides a small,
15
- stable service object — not reactive state, since `save`/`flush` are
16
- actions, not values to observe:
17
-
18
- ```ts
19
- interface ProjectStorage {
20
- save(path: string, blob: Blob): void; // debounced (~2s), per path
21
- flush(): Promise<void>; // cancel debounces, save everything NOW, await it
22
- }
23
- ```
24
-
25
- `path` is resolved under `__project_data/`. Calling `save` schedules a
26
- debounced upload — top-app creates whichever folders in the path don't
27
- exist yet, creates the file the first time, and versions it (no explicit
28
- `versionTag` needed — the backend assigns the next one) every time after.
29
- Saves are debounced *per path*, so unrelated consumers saving around the
30
- same time don't reset each other's timers, and a small floating indicator
31
- (bottom-right, so it doesn't rely on the sidebar, which is optional and can
32
- be hidden) reflects both phases distinctly: just the icon, no background/
33
- border, while a save is debouncing (nothing to tell the user yet — it's
34
- still waiting to see if more changes come in), and the full "Saving..."
35
- pill once an upload is actually in flight. Each consumer only ever decides
36
- *what* to save and *where* — never the CDE mechanics, nor the indicator.
37
-
38
- Use `flush()` whenever you're about to do something that would conflict
39
- with a save still in flight — e.g. triggering a cloud job that reads the
40
- same file you just edited. It cancels any pending debounce, starts the
41
- upload right away, and its returned promise resolves only once that
42
- upload has actually finished.
43
-
44
- By the end, you'll have a tiny "project notes" panel that autosaves free
45
- text to `__project_data/notes/notes.json`, plus a "Save now" button that
46
- demonstrates `flush()`.
47
- */
48
-
49
- import { html, LitElement } from "lit";
50
- import { customElement, state } from "lit/decorators.js";
51
- import { consume } from "@lit/context";
52
- import * as THREE from "three";
53
- import * as OBC from "@thatopen/components";
54
- import * as OBF from "@thatopen/components-front";
55
- import * as FRAGS from "@thatopen/fragments";
56
- import * as BUI from "@thatopen/ui";
57
- import { PlatformClient, UIManager } from "@thatopen/services";
58
- import type { App } from "../../index";
59
- import { projectStorageContext, type ProjectStorage } from "../../src/context";
60
-
61
- // The path is always relative to `__project_data/` — top-app creates
62
- // whichever folders in it don't exist yet.
63
- const NOTES_PATH = "notes/notes.json";
64
-
65
- function notesBlob(text: string): Blob {
66
- return new Blob([JSON.stringify({ text, savedAt: new Date().toISOString() })], {
67
- type: "application/json",
68
- });
69
- }
70
-
71
- @customElement("notes-panel")
72
- class NotesPanel extends LitElement {
73
- @consume({ context: projectStorageContext })
74
- private _storage?: ProjectStorage;
75
-
76
- @state() private _text = "";
77
- @state() private _flushing = false;
78
-
79
- render() {
80
- return html`
81
- <bim-panel label="Project Notes">
82
- <bim-panel-section label="Notes" icon="tabler:notes">
83
- <bim-text-input
84
- vertical
85
- type="area"
86
- label="Note"
87
- placeholder="Type something — autosaves as you go"
88
- .value=${this._text}
89
- @input=${(e: Event) => {
90
- this._text = (e.target as HTMLInputElement).value ?? "";
91
- // Debounced — top-app coalesces rapid edits into one upload.
92
- this._storage?.save(NOTES_PATH, notesBlob(this._text));
93
- }}
94
- ></bim-text-input>
95
- <bim-button
96
- label="Save now"
97
- icon="tabler:cloud-upload"
98
- .loading=${this._flushing}
99
- @click=${async () => {
100
- // Same blob, but flush() cancels the debounce and waits for
101
- // the upload to actually finish before this resolves.
102
- this._storage?.save(NOTES_PATH, notesBlob(this._text));
103
- this._flushing = true;
104
- await this._storage?.flush();
105
- this._flushing = false;
106
- }}
107
- ></bim-button>
108
- </bim-panel-section>
109
- </bim-panel>
110
- `;
111
- }
112
- }
113
-
114
- // ---- app wiring ----
115
-
116
- const client = PlatformClient.fromPlatformContext();
117
- const { components } = (await client.setup(
118
- { OBC, OBF, BUI, THREE, FRAGS },
119
- { uuid: UIManager.uuid },
120
- )) as { components: OBC.Components };
121
- components.get(UIManager).init();
122
-
123
- const app = document.createElement("top-app") as unknown as App;
124
-
125
- app.setup = (waitUntil) => {
126
- waitUntil(
127
- (async () => {
128
- const fragments = components.get(OBC.FragmentsManager);
129
- const workerUrl = await FRAGS.FragmentsModels.getWorker();
130
- fragments.init(await FRAGS.toClassicWorker(workerUrl), { classicWorker: true });
131
- })(),
132
- "Fragments Core",
133
- );
134
- return { components, client };
135
- };
136
-
137
- app.elements = {
138
- viewer: () => html`<top-viewer><top-viewer-tools></top-viewer-tools></top-viewer>`,
139
- panel: () => html`<notes-panel></notes-panel>`,
140
- };
141
-
142
- app.base = "viewer";
143
- app.layouts = {
144
- main: { label: "Main", icon: "solar:home-bold", template: `"panel" 1fr / 22rem` },
145
- };
146
- app.layout = "main";
147
-
148
- const container = document.getElementById("that-open-app") ?? document.body;
149
- container.appendChild(app);
150
- document.body.style.margin = "0";
1
+ /* MD
2
+ ## Persisting project-scoped data with projectStorageContext
3
+ ---
4
+ Most non-trivial apps built on top-app eventually need to persist some of
5
+ their own data inside the project — user preferences, cached results,
6
+ domain state that isn't a 3D model or a regular CDE file. The naive
7
+ approach is for each panel/manager to talk to the CDE client directly:
8
+ resolve or create a folder under `__project_data`, resolve or create the
9
+ file, decide between uploading a new file and versioning an existing one.
10
+ That's the same handful of steps every single consumer ends up
11
+ re-implementing.
12
+
13
+ top-app centralizes exactly that plumbing behind `projectStorageContext`, a
14
+ `@lit/context` value any descendant can `@consume`. It provides a small,
15
+ stable service object — not reactive state, since `save`/`flush` are
16
+ actions, not values to observe:
17
+
18
+ ```ts
19
+ interface ProjectStorage {
20
+ save(path: string, blob: Blob): void; // debounced (~2s), per path
21
+ flush(): Promise<void>; // cancel debounces, save everything NOW, await it
22
+ }
23
+ ```
24
+
25
+ `path` is resolved under `__project_data/`. Calling `save` schedules a
26
+ debounced upload — top-app creates whichever folders in the path don't
27
+ exist yet, creates the file the first time, and versions it (no explicit
28
+ `versionTag` needed — the backend assigns the next one) every time after.
29
+ Saves are debounced *per path*, so unrelated consumers saving around the
30
+ same time don't reset each other's timers, and a small floating indicator
31
+ (bottom-right, so it doesn't rely on the sidebar, which is optional and can
32
+ be hidden) reflects both phases distinctly: just the icon, no background/
33
+ border, while a save is debouncing (nothing to tell the user yet — it's
34
+ still waiting to see if more changes come in), and the full "Saving..."
35
+ pill once an upload is actually in flight. Each consumer only ever decides
36
+ *what* to save and *where* — never the CDE mechanics, nor the indicator.
37
+
38
+ Use `flush()` whenever you're about to do something that would conflict
39
+ with a save still in flight — e.g. triggering a cloud job that reads the
40
+ same file you just edited. It cancels any pending debounce, starts the
41
+ upload right away, and its returned promise resolves only once that
42
+ upload has actually finished.
43
+
44
+ By the end, you'll have a tiny "project notes" panel that autosaves free
45
+ text to `__project_data/notes/notes.json`, plus a "Save now" button that
46
+ demonstrates `flush()`.
47
+ */
48
+
49
+ import { html, LitElement } from "lit";
50
+ import { customElement, state } from "lit/decorators.js";
51
+ import { consume } from "@lit/context";
52
+ import * as THREE from "three";
53
+ import * as OBC from "@thatopen/components";
54
+ import * as OBF from "@thatopen/components-front";
55
+ import * as FRAGS from "@thatopen/fragments";
56
+ import * as BUI from "@thatopen/ui";
57
+ import { PlatformClient, UIManager } from "@thatopen/services";
58
+ import type { App } from "../../index";
59
+ import { projectStorageContext, type ProjectStorage } from "../../src/context";
60
+
61
+ // The path is always relative to `__project_data/` — top-app creates
62
+ // whichever folders in it don't exist yet.
63
+ const NOTES_PATH = "notes/notes.json";
64
+
65
+ function notesBlob(text: string): Blob {
66
+ return new Blob([JSON.stringify({ text, savedAt: new Date().toISOString() })], {
67
+ type: "application/json",
68
+ });
69
+ }
70
+
71
+ @customElement("notes-panel")
72
+ class NotesPanel extends LitElement {
73
+ @consume({ context: projectStorageContext })
74
+ private _storage?: ProjectStorage;
75
+
76
+ @state() private _text = "";
77
+ @state() private _flushing = false;
78
+
79
+ render() {
80
+ return html`
81
+ <bim-panel label="Project Notes">
82
+ <bim-panel-section label="Notes" icon="tabler:notes">
83
+ <bim-text-input
84
+ vertical
85
+ type="area"
86
+ label="Note"
87
+ placeholder="Type something — autosaves as you go"
88
+ .value=${this._text}
89
+ @input=${(e: Event) => {
90
+ this._text = (e.target as HTMLInputElement).value ?? "";
91
+ // Debounced — top-app coalesces rapid edits into one upload.
92
+ this._storage?.save(NOTES_PATH, notesBlob(this._text));
93
+ }}
94
+ ></bim-text-input>
95
+ <bim-button
96
+ label="Save now"
97
+ icon="tabler:cloud-upload"
98
+ .loading=${this._flushing}
99
+ @click=${async () => {
100
+ // Same blob, but flush() cancels the debounce and waits for
101
+ // the upload to actually finish before this resolves.
102
+ this._storage?.save(NOTES_PATH, notesBlob(this._text));
103
+ this._flushing = true;
104
+ await this._storage?.flush();
105
+ this._flushing = false;
106
+ }}
107
+ ></bim-button>
108
+ </bim-panel-section>
109
+ </bim-panel>
110
+ `;
111
+ }
112
+ }
113
+
114
+ // ---- app wiring ----
115
+
116
+ const client = PlatformClient.fromPlatformContext();
117
+ const { components } = (await client.setup(
118
+ { OBC, OBF, BUI, THREE, FRAGS },
119
+ { uuid: UIManager.uuid },
120
+ )) as { components: OBC.Components };
121
+ components.get(UIManager).init();
122
+
123
+ const app = document.createElement("top-app") as unknown as App;
124
+
125
+ app.setup = (waitUntil) => {
126
+ waitUntil(
127
+ (async () => {
128
+ const fragments = components.get(OBC.FragmentsManager);
129
+ const workerUrl = await FRAGS.FragmentsModels.getWorker();
130
+ fragments.init(await FRAGS.toClassicWorker(workerUrl), { classicWorker: true });
131
+ })(),
132
+ "Fragments Core",
133
+ );
134
+ return { components, client };
135
+ };
136
+
137
+ app.elements = {
138
+ viewer: () => html`<top-viewer><top-viewer-tools></top-viewer-tools></top-viewer>`,
139
+ panel: () => html`<notes-panel></notes-panel>`,
140
+ };
141
+
142
+ app.base = "viewer";
143
+ app.layouts = {
144
+ main: { label: "Main", icon: "solar:home-bold", template: `"panel" 1fr / 22rem` },
145
+ };
146
+ app.layout = "main";
147
+
148
+ const container = document.getElementById("that-open-app") ?? document.body;
149
+ container.appendChild(app);
150
+ document.body.style.margin = "0";