Skip to content

Commit

Permalink
QueryEditor: Fix copy-paste with unicode special symbol (#39117)
Browse files Browse the repository at this point in the history
  • Loading branch information
glintik committed Sep 13, 2021
1 parent e1e385b commit 82f5fb7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/grafana-ui/src/slate-plugins/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const getCopiedText = (textBlocks: string[], startOffset: number, endOffset: num
return textBlocks.join('\n').slice(startOffset, excludingLastLineLength + endOffset);
};

// Remove unicode special symbol - byte order mark (BOM), U+FEFF.
const removeBom = (str: string | undefined): string | undefined => {
return str?.replace(/[\uFEFF]/g, '');
};

export function ClipboardPlugin(): Plugin {
const clipboardPlugin: Plugin = {
onCopy(event: Event, editor: CoreEditor, next: () => any) {
Expand All @@ -26,7 +31,7 @@ export function ClipboardPlugin(): Plugin {
.toArray()
.map((block) => block.text);

const copiedText = getCopiedText(selectedBlocks, startOffset, endOffset);
const copiedText = removeBom(getCopiedText(selectedBlocks, startOffset, endOffset));
if (copiedText && clipEvent.clipboardData) {
clipEvent.clipboardData.setData('Text', copiedText);
}
Expand All @@ -38,10 +43,10 @@ export function ClipboardPlugin(): Plugin {
const clipEvent = event as ClipboardEvent;
clipEvent.preventDefault();
if (clipEvent.clipboardData) {
const pastedValue = clipEvent.clipboardData.getData('Text');
const lines = pastedValue.split('\n');
const pastedValue = removeBom(clipEvent.clipboardData.getData('Text'));
const lines = pastedValue?.split('\n');

if (lines.length) {
if (lines && lines.length) {
editor.insertText(lines[0]);
for (const line of lines.slice(1)) {
editor.splitBlock().insertText(line);
Expand Down

0 comments on commit 82f5fb7

Please sign in to comment.