-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add default type option data from type
- Loading branch information
1 parent
699d6e3
commit 54e459f
Showing
9 changed files
with
387 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use collab::util::AnyMapExt; | ||
use yrs::Any; | ||
|
||
use crate::views::{DatabaseLayout, FieldSettingsMap, FieldSettingsMapBuilder}; | ||
|
||
#[repr(u8)] | ||
#[derive(Debug, Default, Clone, Eq, PartialEq)] | ||
pub enum FieldVisibility { | ||
#[default] | ||
AlwaysShown = 0, | ||
HideWhenEmpty = 1, | ||
AlwaysHidden = 2, | ||
} | ||
|
||
macro_rules! impl_into_field_visibility { | ||
($target: ident) => { | ||
impl std::convert::From<$target> for FieldVisibility { | ||
fn from(ty: $target) -> Self { | ||
match ty { | ||
0 => FieldVisibility::AlwaysShown, | ||
1 => FieldVisibility::HideWhenEmpty, | ||
2 => FieldVisibility::AlwaysHidden, | ||
_ => { | ||
tracing::error!("🔴Can't parse FieldVisibility from value: {}", ty); | ||
FieldVisibility::AlwaysShown | ||
}, | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_into_field_visibility!(i64); | ||
impl_into_field_visibility!(u8); | ||
|
||
impl From<FieldVisibility> for i64 { | ||
fn from(value: FieldVisibility) -> Self { | ||
(value as u8) as i64 | ||
} | ||
} | ||
|
||
/// Stores the field settings for a single field | ||
#[derive(Debug, Clone)] | ||
pub struct FieldSettings { | ||
pub field_id: String, | ||
pub visibility: FieldVisibility, | ||
pub width: i32, | ||
pub wrap_cell_content: bool, | ||
} | ||
|
||
pub const VISIBILITY: &str = "visibility"; | ||
pub const WIDTH: &str = "width"; | ||
pub const DEFAULT_WIDTH: i32 = 150; | ||
pub const WRAP_CELL_CONTENT: &str = "wrap"; | ||
|
||
pub fn default_field_visibility(layout_type: DatabaseLayout) -> FieldVisibility { | ||
match layout_type { | ||
DatabaseLayout::Grid => FieldVisibility::AlwaysShown, | ||
DatabaseLayout::Board => FieldVisibility::HideWhenEmpty, | ||
DatabaseLayout::Calendar => FieldVisibility::HideWhenEmpty, | ||
} | ||
} | ||
|
||
impl FieldSettings { | ||
pub fn from_any_map( | ||
field_id: &str, | ||
layout_type: DatabaseLayout, | ||
field_settings: &FieldSettingsMap, | ||
) -> Self { | ||
let visibility = field_settings | ||
.get_as::<i64>(VISIBILITY) | ||
.map(Into::into) | ||
.unwrap_or_else(|| default_field_visibility(layout_type)); | ||
let width = field_settings.get_as::<i32>(WIDTH).unwrap_or(DEFAULT_WIDTH); | ||
let wrap_cell_content: bool = field_settings.get_as(WRAP_CELL_CONTENT).unwrap_or(true); | ||
|
||
Self { | ||
field_id: field_id.to_string(), | ||
visibility, | ||
width, | ||
wrap_cell_content, | ||
} | ||
} | ||
} | ||
|
||
impl From<FieldSettings> for FieldSettingsMap { | ||
fn from(field_settings: FieldSettings) -> Self { | ||
FieldSettingsMapBuilder::from([ | ||
( | ||
VISIBILITY.into(), | ||
Any::BigInt(i64::from(field_settings.visibility)), | ||
), | ||
(WIDTH.into(), Any::BigInt(field_settings.width as i64)), | ||
( | ||
WRAP_CELL_CONTENT.into(), | ||
Any::Bool(field_settings.wrap_cell_content), | ||
), | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
collab-database/src/fields/type_option/checklist_type_option.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::{TypeOptionData, TypeOptionDataBuilder}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, Default)] | ||
pub struct ChecklistTypeOption; | ||
|
||
impl From<TypeOptionData> for ChecklistTypeOption { | ||
fn from(_data: TypeOptionData) -> Self { | ||
Self | ||
} | ||
} | ||
|
||
impl From<ChecklistTypeOption> for TypeOptionData { | ||
fn from(_data: ChecklistTypeOption) -> Self { | ||
TypeOptionDataBuilder::default() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
collab-database/src/fields/type_option/relation_type_option.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use collab::util::AnyMapExt; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::{TypeOptionData, TypeOptionDataBuilder}; | ||
|
||
#[derive(Debug, Clone, Default, Serialize, Deserialize)] | ||
pub struct RelationTypeOption { | ||
pub database_id: String, | ||
} | ||
|
||
impl From<TypeOptionData> for RelationTypeOption { | ||
fn from(data: TypeOptionData) -> Self { | ||
let database_id: String = data.get_as("database_id").unwrap_or_default(); | ||
Self { database_id } | ||
} | ||
} | ||
|
||
impl From<RelationTypeOption> for TypeOptionData { | ||
fn from(data: RelationTypeOption) -> Self { | ||
TypeOptionDataBuilder::from([("database_id".into(), data.database_id.into())]) | ||
} | ||
} |
Oops, something went wrong.