Skip to content

Commit

Permalink
Merge pull request #248 from logisky/jh/input
Browse files Browse the repository at this point in the history
fix: input and jump
  • Loading branch information
ImJeremyHe authored Dec 28, 2024
2 parents 089af13 + 48f745b commit 9ae7268
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 16 deletions.
8 changes: 5 additions & 3 deletions crates/controller/src/api/cell_positioner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ impl<const INTERVAL: usize> CellPositioner<INTERVAL> {
continue;
}
let h = ws.get_row_height(curr)?;
result += h;
result = if reverse { result - h } else { result + h };
curr = advance(reverse, curr);
self.add_cache(true, curr, result);
}
Ok(result)
Expand All @@ -53,8 +54,9 @@ impl<const INTERVAL: usize> CellPositioner<INTERVAL> {
curr = advance(reverse, curr);
continue;
}
let h = ws.get_row_height(curr)?;
result += h;
let w = ws.get_col_width(curr)?;
result = if reverse { result - w } else { result + w };
curr = advance(reverse, curr);
self.add_cache(true, curr, result);
}
Ok(result)
Expand Down
3 changes: 3 additions & 0 deletions crates/controller/src/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use super::Workbook;
fn new_workbook() {
let wb = Workbook::default();
let ws = wb.get_sheet_by_idx(0).unwrap();

ws.get_cell_position(100, 100).unwrap();

let empty_display_window = ws.get_display_window(0, 0, 100, 100).unwrap();
let row_cnt = empty_display_window.rows.len();
let col_cnt = empty_display_window.cols.len();
Expand Down
2 changes: 1 addition & 1 deletion crates/controller/src/navigator/sheet_nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl SheetNav {

impl Default for SheetNav {
fn default() -> Self {
SheetNav::init(1000, 200, 0)
SheetNav::init(1000000, 200, 0)
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/web/src/api/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export class Worksheet {
}

public getCellPosition(row: number, col: number): CellPosition {
return get_cell_position(this._id, this._sheetIdx, row, col)
const result = get_cell_position(this._id, this._sheetIdx, row, col)
return result
}

public getDisplayWindowWithCellPosition(
Expand Down
6 changes: 5 additions & 1 deletion src/components/canvas/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const Internal: FC<CanvasProps> = observer(({selectedCell, selectedCell$}) => {
if (selectedCell.source != 'editbar') return

store.render.canvas.focus()
// jumpTo(selectedCell.row, selectedCell.col, 2)
store.render.jumpTo(selectedCell.row, selectedCell.col)
store.selector.reset()
store.textarea.reset()
}, [selectedCell])
Expand Down Expand Up @@ -125,11 +125,15 @@ const Internal: FC<CanvasProps> = observer(({selectedCell, selectedCell$}) => {
store.scroll()
}

let lastScrollTime = 0
const onMouseWheel = (e: WheelEvent) => {
// only support y scrollbar currently
if (store.anchorY + e.deltaY < 0) return
store.anchorY += e.deltaY

const now = Date.now()
if (now - lastScrollTime < 50) return
lastScrollTime = now
store.render.render()
store.scroll()
}
Expand Down
29 changes: 29 additions & 0 deletions src/components/canvas/store/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ export class Render {
})
}

jumpTo(row: number, col: number) {
const rect = this.canvas.getBoundingClientRect()
const resp = this.store.dataSvc.getCellViewWithCell(
this.store.currSheetIdx,
row,
col,
rect.height,
rect.width
)
resp.then((r) => {
if (isErrorMessage(r)) return
if (r.data.length === 0) return
const data = r.data.find((v) => {
return (
v.fromRow <= row &&
v.toRow >= row &&
v.fromCol <= col &&
v.toCol >= col
)
})
if (!data) return
const firstRow = data.rows[0]
const firstCol = data.cols[0]
this.store.anchorY = firstRow.position.startRow
this.store.anchorX = firstCol.position.startCol
this.render()
})
}

private _painterService = new PainterService()

private _renderCell(renderCell: RenderCell) {
Expand Down
26 changes: 19 additions & 7 deletions src/components/content/edit-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ export const EditBarComponent: FC<EditBarProps> = ({
const dataSvc = useInjection<DataService>(TYPES.Data)
const [coordinate, setCoordinate] = useState('')
const [formula, setFormula] = useState('')
const [isEditing, setIsEditing] = useState(false)

useEffect(() => {
if (isEditing) return
const {row, col} = selectedCell
const notation = toA1notation(selectedCell.col)
const notation = toA1notation(col)
setCoordinate(`${notation}${row + 1}`)
const cell = dataSvc.getCellInfo(dataSvc.getCurrentSheetIdx(), row, col)
cell.then((c) => {
if (isErrorMessage(c)) return
if (c.getFormula() === '') setFormula(c.getText())
else setFormula(c.getFormula())
setFormula(c.getFormula() || c.getText())
})
}, [selectedCell])
}, [selectedCell, isEditing])

const formulaTextChange = (newText: string) => {
const payload = new CellInputBuilder()
.sheetIdx(dataSvc.getCurrentSheetIdx())
Expand All @@ -39,20 +42,29 @@ export const EditBarComponent: FC<EditBarProps> = ({
.build()
dataSvc.handleTransaction(new Transaction([payload], true))
}

const locationChange = (newText: string) => {
const result = parseA1notation(newText)

if (!result) {
setCoordinate(
`${toA1notation(selectedCell.col)}${selectedCell.row + 1}`
)
setIsEditing(false)
return
}

selectedCell$({row: result.rs, col: result.cs, source: 'editbar'})
setIsEditing(false)
}

return (
<div className={styles.host}>
<input
className={styles.a1notation}
defaultValue={coordinate}
value={coordinate}
onChange={(e) => {
setCoordinate(e.target.value)
setIsEditing(true)
}}
onBlur={(e) => locationChange(e.target.value)}
/>
<div className={styles.middle} />
Expand Down
2 changes: 1 addition & 1 deletion src/components/content/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {SelectedCell, CanvasComponent} from '@/components/canvas'
import {FC, useRef} from 'react'
import {FC} from 'react'
import styles from './content.module.scss'
import {EditBarComponent} from './edit-bar'
import {SheetsTabComponent} from '@/components/sheets-tab'
Expand Down
4 changes: 2 additions & 2 deletions src/core/data2/view_manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {pxToPt, pxToWidth} from '../rate'
import {pxToPt, pxToWidth, widthToPx} from '../rate'
import {LeftTop} from '../settings'
import {RenderCell} from './render'
import {CellViewData, Rect, overlap, OverlapType} from './types'
Expand Down Expand Up @@ -30,7 +30,7 @@ export class ViewManager {
})
if (isErrorMessage(result)) return result
const {x, y} = result
return this.getViewResponse(x, y, height, width)
return this.getViewResponse(widthToPx(x), ptToPx(y), height, width)
}

public async getViewResponse(
Expand Down

0 comments on commit 9ae7268

Please sign in to comment.