-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into add_possibility_to_…
…configure_labels_params_metric
- Loading branch information
Showing
136 changed files
with
2,567 additions
and
913 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ression_metric/common/expression_functions/__snapshots__/metric_vis_function.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...pressions/expression_metric/public/components/__snapshots__/with_auto_scale.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/plugins/chart_expressions/expression_metric/public/components/with_auto_scale.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { css } from '@emotion/react'; | ||
import { euiThemeVars } from '@kbn/ui-theme'; | ||
|
||
export const autoScaleWrapperStyle = css({ | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
maxWidth: '100%', | ||
maxHeight: '100%', | ||
overflow: 'hidden', | ||
lineHeight: euiThemeVars.euiLineHeight, | ||
}); |
51 changes: 51 additions & 0 deletions
51
src/plugins/chart_expressions/expression_metric/public/components/with_auto_scale.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { computeScale, withAutoScale } from './with_auto_scale'; | ||
import { mount } from 'enzyme'; | ||
|
||
const mockElement = (clientWidth = 100, clientHeight = 200) => ({ | ||
clientHeight, | ||
clientWidth, | ||
}); | ||
|
||
describe('AutoScale', () => { | ||
describe('computeScale', () => { | ||
it('is 1 if any element is null', () => { | ||
expect(computeScale(null, null)).toBe(1); | ||
expect(computeScale(mockElement(), null)).toBe(1); | ||
expect(computeScale(null, mockElement())).toBe(1); | ||
}); | ||
|
||
it('is never over 1', () => { | ||
expect(computeScale(mockElement(2000, 2000), mockElement(1000, 1000))).toBe(1); | ||
}); | ||
|
||
it('is never under 0.3 in default case', () => { | ||
expect(computeScale(mockElement(2000, 1000), mockElement(1000, 10000))).toBe(0.3); | ||
}); | ||
|
||
it('is never under specified min scale if specified', () => { | ||
expect(computeScale(mockElement(2000, 1000), mockElement(1000, 10000), 0.1)).toBe(0.1); | ||
}); | ||
|
||
it('is the lesser of the x or y scale', () => { | ||
expect(computeScale(mockElement(2000, 2000), mockElement(3000, 5000))).toBe(0.4); | ||
expect(computeScale(mockElement(2000, 3000), mockElement(4000, 3200))).toBe(0.5); | ||
}); | ||
}); | ||
|
||
describe('withAutoScale', () => { | ||
it('renders', () => { | ||
const Component = () => <h1>Hoi!</h1>; | ||
const WrappedComponent = withAutoScale(Component); | ||
expect(mount(<WrappedComponent />)).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
91 changes: 91 additions & 0 deletions
91
src/plugins/chart_expressions/expression_metric/public/components/with_auto_scale.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React, { useRef, useEffect, useState, ComponentType, useMemo } from 'react'; | ||
import { throttle } from 'lodash'; | ||
import { useResizeObserver } from '@elastic/eui'; | ||
import { autoScaleWrapperStyle } from './with_auto_scale.styles'; | ||
|
||
interface AutoScaleParams { | ||
minScale?: number; | ||
} | ||
interface ClientDimensionable { | ||
clientWidth: number; | ||
clientHeight: number; | ||
} | ||
|
||
const MAX_SCALE = 1; | ||
const MIN_SCALE = 0.3; | ||
|
||
/** | ||
* computeScale computes the ratio by which the child needs to shrink in order | ||
* to fit into the parent. This function is only exported for testing purposes. | ||
*/ | ||
export function computeScale( | ||
parent: ClientDimensionable | null, | ||
child: ClientDimensionable | null, | ||
minScale: number = MIN_SCALE | ||
) { | ||
if (!parent || !child) { | ||
return 1; | ||
} | ||
|
||
const scaleX = parent.clientWidth / child.clientWidth; | ||
const scaleY = parent.clientHeight / child.clientHeight; | ||
|
||
return Math.max(Math.min(MAX_SCALE, Math.min(scaleX, scaleY)), minScale); | ||
} | ||
|
||
export function withAutoScale<T>( | ||
WrappedComponent: ComponentType<T>, | ||
autoScaleParams?: AutoScaleParams | ||
) { | ||
return (props: T) => { | ||
// An initial scale of 0 means we always redraw | ||
// at least once, which is sub-optimal, but it | ||
// prevents an annoying flicker. | ||
const [scale, setScale] = useState(0); | ||
const parentRef = useRef<HTMLDivElement>(null); | ||
const childrenRef = useRef<HTMLDivElement>(null); | ||
const parentDimensions = useResizeObserver(parentRef.current); | ||
|
||
const scaleFn = useMemo( | ||
() => | ||
throttle(() => { | ||
const newScale = computeScale( | ||
{ clientHeight: parentDimensions.height, clientWidth: parentDimensions.width }, | ||
childrenRef.current, | ||
autoScaleParams?.minScale | ||
); | ||
|
||
// Prevent an infinite render loop | ||
if (scale !== newScale) { | ||
setScale(newScale); | ||
} | ||
}), | ||
[parentDimensions, setScale, scale] | ||
); | ||
|
||
useEffect(() => { | ||
scaleFn(); | ||
}, [scaleFn]); | ||
|
||
return ( | ||
<div ref={parentRef} css={autoScaleWrapperStyle}> | ||
<div | ||
ref={childrenRef} | ||
style={{ | ||
transform: `scale(${scale || 0})`, | ||
}} | ||
> | ||
<WrappedComponent {...props} /> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getScreenshotContext, setScreenshotContext } from './context'; | ||
|
||
describe('getScreenshotContext', () => { | ||
it('should return a default value if there is no data', () => { | ||
expect(getScreenshotContext('key', 'default')).toBe('default'); | ||
}); | ||
}); | ||
|
||
describe('setScreenshotContext', () => { | ||
it('should store data in the context', () => { | ||
setScreenshotContext('key', 'value'); | ||
|
||
expect(getScreenshotContext('key')).toBe('value'); | ||
}); | ||
|
||
it('should not overwrite data on repetitive calls', () => { | ||
setScreenshotContext('key1', 'value1'); | ||
setScreenshotContext('key2', 'value2'); | ||
|
||
expect(getScreenshotContext('key1')).toBe('value1'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
// **PLEASE NOTE** | ||
// The functionality in this file targets a browser environment AND is intended to be used both in public and server. | ||
// For instance, reporting uses these functions when starting puppeteer to set the current browser into "screenshot" mode. | ||
|
||
type Context = Record<string, unknown>; | ||
|
||
declare global { | ||
interface Window { | ||
__KBN_SCREENSHOT_CONTEXT__?: Context; | ||
} | ||
} | ||
|
||
/** | ||
* Stores a value in the screenshotting context. | ||
* @param key Context key to set. | ||
* @param value Value to set. | ||
*/ | ||
export function setScreenshotContext<T = unknown>(key: string, value: T): void { | ||
// Literal value to prevent adding an external reference | ||
const KBN_SCREENSHOT_CONTEXT = '__KBN_SCREENSHOT_CONTEXT__'; | ||
|
||
if (!window[KBN_SCREENSHOT_CONTEXT]) { | ||
Object.defineProperty(window, KBN_SCREENSHOT_CONTEXT, { | ||
enumerable: true, | ||
writable: true, | ||
configurable: false, | ||
value: {}, | ||
}); | ||
} | ||
|
||
window[KBN_SCREENSHOT_CONTEXT]![key] = value; | ||
} | ||
|
||
/** | ||
* Retrieves a value from the screenshotting context. | ||
* @param key Context key to get. | ||
* @param defaultValue Value to return if the key is not found. | ||
* @return The value stored in the screenshotting context. | ||
*/ | ||
export function getScreenshotContext<T = unknown>(key: string, defaultValue?: T): T | undefined { | ||
// Literal value to prevent adding an external reference | ||
const KBN_SCREENSHOT_CONTEXT = '__KBN_SCREENSHOT_CONTEXT__'; | ||
|
||
return (window[KBN_SCREENSHOT_CONTEXT]?.[key] as T) ?? defaultValue; | ||
} |
Oops, something went wrong.