Skip to content

Commit

Permalink
Add cell clear payload (#223)
Browse files Browse the repository at this point in the history
* feat: add cell clear payload

* feat: new render data provider
  • Loading branch information
ImJeremyHe authored Jan 20, 2024
1 parent d1a2d3c commit 909be04
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 3 deletions.
8 changes: 7 additions & 1 deletion crates/controller/src/api/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,16 @@ impl<'a> Worksheet<'a> {

for row in start_row..=end_row {
let row_info = self.get_row_info(row).unwrap_or(RowInfo::default(row));
if row_info.hidden {
continue;
}
row_infos.push(row_info);

for col in start_col..=end_col {
'col: for col in start_col..=end_col {
let col_info = self.get_col_info(col).unwrap_or(ColInfo::default(col));
if col_info.hidden {
continue 'col;
}
col_infos.push(col_info);

if let Some(comment) = self.get_comment(row, col) {
Expand Down
8 changes: 8 additions & 0 deletions crates/controller/src/container/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ impl ContainerExecutor {
self.container.update_value(sheet_id, cell_id, cell_value);
Ok(self)
}
EditPayload::CellClear(p) => {
let sheet_id = ctx
.fetch_sheet_id_by_index(p.sheet_idx)
.map_err(|l| BasicError::SheetIdxExceed(l))?;
let cell_id = ctx.fetch_cell_id(&sheet_id, p.row, p.col)?;
self.container.remove_cell(sheet_id, &cell_id);
Ok(self)
}
EditPayload::SetColWidth(p) => {
let sheet_id = ctx
.fetch_sheet_id_by_index(p.sheet_idx)
Expand Down
5 changes: 5 additions & 0 deletions crates/controller/src/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ impl DataContainer {
sheet_container.cells.insert(cell_id, cell);
}

pub fn remove_cell(&mut self, sheet_id: SheetId, cell_id: &CellId) {
let sheet_container = self.get_sheet_container_mut(sheet_id);
sheet_container.cells.remove(cell_id);
}

pub fn update_value(&mut self, sheet_id: SheetId, cell_id: CellId, value: CellValue) {
let sheet_container = self.get_sheet_container_mut(sheet_id);
if let Some(v) = sheet_container.cells.get_mut(&cell_id) {
Expand Down
7 changes: 7 additions & 0 deletions crates/controller/src/cube_manager/executors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ impl CubeExecutor {
.map_err(|l| BasicError::SheetIdxExceed(l))?;
Ok(input(self, sheet_id, cell_input.row, cell_input.col, ctx))
}
EditPayload::CellClear(cell_clear) => {
let sheet_idx = cell_clear.sheet_idx;
let sheet_id = ctx
.fetch_sheet_id_by_index(sheet_idx)
.map_err(|l| BasicError::SheetIdxExceed(l))?;
Ok(input(self, sheet_id, cell_clear.row, cell_clear.col, ctx))
}
EditPayload::InsertCols(insert_cols) => {
let sheet_idx = insert_cols.sheet_idx;
let sheet_id = ctx
Expand Down
13 changes: 13 additions & 0 deletions crates/controller/src/edit_action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub enum EditPayload {
BlockStyleUpdate(BlockStyleUpdate),

CellInput(CellInput),
CellClear(CellClear),
SetColWidth(SetColWidth),
SetRowHeight(SetRowHeight),
SetVisible(SetVisible),
Expand Down Expand Up @@ -211,6 +212,18 @@ pub struct CellInput {
pub content: String,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "gents", derive(gents_derives::TS))]
#[cfg_attr(
feature = "gents",
ts(file_name = "cell_clear.ts", rename_all = "camelCase")
)]
pub struct CellClear {
pub sheet_idx: usize,
pub row: usize,
pub col: usize,
}

/// Create a new block.
///
/// Note that the block id is assigned by you. You are supposed to
Expand Down
7 changes: 7 additions & 0 deletions crates/controller/src/range_manager/executors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ impl RangeExecutor {
let res = input(self, sheet_id, p.row, p.col, ctx)?;
Ok(res)
}
EditPayload::CellClear(p) => {
let sheet_id = ctx
.fetch_sheet_id_by_index(p.sheet_idx)
.map_err(|l| BasicError::SheetIdxExceed(l))?;
let res = input(self, sheet_id, p.row, p.col, ctx)?;
Ok(res)
}
EditPayload::DeleteSheet(_) => todo!(),
EditPayload::InsertCols(insert_cols) => {
let sheet_id = ctx
Expand Down
7 changes: 7 additions & 0 deletions crates/controller/src/version_manager/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,12 @@ fn convert_diff<C: VersionExecCtx>(
.map_err(|l| BasicError::SheetIdxExceed(l))?;
Ok(Some((Diff::Unavailable, sheet_id)))
}
EditPayload::CellClear(cr) => {
let sheet_id = ctx
.fetch_sheet_id_by_index(cr.sheet_idx)
.map_err(|l| BasicError::SheetIdxExceed(l))?;
let cell_id = ctx.fetch_cell_id(&sheet_id, cr.row, cr.col)?;
Ok(Some((Diff::CellValue(cell_id), sheet_id)))
}
}
}
15 changes: 14 additions & 1 deletion crates/wasms/server/src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use logisheets_controller::controller::display::{DisplayRequest, DisplayWindowWithStartPoint};
use logisheets_controller::edit_action::{
AsyncFuncResult, BlockInput, CellInput, CreateBlock, CreateSheet, DeleteCols,
AsyncFuncResult, BlockInput, CellClear, CellInput, CreateBlock, CreateSheet, DeleteCols,
DeleteColsInBlock, DeleteRows, DeleteRowsInBlock, DeleteSheet, EditAction, EditPayload,
InsertCols, InsertColsInBlock, InsertRows, InsertRowsInBlock, MoveBlock, PayloadsAction,
SheetRename, StyleUpdate, StyleUpdateType,
Expand Down Expand Up @@ -496,6 +496,19 @@ pub fn cell_input(id: usize, sheet_idx: usize, row: usize, col: usize, content:
);
}

#[wasm_bindgen]
pub fn cell_clear(id: usize, sheet_idx: usize, row: usize, col: usize) {
init();
MANAGER.get_mut().add_payload(
id,
EditPayload::CellClear(CellClear {
sheet_idx,
row,
col,
}),
);
}

#[wasm_bindgen]
pub fn row_insert(id: usize, sheet_idx: usize, start: usize, count: usize) {
init();
Expand Down
3 changes: 3 additions & 0 deletions packages/web/src/api/workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Payload} from '../payloads'
import {
block_input,
block_line_shift,
cell_clear,
cell_input,
col_delete,
col_insert,
Expand Down Expand Up @@ -224,6 +225,8 @@ export class Workbook {
if (p.type === 'deleteSheet') return delete_sheet(this._id, p.sheetIdx)
if (p.type === 'insertSheet')
return create_sheet(this._id, p.sheetIdx, p.name)
if (p.type === 'cellClear')
return cell_clear(this._id, p.sheetIdx, p.row, p.col)
// eslint-disable-next-line no-console
console.log('Unimplemented!')
}
Expand Down
7 changes: 7 additions & 0 deletions packages/web/src/bindings/cell_clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// DO NOT EDIT. CODE GENERATED BY gents.

export interface CellClear {
sheetIdx: number
row: number
col: number
}
2 changes: 2 additions & 0 deletions packages/web/src/bindings/edit_payload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// DO NOT EDIT. CODE GENERATED BY gents.
import {BlockInput} from './block_input'
import {BlockStyleUpdate} from './block_style_update'
import {CellClear} from './cell_clear'
import {CellInput} from './cell_input'
import {CreateBlock} from './create_block'
import {CreateSheet} from './create_sheet'
Expand Down Expand Up @@ -31,6 +32,7 @@ export type EditPayload =
| {styleUpdate: StyleUpdate}
| {blockStyleUpdate: BlockStyleUpdate}
| {cellInput: CellInput}
| {cellClear: CellClear}
| {setColWidth: SetColWidth}
| {setRowHeight: SetRowHeight}
| {setVisible: SetVisible}
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './block_style_update'
export * from './border'
export * from './border_pr'
export * from './cell_alignment'
export * from './cell_clear'
export * from './cell_formula_value'
export * from './cell_id'
export * from './cell_info'
Expand Down
3 changes: 3 additions & 0 deletions packages/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type {
Color,
Comment,
CtCellAlignment,
CellInfo,
CtCellProtection,
CtFontFamily,
CtFontName,
Expand All @@ -19,6 +20,8 @@ export type {
DisplayPatch,
DisplayRequest,
DisplayResponse,
DisplayWindow,
DisplayWindowWithStartPoint,
EditAction,
Fill,
Font,
Expand Down
36 changes: 36 additions & 0 deletions packages/web/src/payloads/cell_clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export interface CellClear {
readonly type: 'cellClear'
readonly sheetIdx: number
readonly row: number
readonly col: number
}

export class CellClearBuilder {
private _sheetIdx?: number
private _row?: number
private _col?: number
public sheetIdx(sheetIdx: number): this {
this._sheetIdx = sheetIdx
return this
}
public row(row: number): this {
this._row = row
return this
}
public col(col: number): this {
this._col = col
return this
}
public build(): CellClear {
if (this._sheetIdx === undefined) throw Error('sheetIdx is undefined!')
if (this._row === undefined) throw Error('row is undefined!')
if (this._col === undefined) throw Error('col is undefined!')

return {
type: 'cellClear',
sheetIdx: this._sheetIdx,
row: this._row,
col: this._col,
}
}
}
3 changes: 3 additions & 0 deletions packages/web/src/payloads/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {SetFont} from './set_font'
import {SetRowHeight} from './set_row_height'
import {SetRowVisible} from './set_row_visible'
import {SheetRename} from './sheet_rename'
import {CellClear} from './cell_clear'

export * from './cell_input'
export * from './create_block'
Expand All @@ -41,10 +42,12 @@ export * from './set_row_height'
export * from './set_row_visible'
export * from './sheet_rename'
export * from './block_input'
export * from './cell_clear'

export type Payload =
| BlockInput
| CellInput
| CellClear
| CreateBlock
| DeleteBlockCols
| DeleteBlockRows
Expand Down
1 change: 1 addition & 0 deletions src/core/data2/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './workbook'
export * from './render'
Loading

0 comments on commit 909be04

Please sign in to comment.