Skip to content

Commit 36585ad

Browse files
refa: format
Signed-off-by: Rostislav Nagimov <[email protected]>
1 parent ee1076c commit 36585ad

File tree

3 files changed

+145
-140
lines changed

3 files changed

+145
-140
lines changed

plugins/workbench-resources/src/components/Workbench.svelte

Lines changed: 135 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@
109109
import WidgetsBar from './sidebar/Sidebar.svelte'
110110
import { sidebarStore, SidebarVariant, syncSidebarState } from '../sidebar'
111111
import { get } from 'svelte/store'
112-
import type { Application, NavigatorModel, SpecialNavModel, ViewConfiguration, WorkbenchTab} from '@hcengineering/workbench';
112+
import type {
113+
Application,
114+
NavigatorModel,
115+
SpecialNavModel,
116+
ViewConfiguration,
117+
WorkbenchTab
118+
} from '@hcengineering/workbench'
113119
114120
import {
115121
getTabDataByLocation,
@@ -120,7 +126,7 @@
120126
tabIdStore,
121127
tabsStore,
122128
updateTabUiState
123-
} from '../workbench';
129+
} from '../workbench'
124130
125131
const HIDE_NAVIGATOR = 720
126132
const FLOAT_ASIDE = 1024 // lg
@@ -250,111 +256,110 @@
250256
}
251257
}
252258
253-
let currentTabId: Ref<WorkbenchTab> | undefined = $tabIdStore;
259+
let currentTabId: Ref<WorkbenchTab> | undefined = $tabIdStore
254260
255-
function saveScrollState() {
256-
if (!currentTabId || !contentPanel) {
257-
return;
258-
}
261+
function saveScrollState () {
262+
if (!currentTabId || !contentPanel) {
263+
return
264+
}
259265
260-
const allScrollers = contentPanel.querySelectorAll<HTMLElement>('.scroll');
261-
if (allScrollers.length === 0) {
262-
return;
263-
}
264-
265-
console.log(`[SAVE] Tab ${currentTabId}. Found a total of ${allScrollers.length} elements with class .scroll.`);
266-
267-
const scrollableElements = Array.from(allScrollers).filter(
268-
(el) => el.scrollHeight > el.clientHeight
269-
);
270-
271-
console.log(`[SAVE] Of these, ${scrollableElements.length} are actually scrollable.`);
272-
273-
if (scrollableElements.length > 0) {
274-
const scrollPositions: Record<string, number> = {};
275-
scrollableElements.forEach((scroller, index) => {
276-
const id = `index-${index}`;
277-
const scrollTop = scroller.scrollTop;
278-
scrollPositions[id] = scrollTop;
279-
console.log(`[SAVE] -> Saving for element #${id} ORIGINAL scrollTop value: ${scrollTop}`);
280-
});
281-
282-
console.log('[SAVE] Final object to save:', scrollPositions);
283-
updateTabUiState(currentTabId, { scrollPositions });
284-
}
285-
}
266+
const allScrollers = contentPanel.querySelectorAll<HTMLElement>('.scroll')
267+
if (allScrollers.length === 0) {
268+
return
269+
}
286270
287-
async function restoreScrollState() {
288-
const tabId = $tabIdStore;
289-
if (!tabId || !contentPanel) {
290-
return;
291-
}
271+
console.log(`[SAVE] Tab ${currentTabId}. Found a total of ${allScrollers.length} elements with class .scroll.`)
292272
293-
console.log('[RESTORE] Waiting for two ticks for components to render...');
294-
await tick();
295-
await tick();
273+
const scrollableElements = Array.from(allScrollers).filter((el) => el.scrollHeight > el.clientHeight)
296274
297-
const tab = get(tabsStore).find((t) => t._id === tabId);
298-
const savedScrollPositions = tab?.uiState?.scrollPositions;
275+
console.log(`[SAVE] Of these, ${scrollableElements.length} are actually scrollable.`)
299276
300-
console.log(`[RESTORE] Tab ${tabId}. Found saved positions:`, savedScrollPositions);
277+
if (scrollableElements.length > 0) {
278+
const scrollPositions: Record<string, number> = {}
279+
scrollableElements.forEach((scroller, index) => {
280+
const id = `index-${index}`
281+
const scrollTop = scroller.scrollTop
282+
scrollPositions[id] = scrollTop
283+
console.log(`[SAVE] -> Saving for element #${id} ORIGINAL scrollTop value: ${scrollTop}`)
284+
})
301285
302-
if (!savedScrollPositions) {
303-
console.log('[RESTORE] Positions object is undefined, exiting.');
304-
return;
286+
console.log('[SAVE] Final object to save:', scrollPositions)
287+
updateTabUiState(currentTabId, { scrollPositions })
288+
}
305289
}
306290
307-
if (Object.keys(savedScrollPositions).length === 0) {
308-
console.log('[RESTORE] No saved positions (object is empty), exiting.');
309-
return;
310-
}
311-
312-
const finalPositions = savedScrollPositions;
313-
const expectedCount = Object.keys(finalPositions).length;
314-
let attempts = 0;
315-
const maxAttempts = 100;
316-
317-
function findAndApplyScroll() {
318-
const allScrollers = contentPanel.querySelectorAll<HTMLElement>('.scroll');
319-
const scrollableElements = Array.from(allScrollers).filter(
320-
(el) => el.scrollHeight > el.clientHeight
321-
);
322-
323-
if (scrollableElements.length === expectedCount) {
324-
console.log(`[RESTORE] Success! Found ${scrollableElements.length} scrollable elements.`);
325-
326-
scrollableElements.forEach((scroller, index) => {
327-
const id = `index-${index}`;
328-
const savedScrollTop = finalPositions[id];
291+
async function restoreScrollState () {
292+
const tabId = $tabIdStore
293+
if (!tabId || !contentPanel) {
294+
return
295+
}
329296
330-
if (savedScrollTop !== undefined) {
331-
console.log(`[RESTORE] -> Direct assignment of scroller.scrollTop = ${savedScrollTop} for element #${id}`);
332-
scroller.scrollTop = savedScrollTop;
333-
}
334-
});
335-
return;
297+
console.log('[RESTORE] Waiting for two ticks for components to render...')
298+
await tick()
299+
await tick()
300+
301+
const tab = get(tabsStore).find((t) => t._id === tabId)
302+
const savedScrollPositions = tab?.uiState?.scrollPositions
303+
304+
console.log(`[RESTORE] Tab ${tabId}. Found saved positions:`, savedScrollPositions)
305+
306+
if (!savedScrollPositions) {
307+
console.log('[RESTORE] Positions object is undefined, exiting.')
308+
return
336309
}
337-
338-
if (attempts < maxAttempts) {
339-
attempts++;
340-
requestAnimationFrame(findAndApplyScroll);
341-
} else {
342-
console.error(`[RESTORE] FAILED to wait for content to load in .scroll after ${maxAttempts} attempts.`);
310+
311+
if (Object.keys(savedScrollPositions).length === 0) {
312+
console.log('[RESTORE] No saved positions (object is empty), exiting.')
313+
return
343314
}
344-
}
345315
346-
findAndApplyScroll();
347-
}
316+
const finalPositions = savedScrollPositions
317+
const expectedCount = Object.keys(finalPositions).length
318+
let attempts = 0
319+
const maxAttempts = 100
320+
321+
function findAndApplyScroll () {
322+
const allScrollers = contentPanel.querySelectorAll<HTMLElement>('.scroll')
323+
const scrollableElements = Array.from(allScrollers).filter((el) => el.scrollHeight > el.clientHeight)
348324
349-
$: {
350-
if (currentTabId !== $tabIdStore) {
351-
console.log(`%c[SWITCH] Tab changed. Old: ${currentTabId}, New: ${$tabIdStore}`, 'color: blue; font-weight: bold;');
325+
if (scrollableElements.length === expectedCount) {
326+
console.log(`[RESTORE] Success! Found ${scrollableElements.length} scrollable elements.`)
352327
353-
saveScrollState();
354-
void restoreScrollState();
355-
currentTabId = $tabIdStore;
328+
scrollableElements.forEach((scroller, index) => {
329+
const id = `index-${index}`
330+
const savedScrollTop = finalPositions[id]
331+
332+
if (savedScrollTop !== undefined) {
333+
console.log(`[RESTORE] -> Direct assignment of scroller.scrollTop = ${savedScrollTop} for element #${id}`)
334+
scroller.scrollTop = savedScrollTop
335+
}
336+
})
337+
return
338+
}
339+
340+
if (attempts < maxAttempts) {
341+
attempts++
342+
requestAnimationFrame(findAndApplyScroll)
343+
} else {
344+
console.error(`[RESTORE] FAILED to wait for content to load in .scroll after ${maxAttempts} attempts.`)
345+
}
346+
}
347+
348+
findAndApplyScroll()
349+
}
350+
351+
$: {
352+
if (currentTabId !== $tabIdStore) {
353+
console.log(
354+
`%c[SWITCH] Tab changed. Old: ${currentTabId}, New: ${$tabIdStore}`,
355+
'color: blue; font-weight: bold;'
356+
)
357+
358+
saveScrollState()
359+
void restoreScrollState()
360+
currentTabId = $tabIdStore
361+
}
356362
}
357-
}
358363
359364
onMount(() => {
360365
pushRootBarComponent('right', view.component.SearchSelector)
@@ -1098,46 +1103,46 @@ $: {
10981103
!(mobileAdaptive && $deviceInfo.isPortrait)}
10991104
data-id={'contentPanel'}
11001105
>
1101-
{#if currentApplication && currentApplication.component}
1102-
<Component
1103-
is={currentApplication.component}
1104-
props={{
1105-
currentSpace,
1106-
workbenchWidth
1107-
}}
1108-
/>
1109-
{:else if specialComponent}
1110-
<Component
1111-
is={specialComponent.component}
1112-
props={{
1113-
model: navigatorModel,
1114-
...specialComponent.componentProps,
1115-
currentSpace,
1116-
space: currentSpace,
1117-
navigationModel: specialComponent?.navigationModel,
1118-
workbenchWidth,
1119-
queryBuilder: specialComponent?.queryBuilder
1120-
}}
1121-
on:action={(e) => {
1122-
if (e?.detail) {
1123-
const loc = getCurrentLocation()
1124-
loc.query = { ...loc.query, ...e.detail }
1125-
navigate(loc)
1126-
}
1127-
}}
1128-
/>
1129-
{:else if currentView?.component !== undefined}
1130-
<Component
1131-
is={currentView.component}
1106+
{#if currentApplication && currentApplication.component}
1107+
<Component
1108+
is={currentApplication.component}
1109+
props={{
1110+
currentSpace,
1111+
workbenchWidth
1112+
}}
1113+
/>
1114+
{:else if specialComponent}
1115+
<Component
1116+
is={specialComponent.component}
1117+
props={{
1118+
model: navigatorModel,
1119+
...specialComponent.componentProps,
1120+
currentSpace,
1121+
space: currentSpace,
1122+
navigationModel: specialComponent?.navigationModel,
1123+
workbenchWidth,
1124+
queryBuilder: specialComponent?.queryBuilder
1125+
}}
1126+
on:action={(e) => {
1127+
if (e?.detail) {
1128+
const loc = getCurrentLocation()
1129+
loc.query = { ...loc.query, ...e.detail }
1130+
navigate(loc)
1131+
}
1132+
}}
1133+
/>
1134+
{:else if currentView?.component !== undefined}
1135+
<Component
1136+
is={currentView.component}
11321137
props={{ ...currentView.componentProps, currentSpace, currentView, workbenchWidth }}
1133-
/>
1134-
{:else if $accessDeniedStore}
1135-
<div class="flex-center h-full">
1136-
<h2><Label label={workbench.string.AccessDenied} /></h2>
1137-
</div>
1138-
{:else}
1139-
<SpaceView {currentSpace} {currentView} {createItemDialog} {createItemLabel} />
1140-
{/if}
1138+
/>
1139+
{:else if $accessDeniedStore}
1140+
<div class="flex-center h-full">
1141+
<h2><Label label={workbench.string.AccessDenied} /></h2>
1142+
</div>
1143+
{:else}
1144+
<SpaceView {currentSpace} {currentView} {createItemDialog} {createItemLabel} />
1145+
{/if}
11411146
</div>
11421147
</div>
11431148
{#if $sidebarStore.variant === SidebarVariant.EXPANDED && !$sidebarStore.float}

plugins/workbench-resources/src/workbench.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ locationWorkspaceStore.subscribe((workspace) => {
6262
tabIdStore.set(getTabFromLocalStorage(workspace ?? ''))
6363
})
6464

65-
export function updateTabUiState(tabId: Ref<WorkbenchTab>, state: Partial<TabUiState>): void {
66-
tabsStore.update(tabs => {
67-
const tab = tabs.find(t => t._id === tabId);
68-
if (tab) {
65+
export function updateTabUiState (tabId: Ref<WorkbenchTab>, state: Partial<TabUiState>): void {
66+
tabsStore.update((tabs) => {
67+
const tab = tabs.find((t) => t._id === tabId)
68+
if (tab != null) {
6969
tab.uiState = {
70-
...(tab.uiState || {}),
70+
...(tab.uiState ?? {}),
7171
...state
72-
};
72+
}
7373
}
74-
return tabs;
75-
});
74+
return tabs
75+
})
7676
}
7777

7878
const syncTabLoc = reduceCalls(async (): Promise<void> => {

plugins/workbench/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export interface WidgetTab {
101101

102102
/** @public */
103103
export interface TabUiState {
104-
scrollPositions?: Record<string, number>;
104+
scrollPositions?: Record<string, number>
105105
}
106106

107107
/** @public */
@@ -110,7 +110,7 @@ export interface WorkbenchTab extends Preference {
110110
location: string
111111
isPinned: boolean
112112
name?: string
113-
uiState?: TabUiState;
113+
uiState?: TabUiState
114114
}
115115

116116
/** @public */

0 commit comments

Comments
 (0)