tgui-core 1.0.0
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/.editorconfig +10 -0
- package/.eslintrc.cjs +78 -0
- package/.gitattributes +4 -0
- package/.prettierrc.yml +1 -0
- package/.vscode/extensions.json +6 -0
- package/.vscode/settings.json +5 -0
- package/README.md +1 -0
- package/global.d.ts +173 -0
- package/package.json +25 -0
- package/src/assets.ts +43 -0
- package/src/backend.ts +369 -0
- package/src/common/collections.ts +349 -0
- package/src/common/color.ts +94 -0
- package/src/common/events.ts +45 -0
- package/src/common/exhaustive.ts +19 -0
- package/src/common/fp.ts +38 -0
- package/src/common/keycodes.ts +86 -0
- package/src/common/keys.ts +39 -0
- package/src/common/math.ts +98 -0
- package/src/common/perf.ts +72 -0
- package/src/common/random.ts +32 -0
- package/src/common/react.ts +65 -0
- package/src/common/redux.ts +196 -0
- package/src/common/storage.js +196 -0
- package/src/common/string.ts +173 -0
- package/src/common/timer.ts +68 -0
- package/src/common/type-utils.ts +41 -0
- package/src/common/types.ts +9 -0
- package/src/common/uuid.ts +24 -0
- package/src/common/vector.ts +51 -0
- package/src/components/AnimatedNumber.tsx +185 -0
- package/src/components/Autofocus.tsx +23 -0
- package/src/components/Blink.jsx +69 -0
- package/src/components/BlockQuote.tsx +15 -0
- package/src/components/BodyZoneSelector.tsx +149 -0
- package/src/components/Box.tsx +255 -0
- package/src/components/Button.tsx +415 -0
- package/src/components/ByondUi.jsx +121 -0
- package/src/components/Chart.tsx +160 -0
- package/src/components/Collapsible.tsx +45 -0
- package/src/components/ColorBox.tsx +30 -0
- package/src/components/Dialog.tsx +85 -0
- package/src/components/Dimmer.tsx +19 -0
- package/src/components/Divider.tsx +26 -0
- package/src/components/DmIcon.tsx +72 -0
- package/src/components/DraggableControl.jsx +282 -0
- package/src/components/Dropdown.tsx +246 -0
- package/src/components/FakeTerminal.jsx +52 -0
- package/src/components/FitText.tsx +99 -0
- package/src/components/Flex.tsx +105 -0
- package/src/components/Grid.tsx +44 -0
- package/src/components/Icon.tsx +91 -0
- package/src/components/Image.tsx +63 -0
- package/src/components/InfinitePlane.jsx +192 -0
- package/src/components/Input.tsx +181 -0
- package/src/components/KeyListener.tsx +40 -0
- package/src/components/Knob.tsx +185 -0
- package/src/components/LabeledControls.tsx +50 -0
- package/src/components/LabeledList.tsx +130 -0
- package/src/components/MenuBar.tsx +238 -0
- package/src/components/Modal.tsx +25 -0
- package/src/components/NoticeBox.tsx +48 -0
- package/src/components/NumberInput.tsx +328 -0
- package/src/components/Popper.tsx +100 -0
- package/src/components/ProgressBar.tsx +79 -0
- package/src/components/RestrictedInput.jsx +301 -0
- package/src/components/RoundGauge.tsx +189 -0
- package/src/components/Section.tsx +125 -0
- package/src/components/Slider.tsx +173 -0
- package/src/components/Stack.tsx +101 -0
- package/src/components/StyleableSection.tsx +30 -0
- package/src/components/Table.tsx +90 -0
- package/src/components/Tabs.tsx +90 -0
- package/src/components/TextArea.tsx +198 -0
- package/src/components/TimeDisplay.jsx +64 -0
- package/src/components/Tooltip.tsx +147 -0
- package/src/components/TrackOutsideClicks.tsx +35 -0
- package/src/components/VirtualList.tsx +69 -0
- package/src/constants.ts +355 -0
- package/src/debug/KitchenSink.jsx +56 -0
- package/src/debug/actions.js +11 -0
- package/src/debug/hooks.js +10 -0
- package/src/debug/index.ts +10 -0
- package/src/debug/middleware.js +86 -0
- package/src/debug/reducer.js +22 -0
- package/src/debug/selectors.js +7 -0
- package/src/drag.ts +280 -0
- package/src/events.ts +237 -0
- package/src/focus.ts +25 -0
- package/src/format.ts +173 -0
- package/src/hotkeys.ts +212 -0
- package/src/http.ts +16 -0
- package/src/layouts/Layout.tsx +75 -0
- package/src/layouts/NtosWindow.tsx +162 -0
- package/src/layouts/Pane.tsx +56 -0
- package/src/layouts/Window.tsx +227 -0
- package/src/renderer.ts +50 -0
- package/styles/base.scss +32 -0
- package/styles/colors.scss +92 -0
- package/styles/components/BlockQuote.scss +20 -0
- package/styles/components/Button.scss +175 -0
- package/styles/components/ColorBox.scss +12 -0
- package/styles/components/Dialog.scss +105 -0
- package/styles/components/Dimmer.scss +22 -0
- package/styles/components/Divider.scss +27 -0
- package/styles/components/Dropdown.scss +72 -0
- package/styles/components/Flex.scss +31 -0
- package/styles/components/Icon.scss +25 -0
- package/styles/components/Input.scss +68 -0
- package/styles/components/Knob.scss +131 -0
- package/styles/components/LabeledList.scss +49 -0
- package/styles/components/MenuBar.scss +75 -0
- package/styles/components/Modal.scss +14 -0
- package/styles/components/NoticeBox.scss +65 -0
- package/styles/components/NumberInput.scss +76 -0
- package/styles/components/ProgressBar.scss +63 -0
- package/styles/components/RoundGauge.scss +88 -0
- package/styles/components/Section.scss +143 -0
- package/styles/components/Slider.scss +54 -0
- package/styles/components/Stack.scss +59 -0
- package/styles/components/Table.scss +44 -0
- package/styles/components/Tabs.scss +144 -0
- package/styles/components/TextArea.scss +84 -0
- package/styles/components/Tooltip.scss +24 -0
- package/styles/functions.scss +79 -0
- package/styles/layouts/Layout.scss +57 -0
- package/styles/layouts/NtosHeader.scss +20 -0
- package/styles/layouts/NtosWindow.scss +26 -0
- package/styles/layouts/TitleBar.scss +111 -0
- package/styles/layouts/Window.scss +103 -0
- package/styles/main.scss +97 -0
- package/styles/reset.scss +68 -0
- package/styles/themes/abductor.scss +68 -0
- package/styles/themes/admin.scss +38 -0
- package/styles/themes/cardtable.scss +57 -0
- package/styles/themes/hackerman.scss +70 -0
- package/styles/themes/malfunction.scss +67 -0
- package/styles/themes/neutral.scss +50 -0
- package/styles/themes/ntOS95.scss +166 -0
- package/styles/themes/ntos.scss +44 -0
- package/styles/themes/ntos_cat.scss +148 -0
- package/styles/themes/ntos_darkmode.scss +44 -0
- package/styles/themes/ntos_lightmode.scss +67 -0
- package/styles/themes/ntos_spooky.scss +69 -0
- package/styles/themes/ntos_synth.scss +99 -0
- package/styles/themes/ntos_terminal.scss +112 -0
- package/styles/themes/paper.scss +184 -0
- package/styles/themes/retro.scss +72 -0
- package/styles/themes/spookyconsole.scss +73 -0
- package/styles/themes/syndicate.scss +67 -0
- package/styles/themes/wizard.scss +68 -0
- package/tsconfig.json +34 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @copyright 2020 Aleksej Komarov
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { classes } from '../common/react';
|
|
8
|
+
|
|
9
|
+
import { BoxProps, computeBoxClassName, computeBoxProps, unit } from './Box';
|
|
10
|
+
|
|
11
|
+
export type FlexProps = Partial<{
|
|
12
|
+
align: string | boolean;
|
|
13
|
+
direction: string;
|
|
14
|
+
inline: boolean;
|
|
15
|
+
justify: string;
|
|
16
|
+
scrollable: boolean;
|
|
17
|
+
style: Partial<HTMLDivElement['style']>;
|
|
18
|
+
wrap: string | boolean;
|
|
19
|
+
}> &
|
|
20
|
+
BoxProps;
|
|
21
|
+
|
|
22
|
+
export const computeFlexClassName = (props: FlexProps) => {
|
|
23
|
+
return classes([
|
|
24
|
+
'Flex',
|
|
25
|
+
props.inline && 'Flex--inline',
|
|
26
|
+
computeBoxClassName(props),
|
|
27
|
+
]);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const computeFlexProps = (props: FlexProps) => {
|
|
31
|
+
const { className, direction, wrap, align, justify, inline, ...rest } = props;
|
|
32
|
+
|
|
33
|
+
return computeBoxProps({
|
|
34
|
+
style: {
|
|
35
|
+
...rest.style,
|
|
36
|
+
flexDirection: direction,
|
|
37
|
+
flexWrap: wrap === true ? 'wrap' : wrap,
|
|
38
|
+
alignItems: align,
|
|
39
|
+
justifyContent: justify,
|
|
40
|
+
},
|
|
41
|
+
...rest,
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const Flex = (props) => {
|
|
46
|
+
const { className, ...rest } = props;
|
|
47
|
+
return (
|
|
48
|
+
<div
|
|
49
|
+
className={classes([className, computeFlexClassName(rest)])}
|
|
50
|
+
{...computeFlexProps(rest)}
|
|
51
|
+
/>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type FlexItemProps = BoxProps &
|
|
56
|
+
Partial<{
|
|
57
|
+
grow: number | boolean;
|
|
58
|
+
order: number;
|
|
59
|
+
shrink: number | boolean;
|
|
60
|
+
basis: string | number;
|
|
61
|
+
align: string | boolean;
|
|
62
|
+
style: Partial<HTMLDivElement['style']>;
|
|
63
|
+
}>;
|
|
64
|
+
|
|
65
|
+
export const computeFlexItemClassName = (props: FlexItemProps) => {
|
|
66
|
+
return classes(['Flex__item', computeBoxClassName(props)]);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const computeFlexItemProps = (props: FlexItemProps) => {
|
|
70
|
+
const { className, style, grow, order, shrink, basis, align, ...rest } =
|
|
71
|
+
props;
|
|
72
|
+
|
|
73
|
+
const computedBasis =
|
|
74
|
+
basis ??
|
|
75
|
+
// IE11: Set basis to specified width if it's known, which fixes certain
|
|
76
|
+
// bugs when rendering tables inside the flex.
|
|
77
|
+
props.width ??
|
|
78
|
+
// If grow is used, basis should be set to 0 to be consistent with
|
|
79
|
+
// flex css shorthand `flex: 1`.
|
|
80
|
+
(grow !== undefined ? 0 : undefined);
|
|
81
|
+
|
|
82
|
+
return computeBoxProps({
|
|
83
|
+
style: {
|
|
84
|
+
...style,
|
|
85
|
+
flexGrow: grow !== undefined && Number(grow),
|
|
86
|
+
flexShrink: shrink !== undefined && Number(shrink),
|
|
87
|
+
flexBasis: unit(computedBasis),
|
|
88
|
+
order: order,
|
|
89
|
+
alignSelf: align,
|
|
90
|
+
},
|
|
91
|
+
...rest,
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const FlexItem = (props) => {
|
|
96
|
+
const { className, ...rest } = props;
|
|
97
|
+
return (
|
|
98
|
+
<div
|
|
99
|
+
className={classes([className, computeFlexItemClassName(props)])}
|
|
100
|
+
{...computeFlexItemProps(rest)}
|
|
101
|
+
/>
|
|
102
|
+
);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
Flex.Item = FlexItem;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @copyright 2020 Aleksej Komarov
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { PropsWithChildren } from 'react';
|
|
8
|
+
|
|
9
|
+
import { logger } from '../logging';
|
|
10
|
+
import { BoxProps } from './Box';
|
|
11
|
+
import { Table } from './Table';
|
|
12
|
+
|
|
13
|
+
/** @deprecated Do not use. Use stack instead. */
|
|
14
|
+
export function Grid(props: PropsWithChildren<BoxProps>) {
|
|
15
|
+
const { children, ...rest } = props;
|
|
16
|
+
logger.error('Grid component is deprecated. Use a Stack instead.');
|
|
17
|
+
return (
|
|
18
|
+
<Table {...rest}>
|
|
19
|
+
<Table.Row>{children}</Table.Row>
|
|
20
|
+
</Table>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type Props = Partial<{
|
|
25
|
+
/** Width of the column in percentage. */
|
|
26
|
+
size: number;
|
|
27
|
+
}> &
|
|
28
|
+
BoxProps;
|
|
29
|
+
|
|
30
|
+
/** @deprecated Do not use. Use stack instead. */
|
|
31
|
+
export function GridColumn(props: Props) {
|
|
32
|
+
const { size = 1, style, ...rest } = props;
|
|
33
|
+
return (
|
|
34
|
+
<Table.Cell
|
|
35
|
+
style={{
|
|
36
|
+
width: size + '%',
|
|
37
|
+
...style,
|
|
38
|
+
}}
|
|
39
|
+
{...rest}
|
|
40
|
+
/>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
Grid.Column = GridColumn;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @copyright 2020 Aleksej Komarov
|
|
4
|
+
* @author Original Aleksej Komarov
|
|
5
|
+
* @author Changes ThePotato97
|
|
6
|
+
* @license MIT
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { BooleanLike, classes } from '../common/react';
|
|
10
|
+
import { ReactNode } from 'react';
|
|
11
|
+
|
|
12
|
+
import { BoxProps, computeBoxClassName, computeBoxProps } from './Box';
|
|
13
|
+
|
|
14
|
+
const FA_OUTLINE_REGEX = /-o$/;
|
|
15
|
+
|
|
16
|
+
type IconPropsUnique = { name: string } & Partial<{
|
|
17
|
+
size: number;
|
|
18
|
+
spin: BooleanLike;
|
|
19
|
+
className: string;
|
|
20
|
+
rotation: number;
|
|
21
|
+
style: Partial<HTMLDivElement['style']>;
|
|
22
|
+
}>;
|
|
23
|
+
|
|
24
|
+
export type IconProps = IconPropsUnique & BoxProps;
|
|
25
|
+
|
|
26
|
+
export const Icon = (props: IconProps) => {
|
|
27
|
+
const { name, size, spin, className, rotation, ...rest } = props;
|
|
28
|
+
|
|
29
|
+
const customStyle = rest.style || {};
|
|
30
|
+
if (size) {
|
|
31
|
+
customStyle.fontSize = size * 100 + '%';
|
|
32
|
+
}
|
|
33
|
+
if (rotation) {
|
|
34
|
+
customStyle.transform = `rotate(${rotation}deg)`;
|
|
35
|
+
}
|
|
36
|
+
rest.style = customStyle;
|
|
37
|
+
|
|
38
|
+
const boxProps = computeBoxProps(rest);
|
|
39
|
+
|
|
40
|
+
let iconClass = '';
|
|
41
|
+
if (name.startsWith('tg-')) {
|
|
42
|
+
// tgfont icon
|
|
43
|
+
iconClass = name;
|
|
44
|
+
} else {
|
|
45
|
+
// font awesome icon
|
|
46
|
+
const faRegular = FA_OUTLINE_REGEX.test(name);
|
|
47
|
+
const faName = name.replace(FA_OUTLINE_REGEX, '');
|
|
48
|
+
const preprendFa = !faName.startsWith('fa-');
|
|
49
|
+
|
|
50
|
+
iconClass = faRegular ? 'far ' : 'fas ';
|
|
51
|
+
if (preprendFa) {
|
|
52
|
+
iconClass += 'fa-';
|
|
53
|
+
}
|
|
54
|
+
iconClass += faName;
|
|
55
|
+
if (spin) {
|
|
56
|
+
iconClass += ' fa-spin';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return (
|
|
60
|
+
<i
|
|
61
|
+
className={classes([
|
|
62
|
+
'Icon',
|
|
63
|
+
iconClass,
|
|
64
|
+
className,
|
|
65
|
+
computeBoxClassName(rest),
|
|
66
|
+
])}
|
|
67
|
+
{...boxProps}
|
|
68
|
+
/>
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
type IconStackUnique = {
|
|
73
|
+
children: ReactNode;
|
|
74
|
+
className?: string;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type IconStackProps = IconStackUnique & BoxProps;
|
|
78
|
+
|
|
79
|
+
export const IconStack = (props: IconStackProps) => {
|
|
80
|
+
const { className, children, ...rest } = props;
|
|
81
|
+
return (
|
|
82
|
+
<span
|
|
83
|
+
className={classes(['IconStack', className, computeBoxClassName(rest)])}
|
|
84
|
+
{...computeBoxProps(rest)}
|
|
85
|
+
>
|
|
86
|
+
{children}
|
|
87
|
+
</span>
|
|
88
|
+
);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
Icon.Stack = IconStack;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
import { BoxProps, computeBoxProps } from './Box';
|
|
4
|
+
|
|
5
|
+
type Props = Partial<{
|
|
6
|
+
/** True is default, this fixes an ie thing */
|
|
7
|
+
fixBlur: boolean;
|
|
8
|
+
/** False by default. Good if you're fetching images on UIs that do not auto update. This will attempt to fix the 'x' icon 5 times. */
|
|
9
|
+
fixErrors: boolean;
|
|
10
|
+
/** Fill is default. */
|
|
11
|
+
objectFit: 'contain' | 'cover';
|
|
12
|
+
}> &
|
|
13
|
+
IconUnion &
|
|
14
|
+
BoxProps;
|
|
15
|
+
|
|
16
|
+
// at least one of these is required
|
|
17
|
+
type IconUnion =
|
|
18
|
+
| {
|
|
19
|
+
className?: string;
|
|
20
|
+
src: string;
|
|
21
|
+
}
|
|
22
|
+
| {
|
|
23
|
+
className: string;
|
|
24
|
+
src?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const maxAttempts = 5;
|
|
28
|
+
|
|
29
|
+
/** Image component. Use this instead of Box as="img". */
|
|
30
|
+
export function Image(props: Props) {
|
|
31
|
+
const {
|
|
32
|
+
fixBlur = true,
|
|
33
|
+
fixErrors = false,
|
|
34
|
+
objectFit = 'fill',
|
|
35
|
+
src,
|
|
36
|
+
...rest
|
|
37
|
+
} = props;
|
|
38
|
+
const attempts = useRef(0);
|
|
39
|
+
|
|
40
|
+
const computedProps = computeBoxProps(rest);
|
|
41
|
+
computedProps['style'] = {
|
|
42
|
+
...computedProps.style,
|
|
43
|
+
'-ms-interpolation-mode': fixBlur ? 'nearest-neighbor' : 'auto',
|
|
44
|
+
objectFit,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<img
|
|
49
|
+
onError={(event) => {
|
|
50
|
+
if (fixErrors && attempts.current < maxAttempts) {
|
|
51
|
+
const imgElement = event.currentTarget;
|
|
52
|
+
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
imgElement.src = `${src}?attempt=${attempts.current}`;
|
|
55
|
+
attempts.current++;
|
|
56
|
+
}, 1000);
|
|
57
|
+
}
|
|
58
|
+
}}
|
|
59
|
+
src={src}
|
|
60
|
+
{...computedProps}
|
|
61
|
+
/>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Component } from 'react';
|
|
2
|
+
|
|
3
|
+
import { computeBoxProps } from './Box';
|
|
4
|
+
import { Button } from './Button';
|
|
5
|
+
import { ProgressBar } from './ProgressBar';
|
|
6
|
+
import { Stack } from './Stack';
|
|
7
|
+
|
|
8
|
+
const ZOOM_MIN_VAL = 0.5;
|
|
9
|
+
const ZOOM_MAX_VAL = 1.5;
|
|
10
|
+
|
|
11
|
+
const ZOOM_INCREMENT = 0.1;
|
|
12
|
+
|
|
13
|
+
export class InfinitePlane extends Component {
|
|
14
|
+
constructor(props) {
|
|
15
|
+
super(props);
|
|
16
|
+
|
|
17
|
+
this.state = {
|
|
18
|
+
mouseDown: false,
|
|
19
|
+
|
|
20
|
+
left: 0,
|
|
21
|
+
top: 0,
|
|
22
|
+
|
|
23
|
+
lastLeft: 0,
|
|
24
|
+
lastTop: 0,
|
|
25
|
+
|
|
26
|
+
zoom: 1,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
this.handleMouseDown = this.handleMouseDown.bind(this);
|
|
30
|
+
this.handleMouseMove = this.handleMouseMove.bind(this);
|
|
31
|
+
this.handleZoomIncrease = this.handleZoomIncrease.bind(this);
|
|
32
|
+
this.handleZoomDecrease = this.handleZoomDecrease.bind(this);
|
|
33
|
+
this.onMouseUp = this.onMouseUp.bind(this);
|
|
34
|
+
|
|
35
|
+
this.doOffsetMouse = this.doOffsetMouse.bind(this);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
componentDidMount() {
|
|
39
|
+
window.addEventListener('mouseup', this.onMouseUp);
|
|
40
|
+
|
|
41
|
+
window.addEventListener('mousedown', this.doOffsetMouse);
|
|
42
|
+
window.addEventListener('mousemove', this.doOffsetMouse);
|
|
43
|
+
window.addEventListener('mouseup', this.doOffsetMouse);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
componentWillUnmount() {
|
|
47
|
+
window.removeEventListener('mouseup', this.onMouseUp);
|
|
48
|
+
|
|
49
|
+
window.removeEventListener('mousedown', this.doOffsetMouse);
|
|
50
|
+
window.removeEventListener('mousemove', this.doOffsetMouse);
|
|
51
|
+
window.removeEventListener('mouseup', this.doOffsetMouse);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
doOffsetMouse(event) {
|
|
55
|
+
const { zoom } = this.state;
|
|
56
|
+
event.screenZoomX = event.screenX * Math.pow(zoom, -1);
|
|
57
|
+
event.screenZoomY = event.screenY * Math.pow(zoom, -1);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
handleMouseDown(event) {
|
|
61
|
+
this.setState((state) => {
|
|
62
|
+
return {
|
|
63
|
+
mouseDown: true,
|
|
64
|
+
lastLeft: event.clientX - state.left,
|
|
65
|
+
lastTop: event.clientY - state.top,
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
onMouseUp() {
|
|
71
|
+
this.setState({
|
|
72
|
+
mouseDown: false,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
handleZoomIncrease(event) {
|
|
77
|
+
const { onZoomChange } = this.props;
|
|
78
|
+
const { zoom } = this.state;
|
|
79
|
+
const newZoomValue = Math.min(zoom + ZOOM_INCREMENT, ZOOM_MAX_VAL);
|
|
80
|
+
this.setState({
|
|
81
|
+
zoom: newZoomValue,
|
|
82
|
+
});
|
|
83
|
+
if (onZoomChange) {
|
|
84
|
+
onZoomChange(newZoomValue);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
handleZoomDecrease(event) {
|
|
89
|
+
const { onZoomChange } = this.props;
|
|
90
|
+
const { zoom } = this.state;
|
|
91
|
+
const newZoomValue = Math.max(zoom - ZOOM_INCREMENT, ZOOM_MIN_VAL);
|
|
92
|
+
this.setState({
|
|
93
|
+
zoom: newZoomValue,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (onZoomChange) {
|
|
97
|
+
onZoomChange(newZoomValue);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
handleMouseMove(event) {
|
|
102
|
+
const { onBackgroundMoved, initialLeft = 0, initialTop = 0 } = this.props;
|
|
103
|
+
if (this.state.mouseDown) {
|
|
104
|
+
let newX, newY;
|
|
105
|
+
this.setState((state) => {
|
|
106
|
+
newX = event.clientX - state.lastLeft;
|
|
107
|
+
newY = event.clientY - state.lastTop;
|
|
108
|
+
return {
|
|
109
|
+
left: newX,
|
|
110
|
+
top: newY,
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
if (onBackgroundMoved) {
|
|
114
|
+
onBackgroundMoved(newX + initialLeft, newY + initialTop);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
render() {
|
|
120
|
+
const {
|
|
121
|
+
children,
|
|
122
|
+
backgroundImage,
|
|
123
|
+
imageWidth,
|
|
124
|
+
initialLeft = 0,
|
|
125
|
+
initialTop = 0,
|
|
126
|
+
...rest
|
|
127
|
+
} = this.props;
|
|
128
|
+
const { left, top, zoom } = this.state;
|
|
129
|
+
|
|
130
|
+
const finalLeft = initialLeft + left;
|
|
131
|
+
const finalTop = initialTop + top;
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<div
|
|
135
|
+
ref={this.ref}
|
|
136
|
+
{...computeBoxProps({
|
|
137
|
+
...rest,
|
|
138
|
+
style: {
|
|
139
|
+
...rest.style,
|
|
140
|
+
overflow: 'hidden',
|
|
141
|
+
position: 'relative',
|
|
142
|
+
},
|
|
143
|
+
})}
|
|
144
|
+
>
|
|
145
|
+
<div
|
|
146
|
+
onMouseDown={this.handleMouseDown}
|
|
147
|
+
onMouseMove={this.handleMouseMove}
|
|
148
|
+
style={{
|
|
149
|
+
position: 'fixed',
|
|
150
|
+
height: '100%',
|
|
151
|
+
width: '100%',
|
|
152
|
+
backgroundImage: `url("${backgroundImage}")`,
|
|
153
|
+
backgroundPosition: `${finalLeft}px ${finalTop}px`,
|
|
154
|
+
backgroundRepeat: 'repeat',
|
|
155
|
+
backgroundSize: `${zoom * imageWidth}px`,
|
|
156
|
+
}}
|
|
157
|
+
/>
|
|
158
|
+
<div
|
|
159
|
+
onMouseDown={this.handleMouseDown}
|
|
160
|
+
onMouseMove={this.handleMouseMove}
|
|
161
|
+
style={{
|
|
162
|
+
position: 'fixed',
|
|
163
|
+
transform: `translate(${finalLeft}px, ${finalTop}px) scale(${zoom})`,
|
|
164
|
+
transformOrigin: 'top left',
|
|
165
|
+
height: '100%',
|
|
166
|
+
width: '100%',
|
|
167
|
+
}}
|
|
168
|
+
>
|
|
169
|
+
{children}
|
|
170
|
+
</div>
|
|
171
|
+
|
|
172
|
+
<Stack position="absolute" width="100%">
|
|
173
|
+
<Stack.Item>
|
|
174
|
+
<Button icon="minus" onClick={this.handleZoomDecrease} />
|
|
175
|
+
</Stack.Item>
|
|
176
|
+
<Stack.Item grow={1}>
|
|
177
|
+
<ProgressBar
|
|
178
|
+
minValue={ZOOM_MIN_VAL}
|
|
179
|
+
value={zoom}
|
|
180
|
+
maxValue={ZOOM_MAX_VAL}
|
|
181
|
+
>
|
|
182
|
+
{zoom}x
|
|
183
|
+
</ProgressBar>
|
|
184
|
+
</Stack.Item>
|
|
185
|
+
<Stack.Item>
|
|
186
|
+
<Button icon="plus" onClick={this.handleZoomIncrease} />
|
|
187
|
+
</Stack.Item>
|
|
188
|
+
</Stack>
|
|
189
|
+
</div>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @copyright 2020 Aleksej Komarov
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { KEY } from '../common/keys';
|
|
8
|
+
import { classes } from '../common/react';
|
|
9
|
+
import { debounce } from '../common/timer';
|
|
10
|
+
import { KeyboardEvent, SyntheticEvent, useEffect, useRef } from 'react';
|
|
11
|
+
|
|
12
|
+
import { Box, BoxProps } from './Box';
|
|
13
|
+
|
|
14
|
+
type ConditionalProps =
|
|
15
|
+
| {
|
|
16
|
+
/**
|
|
17
|
+
* Mark this if you want to debounce onInput.
|
|
18
|
+
*
|
|
19
|
+
* This is useful for expensive filters, large lists etc.
|
|
20
|
+
*
|
|
21
|
+
* Requires `onInput` to be set.
|
|
22
|
+
*/
|
|
23
|
+
expensive?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Fires on each key press / value change. Used for searching.
|
|
26
|
+
*
|
|
27
|
+
* If it's a large list, consider using `expensive` prop.
|
|
28
|
+
*/
|
|
29
|
+
onInput: (event: SyntheticEvent<HTMLInputElement>, value: string) => void;
|
|
30
|
+
}
|
|
31
|
+
| {
|
|
32
|
+
/** This prop requires onInput to be set */
|
|
33
|
+
expensive?: never;
|
|
34
|
+
onInput?: never;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
type OptionalProps = Partial<{
|
|
38
|
+
/** Automatically focuses the input on mount */
|
|
39
|
+
autoFocus: boolean;
|
|
40
|
+
/** Automatically selects the input value on focus */
|
|
41
|
+
autoSelect: boolean;
|
|
42
|
+
/** The class name of the input */
|
|
43
|
+
className: string;
|
|
44
|
+
/** Disables the input */
|
|
45
|
+
disabled: boolean;
|
|
46
|
+
/** Mark this if you want the input to be as wide as possible */
|
|
47
|
+
fluid: boolean;
|
|
48
|
+
/** The maximum length of the input value */
|
|
49
|
+
maxLength: number;
|
|
50
|
+
/** Mark this if you want to use a monospace font */
|
|
51
|
+
monospace: boolean;
|
|
52
|
+
/** Fires when user is 'done typing': Clicked out, blur, enter key */
|
|
53
|
+
onChange: (event: SyntheticEvent<HTMLInputElement>, value: string) => void;
|
|
54
|
+
/** Fires once the enter key is pressed */
|
|
55
|
+
onEnter?: (event: SyntheticEvent<HTMLInputElement>, value: string) => void;
|
|
56
|
+
/** Fires once the escape key is pressed */
|
|
57
|
+
onEscape: (event: SyntheticEvent<HTMLInputElement>) => void;
|
|
58
|
+
/** The placeholder text when everything is cleared */
|
|
59
|
+
placeholder: string;
|
|
60
|
+
/** Clears the input value on enter */
|
|
61
|
+
selfClear: boolean;
|
|
62
|
+
/** The state variable of the input. */
|
|
63
|
+
value: string | number;
|
|
64
|
+
}>;
|
|
65
|
+
|
|
66
|
+
type Props = OptionalProps & ConditionalProps & BoxProps;
|
|
67
|
+
|
|
68
|
+
export function toInputValue(value: string | number | undefined) {
|
|
69
|
+
return typeof value !== 'number' && typeof value !== 'string'
|
|
70
|
+
? ''
|
|
71
|
+
: String(value);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const inputDebounce = debounce((onInput: () => void) => onInput(), 250);
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* ### Input
|
|
78
|
+
* A basic text input which allow users to enter text into a UI.
|
|
79
|
+
* > Input does not support custom font size and height due to the way
|
|
80
|
+
* > it's implemented in CSS. Eventually, this needs to be fixed.
|
|
81
|
+
*/
|
|
82
|
+
export function Input(props: Props) {
|
|
83
|
+
const {
|
|
84
|
+
autoFocus,
|
|
85
|
+
autoSelect,
|
|
86
|
+
className,
|
|
87
|
+
disabled,
|
|
88
|
+
expensive,
|
|
89
|
+
fluid,
|
|
90
|
+
maxLength,
|
|
91
|
+
monospace,
|
|
92
|
+
onChange,
|
|
93
|
+
onEnter,
|
|
94
|
+
onEscape,
|
|
95
|
+
onInput,
|
|
96
|
+
placeholder,
|
|
97
|
+
selfClear,
|
|
98
|
+
value,
|
|
99
|
+
...rest
|
|
100
|
+
} = props;
|
|
101
|
+
|
|
102
|
+
// The ref to the input field
|
|
103
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
104
|
+
|
|
105
|
+
function handleInput(event: SyntheticEvent<HTMLInputElement>) {
|
|
106
|
+
if (!onInput) return;
|
|
107
|
+
|
|
108
|
+
const value = event.currentTarget?.value;
|
|
109
|
+
|
|
110
|
+
if (expensive) {
|
|
111
|
+
inputDebounce(() => onInput(event, value));
|
|
112
|
+
} else {
|
|
113
|
+
onInput(event, value);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function handleKeyDown(event: KeyboardEvent<HTMLInputElement>) {
|
|
118
|
+
if (event.key === KEY.Enter) {
|
|
119
|
+
onEnter?.(event, event.currentTarget.value);
|
|
120
|
+
if (selfClear) {
|
|
121
|
+
event.currentTarget.value = '';
|
|
122
|
+
} else {
|
|
123
|
+
event.currentTarget.blur();
|
|
124
|
+
onChange?.(event, event.currentTarget.value);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (event.key === KEY.Escape) {
|
|
131
|
+
onEscape?.(event);
|
|
132
|
+
|
|
133
|
+
event.currentTarget.value = toInputValue(value);
|
|
134
|
+
event.currentTarget.blur();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Focuses the input on mount */
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
const input = inputRef.current;
|
|
141
|
+
if (!input) return;
|
|
142
|
+
|
|
143
|
+
const newValue = toInputValue(value);
|
|
144
|
+
|
|
145
|
+
if (input.value !== newValue) input.value = newValue;
|
|
146
|
+
|
|
147
|
+
if (!autoFocus && !autoSelect) return;
|
|
148
|
+
|
|
149
|
+
setTimeout(() => {
|
|
150
|
+
input.focus();
|
|
151
|
+
|
|
152
|
+
if (autoSelect) {
|
|
153
|
+
input.select();
|
|
154
|
+
}
|
|
155
|
+
}, 1);
|
|
156
|
+
}, []);
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<Box
|
|
160
|
+
className={classes([
|
|
161
|
+
'Input',
|
|
162
|
+
fluid && 'Input--fluid',
|
|
163
|
+
monospace && 'Input--monospace',
|
|
164
|
+
className,
|
|
165
|
+
])}
|
|
166
|
+
{...rest}
|
|
167
|
+
>
|
|
168
|
+
<div className="Input__baseline">.</div>
|
|
169
|
+
<input
|
|
170
|
+
className="Input__input"
|
|
171
|
+
disabled={disabled}
|
|
172
|
+
maxLength={maxLength}
|
|
173
|
+
onBlur={(event) => onChange?.(event, event.target.value)}
|
|
174
|
+
onChange={handleInput}
|
|
175
|
+
onKeyDown={handleKeyDown}
|
|
176
|
+
placeholder={placeholder}
|
|
177
|
+
ref={inputRef}
|
|
178
|
+
/>
|
|
179
|
+
</Box>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Component } from 'react';
|
|
2
|
+
|
|
3
|
+
import { KeyEvent } from '../events';
|
|
4
|
+
import { listenForKeyEvents } from '../hotkeys';
|
|
5
|
+
|
|
6
|
+
type KeyListenerProps = Partial<{
|
|
7
|
+
onKey: (key: KeyEvent) => void;
|
|
8
|
+
onKeyDown: (key: KeyEvent) => void;
|
|
9
|
+
onKeyUp: (key: KeyEvent) => void;
|
|
10
|
+
}>;
|
|
11
|
+
|
|
12
|
+
export class KeyListener extends Component<KeyListenerProps> {
|
|
13
|
+
dispose: () => void;
|
|
14
|
+
|
|
15
|
+
constructor(props) {
|
|
16
|
+
super(props);
|
|
17
|
+
|
|
18
|
+
this.dispose = listenForKeyEvents((key) => {
|
|
19
|
+
if (this.props.onKey) {
|
|
20
|
+
this.props.onKey(key);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (key.isDown() && this.props.onKeyDown) {
|
|
24
|
+
this.props.onKeyDown(key);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (key.isUp() && this.props.onKeyUp) {
|
|
28
|
+
this.props.onKeyUp(key);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
componentWillUnmount() {
|
|
34
|
+
this.dispose();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
render() {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|