Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
fix(spreadsheet): paste should skip first line (row) when select a wh…
Browse files Browse the repository at this point in the history
…ole column
  • Loading branch information
aligo committed Jul 21, 2022
1 parent ae47e8e commit d303e1e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
import { useSpreadsheetContext } from './SpreadsheetContext'

import { SpreadsheetCell } from './SpreadsheetCell'
import { useFormulaSpreadsheet } from './useFormulaSpreadsheet'

export const SpreadsheetBlockView: React.FC<SpreadsheetViewProps> = ({
editor,
Expand Down Expand Up @@ -273,14 +272,8 @@ export const SpreadsheetBlockView: React.FC<SpreadsheetViewProps> = ({
<SpreadsheetView>
<SpreadsheetHeader rowId="first" context={spreadsheetContext}>
{columns.map((column, i) => {
const handleTitleSave = (value: string): string | undefined => {
if (value && columns.some(c => c.title === value && c.uuid !== column.uuid)) {
return `'${value}' ${t('spreadsheet.column.name_used')}`
} else if (value === title) {
return `'${value}' ${t('spreadsheet.column.name_used')}`
} else {
updateColumn({ ...column, title: value })
}
const handleTitleSave = (title: string): string | undefined => {
return updateColumn({ ...column, title })
}
const onResize = (width: number): void => {
updateColumn({ ...column, width })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,23 @@ export const useSpreadsheetContext = (options: {
)

const pasteToSpreadsheet = React.useCallback(
(pasteMatrix: string[][]) => {
(pasteMatrix: string[][], selection: SpreadsheetSelection) => {
const { columnIds: selectedColumnIds } = selection
let { rowIdx, columnIdx } = getSelectedIdx()
if (rowIdx === rowIds.length) {
rowIdx = 0
}
if (columnIdx === columnIds.length) {
columnIdx = 0
}

let rowDelta = 0
pasteMatrix.forEach((r: string[], ri: number) => {
r.forEach((c: string, ci: number) => {
const rowId = rowIds[rowIdx + ri]
const rowId = rowIds[rowIdx + ri + rowDelta]
const columnId = columnIds[columnIdx + ci]
if (rowId && columnId) {
if (selectedColumnIds?.includes(columnId) && ri === 0) {
rowDelta -= 1
} else if (rowId && columnId) {
const cellId = `${rowId},${columnId}`
MashcardEventBus.dispatch(SpreadsheetUpdateCellValue({ parentId: parentId!, cellId, value: c }))
}
Expand All @@ -166,11 +169,11 @@ export const useSpreadsheetContext = (options: {
if (movement[2]) {
if (nColumnIdx >= columnIds.length) {
nColumnIdx = 0
nRowIdx = nRowIdx + 1
nRowIdx += 1
if (nRowIdx >= rowIds.length) nRowIdx = 0
}
} else {
if (nColumnIdx > columnIds.length) nColumnIdx = 0
} else if (nColumnIdx > columnIds.length) {
nColumnIdx = 0
}
if (nColumnIdx < 0) nColumnIdx = columnIds.length
if (nRowIdx > rowIds.length) nRowIdx = 0
Expand Down Expand Up @@ -206,7 +209,7 @@ export const useSpreadsheetContext = (options: {
const pasteMatrix = parsePasteTable(text)
devLog('paste to spreadsheet', [text])
devLog('parsed', pasteMatrix)
pasteToSpreadsheet(pasteMatrix)
pasteToSpreadsheet(pasteMatrix, selection)
e.preventDefault()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
BlockInput,
Block
} from '@mashcard/schema'
import { useEditorContext } from '../../../hooks'
import { useEditorContext, useEditorI18n } from '../../../hooks'
import { getFormulaContext } from '../FormulaView'
import { useFormulaSpreadsheet } from './useFormulaSpreadsheet'

Expand All @@ -37,7 +37,7 @@ export function useSpreadsheet(options: {
}): {
columns: SpreadsheetColumns
addColumn: (index?: number) => void
updateColumn: (column: SpreadsheetColumn) => void
updateColumn: (column: SpreadsheetColumn) => string | undefined
removeColumn: (column: SpreadsheetColumn) => void
moveColumn: (srcId: string, targetId: string) => void
rows: SpreadsheetRows
Expand All @@ -49,6 +49,7 @@ export function useSpreadsheet(options: {
deleteSpreadsheet: () => void
cellsMap: SpreadsheetCellsMap
} {
const [t] = useEditorI18n()
const { editor } = useEditorContext()
const formulaContext = getFormulaContext(editor)
const { isNew, parentId, data, updateAttributeData, title } = options
Expand Down Expand Up @@ -162,16 +163,21 @@ export function useSpreadsheet(options: {
)

const updateColumn = React.useCallback(
(column: SpreadsheetColumn): void => {
(column: SpreadsheetColumn): string | undefined => {
const oldColumns = columns.filter(c => c.uuid !== column.uuid)
if (column.title && oldColumns.some(c => c.title === column.title)) {
return `'${column.title}' ${t('spreadsheet.column.name_used')}`
} else if (column.title && column.title === title) {
return `'${column.title}' ${t('spreadsheet.column.name_used')}`
}
updateSpreadsheetAttributes(
[...oldColumns.slice(0, column.sort), column, ...oldColumns.slice(column.sort)].map((c, i) => ({
...c,
sort: i
}))
)
},
[columns, updateSpreadsheetAttributes]
[columns, updateSpreadsheetAttributes, title, t]
)

const addColumn = React.useCallback(
Expand Down

0 comments on commit d303e1e

Please sign in to comment.