mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
QueryEditor: Fix copy-paste with unicode special symbol (#39117)
This commit is contained in:
parent
e1e385b318
commit
82f5fb7d6d
@ -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) {
|
||||
@ -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);
|
||||
}
|
||||
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user