Skip to content

Commit 534c468

Browse files
maggieghamrycrob611elasticmachine
authored
[Canvas] Adds argument to open all links in new tab within markdown element (#57017) (#58149)
* Adds toggle to open links in new tab within markdown element * Updating test for markdown function * Switch to using Markdown Component * Update ui.ts Update to change toggle verbiage per Ryan's request, and reuse the Kibana Markdown per Corey's help and recommendation. Still working on updating the styles (consulting Ryan) * Update toggle.js Update to prevent text for "Open links in new tab from wrapping" - the example from the horizontal bar chart is quite differently, and reads from "axisConfig" - when I changed the argType to "axisConfig", the layout was the same, but I'll need some input on which specific styles to add to the "ToggleArgInput" - I think this is where the style updates need to occur to get the toggle to stay on the same line, but may be wrong. * Update ui.ts Update to original message string per Ryan's feedback, now that there is no wrapping * Update to UI styles per Ryan's feedback * updating message per Ryan's request * Update ui.ts update to fix internationalization issues Co-authored-by: Corey Robertson <crob611@gmail.com> Co-authored-by: Corey Robertson <crob611@gmail.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 24e5d4c commit 534c468

File tree

7 files changed

+86
-23
lines changed

7 files changed

+86
-23
lines changed

x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/browser/markdown.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('markdown', () => {
1919
});
2020

2121
describe('args', () => {
22-
describe('expression', () => {
22+
describe('content', () => {
2323
it('sets the content to all strings in expression concatenated', () => {
2424
const result = fn(null, {
2525
content: ['# this ', 'is ', 'some ', 'markdown'],
@@ -54,5 +54,32 @@ describe('markdown', () => {
5454
// TODO: write test when using an instance of the interpreter
5555
// it("defaults to the expression '{font}'", () => {});
5656
});
57+
describe('openLinksInNewTab', () => {
58+
it('sets the value of openLinksInNewTab to true ', () => {
59+
const result = fn(null, {
60+
content: ['some ', 'markdown'],
61+
openLinksInNewTab: true,
62+
});
63+
64+
expect(result.value).toHaveProperty('openLinksInNewTab', true);
65+
});
66+
67+
it('sets the value of openLinksInNewTab to false ', () => {
68+
const result = fn(null, {
69+
content: ['some ', 'markdown'],
70+
openLinksInNewTab: false,
71+
});
72+
73+
expect(result.value).toHaveProperty('openLinksInNewTab', false);
74+
});
75+
76+
it('defaults the value of openLinksInNewTab to false ', () => {
77+
const result = fn(null, {
78+
content: ['some ', 'markdown'],
79+
});
80+
81+
expect(result.value).toHaveProperty('openLinksInNewTab', false);
82+
});
83+
});
5784
});
5885
});

x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/browser/markdown.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ type Context = Datatable | null;
1919
interface Arguments {
2020
content: string[];
2121
font: Style;
22+
openLinksInNewTab: boolean;
2223
}
2324

2425
interface Return {
2526
content: string;
2627
font: Style;
28+
openLinksInNewTab: boolean;
2729
}
2830

2931
export function markdown(): ExpressionFunctionDefinition<
@@ -53,6 +55,11 @@ export function markdown(): ExpressionFunctionDefinition<
5355
help: argHelp.font,
5456
default: '{font}',
5557
},
58+
openLinksInNewTab: {
59+
types: ['boolean'],
60+
help: argHelp.openLinksInNewTab,
61+
default: false,
62+
},
5663
},
5764
fn: (input, args) => {
5865
const compileFunctions = args.content.map(str =>
@@ -71,6 +78,7 @@ export function markdown(): ExpressionFunctionDefinition<
7178
value: {
7279
content: compileFunctions.map(fn => fn(ctx)).join(''),
7380
font: args.font,
81+
openLinksInNewTab: args.openLinksInNewTab,
7482
},
7583
};
7684
},

x-pack/legacy/plugins/canvas/canvas_plugin_src/renderers/markdown/index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,29 @@
66

77
import React from 'react';
88
import ReactDOM from 'react-dom';
9-
import Markdown from 'markdown-it';
109
import { RendererStrings } from '../../../i18n';
10+
import { Markdown } from '../../../../../../../src/legacy/core_plugins/kibana_react/public';
1111

1212
const { markdown: strings } = RendererStrings;
1313

14-
const md = new Markdown();
15-
1614
export const markdown = () => ({
1715
name: 'markdown',
1816
displayName: strings.getDisplayName(),
1917
help: strings.getHelpDescription(),
2018
reuseDomNode: true,
2119
render(domNode, config, handlers) {
22-
const html = { __html: md.render(String(config.content)) };
2320
const fontStyle = config.font ? config.font.spec : {};
2421

25-
/* eslint-disable react/no-danger */
2622
ReactDOM.render(
27-
<div
28-
className="kbnMarkdown__body canvasMarkdown"
23+
<Markdown
24+
className="canvasMarkdown"
2925
style={fontStyle}
30-
dangerouslySetInnerHTML={html}
26+
markdown={config.content}
27+
openLinksInNewTab={config.openLinksInNewTab}
3128
/>,
3229
domNode,
3330
() => handlers.done()
3431
);
35-
/* eslint-enable */
3632

3733
handlers.onDestroy(() => ReactDOM.unmountComponentAtNode(domNode));
3834
},

x-pack/legacy/plugins/canvas/canvas_plugin_src/uis/arguments/toggle.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,37 @@ const ToggleArgInput = ({ onValueChange, argValue, argId, renderError, typeInsta
1919
return null;
2020
}
2121
return (
22-
<EuiFormRow display="columnCompressedSwitch">
23-
<EuiSwitch
24-
compressed
25-
id={argId}
26-
checked={argValue}
27-
onChange={handleChange}
28-
className="canvasArg__switch"
29-
aria-label={typeInstance.displayName}
30-
label=""
31-
showLabel={false}
32-
/>
33-
</EuiFormRow>
22+
<div>
23+
<EuiFormRow display="rowCompressed">
24+
<EuiSwitch
25+
compressed
26+
id={argId}
27+
checked={argValue}
28+
onChange={handleChange}
29+
className="canvasArg__form"
30+
aria-label={typeInstance.displayName}
31+
resize="none"
32+
label={typeInstance.options.labelValue}
33+
showLabel
34+
/>
35+
</EuiFormRow>
36+
</div>
3437
);
3538
};
3639

3740
ToggleArgInput.propTypes = {
3841
onValueChange: PropTypes.func.isRequired,
3942
argValue: PropTypes.oneOfType([PropTypes.bool, PropTypes.string, PropTypes.object]).isRequired,
4043
argId: PropTypes.string.isRequired,
44+
labelValue: PropTypes.string,
45+
showLabelValue: PropTypes.bool,
4146
renderError: PropTypes.func.isRequired,
4247
};
4348

4449
export const toggle = () => ({
4550
name: 'toggle',
4651
displayName: strings.getDisplayName(),
4752
help: strings.getHelp(),
48-
simpleTemplate: templateFromReactComponent(ToggleArgInput),
53+
template: templateFromReactComponent(ToggleArgInput),
4954
default: 'false',
5055
});

x-pack/legacy/plugins/canvas/canvas_plugin_src/uis/views/markdown.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,16 @@ export const markdown = () => ({
2929
name: 'font',
3030
argType: 'font',
3131
},
32+
{
33+
name: 'openLinksInNewTab',
34+
displayName: strings.getOpenLinksInNewTabDisplayName(),
35+
help: strings.getOpenLinksInNewTabHelp(),
36+
label: strings.getOpenLinksInNewTabLabelName(),
37+
argType: 'toggle',
38+
default: false,
39+
options: {
40+
labelValue: 'Open all links in a new tab',
41+
},
42+
},
3243
],
3344
});

x-pack/legacy/plugins/canvas/i18n/functions/dict/markdown.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,9 @@ export const help: FunctionHelp<FunctionFactory<typeof markdown>> = {
3737
fontWeight: 'font-weight',
3838
},
3939
}),
40+
openLinksInNewTab: i18n.translate('xpack.canvas.functions.markdown.args.openLinkHelpText', {
41+
defaultMessage:
42+
'A true/false value for opening links in a new tab. Default value is false. Setting to true will open all links in a new tab.',
43+
}),
4044
},
4145
};

x-pack/legacy/plugins/canvas/i18n/ui.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,18 @@ export const ViewStrings = {
628628
markdown: MARKDOWN,
629629
},
630630
}),
631+
getOpenLinksInNewTabDisplayName: () =>
632+
i18n.translate('xpack.canvas.uis.views.openLinksInNewTabTitle', {
633+
defaultMessage: 'Markdown link settings',
634+
}),
635+
getOpenLinksInNewTabLabelName: () =>
636+
i18n.translate('xpack.canvas.uis.views.openLinksInNewTabLabel', {
637+
defaultMessage: 'Open all links in a new tab',
638+
}),
639+
getOpenLinksInNewTabHelp: () =>
640+
i18n.translate('xpack.canvas.uis.views.openLinksInNewTabHelpLabel', {
641+
defaultMessage: 'Set links to open in new tab',
642+
}),
631643
},
632644
Metric: {
633645
getDisplayName: () =>

0 commit comments

Comments
 (0)