Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pre-req] Convert Palettes and Components #69065

Merged
merged 15 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions x-pack/plugins/canvas/.storybook/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ function loadStories() {
// Find all files ending in *.examples.ts
const req = require.context('./..', true, /.(stories|examples).tsx$/);
req.keys().forEach(filename => req(filename));

// Import Canvas CSS
require('../public/style/index.scss')
}

// Set up the Storybook environment with custom settings.
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/canvas/.storybook/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ module.exports = async ({ config }) => {
config.resolve.alias['ui/url/absolute_to_parsed_url'] = path.resolve(__dirname, '../tasks/mocks/uiAbsoluteToParsedUrl');
config.resolve.alias['ui/chrome'] = path.resolve(__dirname, '../tasks/mocks/uiChrome');
config.resolve.alias.ui = path.resolve(KIBANA_ROOT, 'src/legacy/ui/public');
config.resolve.alias['src/legacy/ui/public/styles/styling_constants'] = path.resolve(KIBANA_ROOT, 'src/legacy/ui/public/styles/_styling_constants.scss');
config.resolve.alias.ng_mock$ = path.resolve(KIBANA_ROOT, 'src/test_utils/public/ng_mock');

return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { functionWrapper } from '../../../__tests__/helpers/function_wrapper';
import { palettes } from '../../../common/lib/palettes';
import { paulTor14 } from '../../../common/lib/palettes';
import { palette } from './palette';

describe('palette', () => {
Expand All @@ -25,7 +25,7 @@ describe('palette', () => {

it('defaults to pault_tor_14 colors', () => {
const result = fn(null);
expect(result.colors).toEqual(palettes.paul_tor_14.colors);
expect(result.colors).toEqual(paulTor14.colors);
});
});

Expand All @@ -47,17 +47,17 @@ describe('palette', () => {
describe('reverse', () => {
it('reverses order of the colors', () => {
const result = fn(null, { reverse: true });
expect(result.colors).toEqual(palettes.paul_tor_14.colors.reverse());
expect(result.colors).toEqual(paulTor14.colors.reverse());
});

it('keeps the original order of the colors', () => {
const result = fn(null, { reverse: false });
expect(result.colors).toEqual(palettes.paul_tor_14.colors);
expect(result.colors).toEqual(paulTor14.colors);
});

it(`defaults to 'false`, () => {
const result = fn(null);
expect(result.colors).toEqual(palettes.paul_tor_14.colors);
expect(result.colors).toEqual(paulTor14.colors);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/

import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
// @ts-ignore untyped local
import { palettes } from '../../../common/lib/palettes';
import { paulTor14 } from '../../../common/lib/palettes';
import { getFunctionHelp } from '../../../i18n';

interface Arguments {
Expand Down Expand Up @@ -52,7 +51,7 @@ export function palette(): ExpressionFunctionDefinition<'palette', null, Argumen
},
fn: (input, args) => {
const { color, reverse, gradient } = args;
const colors = ([] as string[]).concat(color || palettes.paul_tor_14.colors);
const colors = ([] as string[]).concat(color || paulTor14.colors);

return {
type: 'palette',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { storiesOf } from '@storybook/react';
import React from 'react';
import { action } from '@storybook/addon-actions';
import { PaletteArgInput } from '../palette';
import { paulTor14 } from '../../../../common/lib/palettes';

storiesOf('arguments/Palette', module).add('default', () => (
<div className="canvasContainerWrapper" style={{ width: '200px' }}>
<PaletteArgInput
argValue={{
type: 'expression',
chain: [
{
arguments: {
_: paulTor14.colors,
gradient: [paulTor14.gradient],
},
function: 'palette',
type: 'function',
},
],
}}
onValueChange={action('onValueChange')}
renderError={action('renderError')}
/>
</div>
));
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,62 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { FC } from 'react';
import PropTypes from 'prop-types';
import { get } from 'lodash';
import { getType } from '@kbn/interpreter/common';
import { ExpressionAstFunction, ExpressionAstExpression } from 'src/plugins/expressions';
import { PalettePicker } from '../../../public/components/palette_picker';
import { templateFromReactComponent } from '../../../public/lib/template_from_react_component';
import { ArgumentStrings } from '../../../i18n';
import { identifyPalette, ColorPalette } from '../../../common/lib';

const { Palette: strings } = ArgumentStrings;

const PaletteArgInput = ({ onValueChange, argValue, renderError }) => {
// Why is this neccesary? Does the dialog really need to know what parameter it is setting?

const throwNotParsed = () => renderError();
interface Props {
onValueChange: (value: ExpressionAstExpression) => void;
argValue: ExpressionAstExpression;
renderError: () => void;
}

export const PaletteArgInput: FC<Props> = ({ onValueChange, argValue, renderError }) => {
// TODO: This is weird, its basically a reimplementation of what the interpretter would return.
// Probably a better way todo this, and maybe a better way to handle template stype objects in general?
function astToPalette({ chain }) {
// Probably a better way todo this, and maybe a better way to handle template type objects in general?
const astToPalette = ({ chain }: { chain: ExpressionAstFunction[] }): ColorPalette | null => {
if (chain.length !== 1 || chain[0].function !== 'palette') {
throwNotParsed();
renderError();
return null;
}

try {
const colors = chain[0].arguments._.map((astObj) => {
if (getType(astObj) !== 'string') {
throwNotParsed();
renderError();
}
return astObj;
});
}) as string[];

const gradient = get(chain[0].arguments.gradient, '[0]');
const gradient = get<boolean>(chain[0].arguments.gradient, '[0]');
const palette = identifyPalette({ colors, gradient });

return { colors, gradient };
if (palette) {
return palette;
}

return ({
id: 'custom',
label: strings.getCustomPaletteLabel(),
colors,
gradient,
} as any) as ColorPalette;
} catch (e) {
throwNotParsed();
renderError();
}
}
return null;
};

function handleChange(palette) {
const astObj = {
const handleChange = (palette: ColorPalette): void => {
const astObj: ExpressionAstExpression = {
type: 'expression',
chain: [
{
Expand All @@ -57,13 +74,16 @@ const PaletteArgInput = ({ onValueChange, argValue, renderError }) => {
};

onValueChange(astObj);
}
};

const palette = astToPalette(argValue);

return (
<PalettePicker value={palette} onChange={handleChange} ariaLabel={strings.getDisplayName()} />
);
if (!palette) {
renderError();
return null;
}

return <PalettePicker palette={palette} onChange={handleChange} />;
};

PaletteArgInput.propTypes = {
Expand Down
Loading