Skip to content

Commit

Permalink
[Cases] Add Lens markdown plugin (#96703)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkopycinski committed Aug 18, 2021
1 parent d2d1ad9 commit 5968a2c
Show file tree
Hide file tree
Showing 77 changed files with 2,305 additions and 344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface EmbeddableEditorState
| --- | --- | --- |
| [embeddableId](./kibana-plugin-plugins-embeddable-public.embeddableeditorstate.embeddableid.md) | <code>string</code> | |
| [originatingApp](./kibana-plugin-plugins-embeddable-public.embeddableeditorstate.originatingapp.md) | <code>string</code> | |
| [originatingPath](./kibana-plugin-plugins-embeddable-public.embeddableeditorstate.originatingpath.md) | <code>string</code> | |
| [searchSessionId](./kibana-plugin-plugins-embeddable-public.embeddableeditorstate.searchsessionid.md) | <code>string</code> | Pass current search session id when navigating to an editor, Editors could use it continue previous search session |
| [valueInput](./kibana-plugin-plugins-embeddable-public.embeddableeditorstate.valueinput.md) | <code>EmbeddableInput</code> | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-embeddable-public](./kibana-plugin-plugins-embeddable-public.md) &gt; [EmbeddableEditorState](./kibana-plugin-plugins-embeddable-public.embeddableeditorstate.md) &gt; [originatingPath](./kibana-plugin-plugins-embeddable-public.embeddableeditorstate.originatingpath.md)

## EmbeddableEditorState.originatingPath property

<b>Signature:</b>

```typescript
originatingPath?: string;
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@
"redux-saga": "^1.1.3",
"redux-thunk": "^2.3.0",
"redux-thunks": "^1.0.0",
"remark-stringify": "^9.0.0",
"regenerator-runtime": "^0.13.3",
"request": "^2.88.0",
"require-in-the-middle": "^5.0.2",
Expand Down
1 change: 1 addition & 0 deletions src/plugins/embeddable/public/lib/state_transfer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const EMBEDDABLE_EDITOR_STATE_KEY = 'embeddable_editor_state';
*/
export interface EmbeddableEditorState {
originatingApp: string;
originatingPath?: string;
embeddableId?: string;
valueInput?: EmbeddableInput;

Expand Down
2 changes: 2 additions & 0 deletions src/plugins/embeddable/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ export interface EmbeddableEditorState {
embeddableId?: string;
// (undocumented)
originatingApp: string;
// (undocumented)
originatingPath?: string;
searchSessionId?: string;
// (undocumented)
valueInput?: EmbeddableInput;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/saved_objects/public/finder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
export {
SavedObjectMetaData,
SavedObjectFinderUi,
SavedObjectFinderUiProps,
getSavedObjectFinder,
} from './saved_object_finder';
7 changes: 6 additions & 1 deletion src/plugins/saved_objects/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export {
SaveResult,
showSaveModal,
} from './save_modal';
export { getSavedObjectFinder, SavedObjectFinderUi, SavedObjectMetaData } from './finder';
export {
getSavedObjectFinder,
SavedObjectFinderUi,
SavedObjectFinderUiProps,
SavedObjectMetaData,
} from './finder';
export {
SavedObjectLoader,
SavedObjectLoaderFindOptions,
Expand Down
8 changes: 6 additions & 2 deletions x-pack/examples/embedded_lens_example/public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ export const App = (props: {
timeRange: time,
attributes: getLensAttributes(props.defaultIndexPattern!, color),
},
true
{
openInNewTab: true,
}
);
// eslint-disable-next-line no-bitwise
const newColor = '#' + ((Math.random() * 0xffffff) << 0).toString(16);
Expand All @@ -195,7 +197,9 @@ export const App = (props: {
timeRange: time,
attributes: getLensAttributes(props.defaultIndexPattern!, color),
},
false
{
openInNewTab: false,
}
);
}}
>
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/cases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ cases: CasesUiStart;
cases.getCreateCase({
onCancel: handleSetIsCancel,
onSuccess,
lensIntegration?: {
plugins: {
parsingPlugin,
processingPluginRenderer,
uiPlugin,
},
hooks: {
useInsertTimeline,
},
}
timelineIntegration?: {
plugins: {
parsingPlugin,
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/cases/common/ui/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import {
UserActionField,
} from '../api';

export interface CasesUiConfigType {
markdownPlugins: {
lens: boolean;
};
}

export const StatusAll = 'all' as const;
export type StatusAllType = typeof StatusAll;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const LENS_ID = 'lens';
10 changes: 10 additions & 0 deletions x-pack/plugins/cases/common/utils/markdown_plugins/lens/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './constants';
export * from './parser';
export * from './serializer';
77 changes: 77 additions & 0 deletions x-pack/plugins/cases/common/utils/markdown_plugins/lens/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { Plugin } from 'unified';
import { RemarkTokenizer } from '@elastic/eui';
import { LENS_ID } from './constants';

export const LensParser: Plugin = function () {
const Parser = this.Parser;
const tokenizers = Parser.prototype.blockTokenizers;
const methods = Parser.prototype.blockMethods;

const tokenizeLens: RemarkTokenizer = function (eat, value, silent) {
if (value.startsWith(`!{${LENS_ID}`) === false) return true;

const nextChar = value[6];

if (nextChar !== '{' && nextChar !== '}') return false; // this isn't actually a lens

if (silent) {
return true;
}

// is there a configuration?
const hasConfiguration = nextChar === '{';

let match = `!{${LENS_ID}`;
let configuration = {};

if (hasConfiguration) {
let configurationString = '';

let openObjects = 0;

for (let i = 6; i < value.length; i++) {
const char = value[i];
if (char === '{') {
openObjects++;
configurationString += char;
} else if (char === '}') {
openObjects--;
if (openObjects === -1) {
break;
}
configurationString += char;
} else {
configurationString += char;
}
}

match += configurationString;
try {
configuration = JSON.parse(configurationString);
} catch (e) {
const now = eat.now();
this.file.fail(`Unable to parse lens JSON configuration: ${e}`, {
line: now.line,
column: now.column + 6,
});
}
}

match += '}';

return eat(match)({
type: LENS_ID,
...configuration,
});
};

tokenizers.lens = tokenizeLens;
methods.splice(methods.indexOf('text'), 0, LENS_ID);
};
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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { TimeRange } from 'src/plugins/data/common';
import { LENS_ID } from './constants';

export interface LensSerializerProps {
attributes: Record<string, unknown>;
timeRange: TimeRange;
}

export const LensSerializer = ({ timeRange, attributes }: LensSerializerProps) =>
`!{${LENS_ID}${JSON.stringify({
timeRange,
attributes,
})}}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './parser';
export * from './serializer';
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { Plugin } from 'unified';
import { RemarkTokenizer } from '@elastic/eui';
import * as i18n from './translations';

export const ID = 'timeline';
const PREFIX = '[';

export const TimelineParser: Plugin = function () {
const Parser = this.Parser;
const tokenizers = Parser.prototype.blockTokenizers;
const methods = Parser.prototype.blockMethods;

const tokenizeTimeline: RemarkTokenizer = function (eat, value, silent) {
if (
value.startsWith(PREFIX) === false ||
(value.startsWith(PREFIX) === true && !value.includes('timelines?timeline=(id'))
) {
return false;
}

let index = 0;
const nextChar = value[index];

if (nextChar !== PREFIX) {
return false;
}

if (silent) {
return true;
}

function readArg(open: string, close: string) {
if (value[index] !== open) {
throw new Error(i18n.NO_PARENTHESES);
}

index++;

let body = '';
let openBrackets = 0;

for (; index < value.length; index++) {
const char = value[index];

if (char === close && openBrackets === 0) {
index++;
return body;
} else if (char === close) {
openBrackets--;
} else if (char === open) {
openBrackets++;
}

body += char;
}

return '';
}

const timelineTitle = readArg(PREFIX, ']');
const timelineUrl = readArg('(', ')');
const match = `[${timelineTitle}](${timelineUrl})`;

return eat(match)({
type: ID,
match,
});
};

tokenizeTimeline.locator = (value: string, fromIndex: number) => {
return value.indexOf(PREFIX, fromIndex);
};

tokenizers.timeline = tokenizeTimeline;
methods.splice(methods.indexOf('url'), 0, ID);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export interface TimelineSerializerProps {
match: string;
}

export const TimelineSerializer = ({ match }: TimelineSerializerProps) => match;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';

export const NO_PARENTHESES = i18n.translate(
'xpack.cases.markdownEditor.plugins.timeline.noParenthesesErrorMsg',
{
defaultMessage: 'Expected left parentheses',
}
);
4 changes: 4 additions & 0 deletions x-pack/plugins/cases/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
"requiredPlugins":[
"actions",
"esUiShared",
"lens",
"features",
"kibanaReact",
"kibanaUtils",
"triggersActionsUi"
],
"requiredBundles": [
"savedObjects"
],
"server":true,
"ui":true,
"version":"8.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
createWithKibanaMock,
} from '../kibana_react.mock';

export const KibanaServices = { get: jest.fn(), getKibanaVersion: jest.fn(() => '8.0.0') };
export const KibanaServices = {
get: jest.fn(),
getKibanaVersion: jest.fn(() => '8.0.0'),
getConfig: jest.fn(() => null),
};
export const useKibana = jest.fn().mockReturnValue({
services: createStartServicesMock(),
});
Expand Down
Loading

0 comments on commit 5968a2c

Please sign in to comment.