@tma.js/sdk 1.4.4 → 1.4.7
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 +2 -2
- package/dist/dts/bridge/env/index.d.ts +0 -1
- package/dist/dts/bridge/events/events.d.ts +16 -16
- package/dist/dts/bridge/methods/methods.d.ts +27 -27
- package/dist/dts/bridge/methods/popup.d.ts +1 -1
- package/dist/dts/index.d.ts +3 -3
- package/dist/dts/init-data/types.d.ts +3 -3
- package/dist/dts/launch-params/index.d.ts +1 -4
- package/dist/dts/launch-params/retrieveFromLocation.d.ts +2 -2
- package/dist/dts/launch-params/retrieveFromPerformance.d.ts +3 -4
- package/dist/dts/launch-params/retrieveFromUrl.d.ts +3 -2
- package/dist/dts/launch-params/retrieveLaunchData.d.ts +1 -0
- package/dist/dts/launch-params/retrieveLaunchParams.d.ts +6 -0
- package/dist/dts/launch-params/storage.d.ts +3 -5
- package/dist/dts/launch-params/types.d.ts +1 -1
- package/dist/dts/{launch-params → misc}/getFirstNavigationEntry.d.ts +1 -1
- package/dist/dts/misc/index.d.ts +3 -0
- package/dist/dts/{bridge/env → misc}/isIframe.d.ts +1 -1
- package/dist/dts/misc/isPageReload.d.ts +5 -0
- package/dist/dts/theme-params/types.d.ts +1 -1
- package/dist/index.cjs +1 -2
- package/dist/index.iife.js +1 -2
- package/dist/index.mjs +841 -866
- package/package.json +2 -3
- package/dist/dts/launch-params/computeLaunchData.d.ts +0 -6
- package/dist/dts/launch-params/computePageReload.d.ts +0 -6
- package/dist/dts/launch-params/retrieveCurrent.d.ts +0 -5
- package/dist/index.cjs.map +0 -1
- package/dist/index.iife.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/src/__tests__/globals.ts +0 -39
- package/src/back-button/BackButton.ts +0 -90
- package/src/back-button/__tests__/BackButton.ts +0 -129
- package/src/back-button/index.ts +0 -2
- package/src/back-button/types.ts +0 -14
- package/src/bridge/__tests__/parseMessage.ts +0 -23
- package/src/bridge/__tests__/request.ts +0 -236
- package/src/bridge/env/__tests__/hasExternalNotify.ts +0 -15
- package/src/bridge/env/__tests__/hasWebviewProxy.ts +0 -15
- package/src/bridge/env/__tests__/isIframe.ts +0 -30
- package/src/bridge/env/hasExternalNotify.ts +0 -19
- package/src/bridge/env/hasWebviewProxy.ts +0 -19
- package/src/bridge/env/index.ts +0 -3
- package/src/bridge/env/isIframe.ts +0 -11
- package/src/bridge/errors/MethodUnsupportedError.ts +0 -13
- package/src/bridge/errors/ParameterUnsupportedError.ts +0 -13
- package/src/bridge/errors/index.ts +0 -2
- package/src/bridge/events/__tests__/createEmitter.ts +0 -143
- package/src/bridge/events/__tests__/off.ts +0 -34
- package/src/bridge/events/__tests__/on.ts +0 -49
- package/src/bridge/events/__tests__/onTelegramEvent.ts +0 -49
- package/src/bridge/events/__tests__/once.ts +0 -64
- package/src/bridge/events/__tests__/singletonEmitter.ts +0 -22
- package/src/bridge/events/__tests__/subscribe.ts +0 -49
- package/src/bridge/events/__tests__/unsubscribe.ts +0 -34
- package/src/bridge/events/createEmitter.ts +0 -108
- package/src/bridge/events/events.ts +0 -170
- package/src/bridge/events/index.ts +0 -9
- package/src/bridge/events/off.ts +0 -14
- package/src/bridge/events/on.ts +0 -19
- package/src/bridge/events/onTelegramEvent.ts +0 -81
- package/src/bridge/events/once.ts +0 -18
- package/src/bridge/events/parsers/__tests__/clipboardTextReceived.ts +0 -21
- package/src/bridge/events/parsers/__tests__/invoiceClosed.ts +0 -12
- package/src/bridge/events/parsers/__tests__/popupClosed.ts +0 -10
- package/src/bridge/events/parsers/__tests__/qrTextReceived.ts +0 -9
- package/src/bridge/events/parsers/__tests__/theme-changed.ts +0 -42
- package/src/bridge/events/parsers/__tests__/viewportChanged.ts +0 -49
- package/src/bridge/events/parsers/clipboardTextReceived.ts +0 -26
- package/src/bridge/events/parsers/customMethodInvoked.ts +0 -25
- package/src/bridge/events/parsers/index.ts +0 -9
- package/src/bridge/events/parsers/invoiceClosed.ts +0 -26
- package/src/bridge/events/parsers/phoneRequested.ts +0 -14
- package/src/bridge/events/parsers/popupClosed.ts +0 -19
- package/src/bridge/events/parsers/qrTextReceived.ts +0 -14
- package/src/bridge/events/parsers/theme-changed.ts +0 -58
- package/src/bridge/events/parsers/viewportChanged.ts +0 -33
- package/src/bridge/events/parsers/writeAccessRequested.ts +0 -14
- package/src/bridge/events/singletonEmitter.ts +0 -19
- package/src/bridge/events/subscribe.ts +0 -15
- package/src/bridge/events/unsubscribe.ts +0 -10
- package/src/bridge/index.ts +0 -7
- package/src/bridge/invokeCustomMethod.ts +0 -56
- package/src/bridge/methods/__tests__/createPostEvent.ts +0 -37
- package/src/bridge/methods/__tests__/postEvent.ts +0 -137
- package/src/bridge/methods/createPostEvent.ts +0 -40
- package/src/bridge/methods/custom-methods.ts +0 -68
- package/src/bridge/methods/haptic.ts +0 -52
- package/src/bridge/methods/index.ts +0 -6
- package/src/bridge/methods/methods.ts +0 -370
- package/src/bridge/methods/popup.ts +0 -53
- package/src/bridge/methods/postEvent.ts +0 -101
- package/src/bridge/parseMessage.ts +0 -28
- package/src/bridge/request.ts +0 -176
- package/src/classnames/__tests__/classNames.ts +0 -20
- package/src/classnames/__tests__/mergeClassNames.ts +0 -21
- package/src/classnames/classNames.ts +0 -34
- package/src/classnames/index.ts +0 -2
- package/src/classnames/mergeClassNames.ts +0 -60
- package/src/closing-behavior/ClosingBehavior.ts +0 -64
- package/src/closing-behavior/__tests__/ClosingBehavior.ts +0 -86
- package/src/closing-behavior/index.ts +0 -2
- package/src/closing-behavior/types.ts +0 -12
- package/src/cloud-storage/CloudStorage.ts +0 -138
- package/src/cloud-storage/index.ts +0 -1
- package/src/colors/__tests__/isColorDark.ts +0 -12
- package/src/colors/__tests__/isRGB.ts +0 -13
- package/src/colors/__tests__/isRGBShort.ts +0 -13
- package/src/colors/__tests__/toRGB.ts +0 -23
- package/src/colors/index.ts +0 -5
- package/src/colors/isColorDark.ts +0 -22
- package/src/colors/isRGB.ts +0 -9
- package/src/colors/isRGBShort.ts +0 -9
- package/src/colors/toRGB.ts +0 -49
- package/src/colors/types.ts +0 -9
- package/src/css/__tests__/bindMiniAppCSSVars.ts +0 -175
- package/src/css/__tests__/bindThemeCSSVars.ts +0 -52
- package/src/css/__tests__/bindViewportCSSVars.ts +0 -55
- package/src/css/__tests__/setCSSVar.ts +0 -14
- package/src/css/bindMiniAppCSSVars.ts +0 -51
- package/src/css/bindThemeCSSVars.ts +0 -31
- package/src/css/bindViewportCSSVars.ts +0 -36
- package/src/css/index.ts +0 -4
- package/src/css/setCSSVar.ts +0 -8
- package/src/event-emitter/EventEmitter.ts +0 -146
- package/src/event-emitter/__tests__/EventEmitter.ts +0 -145
- package/src/event-emitter/index.ts +0 -2
- package/src/event-emitter/types.ts +0 -60
- package/src/globals.ts +0 -38
- package/src/haptic-feedback/HapticFeedback.ts +0 -70
- package/src/haptic-feedback/__tests__/HapticFeedback.ts +0 -68
- package/src/haptic-feedback/index.ts +0 -1
- package/src/index.ts +0 -185
- package/src/init/catchCustomStyles.ts +0 -17
- package/src/init/creators/__tests__/createViewport.ts +0 -96
- package/src/init/creators/createBackButton.ts +0 -25
- package/src/init/creators/createClosingBehavior.ts +0 -24
- package/src/init/creators/createMainButton.ts +0 -51
- package/src/init/creators/createMiniApp.ts +0 -48
- package/src/init/creators/createRequestIdGenerator.ts +0 -13
- package/src/init/creators/createSettingsButton.ts +0 -25
- package/src/init/creators/createThemeParams.ts +0 -11
- package/src/init/creators/createViewport.ts +0 -94
- package/src/init/creators/index.ts +0 -8
- package/src/init/css/index.ts +0 -1
- package/src/init/css/processCSSVarsOption.ts +0 -55
- package/src/init/index.ts +0 -2
- package/src/init/init.ts +0 -134
- package/src/init/types.ts +0 -94
- package/src/init-data/InitData.ts +0 -96
- package/src/init-data/__tests__/InitData.ts +0 -98
- package/src/init-data/__tests__/chatParser.ts +0 -102
- package/src/init-data/__tests__/initDataParser.ts +0 -136
- package/src/init-data/__tests__/parseInitData.ts +0 -136
- package/src/init-data/__tests__/userParser.ts +0 -96
- package/src/init-data/chatParser.ts +0 -19
- package/src/init-data/index.ts +0 -6
- package/src/init-data/initDataParser.ts +0 -41
- package/src/init-data/parseInitData.ts +0 -10
- package/src/init-data/types.ts +0 -164
- package/src/init-data/userParser.ts +0 -45
- package/src/invoice/Invoice.ts +0 -123
- package/src/invoice/index.ts +0 -2
- package/src/invoice/types.ts +0 -11
- package/src/launch-params/__tests__/retrieveFromUrl.ts +0 -19
- package/src/launch-params/computeLaunchData.ts +0 -81
- package/src/launch-params/computePageReload.ts +0 -13
- package/src/launch-params/getFirstNavigationEntry.ts +0 -10
- package/src/launch-params/index.ts +0 -13
- package/src/launch-params/launchParamsParser.ts +0 -45
- package/src/launch-params/parseLaunchParams.ts +0 -10
- package/src/launch-params/retrieveCurrent.ts +0 -27
- package/src/launch-params/retrieveFromLocation.ts +0 -10
- package/src/launch-params/retrieveFromPerformance.ts +0 -18
- package/src/launch-params/retrieveFromUrl.ts +0 -19
- package/src/launch-params/retrieveLaunchData.ts +0 -30
- package/src/launch-params/serializeLaunchParams.ts +0 -37
- package/src/launch-params/storage.ts +0 -33
- package/src/launch-params/types.ts +0 -62
- package/src/logger/Logger.ts +0 -72
- package/src/logger/__tests__/Logger.ts +0 -107
- package/src/logger/index.ts +0 -1
- package/src/main-button/MainButton.ts +0 -239
- package/src/main-button/__tests__/MainButton.ts +0 -346
- package/src/main-button/index.ts +0 -2
- package/src/main-button/types.ts +0 -26
- package/src/mini-app/MiniApp.ts +0 -348
- package/src/mini-app/__tests__/MiniApp.ts +0 -140
- package/src/mini-app/contactParser.ts +0 -29
- package/src/mini-app/index.ts +0 -2
- package/src/mini-app/types.ts +0 -38
- package/src/misc/__tests__/isRecord.ts +0 -21
- package/src/misc/index.ts +0 -2
- package/src/misc/isRecord.ts +0 -7
- package/src/misc/isTMA.ts +0 -13
- package/src/navigation/HashNavigator/HashNavigator.ts +0 -220
- package/src/navigation/HashNavigator/__tests__/HashNavigator.ts +0 -144
- package/src/navigation/HashNavigator/__tests__/drop.ts +0 -42
- package/src/navigation/HashNavigator/__tests__/go.ts +0 -9
- package/src/navigation/HashNavigator/drop.ts +0 -36
- package/src/navigation/HashNavigator/go.ts +0 -28
- package/src/navigation/HashNavigator/index.ts +0 -2
- package/src/navigation/HashNavigator/types.ts +0 -41
- package/src/navigation/Navigator/Navigator.ts +0 -282
- package/src/navigation/Navigator/index.ts +0 -2
- package/src/navigation/Navigator/types.ts +0 -55
- package/src/navigation/ensurePrefix.ts +0 -9
- package/src/navigation/getHash.ts +0 -17
- package/src/navigation/index.ts +0 -4
- package/src/parsing/ArrayValueParser.ts +0 -79
- package/src/parsing/ParseError.ts +0 -27
- package/src/parsing/ParseSchemaFieldError.ts +0 -21
- package/src/parsing/ValueParser.ts +0 -71
- package/src/parsing/__tests__/ArrayValueParser.ts +0 -18
- package/src/parsing/__tests__/toRecord.ts +0 -10
- package/src/parsing/createValueParserGenerator.ts +0 -16
- package/src/parsing/index.ts +0 -10
- package/src/parsing/parseBySchema.ts +0 -65
- package/src/parsing/parsers/__tests__/array.ts +0 -39
- package/src/parsing/parsers/__tests__/boolean.ts +0 -31
- package/src/parsing/parsers/__tests__/date.ts +0 -25
- package/src/parsing/parsers/__tests__/json.ts +0 -80
- package/src/parsing/parsers/__tests__/number.ts +0 -23
- package/src/parsing/parsers/__tests__/rgb.ts +0 -22
- package/src/parsing/parsers/__tests__/searchParams.ts +0 -105
- package/src/parsing/parsers/__tests__/string.ts +0 -25
- package/src/parsing/parsers/array.ts +0 -9
- package/src/parsing/parsers/boolean.ts +0 -22
- package/src/parsing/parsers/date.ts +0 -11
- package/src/parsing/parsers/index.ts +0 -8
- package/src/parsing/parsers/json.ts +0 -17
- package/src/parsing/parsers/number.ts +0 -21
- package/src/parsing/parsers/rgb.ts +0 -10
- package/src/parsing/parsers/searchParams.ts +0 -24
- package/src/parsing/parsers/string.ts +0 -12
- package/src/parsing/toRecord.ts +0 -27
- package/src/parsing/types.ts +0 -32
- package/src/parsing/unexpectedTypeError.ts +0 -6
- package/src/popup/Popup.ts +0 -91
- package/src/popup/__tests__/Popup.ts +0 -130
- package/src/popup/__tests__/preparePopupParams.ts +0 -85
- package/src/popup/index.ts +0 -2
- package/src/popup/preparePopupParams.ts +0 -59
- package/src/popup/types.ts +0 -69
- package/src/qr-scanner/QRScanner.ts +0 -95
- package/src/qr-scanner/index.ts +0 -2
- package/src/qr-scanner/types.ts +0 -11
- package/src/settings-button/SettingsButton.ts +0 -85
- package/src/settings-button/index.ts +0 -2
- package/src/settings-button/types.ts +0 -15
- package/src/state/State.ts +0 -67
- package/src/state/index.ts +0 -2
- package/src/state/types.ts +0 -31
- package/src/storage.ts +0 -69
- package/src/supports/__tests__/supports.ts +0 -123
- package/src/supports/createSupportsFunc.ts +0 -18
- package/src/supports/createSupportsParamFunc.ts +0 -27
- package/src/supports/index.ts +0 -4
- package/src/supports/supports.ts +0 -84
- package/src/supports/types.ts +0 -1
- package/src/theme-params/ThemeParams.ts +0 -131
- package/src/theme-params/__tests__/keys.ts +0 -19
- package/src/theme-params/__tests__/parseThemeParams.ts +0 -29
- package/src/theme-params/__tests__/serializeThemeParams.ts +0 -29
- package/src/theme-params/__tests__/themeParamsParser.ts +0 -29
- package/src/theme-params/index.ts +0 -6
- package/src/theme-params/keys.ts +0 -24
- package/src/theme-params/parseThemeParams.ts +0 -10
- package/src/theme-params/requestThemeParams.ts +0 -13
- package/src/theme-params/serializeThemeParams.ts +0 -20
- package/src/theme-params/themeParamsParser.ts +0 -22
- package/src/theme-params/types.ts +0 -33
- package/src/timeout/TimeoutError.ts +0 -6
- package/src/timeout/__tests__/isTimeoutError.ts +0 -9
- package/src/timeout/__tests__/withTimeout.ts +0 -28
- package/src/timeout/index.ts +0 -4
- package/src/timeout/isTimeoutError.ts +0 -9
- package/src/timeout/sleep.ts +0 -10
- package/src/timeout/withTimeout.ts +0 -24
- package/src/types/index.ts +0 -4
- package/src/types/methods.ts +0 -18
- package/src/types/platform.ts +0 -14
- package/src/types/request-id.ts +0 -10
- package/src/types/utils.ts +0 -50
- package/src/utils/Utils.ts +0 -107
- package/src/utils/index.ts +0 -1
- package/src/version/__tests__/compareVersions.ts +0 -19
- package/src/version/compareVersions.ts +0 -28
- package/src/version/index.ts +0 -2
- package/src/version/types.ts +0 -4
- package/src/viewport/Viewport.ts +0 -171
- package/src/viewport/__tests__/isStableViewportPlatform.ts +0 -15
- package/src/viewport/__tests__/utils.ts +0 -12
- package/src/viewport/index.ts +0 -4
- package/src/viewport/isStableViewportPlatform.ts +0 -10
- package/src/viewport/requestViewport.ts +0 -23
- package/src/viewport/types.ts +0 -23
- package/src/viewport/utils.ts +0 -7
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { expect, it } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import { toRGB } from '../toRGB';
|
|
4
|
-
|
|
5
|
-
it('should return same value in case, full version of RGB is passed', () => {
|
|
6
|
-
expect(toRGB('#ffffff')).toBe('#ffffff');
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('should return full RGB value in case, its short presentation is passed', () => {
|
|
10
|
-
expect(toRGB('#abc')).toBe('#aabbcc');
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('should return RGB representation of rgb(*,*,*) pattern', () => {
|
|
14
|
-
expect(toRGB('rgb(6,56,11)')).toBe('#06380b');
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('should return RGB representation of rgba(*,*,*) pattern', () => {
|
|
18
|
-
expect(toRGB('rgba(6,56,11,22)')).toBe('#06380b');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('should throw an error in other cases', () => {
|
|
22
|
-
expect(() => toRGB('abc')).toThrow();
|
|
23
|
-
});
|
package/src/colors/index.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { toRGB } from './toRGB.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Returns true in case, the color is recognized as dark.
|
|
5
|
-
* @param color - color in any format acceptable by toRGB function.
|
|
6
|
-
* @see toRGB
|
|
7
|
-
*/
|
|
8
|
-
export function isColorDark(color: string): boolean {
|
|
9
|
-
// Convert color to RGB.
|
|
10
|
-
const rgb = toRGB(color);
|
|
11
|
-
|
|
12
|
-
// Real formula: hsp = Math.sqrt(0.299 * r * r + 0.587 * g * g + 0.114 * b * b)
|
|
13
|
-
// See: https://stackoverflow.com/a/596243
|
|
14
|
-
const hsp = Math.sqrt(
|
|
15
|
-
[0.299, 0.587, 0.114].reduce<number>((acc, modifier, idx) => {
|
|
16
|
-
// Extract part of #RRGGBB pattern and convert it to DEC.
|
|
17
|
-
const dec = parseInt(rgb.slice(1 + idx * 2, 1 + (idx + 1) * 2), 16);
|
|
18
|
-
return acc + dec * dec * modifier;
|
|
19
|
-
}, 0),
|
|
20
|
-
);
|
|
21
|
-
return hsp < 120;
|
|
22
|
-
}
|
package/src/colors/isRGB.ts
DELETED
package/src/colors/isRGBShort.ts
DELETED
package/src/colors/toRGB.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { isRGB } from './isRGB.js';
|
|
2
|
-
import { isRGBShort } from './isRGBShort.js';
|
|
3
|
-
import type { RGB } from './types.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Converts passed value to #RRGGBB format. Accepts following color formats:
|
|
7
|
-
* - `#RGB`
|
|
8
|
-
* - `#RRGGBB`
|
|
9
|
-
* - `rgb(1,2,3)`
|
|
10
|
-
* - `rgba(1,2,3,4)`
|
|
11
|
-
* @param value - value to convert.
|
|
12
|
-
* @throws {Error} Passed value does not satisfy any of known RGB formats.
|
|
13
|
-
*/
|
|
14
|
-
export function toRGB(value: string): RGB {
|
|
15
|
-
// Remove all spaces.
|
|
16
|
-
const clean = value.replace(/\s/g, '').toLowerCase();
|
|
17
|
-
|
|
18
|
-
// Value already has required format.
|
|
19
|
-
if (isRGB(clean)) {
|
|
20
|
-
return clean;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Convert from #RGB.
|
|
24
|
-
if (isRGBShort(clean)) {
|
|
25
|
-
let color = '#';
|
|
26
|
-
|
|
27
|
-
for (let i = 0; i < 3; i += 1) {
|
|
28
|
-
color += clean[1 + i].repeat(2);
|
|
29
|
-
}
|
|
30
|
-
return color as RGB;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Example valid values: rgb(0,3,10) rgba(32,114,8,0)
|
|
34
|
-
const match = clean.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/)
|
|
35
|
-
|| clean.match(/^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),\d{1,3}\)$/);
|
|
36
|
-
|
|
37
|
-
// In case, this didn't work as well, we can't extract RGB color from passed
|
|
38
|
-
// text.
|
|
39
|
-
if (match === null) {
|
|
40
|
-
throw new Error(`Value "${value}" does not satisfy any of known RGB formats.`);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Otherwise, take R, G and B components, convert to hex and create #RRGGBB
|
|
44
|
-
// string.
|
|
45
|
-
return match.slice(1).reduce((acc, component) => {
|
|
46
|
-
const formatted = parseInt(component, 10).toString(16);
|
|
47
|
-
return acc + (formatted.length === 1 ? '0' : '') + formatted;
|
|
48
|
-
}, '#') as RGB;
|
|
49
|
-
}
|
package/src/colors/types.ts
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { expect, vi, it, SpyInstance, afterEach, beforeAll, describe } from 'vitest';
|
|
2
|
-
import { ThemeParams } from '../../theme-params';
|
|
3
|
-
import { dispatchWindowMessageEvent } from '../../../test-utils/dispatchWindowMessageEvent';
|
|
4
|
-
import { bindMiniAppCSSVars } from '../bindMiniAppCSSVars';
|
|
5
|
-
import { MiniApp } from '../../mini-app';
|
|
6
|
-
|
|
7
|
-
let setCSSPropertySpy: SpyInstance<[string, string, string?], void>;
|
|
8
|
-
|
|
9
|
-
beforeAll(() => {
|
|
10
|
-
setCSSPropertySpy = vi
|
|
11
|
-
.spyOn(document.documentElement.style, 'setProperty')
|
|
12
|
-
.mockImplementation(() => {
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
afterEach(() => {
|
|
17
|
-
vi.clearAllMocks();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('background', () => {
|
|
21
|
-
it('should set --tg-background-color equal to miniApp.backgroundColor', () => {
|
|
22
|
-
bindMiniAppCSSVars(
|
|
23
|
-
new MiniApp({
|
|
24
|
-
backgroundColor: '#111111',
|
|
25
|
-
headerColor: '#222222',
|
|
26
|
-
botInline: false,
|
|
27
|
-
version: '7.0',
|
|
28
|
-
createRequestId() {
|
|
29
|
-
return 'abc';
|
|
30
|
-
},
|
|
31
|
-
}),
|
|
32
|
-
new ThemeParams({}),
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-background-color', '#111111');
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should update --tg-background-color according to the changes applied to mini app background color', () => {
|
|
39
|
-
const ma = new MiniApp({
|
|
40
|
-
backgroundColor: '#111111',
|
|
41
|
-
headerColor: '#222222',
|
|
42
|
-
botInline: false,
|
|
43
|
-
version: '7.0',
|
|
44
|
-
postEvent() {
|
|
45
|
-
},
|
|
46
|
-
createRequestId() {
|
|
47
|
-
return 'abc';
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
bindMiniAppCSSVars(ma, new ThemeParams({}));
|
|
52
|
-
setCSSPropertySpy.mockClear();
|
|
53
|
-
|
|
54
|
-
ma.setBackgroundColor('#999999');
|
|
55
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-background-color', '#999999');
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
describe('header', () => {
|
|
60
|
-
it('should set --tg-header-color equal to miniApp.headerColor if this value is RGB', () => {
|
|
61
|
-
bindMiniAppCSSVars(
|
|
62
|
-
new MiniApp({
|
|
63
|
-
backgroundColor: '#111111',
|
|
64
|
-
headerColor: '#222222',
|
|
65
|
-
botInline: false,
|
|
66
|
-
version: '7.0',
|
|
67
|
-
createRequestId() {
|
|
68
|
-
return 'abc';
|
|
69
|
-
},
|
|
70
|
-
}),
|
|
71
|
-
new ThemeParams({}),
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#222222');
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should set --tg-header-color equal to themeParams.backgroundColor if miniApp.headerColor = "bg_color" and backgroundColor property is presented in theme params', () => {
|
|
78
|
-
const tp = new ThemeParams({});
|
|
79
|
-
tp.listen();
|
|
80
|
-
|
|
81
|
-
bindMiniAppCSSVars(
|
|
82
|
-
new MiniApp({
|
|
83
|
-
backgroundColor: '#111111',
|
|
84
|
-
headerColor: 'bg_color',
|
|
85
|
-
botInline: false,
|
|
86
|
-
version: '7.0',
|
|
87
|
-
createRequestId() {
|
|
88
|
-
return 'abc';
|
|
89
|
-
},
|
|
90
|
-
}),
|
|
91
|
-
tp,
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
// Background only.
|
|
95
|
-
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1);
|
|
96
|
-
setCSSPropertySpy.mockClear();
|
|
97
|
-
|
|
98
|
-
dispatchWindowMessageEvent('theme_changed', {
|
|
99
|
-
theme_params: {
|
|
100
|
-
bg_color: '#ffffff',
|
|
101
|
-
},
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1);
|
|
105
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#ffffff');
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('should set --tg-header-color equal to themeParams.secondaryBackgroundColor if miniApp.headerColor = "secondary_bg_color" and secondaryBackgroundColor property is presented in theme params', () => {
|
|
109
|
-
const tp = new ThemeParams({});
|
|
110
|
-
tp.listen();
|
|
111
|
-
|
|
112
|
-
bindMiniAppCSSVars(
|
|
113
|
-
new MiniApp({
|
|
114
|
-
backgroundColor: '#111111',
|
|
115
|
-
headerColor: 'secondary_bg_color',
|
|
116
|
-
botInline: false,
|
|
117
|
-
version: '7.0',
|
|
118
|
-
createRequestId() {
|
|
119
|
-
return 'abc';
|
|
120
|
-
},
|
|
121
|
-
}),
|
|
122
|
-
tp,
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
// Background only.
|
|
126
|
-
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1);
|
|
127
|
-
setCSSPropertySpy.mockClear();
|
|
128
|
-
|
|
129
|
-
dispatchWindowMessageEvent('theme_changed', {
|
|
130
|
-
theme_params: {
|
|
131
|
-
secondary_bg_color: '#000000',
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#000000');
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it('should actualize --tg-header-color if either mini app or theme params changed', () => {
|
|
138
|
-
const ma = new MiniApp({
|
|
139
|
-
backgroundColor: '#111111',
|
|
140
|
-
headerColor: '#aabbcc',
|
|
141
|
-
botInline: false,
|
|
142
|
-
version: '7.0',
|
|
143
|
-
postEvent() {},
|
|
144
|
-
createRequestId() {
|
|
145
|
-
return 'abc';
|
|
146
|
-
},
|
|
147
|
-
});
|
|
148
|
-
const tp = new ThemeParams({
|
|
149
|
-
backgroundColor: '#ffffff',
|
|
150
|
-
secondaryBackgroundColor: '#000000',
|
|
151
|
-
});
|
|
152
|
-
tp.listen();
|
|
153
|
-
|
|
154
|
-
bindMiniAppCSSVars(ma, tp);
|
|
155
|
-
setCSSPropertySpy.mockClear();
|
|
156
|
-
|
|
157
|
-
ma.setHeaderColor('bg_color');
|
|
158
|
-
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1);
|
|
159
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#ffffff');
|
|
160
|
-
setCSSPropertySpy.mockClear();
|
|
161
|
-
|
|
162
|
-
ma.setHeaderColor('secondary_bg_color');
|
|
163
|
-
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1);
|
|
164
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#000000');
|
|
165
|
-
setCSSPropertySpy.mockClear();
|
|
166
|
-
|
|
167
|
-
dispatchWindowMessageEvent('theme_changed', {
|
|
168
|
-
theme_params: {
|
|
169
|
-
secondary_bg_color: '#abcdef',
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#abcdef');
|
|
173
|
-
setCSSPropertySpy.mockClear();
|
|
174
|
-
});
|
|
175
|
-
});
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { expect, vi, it, SpyInstance, afterEach, beforeAll } from 'vitest';
|
|
2
|
-
import { bindThemeCSSVars } from '../bindThemeCSSVars';
|
|
3
|
-
import { ThemeParams } from '../../theme-params';
|
|
4
|
-
import { dispatchWindowMessageEvent } from '../../../test-utils/dispatchWindowMessageEvent';
|
|
5
|
-
|
|
6
|
-
let setCSSPropertySpy: SpyInstance<[string, string, string?], void>;
|
|
7
|
-
|
|
8
|
-
beforeAll(() => {
|
|
9
|
-
setCSSPropertySpy = vi
|
|
10
|
-
.spyOn(document.documentElement.style, 'setProperty')
|
|
11
|
-
.mockImplementation(() => {
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
afterEach(() => {
|
|
16
|
-
vi.clearAllMocks();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should set --tg-theme-{key} CSS vars, where key is a kebab-cased theme keys', () => {
|
|
20
|
-
bindThemeCSSVars(new ThemeParams({
|
|
21
|
-
backgroundColor: '#abcdef',
|
|
22
|
-
accentTextColor: '#000011',
|
|
23
|
-
}));
|
|
24
|
-
|
|
25
|
-
expect(setCSSPropertySpy).toHaveBeenCalledTimes(2);
|
|
26
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-background-color', '#abcdef');
|
|
27
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-accent-text-color', '#000011');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should update --tg-theme-{key}variables to the values, received in the Theme change events', async () => {
|
|
31
|
-
const tp = new ThemeParams({
|
|
32
|
-
backgroundColor: '#abcdef',
|
|
33
|
-
accentTextColor: '#000011',
|
|
34
|
-
});
|
|
35
|
-
bindThemeCSSVars(tp);
|
|
36
|
-
setCSSPropertySpy.mockClear();
|
|
37
|
-
|
|
38
|
-
await tp.listen();
|
|
39
|
-
|
|
40
|
-
dispatchWindowMessageEvent('theme_changed', {
|
|
41
|
-
theme_params: {
|
|
42
|
-
bg_color: '#111111',
|
|
43
|
-
accent_text_color: '#222222',
|
|
44
|
-
text_color: '#333333',
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
expect(setCSSPropertySpy).toHaveBeenCalledTimes(3);
|
|
49
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-background-color', '#111111');
|
|
50
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-accent-text-color', '#222222');
|
|
51
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-text-color', '#333333');
|
|
52
|
-
});
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { expect, vi, it, SpyInstance, afterEach, beforeAll } from 'vitest';
|
|
2
|
-
import { bindViewportCSSVars } from '../bindViewportCSSVars';
|
|
3
|
-
import { Viewport } from '../../viewport';
|
|
4
|
-
import { dispatchWindowMessageEvent } from '../../../test-utils/dispatchWindowMessageEvent';
|
|
5
|
-
|
|
6
|
-
let setCSSPropertySpy: SpyInstance<[string, string, string?], void>;
|
|
7
|
-
|
|
8
|
-
beforeAll(() => {
|
|
9
|
-
setCSSPropertySpy = vi
|
|
10
|
-
.spyOn(document.documentElement.style, 'setProperty')
|
|
11
|
-
.mockImplementation(() => {
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
afterEach(() => {
|
|
16
|
-
vi.clearAllMocks();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should set --tg-viewport-height = viewport.height, --tg-viewport-width = viewport.width, --tg-viewport-stable-height = viewport.stableHeight', () => {
|
|
20
|
-
bindViewportCSSVars(new Viewport({
|
|
21
|
-
height: 192,
|
|
22
|
-
width: 1000,
|
|
23
|
-
isExpanded: false,
|
|
24
|
-
stableHeight: 200,
|
|
25
|
-
}));
|
|
26
|
-
|
|
27
|
-
expect(setCSSPropertySpy).toHaveBeenCalledTimes(3);
|
|
28
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-height', '192px');
|
|
29
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-width', '1000px');
|
|
30
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-stable-height', '200px');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should update --tg-viewport-height, --tg-viewport-width, --tg-viewport-stable-height according to the values, received in the Viewport change events', () => {
|
|
34
|
-
const viewport = new Viewport({
|
|
35
|
-
height: 192,
|
|
36
|
-
width: 1000,
|
|
37
|
-
isExpanded: false,
|
|
38
|
-
stableHeight: 200,
|
|
39
|
-
});
|
|
40
|
-
bindViewportCSSVars(viewport);
|
|
41
|
-
setCSSPropertySpy.mockClear();
|
|
42
|
-
|
|
43
|
-
viewport.listen();
|
|
44
|
-
dispatchWindowMessageEvent('viewport_changed', {
|
|
45
|
-
height: 900,
|
|
46
|
-
is_state_stable: true,
|
|
47
|
-
is_expanded: false,
|
|
48
|
-
width: 1800,
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
expect(setCSSPropertySpy).toHaveBeenCalledTimes(3);
|
|
52
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-height', '900px');
|
|
53
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-width', '1800px');
|
|
54
|
-
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-stable-height', '900px');
|
|
55
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { afterEach, expect, it, vi } from 'vitest';
|
|
2
|
-
import { setCSSVar } from '../setCSSVar';
|
|
3
|
-
|
|
4
|
-
afterEach(() => {
|
|
5
|
-
vi.restoreAllMocks();
|
|
6
|
-
})
|
|
7
|
-
|
|
8
|
-
it('should call document.documentElement.style.setProperty with passed "name" and "value" arguments', () => {
|
|
9
|
-
const spy = vi.spyOn(document.documentElement.style, 'setProperty').mockImplementation(() => {});
|
|
10
|
-
|
|
11
|
-
setCSSVar('tma', 'is cool')
|
|
12
|
-
expect(spy).toHaveBeenCalledOnce();
|
|
13
|
-
expect(spy).toHaveBeenCalledWith('tma', 'is cool');
|
|
14
|
-
})
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { isRGB } from '~/colors/index.js';
|
|
2
|
-
import { setCSSVar } from '~/css/setCSSVar.js';
|
|
3
|
-
import type { MiniApp } from '~/mini-app/index.js';
|
|
4
|
-
import type { ThemeParams } from '~/theme-params/index.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Creates CSS variables connected with WebApp background and header colors based on
|
|
8
|
-
* passed WebApp and ThemeParams instances.
|
|
9
|
-
*
|
|
10
|
-
* Created variables:
|
|
11
|
-
* - `--tg-background-color`
|
|
12
|
-
* - `--tg-header-color`
|
|
13
|
-
*
|
|
14
|
-
* Variables are being automatically updated in case, corresponding properties are updating.
|
|
15
|
-
*
|
|
16
|
-
* @param miniApp - MiniApp instance.
|
|
17
|
-
* @param themeParams - ThemeParams instance.
|
|
18
|
-
*/
|
|
19
|
-
export function bindMiniAppCSSVars(miniApp: MiniApp, themeParams: ThemeParams): void {
|
|
20
|
-
const actualizeBackground = () => {
|
|
21
|
-
setCSSVar('--tg-background-color', miniApp.backgroundColor);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const actualizeHeader = () => {
|
|
25
|
-
const {
|
|
26
|
-
backgroundColor,
|
|
27
|
-
secondaryBackgroundColor,
|
|
28
|
-
} = themeParams;
|
|
29
|
-
|
|
30
|
-
if (isRGB(miniApp.headerColor)) {
|
|
31
|
-
setCSSVar('--tg-header-color', miniApp.headerColor);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (miniApp.headerColor === 'bg_color' && backgroundColor) {
|
|
36
|
-
setCSSVar('--tg-header-color', backgroundColor);
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (miniApp.headerColor === 'secondary_bg_color' && secondaryBackgroundColor) {
|
|
41
|
-
setCSSVar('--tg-header-color', secondaryBackgroundColor);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
themeParams.on('change', actualizeHeader);
|
|
46
|
-
miniApp.on('change:backgroundColor', actualizeBackground);
|
|
47
|
-
miniApp.on('change:headerColor', actualizeHeader);
|
|
48
|
-
|
|
49
|
-
actualizeBackground();
|
|
50
|
-
actualizeHeader();
|
|
51
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { ThemeParams } from '~/theme-params/index.js';
|
|
2
|
-
|
|
3
|
-
import { setCSSVar } from './setCSSVar.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Creates CSS variables connected with theme parameters. Created CSS variables names are
|
|
7
|
-
* following the pattern "--tg-theme-{name}". {name} is a theme parameters key name converted
|
|
8
|
-
* from snake to kebab case.
|
|
9
|
-
*
|
|
10
|
-
* Variables are being automatically updated in case, corresponding properties
|
|
11
|
-
* updated in passed ThemeParams instance.
|
|
12
|
-
*
|
|
13
|
-
* @param themeParams - ThemeParams instance.
|
|
14
|
-
*/
|
|
15
|
-
export function bindThemeCSSVars(themeParams: ThemeParams): void {
|
|
16
|
-
const actualize = () => {
|
|
17
|
-
const state = themeParams.getState();
|
|
18
|
-
|
|
19
|
-
Object.entries(state).forEach(([k, v]) => {
|
|
20
|
-
if (v) {
|
|
21
|
-
const key = k
|
|
22
|
-
.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
23
|
-
setCSSVar(`--tg-theme-${key}`, v);
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
themeParams.on('change', actualize);
|
|
29
|
-
|
|
30
|
-
actualize();
|
|
31
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { Viewport } from '~/viewport/index.js';
|
|
2
|
-
|
|
3
|
-
import { setCSSVar } from './setCSSVar.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Accepts Viewport instance and sets CSS variables connected with viewport
|
|
7
|
-
* sizes.
|
|
8
|
-
*
|
|
9
|
-
* Be careful using this function as long as it can impact application
|
|
10
|
-
* performance. Viewport size is changing rather often, this makes CSS
|
|
11
|
-
* variables update, which leads to possible layout redraw.
|
|
12
|
-
*
|
|
13
|
-
* Variables:
|
|
14
|
-
* - `--tg-viewport-height`
|
|
15
|
-
* - `--tg-viewport-width`
|
|
16
|
-
* - `--tg-viewport-stable-height`
|
|
17
|
-
*
|
|
18
|
-
* Variables are being automatically updated in case, corresponding properties
|
|
19
|
-
* updated in passed Viewport instance.
|
|
20
|
-
*
|
|
21
|
-
* @param viewport - Viewport instance.
|
|
22
|
-
*/
|
|
23
|
-
export function bindViewportCSSVars(viewport: Viewport): void {
|
|
24
|
-
const setHeight = () => setCSSVar('--tg-viewport-height', `${viewport.height}px`);
|
|
25
|
-
const setWidth = () => setCSSVar('--tg-viewport-width', `${viewport.width}px`);
|
|
26
|
-
const setStableHeight = () => setCSSVar('--tg-viewport-stable-height', `${viewport.stableHeight}px`);
|
|
27
|
-
|
|
28
|
-
// TODO: Should probably add debounce or throttle.
|
|
29
|
-
viewport.on('change:height', setHeight);
|
|
30
|
-
viewport.on('change:width', setWidth);
|
|
31
|
-
viewport.on('change:stableHeight', setStableHeight);
|
|
32
|
-
|
|
33
|
-
setHeight();
|
|
34
|
-
setWidth();
|
|
35
|
-
setStableHeight();
|
|
36
|
-
}
|
package/src/css/index.ts
DELETED