Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/controller/base/src/traits/block_ref.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{BlockCellId, SheetId};

pub trait BlockRefTrait {
fn get_all_keys(&self, ref_name: &str) -> Vec<String>;
fn get_all_keys(&self, ref_name: &str) -> Vec<(String, SheetId, BlockCellId)>;
fn get_all_fields(&self, ref_name: &str) -> Vec<String>;
fn resolve(
&self,
Expand Down
98 changes: 69 additions & 29 deletions crates/controller/src/api/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use crate::{
};
use crate::{CellInfo, ColInfo, Comment, MergeCell, RowInfo, Style, Value};
use logisheets_base::errors::BasicError;
use logisheets_base::{BlockCellId, BlockId, CellId, ColId, DiyCellId, RowId, SheetId, StyleId};
use logisheets_base::{
BlockCellId, BlockId, CellId, ColId, DiyCellId, RowId, SheetId, StyleId, TextId,
};
use logisheets_parser::unparse;

use super::workbook::CellPositionerDefault;
Expand Down Expand Up @@ -959,19 +961,36 @@ impl<'a> Worksheet<'a> {
.schemas
.get(&(self.sheet_id, id))
.map(|s| {
let text_fetcher = |id: TextId| {
self.controller
.status
.text_id_manager
.get_string(&id)
.unwrap_or_default()
.clone()
};
let key_value = |cell_id: BlockCellId| {
self.controller
.status
.container
.get_cell(self.sheet_id, &CellId::BlockCell(cell_id))
.map(|cell| cell.value.to_string(&text_fetcher))
.unwrap_or_default()
};

let (keys, fields, random_entries) = match s {
Schema::RowSchema(schema) => {
let keys = schema
.keys
.iter()
.map(|(k, row_id)| {
.get_all_key_cell_ids(id)
.into_iter()
.map(|cell_id| {
let idx = block_place
.rows
.cols
.iter()
.position(|id| id == row_id)
.position(|col_id| col_id == &cell_id.col)
.unwrap();
crate::controller::display::BlockSchemaKeyEntry {
key: k.clone(),
key: key_value(cell_id),
idx,
}
})
Expand All @@ -996,16 +1015,16 @@ impl<'a> Worksheet<'a> {
}
Schema::ColSchema(schema) => {
let keys = schema
.keys
.iter()
.map(|(k, col_id)| {
.get_all_key_cell_ids(id)
.into_iter()
.map(|cell_id| {
let idx = block_place
.cols
.rows
.iter()
.position(|id| id == col_id)
.position(|row_id| row_id == &cell_id.row)
.unwrap();
crate::controller::display::BlockSchemaKeyEntry {
key: k.clone(),
key: key_value(cell_id),
idx,
}
})
Expand All @@ -1032,7 +1051,7 @@ impl<'a> Worksheet<'a> {
let random_entries = schema
.key_field
.iter()
.map(|(k, f, row_id, col_id, render_id)| {
.map(|(k, row_id, col_id, render_id)| {
let row = block_place
.rows
.iter()
Expand All @@ -1045,7 +1064,6 @@ impl<'a> Worksheet<'a> {
.unwrap();
BlockSchemaRandomEntry {
key: k.clone(),
field: f.clone(),
row,
col,
render_id: render_id.clone(),
Expand Down Expand Up @@ -1405,16 +1423,36 @@ impl<'a> Worksheet<'a> {
.schemas
.get(&(self.sheet_id, block_id))
.map(|s| {
let text_fetcher = |id: TextId| {
self.controller
.status
.text_id_manager
.get_string(&id)
.unwrap_or_default()
.clone()
};
let key_value = |cell_id: BlockCellId| {
self.controller
.status
.container
.get_cell(self.sheet_id, &CellId::BlockCell(cell_id))
.map(|cell| cell.value.to_string(&text_fetcher))
.unwrap_or_default()
};

let (keys, fields, random_entries) = match s {
Schema::RowSchema(schema) => {
let keys = schema
.keys
.iter()
.map(|(k, row_id)| {
let idx =
block_place.rows.iter().position(|id| id == row_id).unwrap();
.get_all_key_cell_ids(block_id)
.into_iter()
.map(|cell_id| {
let idx = block_place
.cols
.iter()
.position(|id| id == &cell_id.col)
.unwrap();
crate::controller::display::BlockSchemaKeyEntry {
key: k.clone(),
key: key_value(cell_id),
idx,
}
})
Expand All @@ -1436,13 +1474,16 @@ impl<'a> Worksheet<'a> {
}
Schema::ColSchema(schema) => {
let keys = schema
.keys
.iter()
.map(|(k, col_id)| {
let idx =
block_place.cols.iter().position(|id| id == col_id).unwrap();
.get_all_key_cell_ids(block_id)
.into_iter()
.map(|cell_id| {
let idx = block_place
.rows
.iter()
.position(|id| id == &cell_id.row)
.unwrap();
crate::controller::display::BlockSchemaKeyEntry {
key: k.clone(),
key: key_value(cell_id),
idx,
}
})
Expand All @@ -1466,14 +1507,13 @@ impl<'a> Worksheet<'a> {
let random_entries = schema
.key_field
.iter()
.map(|(k, f, row_id, col_id, render_id)| {
.map(|(k, row_id, col_id, render_id)| {
let row =
block_place.rows.iter().position(|id| id == row_id).unwrap();
let col =
block_place.cols.iter().position(|id| id == col_id).unwrap();
BlockSchemaRandomEntry {
key: k.clone(),
field: f.clone(),
row,
col,
render_id: render_id.clone(),
Expand Down
29 changes: 9 additions & 20 deletions crates/controller/src/block_manager/schema_manager/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,22 @@ impl BlockSchemaExecutor {
};
fields.push((field, (id, render_id)));
}
let mut keys = Vec::new();

for (j, k) in p.keys.into_iter().enumerate() {
let idx = j + p.key_from;
let id = if p.row {
ctx.fetch_block_cell_id(&sheet_id, &block_id, idx, 0)?.row
} else {
ctx.fetch_block_cell_id(&sheet_id, &block_id, 0, idx)?.col
};
keys.push((k, id));
}
let schema = if p.row {
let key = ctx
.fetch_block_cell_id(&sheet_id, &block_id, p.key_idx, 0)?
.row;
Schema::RowSchema(RowSchema {
fields,
keys,
key,
name: p.ref_name.clone(),
})
} else {
let key = ctx
.fetch_block_cell_id(&sheet_id, &block_id, 0, p.key_idx)?
.col;
Schema::ColSchema(ColSchema {
fields,
keys,
key,
name: p.ref_name.clone(),
})
};
Expand Down Expand Up @@ -108,13 +103,7 @@ impl BlockSchemaExecutor {
let r = unit.row;
let c = unit.col;
let cell_id = ctx.fetch_block_cell_id(&sheet_id, &block_id, r, c)?;
key_field.push((
unit.key,
unit.field,
cell_id.row,
cell_id.col,
unit.render_id,
));
key_field.push((unit.key, cell_id.row, cell_id.col, unit.render_id));
}
let schema = Schema::RandomSchema(RandomSchema {
key_field,
Expand Down
45 changes: 13 additions & 32 deletions crates/controller/src/block_manager/schema_manager/manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use imbl::HashMap;
use logisheets_base::{BlockCellId, BlockId, ColId, RowId, SheetId};
use logisheets_base::{BlockCellId, BlockId, SheetId};

use super::schema::{Field, Key, RenderId, Schema, SchemaTrait};
use super::schema::{Field, RenderId, Schema, SchemaTrait};

#[derive(Debug, Clone, Default)]
pub struct SchemaManager {
Expand All @@ -17,42 +17,23 @@ impl SchemaManager {
}
}

#[inline]
pub fn resolve_by_ref_name(
// Make sure you know what you are doing.
// Given the key cell id, return the key-field cell id.
pub fn partially_resolve(
&self,
name: &str,
key: &Key,
field: &Field,
) -> Option<(SheetId, BlockCellId)> {
let (sheet_id, block_id) = self.refs.get(name)?;
let (row_id, col_id) = self.resolve_by_block_id(sheet_id, block_id, key, field)?;
Some((
*sheet_id,
BlockCellId {
block_id: *block_id,
row: row_id,
col: col_id,
},
))
}

#[inline]
pub fn resolve_by_block_id(
&self,
sheet_id: &SheetId,
block_id: &BlockId,
key: &Key,
field: &Field,
) -> Option<(RowId, ColId)> {
ref_name: &str,
key: BlockCellId,
field: &String,
) -> Option<BlockCellId> {
let (sheet_id, block_id) = self.refs.get(ref_name)?;
let schema = self.schemas.get(&(*sheet_id, *block_id))?;
schema.resolve(key, field)
schema.partially_resolve(key, field)
}

#[inline]
pub fn get_all_keys(&self, ref_name: &str) -> Option<Vec<Key>> {
pub fn get_all_key_cell_ids(&self, ref_name: &str) -> Option<(SheetId, Vec<BlockCellId>)> {
let (sheet_id, block_id) = self.refs.get(ref_name)?;
let schema = self.schemas.get(&(*sheet_id, *block_id))?;
Some(schema.get_all_keys())
Some((*sheet_id, schema.get_all_key_cell_ids(*block_id)))
}

#[inline]
Expand Down
Loading