Skip to content

Commit

Permalink
Add Image Scale Algorithm option (#89)
Browse files Browse the repository at this point in the history
* Add Image Scale Algorithm option

* Fix tests

* Formatting

* Update image.ts

---------

Co-authored-by: Mikhail <[email protected]>
  • Loading branch information
asimonok and mikhail-vl authored Oct 29, 2023
1 parent 808eb98 commit 4d11e8b
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features / Enhancements

- Update ESLint configuration (#84)
- Add Image Scale Algorithm option (#89)

## 4.0.0 (2023-06-26)

Expand Down
File renamed without changes.
11 changes: 9 additions & 2 deletions src/components/ImagePanel/ImagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { css, cx } from '@emotion/css';
import { FieldType, PanelProps } from '@grafana/data';
import { Alert, PageToolbar, ToolbarButton, useStyles2 } from '@grafana/ui';
import { ImageSizeModes, ImageTypesSymbols, SupportedTypes, TestIds } from '../../constants';
import { Styles } from '../../styles';
import { ButtonType, PanelOptions, ZoomType } from '../../types';
import { base64toBlob } from '../../utils';
import { Styles } from './ImagePanel.styles';

/**
* Properties
Expand Down Expand Up @@ -313,7 +313,14 @@ export const ImagePanel: React.FC<Props> = ({ options, data, width, height, repl
* Add URL to Image
*/
let image = (
<img width={imageWidth || ''} height={imageHeight || ''} src={img} data-testid={TestIds.panel.image} alt="" />
<img
width={imageWidth || ''}
height={imageHeight || ''}
src={img}
data-testid={TestIds.panel.image}
alt=""
style={{ imageRendering: options.scale }}
/>
);
if (options.url) {
const url = replaceVariables(options.url);
Expand Down
3 changes: 2 additions & 1 deletion src/constants/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PanelOptions, ZoomType } from '../types';
import { ImageScale, PanelOptions, ZoomType } from '../types';
import { ImageSizeModes } from './image';

/**
Expand All @@ -20,4 +20,5 @@ export const DefaultOptions: PanelOptions = {
widthMode: ImageSizeModes.AUTO,
widthName: '',
zoomType: ZoomType.DEFAULT,
scale: ImageScale.AUTO,
};
21 changes: 21 additions & 0 deletions src/constants/image.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ImageScale } from '../types';

/**
* Image Types
*/
Expand Down Expand Up @@ -50,3 +52,22 @@ export const SizeModeOptions = [
{ value: ImageSizeModes.ORIGINAL, label: 'Original' },
{ value: ImageSizeModes.CUSTOM, label: 'Custom' },
];

/**
* Image Scale Options
*/
export const ImageScaleOptions = [
{ value: ImageScale.AUTO, label: 'Auto' },
{
value: ImageScale.CRISP_EDGES,
label: 'Crisp Edges',
description:
'The image is scaled with an algorithm that preserves contrast and edges in the image. Generally intended for images such as pixel art or line drawings, no blurring or color smoothing occurs.',
},
{
value: ImageScale.PIXELATED,
label: 'Pixelated',
description:
'The image is scaled with the "nearest neighbor" or similar algorithm, preserving a "pixelated" look as the image changes in size.',
},
];
1 change: 1 addition & 0 deletions src/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('plugin', () => {
addRadio: jest.fn().mockImplementation(() => builder),
addTextInput: jest.fn().mockImplementation(() => builder),
addMultiSelect: jest.fn().mockImplementation(() => builder),
addSelect: jest.fn().mockImplementation(() => builder),
};

it('Should be instance of PanelPlugin', () => {
Expand Down
29 changes: 27 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Field, FieldType, PanelPlugin } from '@grafana/data';
import { ImagePanel } from './components';
import { ButtonsOptions, DefaultOptions, ImageSizeModes, SizeModeOptions, ZoomOptions } from './constants';
import {
ButtonsOptions,
DefaultOptions,
ImageScaleOptions,
ImageSizeModes,
SizeModeOptions,
ZoomOptions,
} from './constants';
import { ButtonType, PanelOptions } from './types';

/**
Expand All @@ -26,7 +33,12 @@ export const plugin = new PanelPlugin<PanelOptions>(ImagePanel).setNoPadding().s
filter: (f: Field) => f.type === FieldType.string,
noFieldsMessage: 'No strings fields found',
},
})
});

/**
* ToolBar
*/
builder
.addRadio({
path: 'toolbar',
name: 'Images and PDF only.',
Expand Down Expand Up @@ -140,6 +152,19 @@ export const plugin = new PanelPlugin<PanelOptions>(ImagePanel).setNoPadding().s
showIf: (options: PanelOptions) => options.heightMode === ImageSizeModes.CUSTOM,
});

/**
* Image
*/
builder.addSelect({
path: 'scale',
name: 'Scale Algorithm',
category: ['Image'],
settings: {
options: ImageScaleOptions,
},
defaultValue: DefaultOptions.scale,
});

/**
* Video / Audio
*/
Expand Down
16 changes: 16 additions & 0 deletions src/types/panel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { ImageSizeModes } from '../constants';
import { ButtonType, ZoomType } from './toolbar';

/**
* Image Scale
*/
export enum ImageScale {
AUTO = 'auto',
CRISP_EDGES = 'crisp-edges',
PIXELATED = 'pixelated',
}

/**
* Options
*/
Expand Down Expand Up @@ -109,4 +118,11 @@ export interface PanelOptions {
* @type {ZoomType}
*/
zoomType: ZoomType;

/**
* Scale
*
* @type {ImageScale}
*/
scale: ImageScale;
}

0 comments on commit 4d11e8b

Please sign in to comment.