@geode/opengeodeweb-front 10.44.0-rc.7 → 10.44.0-rc.9

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 (32) hide show
  1. package/.build/server/utils/scripts.js +34 -23
  2. package/app/components/MissingFilesSelector.vue +30 -17
  3. package/app/components/ObjectSelector.vue +34 -23
  4. package/app/composables/hover_highlight.ts +13 -13
  5. package/app/composables/overlapping_picker_menu.ts +43 -0
  6. package/app/composables/project_manager_helpers/restore.ts +20 -11
  7. package/app/composables/use_overlapping_picker.ts +23 -37
  8. package/app/composables/virtual_tree_flatten.ts +66 -43
  9. package/app/stores/app_helpers/extension.ts +90 -51
  10. package/app/stores/infra.ts +26 -21
  11. package/app/utils/color_picker.ts +18 -15
  12. package/internal/database/database.ts +47 -32
  13. package/internal/stores/data_style/mesh/base_style.ts +74 -0
  14. package/internal/stores/data_style/mesh/index.ts +2 -57
  15. package/internal/stores/data_style/model/blocks/color.ts +58 -57
  16. package/internal/stores/data_style/model/blocks/polyhedron.ts +88 -44
  17. package/internal/stores/data_style/model/blocks/vertex.ts +88 -44
  18. package/internal/stores/data_style/model/color.ts +25 -26
  19. package/internal/stores/data_style/model/corners/index.ts +85 -78
  20. package/internal/stores/data_style/model/corners/vertex.ts +83 -39
  21. package/internal/stores/data_style/model/lines/color.ts +53 -40
  22. package/internal/stores/data_style/model/lines/edge.ts +88 -44
  23. package/internal/stores/data_style/model/lines/vertex.ts +88 -44
  24. package/internal/stores/data_style/model/surfaces/color.ts +68 -56
  25. package/internal/stores/data_style/model/surfaces/polygon.ts +80 -36
  26. package/internal/stores/data_style/model/surfaces/vertex.ts +80 -36
  27. package/internal/stores/data_style/model/visibility_dispatch.ts +49 -99
  28. package/internal/stores/hybrid_viewer/highlight.ts +70 -54
  29. package/internal/stores/hybrid_viewer/scene.ts +31 -25
  30. package/package.json +1 -1
  31. package/server/api/local/extensions/upload.put.ts +47 -20
  32. package/server/utils/scripts.ts +35 -30
@@ -25,31 +25,46 @@ const encoder = new TextEncoder();
25
25
  function byteLength(str) {
26
26
  return encoder.encode(str).byteLength;
27
27
  }
28
+ function truncateLine(lineOutput) {
29
+ const halfBuffer = MAX_ERROR_BUFFER_BYTES / 2;
30
+ return byteLength(lineOutput) > halfBuffer ? `${lineOutput.slice(0, halfBuffer)}…[truncated]` : lineOutput;
31
+ }
32
+ function trimOutputBuffer(output) {
33
+ let trimmed = output;
34
+ while (byteLength(trimmed) > MAX_ERROR_BUFFER_BYTES) {
35
+ const newline = trimmed.indexOf("\n");
36
+ if (newline === -1) return "";
37
+ trimmed = trimmed.slice(newline + 1);
38
+ }
39
+ return trimmed;
40
+ }
41
+ function createOutputRecorder() {
42
+ let recentOutput = "";
43
+ function record(lineOutput) {
44
+ recentOutput = trimOutputBuffer(`${recentOutput} ${truncateLine(lineOutput)}\n`);
45
+ }
46
+ function read() {
47
+ return recentOutput;
48
+ }
49
+ return {
50
+ record,
51
+ read
52
+ };
53
+ }
28
54
  async function waitForReady(child, expectedResponse, signal) {
29
55
  return new Promise((resolve, reject) => {
30
56
  const readlineStdout = readline.createInterface({ input: child.stdout });
31
57
  const readlineStderr = readline.createInterface({ input: child.stderr });
32
- let recentOutput = "";
33
- function recordOutput(lineOutput) {
34
- const safeLine = byteLength(lineOutput) > MAX_ERROR_BUFFER_BYTES / 2 ? `${lineOutput.slice(0, MAX_ERROR_BUFFER_BYTES / 2)}…[truncated]` : lineOutput;
35
- recentOutput = `${recentOutput} ${safeLine}\n`;
36
- while (byteLength(recentOutput) > MAX_ERROR_BUFFER_BYTES) {
37
- const newline = recentOutput.indexOf("\n");
38
- if (newline === -1) {
39
- recentOutput = "";
40
- break;
41
- }
42
- recentOutput = recentOutput.slice(newline + 1);
43
- }
44
- }
58
+ const recorder = createOutputRecorder();
45
59
  let onLine = void 0;
46
- let onErrLine = void 0;
47
60
  let onError = void 0;
48
61
  let onClose = void 0;
49
62
  let onAbort = void 0;
50
63
  function cleanup() {
51
- if (onLine) readlineStdout.removeListener("line", onLine);
52
- if (onErrLine) readlineStderr.removeListener("line", onErrLine);
64
+ if (onLine) {
65
+ readlineStdout.removeListener("line", onLine);
66
+ readlineStderr.removeListener("line", onLine);
67
+ }
53
68
  if (onError) child.removeListener("error", onError);
54
69
  if (onClose) child.removeListener("close", onClose);
55
70
  if (signal && onAbort) signal.removeEventListener("abort", onAbort);
@@ -69,14 +84,9 @@ async function waitForReady(child, expectedResponse, signal) {
69
84
  }
70
85
  onLine = (lineOutput) => {
71
86
  consola.info(`[${child.name}] ${lineOutput}`);
72
- recordOutput(lineOutput);
87
+ recorder.record(lineOutput);
73
88
  if (lineOutput.includes(expectedResponse)) becomeReady();
74
89
  };
75
- onErrLine = (line) => {
76
- consola.info(`[${child.name}] ${line}`);
77
- recordOutput(line);
78
- if (line.includes(expectedResponse)) becomeReady();
79
- };
80
90
  onError = (err) => {
81
91
  cleanup();
82
92
  reject(err);
@@ -84,6 +94,7 @@ async function waitForReady(child, expectedResponse, signal) {
84
94
  onClose = (code) => {
85
95
  consola.info(`[${child.name}] exited with code ${code}`);
86
96
  cleanup();
97
+ const recentOutput = recorder.read();
87
98
  reject(/* @__PURE__ */ new Error(`[${child.name}] exited with code ${code} before becoming ready.${recentOutput ? `\nRecent output:\n${recentOutput}` : ""}`));
88
99
  };
89
100
  onAbort = () => {
@@ -91,7 +102,7 @@ async function waitForReady(child, expectedResponse, signal) {
91
102
  reject(/* @__PURE__ */ new Error(`[${child.name}] timed out waiting for "${expectedResponse}"`));
92
103
  };
93
104
  readlineStdout.on("line", onLine);
94
- readlineStderr.on("line", onErrLine);
105
+ readlineStderr.on("line", onLine);
95
106
  child.once("error", onError);
96
107
  child.once("close", onClose);
97
108
  if (signal) signal.addEventListener("abort", onAbort, { once: true });
@@ -94,29 +94,30 @@ function isCsvFile(filename: string): boolean {
94
94
  return filename.toLowerCase().endsWith(".csv") || filename.toLowerCase().endsWith(".csv.json");
95
95
  }
96
96
 
97
- async function missing_files(): Promise<void> {
98
- toggle_loading();
97
+ function reset_missing_state(): void {
99
98
  has_missing_files.value = false;
100
99
  mandatory_files.value = [];
101
100
  additional_files.value = [];
102
101
  missing_by_file.value = [];
102
+ }
103
+
104
+ function request_file_plan(filename: string): Promise<FilePlan> {
105
+ if (isCsvFile(filename)) {
106
+ return Promise.resolve({
107
+ has_missing_files: false,
108
+ mandatory_files: [],
109
+ additional_files: [],
110
+ });
111
+ }
103
112
  const backStore = useBackStore();
113
+ if (timeSeries) {
114
+ return backStore.request({ schema: time_series_schema, params: { filename } });
115
+ }
116
+ const params = { geode_object_type: geodeObjectTypes[filename] ?? "", filename };
117
+ return backStore.request({ schema, params });
118
+ }
104
119
 
105
- const promise_array = checked_filenames.value.map((filename): Promise<FilePlan> => {
106
- if (isCsvFile(filename)) {
107
- return Promise.resolve({
108
- has_missing_files: false,
109
- mandatory_files: [],
110
- additional_files: [],
111
- });
112
- }
113
- if (timeSeries) {
114
- return backStore.request({ schema: time_series_schema, params: { filename } });
115
- }
116
- const params = { geode_object_type: geodeObjectTypes[filename] ?? "", filename };
117
- return backStore.request({ schema, params });
118
- });
119
- const values = await Promise.all(promise_array);
120
+ function collect_file_plans(values: FilePlan[]): void {
120
121
  for (const [index, value] of values.entries()) {
121
122
  if (value.has_missing_files) {
122
123
  has_missing_files.value = true;
@@ -129,6 +130,9 @@ async function missing_files(): Promise<void> {
129
130
  mandatory_files.value = [...mandatory_files.value, ...value.mandatory_files];
130
131
  additional_files.value = [...additional_files.value, ...value.additional_files];
131
132
  }
133
+ }
134
+
135
+ function flag_unconfigured_csvs(): void {
132
136
  const unconfigured_csvs = files.filter((file) => isCsvFile(file.name) && !file.isConfigured);
133
137
  if (unconfigured_csvs.length > 0) {
134
138
  has_missing_files.value = true;
@@ -136,6 +140,15 @@ async function missing_files(): Promise<void> {
136
140
  accept.value = ".json";
137
141
  }
138
142
  }
143
+ }
144
+
145
+ async function missing_files(): Promise<void> {
146
+ toggle_loading();
147
+ reset_missing_state();
148
+
149
+ const values = await Promise.all(checked_filenames.value.map(request_file_plan));
150
+ collect_file_plans(values);
151
+ flag_unconfigured_csvs();
139
152
 
140
153
  if (!has_missing_files.value) {
141
154
  emit("update_values", { skipped_files: [] });
@@ -120,6 +120,37 @@ function showCommonGeodeObjects(
120
120
  return resolved.selectedGeodeObject;
121
121
  }
122
122
 
123
+ function toResolvedEntries(loadable: readonly FileAllowedObjects[]): [string, string][] {
124
+ return loadable.flatMap((file): [string, string][] => {
125
+ if (file.selectedGeodeObject === undefined) {
126
+ return [];
127
+ }
128
+ return [[file.filename, file.selectedGeodeObject]];
129
+ });
130
+ }
131
+
132
+ // Tries to settle the pending files without the per file panel. Returns true when it did
133
+ function resolveWithoutPanel(
134
+ pending: readonly FileAllowedObjects[],
135
+ allowedObjectsList: readonly AllowedObjectMap[],
136
+ ): boolean {
137
+ if (pending.length === 0) {
138
+ autoResolved.value = true;
139
+ emitGeodeObjectTypes(resolvedTypes.value);
140
+ return true;
141
+ }
142
+ const pendingAllowedObjects = pending.map(
143
+ (file) => allowedObjectsList[filenames.indexOf(file.filename)] ?? {},
144
+ );
145
+ // Pending files sharing a loadable type: one choice for all of them
146
+ const selected = showCommonGeodeObjects(pendingFilenames.value, pendingAllowedObjects);
147
+ if (Object.values(allowedGeodeObjects.value).some((object) => object.is_loadable > 0)) {
148
+ setGeodeObject(selected);
149
+ return true;
150
+ }
151
+ return false;
152
+ }
153
+
123
154
  // Each file gets its own type. Returns true when the per file panel is needed, i.e. the files cannot be handled by a single choice
124
155
  function resolvePerFile(allowedObjectsList: readonly AllowedObjectMap[]): boolean {
125
156
  const files = resolveAllowedObjectsPerFile(filenames, allowedObjectsList);
@@ -130,33 +161,13 @@ function resolvePerFile(allowedObjectsList: readonly AllowedObjectMap[]): boolea
130
161
  return false;
131
162
  }
132
163
  const pending = loadable.filter((file) => file.selectedGeodeObject === undefined);
133
- resolvedTypes.value = Object.fromEntries(
134
- loadable.flatMap((file) => {
135
- if (file.selectedGeodeObject === undefined) {
136
- return [];
137
- }
138
- return [[file.filename, file.selectedGeodeObject]];
139
- }),
140
- );
164
+ resolvedTypes.value = Object.fromEntries(toResolvedEntries(loadable));
141
165
  pendingFilenames.value = pending.map((file) => file.filename);
142
166
  skippedFiles.value = files
143
167
  .filter((file) => file.candidates.length === 0)
144
168
  .map((file) => file.filename);
145
- if (skippedFiles.value.length === 0) {
146
- if (pending.length === 0) {
147
- autoResolved.value = true;
148
- emitGeodeObjectTypes(resolvedTypes.value);
149
- return false;
150
- }
151
- const pendingAllowedObjects = pending.map(
152
- (file) => allowedObjectsList[filenames.indexOf(file.filename)] ?? {},
153
- );
154
- // Pending files sharing a loadable type: one choice for all of them
155
- const selected = showCommonGeodeObjects(pendingFilenames.value, pendingAllowedObjects);
156
- if (Object.values(allowedGeodeObjects.value).some((object) => object.is_loadable > 0)) {
157
- setGeodeObject(selected);
158
- return false;
159
- }
169
+ if (skippedFiles.value.length === 0 && resolveWithoutPanel(pending, allowedObjectsList)) {
170
+ return false;
160
171
  }
161
172
  pendingGroups.value = groupPendingFiles(pending);
162
173
  return true;
@@ -17,8 +17,10 @@ export function useHoverhighlight(): {
17
17
  const viewerStore = useViewerStore();
18
18
  const dataStore = useDataStore();
19
19
  let timer: ReturnType<typeof setTimeout> | undefined = undefined;
20
+ // Item hovered last, whose highlight may still be pending
20
21
  let currentId: string | undefined = undefined;
21
- let currentType: HighlightType | undefined = undefined;
22
+ // Item the viewer was asked to highlight, the only one to unhighlight
23
+ let highlighted: { id: string; type: HighlightType } | undefined = undefined;
22
24
 
23
25
  function onHoverEnter(
24
26
  id: string,
@@ -34,7 +36,6 @@ export function useHoverhighlight(): {
34
36
 
35
37
  async function highlightAction(): Promise<void> {
36
38
  currentId = id;
37
- currentType = type;
38
39
 
39
40
  if (!(await dataStore.isItemViewable(id))) {
40
41
  return;
@@ -55,6 +56,7 @@ export function useHoverhighlight(): {
55
56
  return;
56
57
  }
57
58
 
59
+ highlighted = { id, type };
58
60
  const params = {
59
61
  id,
60
62
  visibility: true,
@@ -107,22 +109,20 @@ export function useHoverhighlight(): {
107
109
  clearTimeout(timer);
108
110
  timer = undefined;
109
111
  }
110
- if (currentId === id && currentType) {
111
- const schema = vtk_schemas.opengeodeweb_viewer[currentType].highlight;
112
+ if (currentId === id) {
113
+ currentId = undefined;
114
+ }
115
+ if (highlighted?.id === id) {
116
+ const { type } = highlighted;
117
+ const schema = vtk_schemas.opengeodeweb_viewer[type].highlight;
112
118
  const params = {
113
119
  id,
114
120
  visibility: false,
115
- ...(currentType === "model" && { block_ids: [] }),
121
+ ...(type === "model" && { block_ids: [] }),
116
122
  };
117
123
  // oxlint-disable-next-line typescript/no-floating-promises -- unhighlightAction catches its own errors internally.
118
- unhighlightAction(currentType, id, { schema, params });
119
- currentId = undefined;
120
- currentType = undefined;
121
- }
122
-
123
- if (currentId === undefined) {
124
- currentId = undefined;
125
- currentType = undefined;
124
+ unhighlightAction(type, id, { schema, params });
125
+ highlighted = undefined;
126
126
  }
127
127
  }
128
128
 
@@ -0,0 +1,43 @@
1
+ interface MenuPositionParams {
2
+ x: number;
3
+ y: number;
4
+ containerWidth: number;
5
+ containerHeight: number;
6
+ itemCount: number;
7
+ }
8
+
9
+ interface MenuPosition {
10
+ x: number;
11
+ y: number;
12
+ }
13
+
14
+ const MENU_WIDTH = 340;
15
+ const MENU_BASE_HEIGHT = 55;
16
+ const MENU_ITEM_HEIGHT = 48;
17
+ const CLAMP_MARGIN = 10;
18
+
19
+ function clampMenuY(yUI: number, containerHeight: number, menuHeight: number): number {
20
+ let clampedY = yUI;
21
+ if (yUI + menuHeight > containerHeight - CLAMP_MARGIN) {
22
+ clampedY = Math.max(yUI - menuHeight, CLAMP_MARGIN);
23
+ } else {
24
+ clampedY = Math.max(yUI, CLAMP_MARGIN);
25
+ }
26
+ return Math.min(Math.max(clampedY, CLAMP_MARGIN), containerHeight - menuHeight - CLAMP_MARGIN);
27
+ }
28
+
29
+ function computeMenuPosition({
30
+ x,
31
+ y,
32
+ containerWidth,
33
+ containerHeight,
34
+ itemCount,
35
+ }: MenuPositionParams): MenuPosition {
36
+ const menuHeight = MENU_BASE_HEIGHT + itemCount * MENU_ITEM_HEIGHT;
37
+ const clampedX = Math.min(Math.max(x, CLAMP_MARGIN), containerWidth - MENU_WIDTH - CLAMP_MARGIN);
38
+ const clampedY = clampMenuY(containerHeight - y, containerHeight, menuHeight);
39
+ return { x: clampedX, y: clampedY };
40
+ }
41
+
42
+ export { computeMenuPosition };
43
+ export type { MenuPosition, MenuPositionParams };
@@ -22,27 +22,24 @@ function restoreCrs(snapshot: ProjectSnapshot): void {
22
22
  }
23
23
  }
24
24
 
25
- // Loads a project snapshot into the (already reset) viewer and stores.
26
- async function restoreProject(snapshot: ProjectSnapshot): Promise<void> {
27
- const dataStyleStore = useDataStyleStore();
28
- const viewerStore = useViewerStore();
25
+ async function restoreData(snapshot: ProjectSnapshot): Promise<void> {
29
26
  const dataStore = useDataStore();
30
27
  const treeviewStore = useTreeviewStore();
31
28
  const hybridViewerStore = useHybridViewerStore();
32
- const basemapStore = useBasemapStore();
33
-
34
- treeviewStore.isImporting = true;
35
-
36
- await importProjectToViewer(viewerStore.client);
37
29
 
38
30
  treeviewStore.importStores(snapshot.treeview);
39
31
  if (snapshot.data !== undefined) {
40
32
  await dataStore.importStores(snapshot.data);
41
33
  }
42
34
  await hybridViewerStore.initHybridViewer();
35
+ await importWorkflowFromSnapshot(snapshot.data?.items ?? []);
36
+ }
37
+
38
+ async function restoreViews(snapshot: ProjectSnapshot): Promise<void> {
39
+ const dataStyleStore = useDataStyleStore();
40
+ const hybridViewerStore = useHybridViewerStore();
41
+ const basemapStore = useBasemapStore();
43
42
 
44
- const items = snapshot.data?.items ?? [];
45
- await importWorkflowFromSnapshot(items);
46
43
  restoreCrs(snapshot);
47
44
  await basemapStore.importStores(snapshot.basemap);
48
45
  await hybridViewerStore.importStores(snapshot.hybridViewer);
@@ -50,6 +47,18 @@ async function restoreProject(snapshot: ProjectSnapshot): Promise<void> {
50
47
  await dataStyleStore.importStores(snapshot.dataStyle);
51
48
  }
52
49
  await dataStyleStore.applyAllStylesFromState();
50
+ }
51
+
52
+ // Loads a project snapshot into the (already reset) viewer and stores.
53
+ async function restoreProject(snapshot: ProjectSnapshot): Promise<void> {
54
+ const viewerStore = useViewerStore();
55
+ const treeviewStore = useTreeviewStore();
56
+
57
+ treeviewStore.isImporting = true;
58
+
59
+ await importProjectToViewer(viewerStore.client);
60
+ await restoreData(snapshot);
61
+ await restoreViews(snapshot);
53
62
 
54
63
  treeviewStore.finalizeImportSelection();
55
64
  treeviewStore.isImporting = false;
@@ -2,6 +2,7 @@ import { ref } from "vue";
2
2
  import viewer_schemas from "@geode/opengeodeweb-viewer/opengeodeweb_viewer_typed_schemas.js";
3
3
 
4
4
  import { type DataItem, useDataStore } from "@ogw_front/stores/data";
5
+ import { computeMenuPosition } from "@ogw_front/composables/overlapping_picker_menu";
5
6
  import { useDataStyleStore } from "@ogw_front/stores/data_style";
6
7
  import { useViewerStore } from "@ogw_front/stores/viewer";
7
8
 
@@ -102,13 +103,7 @@ export function useOverlappingPicker(): UseOverlappingPickerReturn {
102
103
  }
103
104
  }
104
105
 
105
- async function get_viewer_id({
106
- x,
107
- y,
108
- containerWidth,
109
- containerHeight,
110
- containerRect,
111
- }: GetViewerIdParams): Promise<ViewerIdResult> {
106
+ async function getViewableIds(): Promise<string[]> {
112
107
  const activeIds = new Set(dataItems.value.map((i: DataItem) => i.id));
113
108
  const visibleStyleIds = Object.keys(dataStyleStore.styles).filter(
114
109
  (styleId) => activeIds.has(styleId) && dataStyleStore.objectVisibility(styleId) === true,
@@ -123,15 +118,30 @@ export function useOverlappingPicker(): UseOverlappingPickerReturn {
123
118
  }
124
119
  }),
125
120
  );
126
- const ids = viewableChecks.filter((id): id is string => Boolean(id));
121
+ return viewableChecks.filter((id): id is string => Boolean(id));
122
+ }
123
+
124
+ function showIntermediateMenu(
125
+ { x, y, containerWidth, containerHeight, containerRect }: GetViewerIdParams,
126
+ itemCount: number,
127
+ ): void {
128
+ const position = computeMenuPosition({ x, y, containerWidth, containerHeight, itemCount });
129
+ intermediateMenuX.value = containerRect.left + position.x;
130
+ intermediateMenuY.value = containerRect.top + position.y;
131
+ displayIntermediate.value = true;
132
+ }
133
+
134
+ async function get_viewer_id(params: GetViewerIdParams): Promise<ViewerIdResult> {
135
+ const ids = await getViewableIds();
127
136
 
128
- const result: ViewerIdResult = { id: undefined, viewer_id: undefined };
129
137
  const schema = viewer_schemas.opengeodeweb_viewer.viewer.picked_ids;
130
- const params = { x, y, ids };
131
- const { array_ids, viewer_id } = await viewerStore.request({ schema, params });
138
+ const { array_ids, viewer_id } = await viewerStore.request({
139
+ schema,
140
+ params: { x: params.x, y: params.y, ids },
141
+ });
132
142
 
133
143
  if (array_ids.length === 0) {
134
- return result;
144
+ return { id: undefined, viewer_id: undefined };
135
145
  }
136
146
 
137
147
  const pickedList: PickedItem[] = array_ids.map((pickId) => ({ id: pickId, viewer_id }));
@@ -142,31 +152,7 @@ export function useOverlappingPicker(): UseOverlappingPickerReturn {
142
152
 
143
153
  const proposedItems = await fetchProposedItems(pickedList);
144
154
  intermediateItems.value = proposedItems;
145
-
146
- const yUI = containerHeight - y;
147
- const MENU_WIDTH = 340;
148
- const MENU_BASE_HEIGHT = 55;
149
- const MENU_ITEM_HEIGHT = 48;
150
- const MENU_HEIGHT = MENU_BASE_HEIGHT + proposedItems.length * MENU_ITEM_HEIGHT;
151
- const CLAMP_MARGIN = 10;
152
- const clampedX = Math.min(
153
- Math.max(x, CLAMP_MARGIN),
154
- containerWidth - MENU_WIDTH - CLAMP_MARGIN,
155
- );
156
- let clampedY = yUI;
157
- if (yUI + MENU_HEIGHT > containerHeight - CLAMP_MARGIN) {
158
- clampedY = Math.max(yUI - MENU_HEIGHT, CLAMP_MARGIN);
159
- } else {
160
- clampedY = Math.max(yUI, CLAMP_MARGIN);
161
- }
162
- clampedY = Math.min(
163
- Math.max(clampedY, CLAMP_MARGIN),
164
- containerHeight - MENU_HEIGHT - CLAMP_MARGIN,
165
- );
166
-
167
- intermediateMenuX.value = containerRect.left + clampedX;
168
- intermediateMenuY.value = containerRect.top + clampedY;
169
- displayIntermediate.value = true;
155
+ showIntermediateMenu(params, proposedItems.length);
170
156
 
171
157
  /* eslint-disable-next-line promise/avoid-new */
172
158
  return new Promise<ViewerIdResult>((resolve) => {
@@ -27,57 +27,80 @@ function matchesSearch(ctx: FlattenContext, item: TreeItem, id: unknown, search:
27
27
  );
28
28
  }
29
29
 
30
- function flattenTree(ctx: FlattenContext, nodes: readonly TreeItem[], depth = 0): DisplayItem[] {
31
- const { search } = ctx.props.value;
32
- const result: DisplayItem[] = [];
30
+ function createDisplayItem(ctx: FlattenContext, item: TreeItem, depth: number): DisplayItem {
31
+ const id = item[ctx.actualItemProps.value.value];
32
+ return {
33
+ raw: item,
34
+ id,
35
+ depth,
36
+ isOpen: ctx.openedSet.value.has(id),
37
+ isActive: ctx.activeSet.value.has(id),
38
+ isLeaf: true,
39
+ };
40
+ }
33
41
 
34
- for (const item of nodes) {
35
- const id = item[ctx.actualItemProps.value.value];
36
- const children = ctx.getChildrenOf(item);
37
- const hasChildren = Boolean(children && children.length > 0);
42
+ type FlattenChildren = (children: readonly TreeItem[]) => DisplayItem[];
38
43
 
39
- const isOpen = ctx.openedSet.value.has(id);
40
- const isActive = ctx.activeSet.value.has(id);
44
+ // A branch is kept when it matches or when one of its descendants does
45
+ function flattenSearchedBranch(
46
+ entry: DisplayItem,
47
+ subtree: readonly DisplayItem[],
48
+ matches: boolean,
49
+ ): DisplayItem[] {
50
+ if (subtree.length === 0 && !matches) {
51
+ return [];
52
+ }
53
+ const branch = { ...entry, isLeaf: false };
54
+ if (!branch.isOpen) {
55
+ return [branch];
56
+ }
57
+ return [branch, ...subtree];
58
+ }
41
59
 
42
- if (search !== undefined && search !== "") {
43
- const matches = matchesSearch(ctx, item, id, search);
60
+ function flattenOpenBranch(
61
+ entry: DisplayItem,
62
+ children: readonly TreeItem[],
63
+ flattenChildren: FlattenChildren,
64
+ ): DisplayItem[] {
65
+ const branch = { ...entry, isLeaf: false };
66
+ if (!branch.isOpen) {
67
+ return [branch];
68
+ }
69
+ return [branch, ...flattenChildren(children)];
70
+ }
44
71
 
45
- if (hasChildren) {
46
- const subtree = flattenTree(ctx, children ?? [], depth + 1);
47
- if (subtree.length === 0 && !matches) {
48
- continue;
49
- }
72
+ function flattenNode(
73
+ ctx: FlattenContext,
74
+ item: TreeItem,
75
+ depth: number,
76
+ flattenChildren: FlattenChildren,
77
+ ): DisplayItem[] {
78
+ const { search } = ctx.props.value;
79
+ const entry = createDisplayItem(ctx, item, depth);
80
+ const children = ctx.getChildrenOf(item) ?? [];
81
+ const searching = search !== undefined && search !== "";
50
82
 
51
- result.push({
52
- raw: item,
53
- id,
54
- depth,
55
- isOpen,
56
- isActive,
57
- isLeaf: false,
58
- });
59
- if (isOpen) {
60
- result.push(...subtree);
61
- }
62
- continue;
63
- }
64
- if (!matches) {
65
- continue;
66
- }
83
+ if (children.length === 0) {
84
+ if (searching && !matchesSearch(ctx, item, entry.id, search)) {
85
+ return [];
67
86
  }
87
+ return [entry];
88
+ }
89
+ if (searching) {
90
+ const matches = matchesSearch(ctx, item, entry.id, search);
91
+ return flattenSearchedBranch(entry, flattenChildren(children), matches);
92
+ }
93
+ return flattenOpenBranch(entry, children, flattenChildren);
94
+ }
68
95
 
69
- result.push({
70
- raw: item,
71
- id,
72
- depth,
73
- isOpen,
74
- isActive,
75
- isLeaf: !hasChildren,
76
- });
96
+ function flattenTree(ctx: FlattenContext, nodes: readonly TreeItem[], depth = 0): DisplayItem[] {
97
+ const result: DisplayItem[] = [];
98
+ function flattenChildren(children: readonly TreeItem[]): DisplayItem[] {
99
+ return flattenTree(ctx, children, depth + 1);
100
+ }
77
101
 
78
- if (isOpen && hasChildren) {
79
- result.push(...flattenTree(ctx, children ?? [], depth + 1));
80
- }
102
+ for (const item of nodes) {
103
+ result.push(...flattenNode(ctx, item, depth, flattenChildren));
81
104
  }
82
105
  return result;
83
106
  }