-
Notifications
You must be signed in to change notification settings - Fork 2
/
paranext-dock-layout.component.tsx
340 lines (308 loc) · 12.1 KB
/
paranext-dock-layout.component.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import 'rc-dock/dist/rc-dock.css';
import './paranext-dock-layout.component.css';
import { useRef, useEffect } from 'react';
import DockLayout, {
BoxData,
FloatPosition,
LayoutSize,
PanelData,
TabData,
TabGroup,
} from 'rc-dock';
import { createErrorTab } from '@renderer/components/docking/error-tab.component';
import ParanextPanel from '@renderer/components/docking/paranext-panel.component';
import ParanextTabTitle from '@renderer/components/docking/paranext-tab-title.component';
import {
loadWebViewTab,
TAB_TYPE_WEBVIEW,
saveWebViewTab,
} from '@renderer/components/web-view.component';
import { loadAboutTab, TAB_TYPE_ABOUT } from '@renderer/testing/about-panel.component';
import { loadButtonsTab, TAB_TYPE_BUTTONS } from '@renderer/testing/test-buttons-panel.component';
import testLayout from '@renderer/testing/test-layout.data';
import { loadTestTab, TAB_TYPE_TEST } from '@renderer/testing/test-panel.component';
import {
loadQuickVerseHeresyTab,
TAB_TYPE_QUICK_VERSE_HERESY,
} from '@renderer/testing/test-quick-verse-heresy-panel.component';
import {
FloatLayout,
SavedTabInfo,
TabLoader,
TabInfo,
TabSaver,
Layout,
WebViewProps,
} from '@shared/data/web-view.model';
import LogError from '@shared/log-error.model';
import {
OnLayoutChangeRCDock,
registerDockLayout,
saveTabInfoBase,
} from '@shared/services/web-view.service';
import { getErrorMessage } from '@shared/utils/util';
import {
loadOpenProjectTab,
TAB_TYPE_OPEN_PROJECT_DIALOG,
} from '@renderer/components/project-dialogs/open-project-tab.component';
import {
loadDownloadUpdateProjectTab,
TAB_TYPE_DOWNLOAD_UPDATE_PROJECT_DIALOG,
} from '@renderer/components/project-dialogs/download-update-project-tab.component';
import {
loadOpenMultipleProjectsTab,
TAB_TYPE_OPEN_MULTIPLE_PROJECTS_DIALOG,
} from '@renderer/components/project-dialogs/open-multiple-projects-tab.component';
import {
TAB_TYPE_EXTENSION_MANAGER,
loadExtensionManagerTab,
} from '@renderer/components/extension-manager/extension-manager-tab.component';
import {
TAB_TYPE_RUN_BASIC_CHECKS,
loadRunBasicChecksTab,
} from '../run-basic-checks-dialog/run-basic-checks-tab.component';
type TabType = string;
type RCDockTabInfo = TabData & TabInfo;
const DOCK_FLOAT_OFFSET = 28;
// NOTE: 'card' is a built-in style. We can likely remove it when we create a full theme for
// Paranext.
const TAB_GROUP = 'card paranext';
const groups: { [key: string]: TabGroup } = {
[TAB_GROUP]: {
maximizable: false, // Don't allow groups of tabs to be maximized
floatable: true, // Allow tabs to be floated
animated: false, // Don't animate tab transitions
// TODO: Currently allowing newWindow crashes since electron doesn't seem to have window.open defined?
// newWindow: true, // Allow floating windows to show in a native window
},
};
// #region utility functions that deal with loading, saving, and adding webviews. This code should
// really be in `web-view.service.ts`, but that file cannot currently import renderer code as it is
// a shared file.
// TODO: please move these utility functions with #203
/** tab loader functions for each Paranext tab type */
const tabLoaderMap = new Map<TabType, TabLoader>([
[TAB_TYPE_ABOUT, loadAboutTab],
[TAB_TYPE_BUTTONS, loadButtonsTab],
[TAB_TYPE_QUICK_VERSE_HERESY, loadQuickVerseHeresyTab],
[TAB_TYPE_TEST, loadTestTab],
[TAB_TYPE_WEBVIEW, loadWebViewTab],
[TAB_TYPE_OPEN_PROJECT_DIALOG, loadOpenProjectTab],
[TAB_TYPE_DOWNLOAD_UPDATE_PROJECT_DIALOG, loadDownloadUpdateProjectTab],
[TAB_TYPE_OPEN_MULTIPLE_PROJECTS_DIALOG, loadOpenMultipleProjectsTab],
[TAB_TYPE_EXTENSION_MANAGER, loadExtensionManagerTab],
[TAB_TYPE_RUN_BASIC_CHECKS, loadRunBasicChecksTab],
]);
/** tab saver functions for each Paranext tab type that wants to override the default */
const tabSaverMap = new Map<TabType, TabSaver>([[TAB_TYPE_WEBVIEW, saveWebViewTab]]);
let previousTabId: string | undefined;
let floatPosition: FloatPosition = { left: 0, top: 0, width: 0, height: 0 };
/**
* Loads tab data from the specified saved tab information by running the tab loader provided by the
* component file that registered this tab type
* @param savedTabInfo Data that is to be used to create the new tab (comes from rc-dock)
* @returns tab that holds all info needed to make an actual tab in the dock layout
*/
function loadSavedTabInfo(savedTabInfo: SavedTabInfo): TabInfo {
const tabLoader = tabLoaderMap.get(savedTabInfo.tabType);
if (!tabLoader) return createErrorTab(`No tab loader for tabType '${savedTabInfo.tabType}'`);
// Call the creation method to let the extension method create the tab
try {
return tabLoader(savedTabInfo);
} catch (e) {
// If the tab couldn't be created, replace it with an error tab
return createErrorTab(getErrorMessage(e));
}
}
/**
* Loads tab data from the specified saved tab information into an actual dock layout tab
* @param savedTabInfo Data that is to be used to create the new tab (comes from rc-dock)
* @returns live dock layout tab ready to used
*/
export function loadTab(savedTabInfo: SavedTabInfo): RCDockTabInfo {
if (!savedTabInfo.id) throw new LogError('loadTab: "id" is missing.');
// Load the tab from the saved tab info
const tabInfo = loadSavedTabInfo(savedTabInfo);
// Translate the data from the loaded tab to be in the form needed by rc-dock
return {
...tabInfo,
title: <ParanextTabTitle text={tabInfo.tabTitle} />,
content: <ParanextPanel>{tabInfo.content}</ParanextPanel>,
group: TAB_GROUP,
closable: true,
};
}
/**
* Converts the tab data into saved tab information by running the tab saver provided by the
* component file that registered this tab type
* @param dockTabInfo the tab data to save
* @returns saved tab info ready to be saved into the layout
*/
function saveTab(dockTabInfo: RCDockTabInfo): SavedTabInfo {
// Remove the rc-dock properties that are not also in SavedTabInfo
// We don't need to use the other properties, but we need to remove them
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { parent, group, closable, title, ...strippedTabInfo } = dockTabInfo;
const tabInfo: TabInfo = strippedTabInfo;
const tabSaver = tabSaverMap.get(tabInfo.tabType);
return tabSaver ? tabSaver(tabInfo) : saveTabInfoBase(tabInfo);
}
/**
* Check if the input item is just a tab, i.e. not a panel, box, or float.
* @param tab to check.
* @returns `true` if its a tab or `false` otherwise.
*/
function isTab(tab: PanelData | TabData | BoxData | undefined): boolean {
if (!tab || (tab as TabData).title == null) return false;
return true;
}
function offsetOrOverflowAxis(
axis: number,
size: number,
max: number,
offset = DOCK_FLOAT_OFFSET,
): number {
if (axis + size + offset >= max) return offset;
return axis + offset;
}
/**
* Get left & top so float windows cascade their position. Float window should not overflow the
* layout but start cascading again.
* @param layout specified by the WebView.
* @param previousPosition used with the previous float window.
* @param layoutSize of the whole dock layout.
* @returns cascaded position.
*/
export function getFloatPosition(
layout: FloatLayout,
previousPosition: FloatPosition,
layoutSize: LayoutSize,
): FloatPosition {
// Defaults are added in `web-view.service.ts`.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { width, height } = layout.floatSize!;
let { left, top } = previousPosition;
left = offsetOrOverflowAxis(left, width, layoutSize.width);
top = offsetOrOverflowAxis(top, height, layoutSize.height);
return { left, top, width, height };
}
/** Find the previous tab if one was previously added or find the first tab otherwise */
function findPreviousTab(dockLayout: DockLayout) {
// If we have a previous tab, try to find it
if (previousTabId !== undefined) {
const previousTab = dockLayout.find(previousTabId);
// If we found the previous tab, go with that
if (previousTab) return previousTab;
}
// We don't have a previous tab or we didn't find the one we thought we had. Just find the first
// available tab
return dockLayout.find((tabData) => isTab(tabData)) as TabData;
}
/**
* Function to call to add or update a webview in the layout
* @param webView web view to add or update
* @param layout information about where to put a new webview
* @param dockLayout The rc-dock dock layout React component ref. Used to perform operations on the
* layout
*/
export function addWebViewToDock(webView: WebViewProps, layout: Layout, dockLayout: DockLayout) {
const tabId = webView.id;
const tab = loadTab({ id: tabId, tabType: TAB_TYPE_WEBVIEW, data: webView });
let targetTab = dockLayout.find(tabId);
// Update existing WebView
if (targetTab) {
dockLayout.updateTab(tabId, tab);
if (isTab(targetTab)) previousTabId = tabId;
return;
}
// Add new WebView
const unknownLayoutType = layout.type;
switch (layout.type) {
case 'tab':
targetTab = findPreviousTab(dockLayout);
if (targetTab) {
if (previousTabId === undefined)
// The target tab is the first found tab, so just add this as a new panel on top
dockLayout.dockMove(tab, targetTab.parent as PanelData, 'top');
// The target tab is a previously added tab, so add this as a tab next to it
else dockLayout.dockMove(tab, targetTab, 'after-tab');
}
// Didn't find any tabs. Add as a new tab
else
dockLayout.dockMove(
tab,
// Find the first thing (the dock box) and add the tab to it
dockLayout.find(() => true) ?? null,
'middle',
);
previousTabId = tabId;
break;
case 'float':
floatPosition = getFloatPosition(layout, floatPosition, dockLayout.getLayoutSize());
dockLayout.dockMove(tab, null, 'float', floatPosition);
break;
case 'panel':
if (layout.targetTabId !== undefined) {
// Look for a specific tab
targetTab = dockLayout.find(layout.targetTabId);
if (!isTab(targetTab))
throw new LogError(`When adding a panel, unknown target tab: '${layout.targetTabId}'`);
}
// Didn't ask for a specific tab, so just get the previous tab and go from there
else targetTab = findPreviousTab(dockLayout);
dockLayout.dockMove(
tab,
// Add to the parent of the found tab if we found a tab
(targetTab?.parent as PanelData) ??
// Otherwise find the first thing (the dock box) and add the tab to it
dockLayout.find(() => true) ??
null,
// Defaults are added in `web-view.service.ts`.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
layout.direction!,
);
break;
default:
throw new LogError(`Unknown layoutType: '${unknownLayoutType}'`);
}
}
// #endregion
export default function ParanextDockLayout() {
// This ref will always be defined
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const dockLayoutRef = useRef<DockLayout>(null!);
/**
* onLayoutChange function from `web-view.service.ts` once this docklayout is registered.
*
* TODO: Strange pattern that we are setting a ref to a service function. Investigate changing
* this pattern in some way. Maybe just export `onLayoutChange`?
*/
const onLayoutChangeRef = useRef<OnLayoutChangeRCDock | undefined>();
useEffect(() => {
// Register with `web-view.service.ts` so it can perform operations on us
const unsub = registerDockLayout({
dockLayout: dockLayoutRef.current,
onLayoutChangeRef,
addWebViewToDock: (webView: WebViewProps, layout: Layout) =>
addWebViewToDock(webView, layout, dockLayoutRef.current),
testLayout,
});
return () => {
unsub();
};
// Is there any situation where dockLayoutRef will change? We need to add to dependencies if so
}, []);
return (
<DockLayout
ref={dockLayoutRef}
groups={groups}
defaultLayout={{ dockbox: { mode: 'horizontal', children: [] } }}
dropMode="edge"
loadTab={loadTab}
saveTab={saveTab}
onLayoutChange={(...args) => {
if (onLayoutChangeRef.current) onLayoutChangeRef.current(...args);
}}
/>
);
}