Skip to content

Commit

Permalink
Merge pull request #273 from tamagokun/mk-paste-fix
Browse files Browse the repository at this point in the history
fix issues when pasting multiple cells with quoted text and new lines
  • Loading branch information
nick-keller authored Nov 27, 2024
2 parents f227436 + 12015ef commit 7455275
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/utils/copyPasting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ describe('parsePlainTextData', () => {
expect(parseTextPlainData('"foo\nbar""baz"""')).toEqual([['foo\nbar"baz"']])
})

test('single cell multi line clean', () => {
expect(parseTextPlainData('"foo\nbar"')).toEqual([['foo\nbar']])
})

test('multi cell multi line', () => {
expect(parseTextPlainData('"foo\nbar"\n"baz\nqux"')).toEqual([
['foo\nbar'],
['baz\nqux'],
])
})

test('multi cell single line', () => {
expect(parseTextPlainData('"foo\nbar"\t"baz\nqux"')).toEqual([
['foo\nbar', 'baz\nqux'],
])
})

test('quoted first cell', () => {
expect(parseTextPlainData('"foo\nbar')).toEqual([['"foo'], ['bar']])
})
Expand Down
4 changes: 4 additions & 0 deletions src/utils/copyPasting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ export const parseTextPlainData = (data: string): string[][] => {

const saveCell = () => {
let str = cleanData.slice(startCell, cursor)
if (str[0] === '"' && str[str.length - 1] === '"') {
quoted = true
}

if (quoted && str[str.length - 1] === '"' && str.includes('\n')) {
str = str.slice(1, str.length - 1).replace(/""/g, '"')
quoted = false
}

if (quoted && str[str.length - 1] !== '"') {
Expand Down

0 comments on commit 7455275

Please sign in to comment.