Skip to content

Commit

Permalink
[Drilldowns] Fix drilldown add variable adding additional brackets (e…
Browse files Browse the repository at this point in the history
…lastic#196539)

## Summary

This PR adds a check to create/edit drilldown template editor for
whether the cursor (where variable would be inserted) is already between
double brackets.

Fixes: elastic#121421
  • Loading branch information
kowalczyk-krzysztof authored Oct 17, 2024
1 parent 6a0ad10 commit 3c3b7c8
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<UrlDrilldownCollectConfigProps> = ({
config,
variables,
Expand Down Expand Up @@ -64,9 +77,10 @@ export const UrlDrilldownCollectConfig: React.FC<UrlDrilldownCollectConfigProps>
onSelect={(variable: string) => {
const editor = editorRef.current;
if (!editor) return;
const text = isCursorBetweenDoubleCurlyBrackets(editor) ? variable : `{{${variable}}}`;

editor.trigger('keyboard', 'type', {
text: '{{' + variable + '}}',
text,
});
}}
/>
Expand Down

0 comments on commit 3c3b7c8

Please sign in to comment.