@teselagen/ui 0.8.6-beta.24 → 0.8.6-beta.26

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 (56) hide show
  1. package/DataTable/utils/formatPasteData.d.ts +11 -5
  2. package/DataTable/utils/getAllRows.d.ts +4 -1
  3. package/DataTable/utils/getCellCopyText.d.ts +1 -1
  4. package/DataTable/utils/getCellInfo.d.ts +20 -15
  5. package/DataTable/utils/getFieldPathToField.d.ts +5 -1
  6. package/DataTable/utils/getIdOrCodeOrIndex.d.ts +2 -1
  7. package/DataTable/utils/getLastSelectedEntity.d.ts +7 -1
  8. package/DataTable/utils/getNewEntToSelect.d.ts +7 -6
  9. package/DataTable/utils/getRowCopyText.d.ts +1 -1
  10. package/DataTable/utils/initializeHasuraWhereAndFilter.d.ts +22 -1
  11. package/DataTable/utils/types/Entity.d.ts +5 -0
  12. package/DataTable/utils/types/Field.d.ts +4 -0
  13. package/DataTable/utils/types/OrderBy.d.ts +11 -0
  14. package/DataTable/utils/types/Schema.d.ts +4 -0
  15. package/DataTable/utils/utils.d.ts +16 -5
  16. package/MenuBar/index.d.ts +3 -1
  17. package/Timeline/TimelineEvent.d.ts +7 -4
  18. package/Timeline/index.d.ts +5 -1
  19. package/index.cjs.js +1638 -1806
  20. package/index.d.ts +0 -1
  21. package/index.es.js +1638 -1806
  22. package/package.json +3 -3
  23. package/src/DataTable/utils/formatPasteData.ts +34 -0
  24. package/src/DataTable/utils/getAllRows.ts +11 -0
  25. package/src/DataTable/utils/getCellCopyText.ts +7 -0
  26. package/src/DataTable/utils/getCellInfo.ts +46 -0
  27. package/src/DataTable/utils/getFieldPathToField.ts +10 -0
  28. package/src/DataTable/utils/getIdOrCodeOrIndex.ts +14 -0
  29. package/src/DataTable/utils/getLastSelectedEntity.ts +15 -0
  30. package/src/DataTable/utils/getNewEntToSelect.ts +32 -0
  31. package/src/DataTable/utils/initializeHasuraWhereAndFilter.ts +35 -0
  32. package/src/DataTable/utils/types/Entity.ts +7 -0
  33. package/src/DataTable/utils/types/Field.ts +4 -0
  34. package/src/DataTable/utils/types/OrderBy.ts +15 -0
  35. package/src/DataTable/utils/types/Schema.ts +5 -0
  36. package/src/DataTable/utils/utils.ts +39 -0
  37. package/src/Timeline/TimelineEvent.tsx +36 -0
  38. package/src/Timeline/index.tsx +21 -0
  39. package/src/index.js +0 -1
  40. package/src/utils/browserUtils.ts +3 -0
  41. package/src/utils/determineBlackOrWhiteTextColor.ts +11 -0
  42. package/src/utils/getTextFromEl.ts +45 -0
  43. package/src/utils/handlerHelpers.ts +32 -0
  44. package/src/utils/hooks/index.ts +1 -0
  45. package/src/utils/hooks/useDeepEqualMemo.ts +10 -0
  46. package/src/utils/hooks/useStableReference.ts +9 -0
  47. package/src/utils/hotkeyUtils.tsx +155 -0
  48. package/src/utils/isBeingCalledExcessively.ts +37 -0
  49. package/utils/browserUtils.d.ts +1 -1
  50. package/utils/determineBlackOrWhiteTextColor.d.ts +1 -1
  51. package/utils/getTextFromEl.d.ts +8 -1
  52. package/utils/handlerHelpers.d.ts +7 -7
  53. package/utils/hooks/useDeepEqualMemo.d.ts +1 -2
  54. package/utils/hooks/useStableReference.d.ts +1 -1
  55. package/utils/hotkeyUtils.d.ts +21 -4
  56. package/utils/isBeingCalledExcessively.d.ts +3 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teselagen/ui",
3
- "version": "0.8.6-beta.24",
3
+ "version": "0.8.6-beta.26",
4
4
  "main": "./src/index.js",
5
5
  "type": "module",
6
6
  "exports": {
@@ -19,7 +19,7 @@
19
19
  "@dnd-kit/sortable": "^8.0.0",
20
20
  "@teselagen/react-table": "6.10.18",
21
21
  "classnames": "^2.3.2",
22
- "color": "^3.2.1",
22
+ "color": "^5.0.0",
23
23
  "copy-to-clipboard": "^3.3.1",
24
24
  "dayjs": "^1.10.4",
25
25
  "dom-scroll-into-view": "^2.0.1",
@@ -30,7 +30,7 @@
30
30
  "lodash-es": "^4.17.21",
31
31
  "math-expression-evaluator": "^1.3.7",
32
32
  "mobx": "^6.10.2",
33
- "nanoid": "^4.0.0",
33
+ "nanoid": "^5.1.5",
34
34
  "papaparse": "5.3.2",
35
35
  "qs": "^6.9.6",
36
36
  "react": "^18.3.1",
@@ -0,0 +1,34 @@
1
+ import { getFieldPathToField } from "./getFieldPathToField";
2
+ import type { Schema } from "./types/Schema";
3
+
4
+ type GenericSelectValue = {
5
+ __strVal: string;
6
+ __genSelCol: string;
7
+ };
8
+
9
+ export const formatPasteData = ({
10
+ schema,
11
+ newVal,
12
+ path
13
+ }: {
14
+ schema: Schema;
15
+ newVal: GenericSelectValue | string | number | boolean | null | undefined;
16
+ path: string;
17
+ }) => {
18
+ const pathToField = getFieldPathToField(schema);
19
+ const column = pathToField[path];
20
+ if (column.type === "genericSelect") {
21
+ const value = newVal as GenericSelectValue;
22
+ if (value.__genSelCol === path) {
23
+ newVal = value.__strVal;
24
+ } else {
25
+ newVal = undefined;
26
+ }
27
+ } else {
28
+ newVal =
29
+ typeof newVal === "object" && newVal !== null && "__strVal" in newVal
30
+ ? newVal.__strVal
31
+ : newVal;
32
+ }
33
+ return newVal;
34
+ };
@@ -0,0 +1,11 @@
1
+ import type { RefObject } from "react";
2
+
3
+ export const getAllRows = (
4
+ tableRef: RefObject<{ tableRef: HTMLDivElement }>
5
+ ) => {
6
+ const allRowEls = tableRef.current?.tableRef?.querySelectorAll(".rt-tr");
7
+ if (!allRowEls || !allRowEls.length) {
8
+ return;
9
+ }
10
+ return allRowEls;
11
+ };
@@ -0,0 +1,7 @@
1
+ export const getCellCopyText = (cellWrapper: HTMLElement | null) => {
2
+ const text = cellWrapper?.getAttribute("data-copy-text");
3
+ const jsonText = cellWrapper?.getAttribute("data-copy-json");
4
+
5
+ const textContent = text || cellWrapper?.textContent || "";
6
+ return [textContent, jsonText];
7
+ };
@@ -0,0 +1,46 @@
1
+ import { getIdOrCodeOrIndex } from "./getIdOrCodeOrIndex";
2
+ import { Entity } from "./types/Entity";
3
+
4
+ export const getCellInfo = <T extends Entity>({
5
+ columnIndex,
6
+ columnPath,
7
+ rowId,
8
+ schema,
9
+ entities,
10
+ rowIndex,
11
+ isEntityDisabled,
12
+ entity
13
+ }: {
14
+ columnIndex: number;
15
+ columnPath: string;
16
+ rowId: string | number;
17
+ schema: { fields: { path: string }[] };
18
+ entities: T[];
19
+ rowIndex: number;
20
+ isEntityDisabled: (entity: T) => boolean;
21
+ entity: T;
22
+ }) => {
23
+ const leftpath = schema.fields[columnIndex - 1]?.path;
24
+ const rightpath = schema.fields[columnIndex + 1]?.path;
25
+ const cellIdToLeft = leftpath && `${rowId}:${leftpath}`;
26
+ const cellIdToRight = rightpath && `${rowId}:${rightpath}`;
27
+ const rowAboveId =
28
+ entities[rowIndex - 1] &&
29
+ getIdOrCodeOrIndex(entities[rowIndex - 1], rowIndex - 1);
30
+ const rowBelowId =
31
+ entities[rowIndex + 1] &&
32
+ getIdOrCodeOrIndex(entities[rowIndex + 1], rowIndex + 1);
33
+ const cellIdAbove = rowAboveId && `${rowAboveId}:${columnPath}`;
34
+ const cellIdBelow = rowBelowId && `${rowBelowId}:${columnPath}`;
35
+
36
+ const cellId = `${rowId}:${columnPath}`;
37
+ const rowDisabled = isEntityDisabled(entity);
38
+ return {
39
+ cellId,
40
+ cellIdAbove,
41
+ cellIdToRight,
42
+ cellIdBelow,
43
+ cellIdToLeft,
44
+ rowDisabled
45
+ };
46
+ };
@@ -0,0 +1,10 @@
1
+ import { Field } from "./types/Field";
2
+ import { Schema } from "./types/Schema";
3
+
4
+ export const getFieldPathToField = (schema: Schema) => {
5
+ const fieldPathToField: { [path: string]: Field } = {};
6
+ schema.fields.forEach(f => {
7
+ fieldPathToField[f.path] = f;
8
+ });
9
+ return fieldPathToField;
10
+ };
@@ -0,0 +1,14 @@
1
+ import type { Entity } from "./types/Entity";
2
+
3
+ export const getIdOrCodeOrIndex = (record: Entity, rowIndex?: number) => {
4
+ if ("id" in record && (record.id || record.id === 0)) {
5
+ return record.id;
6
+ } else if ("code" in record && record.code) {
7
+ return record.code;
8
+ } else {
9
+ if (rowIndex === undefined || rowIndex === null) {
10
+ throw new Error("id, code, or rowIndex must be provided");
11
+ }
12
+ return rowIndex;
13
+ }
14
+ };
@@ -0,0 +1,15 @@
1
+ import { Entity } from "./types/Entity";
2
+
3
+ export const getLastSelectedEntity = (idMap: {
4
+ [id: string]: { time: number; entity: Entity };
5
+ }) => {
6
+ let lastSelectedEnt;
7
+ let latestTime: number | null = null;
8
+ Object.values(idMap).forEach(({ time, entity }) => {
9
+ if (!latestTime || time > latestTime) {
10
+ lastSelectedEnt = entity;
11
+ latestTime = time;
12
+ }
13
+ });
14
+ return lastSelectedEnt;
15
+ };
@@ -0,0 +1,32 @@
1
+ import { Entity } from "./types/Entity";
2
+
3
+ export const getNewEntToSelect = ({
4
+ type,
5
+ lastSelectedIndex,
6
+ entities,
7
+ isEntityDisabled
8
+ }: {
9
+ type: "up" | "down";
10
+ lastSelectedIndex: number;
11
+ entities: Entity[];
12
+ isEntityDisabled?: (entity: Entity) => boolean;
13
+ }): Entity | undefined => {
14
+ let newIndexToSelect;
15
+ if (type === "up") {
16
+ newIndexToSelect = lastSelectedIndex - 1;
17
+ } else {
18
+ newIndexToSelect = lastSelectedIndex + 1;
19
+ }
20
+ const newEntToSelect = entities[newIndexToSelect];
21
+ if (!newEntToSelect) return;
22
+ if (isEntityDisabled && isEntityDisabled(newEntToSelect)) {
23
+ return getNewEntToSelect({
24
+ type,
25
+ lastSelectedIndex: newIndexToSelect,
26
+ entities,
27
+ isEntityDisabled
28
+ }) as Entity;
29
+ } else {
30
+ return newEntToSelect;
31
+ }
32
+ };
@@ -0,0 +1,35 @@
1
+ type Filter = {
2
+ [key: string]:
3
+ | { _eq: string | number | boolean }
4
+ | { _in: (string | number | boolean)[] }
5
+ | { _gt: number }
6
+ | { _lt: number }
7
+ | { _gte: number }
8
+ | { _lte: number };
9
+ };
10
+ type Where = { _and?: Filter[]; _or?: Filter[] };
11
+ type CurrentParams = object;
12
+
13
+ export function initializeHasuraWhereAndFilter(
14
+ additionalFilter:
15
+ | ((where: Where, currentParams: CurrentParams) => Filter | void)
16
+ | Filter
17
+ | undefined
18
+ | null,
19
+ where: Where = {},
20
+ currentParams: CurrentParams
21
+ ) {
22
+ where._and = where._and || [];
23
+ where._or = where._or || [];
24
+ if (typeof additionalFilter === "function") {
25
+ const newWhere = additionalFilter(where, currentParams);
26
+ if (newWhere) {
27
+ Object.assign(where, newWhere);
28
+ }
29
+ } else if (
30
+ typeof additionalFilter === "object" &&
31
+ additionalFilter !== null
32
+ ) {
33
+ where._and.push(additionalFilter);
34
+ }
35
+ }
@@ -0,0 +1,7 @@
1
+ export type Entity =
2
+ | {
3
+ id?: string | number;
4
+ }
5
+ | {
6
+ code?: string;
7
+ };
@@ -0,0 +1,4 @@
1
+ export type Field = {
2
+ path: string;
3
+ type: "genericSelect" | string;
4
+ };
@@ -0,0 +1,15 @@
1
+ type OrderByClause<T = { id: string }> = {
2
+ path?: string;
3
+ direction?: "asc" | "desc";
4
+ type?: string;
5
+ sortFn?:
6
+ | ((record: T) => unknown)
7
+ | string
8
+ | Array<((record: T) => unknown) | string>;
9
+ getValueToFilterOn?: (record: T) => unknown;
10
+ };
11
+
12
+ export type OrderBy =
13
+ | OrderByClause
14
+ | OrderByClause[]
15
+ | Record<string, "asc" | "desc">;
@@ -0,0 +1,5 @@
1
+ import { Field } from "./Field";
2
+
3
+ export type Schema = {
4
+ fields: Field[];
5
+ };
@@ -0,0 +1,39 @@
1
+ import { getIdOrCodeOrIndex } from "./getIdOrCodeOrIndex";
2
+ import { Entity } from "./types/Entity";
3
+ import { Field } from "./types/Field";
4
+
5
+ export const getFieldPathToIndex = (schema: { fields: Field[] }) => {
6
+ const fieldToIndex: { [path: string]: number } = {};
7
+ schema.fields.forEach((f, i) => {
8
+ fieldToIndex[f.path] = i;
9
+ });
10
+ return fieldToIndex;
11
+ };
12
+
13
+ export const defaultParsePaste = (str: string) => {
14
+ return str.split(/\r\n|\n|\r/).map(row => row.split("\t"));
15
+ };
16
+
17
+ export const getEntityIdToEntity = (entities: Entity[]) => {
18
+ const entityIdToEntity: { [id: string]: { e: Entity; i: number } } = {};
19
+ entities.forEach((e, i) => {
20
+ entityIdToEntity[getIdOrCodeOrIndex(e, i)] = { e, i };
21
+ });
22
+ return entityIdToEntity;
23
+ };
24
+
25
+ const endsWithNumber = (str: string) => {
26
+ return /[0-9]+$/.test(str);
27
+ };
28
+
29
+ export const getNumberStrAtEnd = (str: string) => {
30
+ if (endsWithNumber(str)) {
31
+ return str.match(/[0-9]+$/)?.[0];
32
+ }
33
+
34
+ return null;
35
+ };
36
+
37
+ export const stripNumberAtEnd = (str: string) => {
38
+ return str?.replace?.(getNumberStrAtEnd(str) || "", "");
39
+ };
@@ -0,0 +1,36 @@
1
+ import React from "react";
2
+ import dayjs from "dayjs";
3
+ import { Classes } from "@blueprintjs/core";
4
+ import classNames from "classnames";
5
+ import relativeTime from "dayjs/plugin/relativeTime";
6
+
7
+ dayjs.extend(relativeTime);
8
+
9
+ interface TimelineEventProps {
10
+ date: string | number | Date | dayjs.Dayjs;
11
+ children?: React.ReactNode;
12
+ }
13
+
14
+ function TimelineEvent({ date, children }: TimelineEventProps) {
15
+ return (
16
+ <div className="tg-timeline-event">
17
+ <div
18
+ style={{
19
+ display: "flex",
20
+ alignItems: "center"
21
+ }}
22
+ >
23
+ <div className="tg-timeline-circle" />
24
+ {children}
25
+ <div
26
+ style={{ marginLeft: 5 }}
27
+ className={classNames(Classes.TEXT_SMALL, Classes.TEXT_MUTED)}
28
+ >
29
+ ({dayjs(date).fromNow()})
30
+ </div>
31
+ </div>
32
+ </div>
33
+ );
34
+ }
35
+
36
+ export default TimelineEvent;
@@ -0,0 +1,21 @@
1
+ import React, { ReactNode, FC } from "react";
2
+ import "./style.css";
3
+
4
+ interface TimelineProps {
5
+ children: ReactNode;
6
+ }
7
+
8
+ const Timeline: FC<TimelineProps> = props => {
9
+ return (
10
+ <div className="tg-timeline">
11
+ {React.Children.count(props.children) > 1 && (
12
+ <div className="tg-timeline-line" />
13
+ )}
14
+ {props.children}
15
+ </div>
16
+ );
17
+ };
18
+
19
+ export { default as TimelineEvent } from "./TimelineEvent";
20
+
21
+ export default Timeline;
package/src/index.js CHANGED
@@ -35,7 +35,6 @@ export { default as PromptUnsavedChanges } from "./PromptUnsavedChanges";
35
35
  export { default as BlueprintError } from "./BlueprintError";
36
36
  export { default as DropdownButton } from "./DropdownButton";
37
37
  export { default as DialogFooter } from "./DialogFooter";
38
- export { default as adHoc } from "./utils/adHoc";
39
38
  export { default as IntentText } from "./IntentText";
40
39
  export { default as popoverOverflowModifiers } from "./utils/popoverOverflowModifiers";
41
40
  export * from "./utils/tgFormValues";
@@ -0,0 +1,3 @@
1
+ export const isSafari = /^((?!chrome|android).)*safari/i.test(
2
+ navigator.userAgent
3
+ );
@@ -0,0 +1,11 @@
1
+ /* Copyright (C) 2018 TeselaGen Biotechnology, Inc. */
2
+ import Color from "color";
3
+
4
+ export default function determineBlackOrWhiteTextColor(c: string) {
5
+ try {
6
+ return Color(c).isLight() ? "#000000" : "#FFFFFF";
7
+ } catch (e) {
8
+ console.error("Error in color parsing:", e);
9
+ return "#000000"; // Fallback to black if color parsing fails
10
+ }
11
+ }
@@ -0,0 +1,45 @@
1
+ import React from "react";
2
+
3
+ type Node = React.ReactElement<{ children?: Node[] | Node }> | string | number;
4
+
5
+ const isReactElement = (
6
+ el: Node
7
+ ): el is React.ReactElement<{ children?: Node[] | Node }> => {
8
+ if (el) {
9
+ const newEl = el as React.ReactElement<{ children?: Node[] | Node }>;
10
+ if (newEl.props && newEl.props.children) {
11
+ return true;
12
+ }
13
+ }
14
+ return false;
15
+ };
16
+
17
+ export default function getTextFromEl<T extends Node>(
18
+ el: T,
19
+ options: { lowerCase?: boolean } = {}
20
+ ): string {
21
+ const { lowerCase } = options;
22
+ if (React.isValidElement<{ children?: Node[] | Node }>(el)) {
23
+ return el && el.props && el.props.children
24
+ ? (Array.isArray(el.props.children)
25
+ ? el.props.children
26
+ : [el.props.children]
27
+ ).reduce((acc: string, child) => {
28
+ if (isReactElement(child)) {
29
+ acc += getTextFromEl(child);
30
+ } else if (typeof child === "string") {
31
+ if (lowerCase) {
32
+ acc += child.toLowerCase();
33
+ } else {
34
+ acc += child;
35
+ }
36
+ } else if (typeof child === "number") {
37
+ acc += child + "";
38
+ }
39
+ return acc;
40
+ }, "")
41
+ : "";
42
+ } else {
43
+ return el as string;
44
+ }
45
+ }
@@ -0,0 +1,32 @@
1
+ export const onEnterHelper = (
2
+ callback: (event: React.KeyboardEvent<Element>) => void
3
+ ) => ({
4
+ onKeyDown: (event: React.KeyboardEvent<Element>) => {
5
+ if (event.key === "Enter") {
6
+ callback(event);
7
+ }
8
+ }
9
+ });
10
+
11
+ export const onBlurHelper = (
12
+ callback: (event: React.FocusEvent<Element>) => void
13
+ ) => ({
14
+ onBlur: (event: React.FocusEvent<Element>) => {
15
+ callback(event);
16
+ }
17
+ });
18
+
19
+ export const onEnterOrBlurHelper = (
20
+ callback: (
21
+ event: React.KeyboardEvent<Element> | React.FocusEvent<Element>
22
+ ) => void
23
+ ) => ({
24
+ onKeyDown: function (event: React.KeyboardEvent<Element>) {
25
+ if (event.key === "Enter") {
26
+ callback(event);
27
+ }
28
+ },
29
+ onBlur: function (event: React.FocusEvent<Element>) {
30
+ callback(event);
31
+ }
32
+ });
@@ -0,0 +1 @@
1
+ export { useDeepEqualMemo } from "./useDeepEqualMemo";
@@ -0,0 +1,10 @@
1
+ import { isEqual } from "lodash-es";
2
+ import { useRef } from "react";
3
+
4
+ export const useDeepEqualMemo = (value: unknown) => {
5
+ const ref = useRef<unknown>();
6
+ if (!isEqual(value, ref.current)) {
7
+ ref.current = value;
8
+ }
9
+ return ref.current;
10
+ };
@@ -0,0 +1,9 @@
1
+ import { useEffect, useRef } from "react";
2
+
3
+ export const useStableReference = (value: unknown) => {
4
+ const ref = useRef<unknown>();
5
+ useEffect(() => {
6
+ ref.current = value;
7
+ }, [value]);
8
+ return ref;
9
+ };
@@ -0,0 +1,155 @@
1
+ import React, { useMemo } from "react";
2
+ import { useHotkeys } from "@blueprintjs/core";
3
+ import { startCase } from "lodash-es";
4
+
5
+ type Out = {
6
+ combo: string;
7
+ label: string;
8
+ [key: string]: unknown;
9
+ global?: boolean;
10
+ };
11
+ type Hotkeys = {
12
+ [key: string]: string | [string, string, object] | Out;
13
+ };
14
+
15
+ // This has been mostly superseded by blueprint's KeyCombo component, but may
16
+ // still be useful for cases where we need plain text
17
+ export function comboToLabel(
18
+ def: string | { combo: string },
19
+ useSymbols = true
20
+ ) {
21
+ const combo = typeof def === "string" ? def : def.combo;
22
+
23
+ if (useSymbols) {
24
+ let parts = combo.replace("++", "+plus").split("+");
25
+ parts = parts.map(p =>
26
+ p in symbols ? symbols[p as keyof typeof symbols] : startCase(p) || p
27
+ );
28
+ return parts.join("");
29
+ } else {
30
+ return combo
31
+ .split("+")
32
+ .map(p => startCase(p) || p)
33
+ .join(" + ")
34
+ .replace("Meta", isMac ? "Cmd" : "Ctrl")
35
+ .replace("Mod", isMac ? "Cmd" : "Ctrl")
36
+ .replace("Alt", isMac ? "Option" : "Alt");
37
+ }
38
+ }
39
+
40
+ // HOF to get hotkey combos by id
41
+ export const hotkeysById =
42
+ (hotkeys: Hotkeys, mode = "raw") =>
43
+ (id: string) => {
44
+ const def = getHotkeyProps(hotkeys[id]);
45
+ return (
46
+ def &&
47
+ (mode === "raw" ? def.combo : comboToLabel(def.combo, mode === "symbols"))
48
+ );
49
+ };
50
+
51
+ // Translate shorthand array if needed
52
+ export const getHotkeyProps = (
53
+ def: string | [string, string, object] | Out,
54
+ id?: string
55
+ ) => {
56
+ let out: Out;
57
+ if (typeof def === "string") {
58
+ out = { combo: def, label: def };
59
+ } else if (def instanceof Array) {
60
+ out = { combo: def[0], label: def[1], ...(def[2] || {}) };
61
+ } else {
62
+ out = def;
63
+ }
64
+ out.label = out.label || startCase(id);
65
+ return out;
66
+ };
67
+
68
+ /*
69
+ * HOC to add hotkey support to components. Use this instead of blueprint's one.
70
+ *
71
+ * Arguments:
72
+ * - hotkeySpec: either a named hotkey section previously registered, or an
73
+ * object mapping command ids to hotkey definitions, where each hotkey can
74
+ * be either:
75
+ * - a string consisting in the key combo (e.g. 'ctrl+shift+x')
76
+ * - an array holding the combo, label, and an object with any other props
77
+ * - an object holding all props
78
+ * - handlers: an object mapping command ids to handler functions
79
+ * - options: an object that may specify the follownig options:
80
+ * - functional: boolean indicating if the wrapped component will be a
81
+ * functional stateless component instead of a class-based one
82
+ *
83
+ * Returns a function that can be invoked with a component class, or a
84
+ * stateless component function (if specified in the options) and returns
85
+ * the decorated class. It may also be invoked without arguments to generate a
86
+ * dummy ad-hoc component with no output.
87
+ *
88
+ */
89
+ export const withHotkeys = (
90
+ hotkeys: Hotkeys,
91
+ handlers: { [key: string]: (e: KeyboardEvent) => void }
92
+ ) => {
93
+ return ({ children }: { children?: React.ReactElement } = {}) => {
94
+ const memoedHotkeys = useMemo(
95
+ () =>
96
+ Object.keys(hotkeys).map(id => {
97
+ const { ...props } = getHotkeyProps(hotkeys[id], id);
98
+ return {
99
+ key: id,
100
+ global: props.global !== false,
101
+ onKeyDown: function (e: KeyboardEvent) {
102
+ return handlers[id](e);
103
+ },
104
+ ...props
105
+ };
106
+ }),
107
+ []
108
+ );
109
+
110
+ const { handleKeyDown, handleKeyUp } = useHotkeys(memoedHotkeys);
111
+ const newProps = {
112
+ tabIndex: 0,
113
+ onKeyDown: handleKeyDown,
114
+ onKeyUp: handleKeyUp
115
+ };
116
+ return children ? ( //tnr: if children are passed, we'll clone them with the new props
117
+ React.cloneElement(children, newProps)
118
+ ) : (
119
+ //if not, then we'll return a div that can be used
120
+ <div className="hotkeyHandler" {...newProps} />
121
+ );
122
+ };
123
+ };
124
+
125
+ const isMac = navigator.userAgent.includes("Mac OS X");
126
+
127
+ const cmd = "⌘";
128
+ const meta = "⌘";
129
+ const ctrl = "⌃";
130
+
131
+ // TODO maybe avoid using symbols by default when not on Mac?
132
+ // Anyway, alternative 'Key + Key' description is provided as well
133
+ const symbols = {
134
+ cmd,
135
+ meta,
136
+ ctrl,
137
+ alt: "⌥",
138
+ shift: "⇧",
139
+ esc: "␛", //'⎋',
140
+ enter: "⏎",
141
+ backspace: "⌫",
142
+ plus: "+",
143
+ tab: "⇥",
144
+ space: "␣",
145
+ capslock: "⇪",
146
+ pageup: "⇞",
147
+ pagedown: "⇟",
148
+ home: "↖",
149
+ end: "↘",
150
+ left: "←",
151
+ right: "→",
152
+ up: "↑",
153
+ down: "↓",
154
+ mod: isMac ? cmd : ctrl
155
+ } as const;