From 2d7ceb5d31c07eeedf31fcb6d698fa7c2ac70995 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 17 Oct 2024 22:39:17 +1100 Subject: [PATCH] [8.x] [Drilldowns] Fix drilldown add variable adding additional brackets (#196539) (#196655) # Backport This will backport the following commits from `main` to `8.x`: - [[Drilldowns] Fix drilldown add variable adding additional brackets (#196539)](https://github.com/elastic/kibana/pull/196539) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Krzysztof Kowalczyk --- .../url_drilldown_collect_config.tsx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/components/url_drilldown_collect_config/url_drilldown_collect_config.tsx b/src/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/components/url_drilldown_collect_config/url_drilldown_collect_config.tsx index bf9d965f945cf..60b8cc33c178f 100644 --- a/src/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/components/url_drilldown_collect_config/url_drilldown_collect_config.tsx +++ b/src/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/components/url_drilldown_collect_config/url_drilldown_collect_config.tsx @@ -31,6 +31,19 @@ export interface UrlDrilldownCollectConfigProps { variablesHelpDocsLink?: string; } +const isCursorBetweenDoubleCurlyBrackets = (editor: monaco.editor.IStandaloneCodeEditor) => { + const model = editor.getModel(); + const position = editor.getPosition(); + if (!model || !position) return false; + + const offset = model.getOffsetAt(position); + const text = model.getValue(); + const twoCharactersBeforeOffset = text.slice(offset - 2, offset); + const twoCharactersAfterOffset = text.slice(offset, offset + 2); + + return twoCharactersBeforeOffset === '{{' && twoCharactersAfterOffset === '}}'; +}; + export const UrlDrilldownCollectConfig: React.FC = ({ config, variables, @@ -64,9 +77,10 @@ export const UrlDrilldownCollectConfig: React.FC onSelect={(variable: string) => { const editor = editorRef.current; if (!editor) return; + const text = isCursorBetweenDoubleCurlyBrackets(editor) ? variable : `{{${variable}}}`; editor.trigger('keyboard', 'type', { - text: '{{' + variable + '}}', + text, }); }} />