diff --git a/packages/core/http/http-shared.ts b/packages/core/http/http-shared.ts new file mode 100644 index 000000000..827ea86ea --- /dev/null +++ b/packages/core/http/http-shared.ts @@ -0,0 +1,9 @@ +// Shared types and interfaces for http and image-source modules. +// Only put platform-agnostic types/interfaces here. + +// Example: (add more as needed) +// TODO: look at removing this after circulars are completely resolved. +export interface ImageSourceLike { + toBase64String(format: string, quality?: number): string; + // ... add other shared methods/properties as needed +} diff --git a/packages/core/http/index.d.ts b/packages/core/http/index.d.ts index 724f78ed9..b039c73c1 100644 --- a/packages/core/http/index.d.ts +++ b/packages/core/http/index.d.ts @@ -1,4 +1,5 @@ -import { ImageSource } from '../image-source'; +// import { ImageSource } from '../image-source'; +import type { ImageSourceLike } from './http-shared'; import { File } from '../file-system'; /** @@ -29,13 +30,13 @@ export function getJSON(options: HttpRequestOptions): Promise; * Downloads the content from the specified URL and attempts to decode it as an image. * @param url The URL to request from. */ -export function getImage(url: string): Promise; +export function getImage(url: string): Promise; /** * Downloads the content from the specified URL and attempts to decode it as an image. * @param options An object that specifies various request options. */ -export function getImage(options: HttpRequestOptions): Promise; +export function getImage(options: HttpRequestOptions): Promise; /** * Downloads the content from the specified URL and attempts to save it as file. @@ -157,7 +158,7 @@ export interface HttpContent { /** * Gets the response body as ImageSource. */ - toImage: () => Promise; + toImage: () => Promise; /** * Gets the response body as file. diff --git a/packages/core/http/index.ts b/packages/core/http/index.ts index 52e47f17c..6f8517513 100644 --- a/packages/core/http/index.ts +++ b/packages/core/http/index.ts @@ -1,4 +1,4 @@ -import { ImageSource } from '../image-source'; +import type { ImageSourceLike } from './http-shared'; import { File } from '../file-system'; import * as httpRequest from './http-request'; export * from './http-request'; @@ -91,7 +91,7 @@ export interface HttpContent { /** * Gets the response body as ImageSource. */ - toImage: () => Promise; + toImage: () => Promise; /** * Gets the response body as file. @@ -131,7 +131,7 @@ export function getJSON(arg: any): Promise { }); } -export function getImage(arg: any): Promise { +export function getImage(arg: any): Promise { return new Promise((resolve, reject) => { httpRequest.request(typeof arg === 'string' ? { url: arg, method: 'GET' } : arg).then( (r) => { diff --git a/packages/core/image-source/index.android.ts b/packages/core/image-source/index.android.ts index b0c3f4ffd..f061bf38a 100644 --- a/packages/core/image-source/index.android.ts +++ b/packages/core/image-source/index.android.ts @@ -1,7 +1,7 @@ // Definitions. import { ImageSource as ImageSourceDefinition, iosSymbolScaleType } from '.'; import { ImageAsset } from '../image-asset'; -import * as httpModule from '../http'; +import * as http from '../http'; // Types. import { path as fsPath, knownFolders } from '../file-system'; @@ -14,13 +14,6 @@ import { getScaledDimensions } from './image-source-common'; export { isFileOrResourcePath }; -let http: typeof httpModule; -function ensureHttp() { - if (!http) { - http = require('../http'); - } -} - let application: android.app.Application; let resources: android.content.res.Resources; @@ -87,10 +80,8 @@ export class ImageSource implements ImageSourceDefinition { }); } - static fromUrl(url: string): Promise { - ensureHttp(); - - return http.getImage(url); + static fromUrl(url: string): Promise { + return http.getImage(url) as Promise; } static fromResourceSync(name: string): ImageSource { diff --git a/packages/core/image-source/index.ios.ts b/packages/core/image-source/index.ios.ts index e4ed7dbc8..aa555d427 100644 --- a/packages/core/image-source/index.ios.ts +++ b/packages/core/image-source/index.ios.ts @@ -2,7 +2,6 @@ import { ImageSource as ImageSourceDefinition, iosSymbolScaleType } from '.'; import { ImageAsset } from '../image-asset'; import type { ImageBase } from '../ui/image/image-common'; -import * as httpModule from '../http'; import { Font } from '../ui/styling/font'; import { Color } from '../color'; import { Trace } from '../trace'; @@ -12,16 +11,10 @@ import { path as fsPath, knownFolders } from '../file-system'; import { isFileOrResourcePath, RESOURCE_PREFIX, layout, releaseNativeObject, SYSTEM_PREFIX } from '../utils'; import { getScaledDimensions } from './image-source-common'; +import * as http from '../http'; export { isFileOrResourcePath }; -let http: typeof httpModule; -function ensureHttp() { - if (!http) { - http = require('../http'); - } -} - export class ImageSource implements ImageSourceDefinition { public android: android.graphics.Bitmap; public ios: UIImage; @@ -69,9 +62,7 @@ export class ImageSource implements ImageSourceDefinition { } static fromUrl(url: string): Promise { - ensureHttp(); - - return http.getImage(url); + return http.getImage(url) as Promise; } static iosSystemScaleFor(scale: iosSymbolScaleType) { diff --git a/packages/core/ui/core/view-base/index.ts b/packages/core/ui/core/view-base/index.ts index 9d5034b45..025a732b0 100644 --- a/packages/core/ui/core/view-base/index.ts +++ b/packages/core/ui/core/view-base/index.ts @@ -1,12 +1,11 @@ import { AlignSelf, Flex, FlexFlow, FlexGrow, FlexShrink, FlexWrapBefore, Order } from '../../layouts/flexbox-layout'; import { Page } from '../../page'; -import { CoreTypes } from '../../../core-types'; +import { CoreTypes, Trace } from '../../styling/styling-shared'; import { Property, CssProperty, CssAnimationProperty, InheritedProperty, clearInheritedProperties, propagateInheritableProperties, propagateInheritableCssProperties, initNativeView } from '../properties'; import { CSSUtils } from '../../../css/system-classes'; import { Source } from '../../../utils/debug-source'; import { Binding } from '../bindable'; import { BindingOptions } from '../bindable/bindable-types'; -import { Trace } from '../../../trace'; import { Observable, PropertyChangeData, WrappedValue } from '../../../data/observable'; import { Style } from '../../styling/style'; import { paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty } from '../../styling/style-properties'; diff --git a/packages/core/ui/core/view/view-common.ts b/packages/core/ui/core/view/view-common.ts index a9c49a5b9..dfa7f3850 100644 --- a/packages/core/ui/core/view/view-common.ts +++ b/packages/core/ui/core/view/view-common.ts @@ -8,8 +8,6 @@ import { sanitizeModuleName } from '../../../utils/common'; import { Color } from '../../../color'; import { Property, InheritedProperty } from '../properties'; import { EventData } from '../../../data/observable'; -import { Trace } from '../../../trace'; -import { CoreTypes } from '../../../core-types'; import { ViewHelper } from './view-helper'; import { setupAccessibleView } from '../../../application'; @@ -31,6 +29,7 @@ import { accessibilityBlurEvent, accessibilityFocusChangedEvent, accessibilityFo import { ShadowCSSValues } from '../../styling/css-shadow'; import { SharedTransition, SharedTransitionInteractiveOptions } from '../../transition/shared-transition'; import { Flex, FlexFlow } from '../../layouts/flexbox-layout'; +import { CoreTypes, Trace } from '../../styling/styling-shared'; // helpers (these are okay re-exported here) export * from './view-helper'; diff --git a/packages/core/ui/core/view/view-helper/index.ios.ts b/packages/core/ui/core/view/view-helper/index.ios.ts index cf9898b7d..ffb5edb5e 100644 --- a/packages/core/ui/core/view/view-helper/index.ios.ts +++ b/packages/core/ui/core/view/view-helper/index.ios.ts @@ -5,8 +5,8 @@ import type { View } from '..'; // Requires import { ViewHelper } from './view-helper-common'; import { SDK_VERSION } from '../../../../utils/constants'; -import { ios as iOSUtils, layout } from '../../../../utils'; -import { Trace } from '../../../../trace'; +import { layout, Trace } from './view-helper-shared'; +import { ios as iOSUtils } from '../../../../utils'; export * from './view-helper-common'; export const AndroidHelper = 0; diff --git a/packages/core/ui/core/view/view-helper/view-helper-common.ts b/packages/core/ui/core/view/view-helper/view-helper-common.ts index 14b638f7d..54a01e4f1 100644 --- a/packages/core/ui/core/view/view-helper/view-helper-common.ts +++ b/packages/core/ui/core/view/view-helper/view-helper-common.ts @@ -1,10 +1,6 @@ // Types import { View as ViewDefinition } from '..'; -import { CoreTypes } from '../../../../core-types'; - -// Requires -import { layout } from '../../../../utils'; -import { Trace } from '../../../../trace'; +import { CoreTypes, layout, Trace } from './view-helper-shared'; export class ViewHelper { public static measureChild(parent: ViewDefinition, child: ViewDefinition, widthMeasureSpec: number, heightMeasureSpec: number): { measuredWidth: number; measuredHeight: number } { diff --git a/packages/core/ui/core/view/view-helper/view-helper-shared.ts b/packages/core/ui/core/view/view-helper/view-helper-shared.ts new file mode 100644 index 000000000..1bcb1031c --- /dev/null +++ b/packages/core/ui/core/view/view-helper/view-helper-shared.ts @@ -0,0 +1,9 @@ +// Shared helpers and types for view-helper, used by both common and platform-specific files. +// Only put platform-agnostic logic here. + +import { CoreTypes } from '../../../../core-types'; +import { layout } from '../../../../utils'; +import { Trace } from '../../../../trace'; + +export type { CoreTypes }; +export { layout, Trace }; diff --git a/packages/core/ui/styling/css-selector.ts b/packages/core/ui/styling/css-selector.ts index 40c4d5ffb..3cc4bf640 100644 --- a/packages/core/ui/styling/css-selector.ts +++ b/packages/core/ui/styling/css-selector.ts @@ -1,7 +1,7 @@ import { parse as convertToCSSWhatSelector, Selector as CSSWhatSelector, DataType as CSSWhatDataType } from 'css-what'; import '../../globals'; import { isCssVariable } from '../core/properties'; -import { Trace } from '../../trace'; +import { Trace, CoreTypes } from './styling-shared'; import { isNullOrUndefined } from '../../utils/types'; import * as ReworkCSS from '../../css'; diff --git a/packages/core/ui/styling/style-scope.ts b/packages/core/ui/styling/style-scope.ts index 08eaecd39..37ed20c2a 100644 --- a/packages/core/ui/styling/style-scope.ts +++ b/packages/core/ui/styling/style-scope.ts @@ -4,10 +4,10 @@ import { unsetValue, _evaluateCssVariableExpression, _evaluateCssCalcExpression, import * as ReworkCSS from '../../css'; import { RuleSet, StyleSheetSelectorScope, SelectorCore, SelectorsMatch, ChangeMap, fromAstNode, Node, MEDIA_QUERY_SEPARATOR, matchMediaQueryString } from './css-selector'; -import { Trace } from '../../trace'; +import { Trace, CoreTypes } from './styling-shared'; import { File, knownFolders, path } from '../../file-system'; import { Application, CssChangedEventData, LoadAppCSSEventData } from '../../application'; -import { profile } from '../../profiling'; +import { profile } from './styling-profile'; import * as kam from '../animation/keyframe-animation'; let keyframeAnimationModule: typeof kam; diff --git a/packages/core/ui/styling/styling-profile.ts b/packages/core/ui/styling/styling-profile.ts new file mode 100644 index 000000000..9541ec9cf --- /dev/null +++ b/packages/core/ui/styling/styling-profile.ts @@ -0,0 +1,4 @@ +// This file exists to break cycles caused by importing profiling into style-scope.ts. +// Only put the @profile decorator and related logic here. + +export { profile } from '../../profiling'; diff --git a/packages/core/ui/styling/styling-shared.ts b/packages/core/ui/styling/styling-shared.ts new file mode 100644 index 000000000..2c83f3309 --- /dev/null +++ b/packages/core/ui/styling/styling-shared.ts @@ -0,0 +1,7 @@ +// Shared helpers and types for styling, used by both styling and view/dialogs/frame modules. +// Only put platform-agnostic logic here. + +import { Trace } from '../../trace'; +import { CoreTypes } from '../../core-types'; + +export { Trace, CoreTypes }; diff --git a/packages/core/utils/common.ts b/packages/core/utils/common.ts index 2f8f46379..2b1102633 100644 --- a/packages/core/utils/common.ts +++ b/packages/core/utils/common.ts @@ -101,20 +101,6 @@ export function isDataURI(uri: string): boolean { return firstSegment && firstSegment.indexOf('data:') === 0 && firstSegment.indexOf('base64') >= 0; } -/** - * Get file extension from file path - * @param path file path - * @returns file extension - */ -export function getFileExtension(path: string): string { - const dotIndex = path.lastIndexOf('.'); - if (dotIndex && dotIndex >= 0 && dotIndex < path.length) { - return path.substring(dotIndex); - } - - return ''; -} - export function mergeSort(arr, compareFunc) { if (arr.length < 2) { return arr; @@ -292,3 +278,5 @@ export function getDurationWithDampingFromSpring(springSettings?: { tension?: nu damping, }; } + +export { getFileExtension } from './utils-shared'; diff --git a/packages/core/utils/native-helper.ios.ts b/packages/core/utils/native-helper.ios.ts index ee396a4cd..1c50b8ed2 100644 --- a/packages/core/utils/native-helper.ios.ts +++ b/packages/core/utils/native-helper.ios.ts @@ -4,6 +4,7 @@ import { Color } from '../color'; import { Trace } from '../trace'; import { CORE_ANIMATION_DEFAULTS, getDurationWithDampingFromSpring } from './common'; import { SDK_VERSION } from './constants'; +import { getFileExtension } from './utils-shared'; export function dataDeserialize(nativeData?: any) { if (isNullOrUndefined(nativeData)) { diff --git a/packages/core/utils/utils-shared.ts b/packages/core/utils/utils-shared.ts new file mode 100644 index 000000000..35bec0906 --- /dev/null +++ b/packages/core/utils/utils-shared.ts @@ -0,0 +1,12 @@ +// Shared helpers and types for utils, used by both native-helper and common. +// Only put platform-agnostic logic here. + +export function getFileExtension(path: string): string { + if (!path) { + return ''; + } + const index = path.lastIndexOf('.'); + return index !== -1 ? path.substring(index + 1) : ''; +} + +// Add more shared helpers/types/constants as needed.