@teselagen/ui 0.0.9 → 0.0.12
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.
- package/README.md +7 -0
- package/cypress.config.ts +6 -0
- package/index.html +12 -0
- package/package.json +2 -2
- package/project.json +74 -0
- package/src/AdvancedOptions.js +33 -0
- package/src/AdvancedOptions.spec.js +24 -0
- package/src/AssignDefaultsModeContext.js +21 -0
- package/src/AsyncValidateFieldSpinner/index.js +12 -0
- package/src/BlueprintError/index.js +14 -0
- package/src/BounceLoader/index.js +16 -0
- package/src/BounceLoader/style.css +45 -0
- package/src/CollapsibleCard/index.js +92 -0
- package/src/CollapsibleCard/style.css +21 -0
- package/src/DNALoader/index.js +20 -0
- package/src/DNALoader/style.css +251 -0
- package/src/DataTable/CellDragHandle.js +130 -0
- package/src/DataTable/DisabledLoadingComponent.js +15 -0
- package/src/DataTable/DisplayOptions.js +218 -0
- package/src/DataTable/FilterAndSortMenu.js +397 -0
- package/src/DataTable/PagingTool.js +232 -0
- package/src/DataTable/SearchBar.js +57 -0
- package/src/DataTable/SortableColumns.js +53 -0
- package/src/DataTable/TableFormTrackerContext.js +10 -0
- package/src/DataTable/dataTableEnhancer.js +291 -0
- package/src/DataTable/defaultFormatters.js +32 -0
- package/src/DataTable/defaultProps.js +45 -0
- package/src/DataTable/defaultValidators.js +40 -0
- package/src/DataTable/editCellHelper.js +44 -0
- package/src/DataTable/getCellVal.js +20 -0
- package/src/DataTable/getVals.js +8 -0
- package/src/DataTable/index.js +3537 -0
- package/src/DataTable/isTruthy.js +12 -0
- package/src/DataTable/isValueEmpty.js +3 -0
- package/src/DataTable/style.css +600 -0
- package/src/DataTable/utils/computePresets.js +42 -0
- package/src/DataTable/utils/convertSchema.js +69 -0
- package/src/DataTable/utils/getIdOrCodeOrIndex.js +9 -0
- package/src/DataTable/utils/getTableConfigFromStorage.js +5 -0
- package/src/DataTable/utils/queryParams.js +1032 -0
- package/src/DataTable/utils/rowClick.js +156 -0
- package/src/DataTable/utils/selection.js +8 -0
- package/src/DataTable/utils/withSelectedEntities.js +65 -0
- package/src/DataTable/utils/withTableParams.js +328 -0
- package/src/DataTable/validateTableWideErrors.js +135 -0
- package/src/DataTable/viewColumn.js +37 -0
- package/src/DialogFooter/index.js +79 -0
- package/src/DialogFooter/style.css +9 -0
- package/src/DropdownButton.js +36 -0
- package/src/FillWindow.css +6 -0
- package/src/FillWindow.js +69 -0
- package/src/FormComponents/Uploader.js +1197 -0
- package/src/FormComponents/getNewName.js +31 -0
- package/src/FormComponents/index.js +1384 -0
- package/src/FormComponents/itemUpload.js +84 -0
- package/src/FormComponents/sortify.js +73 -0
- package/src/FormComponents/style.css +247 -0
- package/src/FormComponents/tryToMatchSchemas.js +222 -0
- package/src/FormComponents/utils.js +6 -0
- package/src/HotkeysDialog/index.js +79 -0
- package/src/HotkeysDialog/style.css +54 -0
- package/src/InfoHelper/index.js +83 -0
- package/src/InfoHelper/style.css +7 -0
- package/src/IntentText/index.js +18 -0
- package/src/Loading/index.js +74 -0
- package/src/Loading/style.css +4 -0
- package/src/MatchHeaders.js +223 -0
- package/src/MenuBar/index.js +416 -0
- package/src/MenuBar/style.css +45 -0
- package/src/PromptUnsavedChanges/index.js +40 -0
- package/src/ResizableDraggableDialog/index.js +138 -0
- package/src/ResizableDraggableDialog/style.css +42 -0
- package/src/ScrollToTop/index.js +72 -0
- package/src/SimpleStepViz.js +26 -0
- package/src/TgSelect/index.js +465 -0
- package/src/TgSelect/style.css +34 -0
- package/src/TgSuggest/index.js +121 -0
- package/src/Timeline/TimelineEvent.js +31 -0
- package/src/Timeline/index.js +22 -0
- package/src/Timeline/style.css +29 -0
- package/src/UploadCsvWizard.css +4 -0
- package/src/UploadCsvWizard.js +731 -0
- package/src/autoTooltip.js +89 -0
- package/src/constants.js +1 -0
- package/src/customIcons.js +361 -0
- package/src/enhancers/withDialog/index.js +196 -0
- package/src/enhancers/withDialog/tg_modalState.js +46 -0
- package/src/enhancers/withField.js +20 -0
- package/src/enhancers/withFields.js +11 -0
- package/src/enhancers/withLocalStorage.js +11 -0
- package/src/index.js +76 -0
- package/src/rerenderOnWindowResize.js +27 -0
- package/src/showAppSpinner.js +12 -0
- package/src/showConfirmationDialog/index.js +116 -0
- package/src/showDialogOnDocBody.js +37 -0
- package/src/style.css +214 -0
- package/src/toastr.js +92 -0
- package/src/typeToCommonType.js +6 -0
- package/src/useDialog.js +64 -0
- package/src/utils/S3Download.js +14 -0
- package/src/utils/adHoc.js +10 -0
- package/src/utils/basicHandleActionsWithFullState.js +14 -0
- package/src/utils/combineReducersWithFullState.js +14 -0
- package/src/utils/commandControls.js +83 -0
- package/src/utils/commandUtils.js +112 -0
- package/src/utils/determineBlackOrWhiteTextColor.js +4 -0
- package/src/utils/getDayjsFormatter.js +35 -0
- package/src/utils/getTextFromEl.js +28 -0
- package/src/utils/handlerHelpers.js +30 -0
- package/src/utils/hotkeyUtils.js +129 -0
- package/src/utils/menuUtils.js +402 -0
- package/src/utils/popoverOverflowModifiers.js +11 -0
- package/src/utils/pureNoFunc.js +31 -0
- package/src/utils/renderOnDoc.js +29 -0
- package/src/utils/showProgressToast.js +22 -0
- package/src/utils/tagUtils.js +45 -0
- package/src/utils/tgFormValues.js +32 -0
- package/src/utils/withSelectTableRecords.js +38 -0
- package/src/utils/withStore.js +10 -0
- package/src/wrapDialog.js +112 -0
- package/tsconfig.json +4 -0
- package/vite.config.ts +7 -0
- package/index.js +0 -80652
- package/index.mjs +0 -80636
- package/index.umd.js +0 -80649
- package/style.css +0 -10421
package/src/useDialog.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
|
|
5
|
+
const {toggleDialog, comp} = useDialog({
|
|
6
|
+
ModalComponent: SimpleInsertData,
|
|
7
|
+
validateAgainstSchema,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
return <div>
|
|
11
|
+
{comp} //stick the returned dialog comp somewhere in the dom! (it should not effect layout)
|
|
12
|
+
{...your code here}
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
*/
|
|
16
|
+
export const useDialog = ({ ModalComponent, ...rest }) => {
|
|
17
|
+
const [isOpen, setOpen] = useState(false);
|
|
18
|
+
const [additionalProps, setAdditionalProps] = useState(false);
|
|
19
|
+
const comp = (
|
|
20
|
+
<ModalComponent
|
|
21
|
+
hideModal={() => {
|
|
22
|
+
setOpen(false);
|
|
23
|
+
}}
|
|
24
|
+
hideDialog={() => {
|
|
25
|
+
setOpen(false);
|
|
26
|
+
}}
|
|
27
|
+
{...rest}
|
|
28
|
+
{...additionalProps}
|
|
29
|
+
dialogProps={{
|
|
30
|
+
isOpen,
|
|
31
|
+
...rest?.dialogProps,
|
|
32
|
+
...additionalProps?.dialogProps
|
|
33
|
+
}}
|
|
34
|
+
></ModalComponent>
|
|
35
|
+
);
|
|
36
|
+
const toggleDialog = () => {
|
|
37
|
+
setOpen(!isOpen);
|
|
38
|
+
};
|
|
39
|
+
async function showDialogPromise(handlerName, moreProps = {}) {
|
|
40
|
+
return new Promise(resolve => {
|
|
41
|
+
//return a promise that can be awaited
|
|
42
|
+
setAdditionalProps({
|
|
43
|
+
hideModal: () => {
|
|
44
|
+
//override hideModal to resolve also
|
|
45
|
+
setOpen(false);
|
|
46
|
+
resolve({});
|
|
47
|
+
},
|
|
48
|
+
hideDialog: () => {
|
|
49
|
+
//override hideModal to resolve also
|
|
50
|
+
setOpen(false);
|
|
51
|
+
resolve({});
|
|
52
|
+
},
|
|
53
|
+
[handlerName]: r => {
|
|
54
|
+
setOpen(false);
|
|
55
|
+
resolve(r || {});
|
|
56
|
+
},
|
|
57
|
+
//pass any additional props to the dialog
|
|
58
|
+
...moreProps
|
|
59
|
+
});
|
|
60
|
+
setOpen(true); //open the dialog
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return { comp, showDialogPromise, toggleDialog, setAdditionalProps };
|
|
64
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
|
|
3
|
+
const S3Download = request => {
|
|
4
|
+
const url =
|
|
5
|
+
(request.server || "/") +
|
|
6
|
+
"/s3/" +
|
|
7
|
+
(request.s3path || "") +
|
|
8
|
+
request.file +
|
|
9
|
+
"?bucket=" +
|
|
10
|
+
(request.bucket || "");
|
|
11
|
+
return axios.get(url, { responseType: "blob" }).then(res => res.data);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default S3Download;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { compose } from "recompose";
|
|
3
|
+
|
|
4
|
+
//adHoc allows you to add dynamic HOCs to a component
|
|
5
|
+
export default func => WrappedComponent => props => {
|
|
6
|
+
const calledFunc = func(props);
|
|
7
|
+
const composeArgs = Array.isArray(calledFunc) ? calledFunc : [calledFunc];
|
|
8
|
+
const ComposedAndWrapped = compose(...composeArgs)(WrappedComponent);
|
|
9
|
+
return <ComposedAndWrapped {...props} />;
|
|
10
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default function basicHandleActionsWithFullState(
|
|
2
|
+
handlers,
|
|
3
|
+
defaultState
|
|
4
|
+
) {
|
|
5
|
+
return (state = defaultState, action, fullState) => {
|
|
6
|
+
const { type } = action
|
|
7
|
+
const handler = handlers[type]
|
|
8
|
+
if (handler) {
|
|
9
|
+
return handler(state, action, fullState)
|
|
10
|
+
} else {
|
|
11
|
+
return state
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default function combineReducers(reducers = {}) {
|
|
2
|
+
const reducerKeys = Object.keys(reducers)
|
|
3
|
+
return function combination(state = {}, action, fullState) {
|
|
4
|
+
let hasChanged = false
|
|
5
|
+
const nextState = {}
|
|
6
|
+
fullState = fullState || state
|
|
7
|
+
for (let i = 0; i < reducerKeys.length; i++) {
|
|
8
|
+
const key = reducerKeys[i]
|
|
9
|
+
nextState[key] = reducers[key](state[key], action, fullState)
|
|
10
|
+
hasChanged = hasChanged || nextState[key] !== state[key]
|
|
11
|
+
}
|
|
12
|
+
return hasChanged ? nextState : state
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Tooltip, Checkbox, Button, Switch } from "@blueprintjs/core";
|
|
3
|
+
|
|
4
|
+
export const withCommand = mappings => WrappedComponent => ({
|
|
5
|
+
cmd,
|
|
6
|
+
cmdOptions = {},
|
|
7
|
+
...props
|
|
8
|
+
}) => {
|
|
9
|
+
const mappedProps = {};
|
|
10
|
+
Object.keys(mappings).forEach(k => {
|
|
11
|
+
mappedProps[k] =
|
|
12
|
+
mappings[k] === "execute"
|
|
13
|
+
? event => cmd.execute({ event })
|
|
14
|
+
: typeof mappings[k] === "function"
|
|
15
|
+
? mappings[k](cmd, props)
|
|
16
|
+
: cmd[mappings[k]];
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
let out = <WrappedComponent {...mappedProps} {...props} />;
|
|
20
|
+
const tooltip =
|
|
21
|
+
cmd.tooltip || (typeof cmd.isDisabled === "string" && cmd.isDisabled);
|
|
22
|
+
if (tooltip && !cmdOptions.ignoreTooltip) {
|
|
23
|
+
out = <Tooltip content={tooltip}>{out}</Tooltip>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return cmd.isHidden && !cmdOptions.ignoreHidden ? null : out;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const CmdCheckbox = withCommand({
|
|
30
|
+
onChange: "execute",
|
|
31
|
+
label: (cmd, props) =>
|
|
32
|
+
props.name ||
|
|
33
|
+
(props.prefix && (
|
|
34
|
+
<React.Fragment>
|
|
35
|
+
{props.prefix}
|
|
36
|
+
{cmd.name}
|
|
37
|
+
</React.Fragment>
|
|
38
|
+
)) ||
|
|
39
|
+
cmd.name,
|
|
40
|
+
disabled: "isDisabled",
|
|
41
|
+
checked: "isActive"
|
|
42
|
+
})(Checkbox);
|
|
43
|
+
|
|
44
|
+
export const CmdSwitch = withCommand({
|
|
45
|
+
onChange: "execute",
|
|
46
|
+
label: (cmd, props) =>
|
|
47
|
+
props.name ||
|
|
48
|
+
(props.prefix && (
|
|
49
|
+
<React.Fragment>
|
|
50
|
+
{props.prefix}
|
|
51
|
+
{cmd.name}
|
|
52
|
+
</React.Fragment>
|
|
53
|
+
)) ||
|
|
54
|
+
cmd.name,
|
|
55
|
+
disabled: "isDisabled",
|
|
56
|
+
checked: "isActive"
|
|
57
|
+
})(Switch);
|
|
58
|
+
|
|
59
|
+
const Div = ({ onChange, children }) => {
|
|
60
|
+
return <div onClick={onChange}>{children}</div>;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const CmdDiv = withCommand({
|
|
64
|
+
onChange: "execute",
|
|
65
|
+
children: (cmd, props) =>
|
|
66
|
+
props.name ||
|
|
67
|
+
(props.prefix && (
|
|
68
|
+
<React.Fragment>
|
|
69
|
+
{props.prefix}
|
|
70
|
+
{cmd.name}
|
|
71
|
+
</React.Fragment>
|
|
72
|
+
)) ||
|
|
73
|
+
cmd.name,
|
|
74
|
+
disabled: "isDisabled",
|
|
75
|
+
checked: "isActive"
|
|
76
|
+
})(Div);
|
|
77
|
+
|
|
78
|
+
export const CmdButton = withCommand({
|
|
79
|
+
onClick: "execute",
|
|
80
|
+
text: cmd => (cmd.isActive === false && cmd.inactiveName) || cmd.name,
|
|
81
|
+
icon: "icon",
|
|
82
|
+
disabled: "isDisabled"
|
|
83
|
+
})(Button);
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { startCase } from "lodash";
|
|
2
|
+
|
|
3
|
+
// Generic factory function to create command objects.
|
|
4
|
+
// TODO add documentation
|
|
5
|
+
export function genericCommandFactory(config) {
|
|
6
|
+
const out = {};
|
|
7
|
+
// eslint-disable-next-line no-unused-vars
|
|
8
|
+
for (const cmdId in config.commandDefs) {
|
|
9
|
+
const def = config.commandDefs[cmdId];
|
|
10
|
+
const command = { id: cmdId };
|
|
11
|
+
command.execute = (...execArgs) => {
|
|
12
|
+
config.handleReturn(
|
|
13
|
+
cmdId,
|
|
14
|
+
def.handler &&
|
|
15
|
+
def.handler.apply(command, config.getArguments(cmdId, execArgs))
|
|
16
|
+
);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const properties = [
|
|
20
|
+
"icon",
|
|
21
|
+
"name",
|
|
22
|
+
"component",
|
|
23
|
+
"shortName",
|
|
24
|
+
"description",
|
|
25
|
+
"hotkey",
|
|
26
|
+
"hotkeyProps",
|
|
27
|
+
"isDisabled",
|
|
28
|
+
"submenu",
|
|
29
|
+
"isActive",
|
|
30
|
+
"isHidden",
|
|
31
|
+
"tooltip",
|
|
32
|
+
"inactiveIcon",
|
|
33
|
+
"inactiveName"
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
properties.forEach(prop => {
|
|
37
|
+
if (def[prop] !== undefined) {
|
|
38
|
+
if (typeof def[prop] === "function") {
|
|
39
|
+
Object.defineProperty(command, prop, {
|
|
40
|
+
get: () => {
|
|
41
|
+
return def[prop].apply(command, config.getArguments(cmdId, []));
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
} else {
|
|
45
|
+
command[prop] = def[prop];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// If no name was specified in the definition, let's try to give some
|
|
51
|
+
// auto-generated names
|
|
52
|
+
if (!def.name) {
|
|
53
|
+
command.name = startCase(cmdId);
|
|
54
|
+
if (def.toggle && cmdId.startsWith("toggle")) {
|
|
55
|
+
command.name = startCase(cmdId.replace("toggle", def.toggle[0] || ""));
|
|
56
|
+
command.inactiveName = startCase(
|
|
57
|
+
cmdId.replace("toggle", def.toggle[1] || "")
|
|
58
|
+
);
|
|
59
|
+
command.shortName = startCase(cmdId.replace("toggle", ""));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
out[cmdId] = command;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Extract hotkey props from the given commands or command defs, returning
|
|
70
|
+
// a mapping of command ids to hotkey prop objects
|
|
71
|
+
export function getCommandHotkeys(commandsOrDefs) {
|
|
72
|
+
const hotkeyDefs = {};
|
|
73
|
+
Object.keys(commandsOrDefs).forEach(cmdId => {
|
|
74
|
+
if (commandsOrDefs[cmdId].hotkey) {
|
|
75
|
+
hotkeyDefs[cmdId] = {
|
|
76
|
+
combo: commandsOrDefs[cmdId].hotkey,
|
|
77
|
+
label: commandsOrDefs[cmdId].name || startCase(cmdId),
|
|
78
|
+
...commandsOrDefs[cmdId].hotkeyProps
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return hotkeyDefs;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Extract handler functions from the given commands, returning a mapping of
|
|
87
|
+
// command ids to handlers (directly - no checks added).
|
|
88
|
+
export function getCommandHandlers(commands) {
|
|
89
|
+
const handlers = {};
|
|
90
|
+
Object.keys(commands).forEach(cmdId => {
|
|
91
|
+
handlers[cmdId] = commands[cmdId].execute;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return handlers;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Get hotkey handler functions for the given commands, returning a mapping of
|
|
98
|
+
// command ids to hotkey handlers.
|
|
99
|
+
export function getCommandHotkeyHandlers(commands) {
|
|
100
|
+
const handlers = {};
|
|
101
|
+
Object.keys(commands).forEach(cmdId => {
|
|
102
|
+
if (commands[cmdId].hotkey) {
|
|
103
|
+
handlers[cmdId] = event => {
|
|
104
|
+
if (!commands[cmdId].isDisabled && !commands[cmdId].isHidden) {
|
|
105
|
+
commands[cmdId].execute({ event, viaHotkey: true });
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return handlers;
|
|
112
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
import LocalizedFormat from "dayjs/plugin/localizedFormat";
|
|
3
|
+
import localeData from "dayjs/plugin/localeData";
|
|
4
|
+
|
|
5
|
+
dayjs.extend(localeData);
|
|
6
|
+
dayjs.extend(LocalizedFormat);
|
|
7
|
+
const userLocale = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
8
|
+
|
|
9
|
+
if (userLocale) {
|
|
10
|
+
const requireLocale = (newLocale, skipCall) => {
|
|
11
|
+
if (dayjs.locale() !== newLocale) {
|
|
12
|
+
try {
|
|
13
|
+
require(`dayjs/locale/${newLocale}.js`);
|
|
14
|
+
dayjs.locale(newLocale);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
// error
|
|
17
|
+
if (!skipCall && newLocale.includes("-")) {
|
|
18
|
+
requireLocale(newLocale.split("-")[0], true);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const localeToUse = userLocale.toLowerCase();
|
|
24
|
+
requireLocale(localeToUse);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default function getDayjsFormatter(format) {
|
|
28
|
+
return {
|
|
29
|
+
formatDate: date => dayjs(date).format(format),
|
|
30
|
+
parseDate: str => dayjs(str, format).toDate(),
|
|
31
|
+
placeholder: format?.toLowerCase().includes("l")
|
|
32
|
+
? dayjs.Ls[dayjs.locale()]?.formats[format.toUpperCase()]
|
|
33
|
+
: format
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
export default function getTextFromEl(el, options = {}) {
|
|
4
|
+
const { lowerCase } = options;
|
|
5
|
+
if (React.isValidElement(el)) {
|
|
6
|
+
return el && el.props && el.props.children
|
|
7
|
+
? (el.props.children.reduce
|
|
8
|
+
? el.props.children
|
|
9
|
+
: [el.props.children]
|
|
10
|
+
).reduce((acc, child) => {
|
|
11
|
+
if (child && child.props && child.props.children) {
|
|
12
|
+
acc += getTextFromEl(child);
|
|
13
|
+
} else if (typeof child === "string") {
|
|
14
|
+
if (lowerCase) {
|
|
15
|
+
acc += child.toLowerCase();
|
|
16
|
+
} else {
|
|
17
|
+
acc += child;
|
|
18
|
+
}
|
|
19
|
+
} else if (typeof child === "number") {
|
|
20
|
+
acc += child + "";
|
|
21
|
+
}
|
|
22
|
+
return acc;
|
|
23
|
+
}, "")
|
|
24
|
+
: "";
|
|
25
|
+
} else {
|
|
26
|
+
return el;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function onEnterHelper(callback) {
|
|
2
|
+
return {
|
|
3
|
+
onKeyDown: function(event) {
|
|
4
|
+
if (event.key === "Enter") {
|
|
5
|
+
callback(event);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function onBlurHelper(callback) {
|
|
12
|
+
return {
|
|
13
|
+
onBlur: function(event) {
|
|
14
|
+
callback(event);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function onEnterOrBlurHelper(callback) {
|
|
20
|
+
return {
|
|
21
|
+
onKeyDown: function(event) {
|
|
22
|
+
if (event.key === "Enter") {
|
|
23
|
+
callback(event);
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
onBlur: function(event) {
|
|
27
|
+
callback(event);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import React, { useMemo } from "react";
|
|
2
|
+
import { useHotkeys } from "@blueprintjs/core";
|
|
3
|
+
import { startCase } from "lodash";
|
|
4
|
+
|
|
5
|
+
// This has been mostly superseded by blueprint's KeyCombo component, but may
|
|
6
|
+
// still be useful for cases where we need plain text
|
|
7
|
+
export function comboToLabel(def, useSymbols = true) {
|
|
8
|
+
const combo = typeof def === "string" ? def : def.combo;
|
|
9
|
+
|
|
10
|
+
if (useSymbols) {
|
|
11
|
+
let parts = combo.replace("++", "+plus").split("+");
|
|
12
|
+
parts = parts.map(p => symbols[p] || startCase(p) || p);
|
|
13
|
+
return parts.join("");
|
|
14
|
+
} else {
|
|
15
|
+
return combo
|
|
16
|
+
.split("+")
|
|
17
|
+
.map(p => startCase(p) || p)
|
|
18
|
+
.join(" + ")
|
|
19
|
+
.replace("Meta", isMac ? "Cmd" : "Ctrl")
|
|
20
|
+
.replace("Mod", isMac ? "Cmd" : "Ctrl")
|
|
21
|
+
.replace("Alt", isMac ? "Option" : "Alt");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// HOF to get hotkey combos by id
|
|
26
|
+
export const hotkeysById = (hotkeys, mode = "raw") => id => {
|
|
27
|
+
const def = getHotkeyProps(hotkeys[id]);
|
|
28
|
+
return (
|
|
29
|
+
def &&
|
|
30
|
+
(mode === "raw" ? def.combo : comboToLabel(def.combo, mode === "symbols"))
|
|
31
|
+
);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Translate shorthand array if needed
|
|
35
|
+
export const getHotkeyProps = (def, id) => {
|
|
36
|
+
let out;
|
|
37
|
+
if (typeof def === "string") {
|
|
38
|
+
out = { combo: def };
|
|
39
|
+
} else if (def instanceof Array) {
|
|
40
|
+
out = { combo: def[0], label: def[1], ...(def[2] || {}) };
|
|
41
|
+
} else {
|
|
42
|
+
out = def;
|
|
43
|
+
}
|
|
44
|
+
out.label = out.label || startCase(id);
|
|
45
|
+
return out;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/*
|
|
49
|
+
* HOC to add hotkey support to components. Use this instead of blueprint's one.
|
|
50
|
+
*
|
|
51
|
+
* Arguments:
|
|
52
|
+
* - hotkeySpec: either a named hotkey section previously registered, or an
|
|
53
|
+
* object mapping command ids to hotkey definitions, where each hotkey can
|
|
54
|
+
* be either:
|
|
55
|
+
* - a string consisting in the key combo (e.g. 'ctrl+shift+x')
|
|
56
|
+
* - an array holding the combo, label, and an object with any other props
|
|
57
|
+
* - an object holding all props
|
|
58
|
+
* - handlers: an object mapping command ids to handler functions
|
|
59
|
+
* - options: an object that may specify the follownig options:
|
|
60
|
+
* - functional: boolean indicating if the wrapped component will be a
|
|
61
|
+
* functional stateless component instead of a class-based one
|
|
62
|
+
*
|
|
63
|
+
* Returns a function that can be invoked with a component class, or a
|
|
64
|
+
* stateless component function (if specified in the options) and returns
|
|
65
|
+
* the decorated class. It may also be invoked without arguments to generate a
|
|
66
|
+
* dummy ad-hoc component with no output.
|
|
67
|
+
*
|
|
68
|
+
*/
|
|
69
|
+
export const withHotkeys = (hotkeys, handlers) => {
|
|
70
|
+
return ({ children } = {}) => {
|
|
71
|
+
const memoedHotkeys = useMemo(
|
|
72
|
+
() =>
|
|
73
|
+
Object.keys(hotkeys).map(id => {
|
|
74
|
+
const { ...props } = getHotkeyProps(hotkeys[id], id);
|
|
75
|
+
return {
|
|
76
|
+
key: id,
|
|
77
|
+
global: props.global !== false,
|
|
78
|
+
onKeyDown: function(e) {
|
|
79
|
+
return handlers[id](e);
|
|
80
|
+
},
|
|
81
|
+
...props
|
|
82
|
+
};
|
|
83
|
+
}),
|
|
84
|
+
[]
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const { handleKeyDown, handleKeyUp } = useHotkeys(memoedHotkeys);
|
|
88
|
+
const newProps = {
|
|
89
|
+
tabIndex: 0,
|
|
90
|
+
onKeyDown: handleKeyDown,
|
|
91
|
+
onKeyUp: handleKeyUp
|
|
92
|
+
};
|
|
93
|
+
return children ? ( //tnr: if children are passed, we'll clone them with the new props
|
|
94
|
+
React.cloneElement(children, newProps)
|
|
95
|
+
) : (
|
|
96
|
+
//if not, then we'll return a div that can be used
|
|
97
|
+
<div className="hotkeyHandler" {...newProps}></div>
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const isMac = navigator.userAgent.includes("Mac OS X");
|
|
103
|
+
|
|
104
|
+
// TODO maybe avoid using symbols by default when not on Mac?
|
|
105
|
+
// Anyway, alternative 'Key + Key' description is provided as well
|
|
106
|
+
const symbols = {
|
|
107
|
+
cmd: "⌘",
|
|
108
|
+
meta: "⌘",
|
|
109
|
+
ctrl: "⌃",
|
|
110
|
+
alt: "⌥",
|
|
111
|
+
shift: "⇧",
|
|
112
|
+
esc: "␛", //'⎋',
|
|
113
|
+
enter: "⏎",
|
|
114
|
+
backspace: "⌫",
|
|
115
|
+
plus: "+",
|
|
116
|
+
tab: "⇥",
|
|
117
|
+
space: "␣",
|
|
118
|
+
capslock: "⇪",
|
|
119
|
+
pageup: "⇞",
|
|
120
|
+
pagedown: "⇟",
|
|
121
|
+
home: "↖",
|
|
122
|
+
end: "↘",
|
|
123
|
+
left: "←",
|
|
124
|
+
right: "→",
|
|
125
|
+
up: "↑",
|
|
126
|
+
down: "↓"
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
symbols.mod = symbols[isMac ? "meta" : "ctrl"];
|