Skip to content

Commit e009a74

Browse files
committed
feat: add field settings, type options and layout settings
1 parent 699d6e3 commit e009a74

File tree

14 files changed

+609
-13
lines changed

14 files changed

+609
-13
lines changed

collab-database/src/entity.rs

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
#![allow(clippy::upper_case_acronyms)]
22
use crate::database::{gen_database_id, gen_database_view_id, gen_row_id, timestamp, DatabaseData};
33
use crate::error::DatabaseError;
4-
use crate::fields::Field;
4+
use crate::fields::checkbox_type_option::CheckboxTypeOption;
5+
use crate::fields::checklist_type_option::ChecklistTypeOption;
6+
use crate::fields::date_type_option::{DateTypeOption, TimeTypeOption};
7+
use crate::fields::media_type_option::MediaTypeOption;
8+
use crate::fields::number_type_option::NumberTypeOption;
9+
use crate::fields::relation_type_option::RelationTypeOption;
10+
use crate::fields::select_type_option::{MultiSelectTypeOption, SingleSelectTypeOption};
11+
use crate::fields::summary_type_option::SummarizationTypeOption;
12+
use crate::fields::text_type_option::RichTextTypeOption;
13+
use crate::fields::timestamp_type_option::TimestampTypeOption;
14+
use crate::fields::translate_type_option::TranslateTypeOption;
15+
use crate::fields::url_type_option::URLTypeOption;
16+
use crate::fields::{Field, TypeOptionData};
517
use crate::rows::CreateRowParams;
618
use crate::views::{
719
DatabaseLayout, FieldOrder, FieldSettingsByFieldIdMap, FieldSettingsMap, FilterMap,
@@ -13,6 +25,7 @@ use collab_entity::CollabType;
1325
use serde::{Deserialize, Serialize};
1426
use serde_repr::{Deserialize_repr, Serialize_repr};
1527
use std::collections::HashMap;
28+
use std::fmt::{Display, Formatter};
1629
use tracing::error;
1730
use yrs::{Any, Out};
1831

@@ -254,7 +267,7 @@ impl CreateDatabaseParams {
254267
}
255268
}
256269

257-
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
270+
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
258271
#[repr(u8)]
259272
pub enum FieldType {
260273
RichText = 0,
@@ -276,7 +289,7 @@ pub enum FieldType {
276289

277290
impl FieldType {
278291
pub fn type_id(&self) -> String {
279-
(self.clone() as i64).to_string()
292+
(*self as i64).to_string()
280293
}
281294
}
282295

@@ -286,6 +299,12 @@ impl From<FieldType> for i64 {
286299
}
287300
}
288301

302+
impl From<&FieldType> for i64 {
303+
fn from(field_type: &FieldType) -> Self {
304+
*field_type as i64
305+
}
306+
}
307+
289308
impl TryFrom<yrs::Out> for FieldType {
290309
type Error = yrs::Out;
291310

@@ -297,6 +316,120 @@ impl TryFrom<yrs::Out> for FieldType {
297316
}
298317
}
299318

319+
impl Display for FieldType {
320+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
321+
let value: i64 = (*self).into();
322+
f.write_fmt(format_args!("{}", value))
323+
}
324+
}
325+
326+
impl AsRef<FieldType> for FieldType {
327+
fn as_ref(&self) -> &FieldType {
328+
self
329+
}
330+
}
331+
332+
impl From<&FieldType> for FieldType {
333+
fn from(field_type: &FieldType) -> Self {
334+
*field_type
335+
}
336+
}
337+
338+
impl FieldType {
339+
pub fn value(&self) -> i64 {
340+
(*self).into()
341+
}
342+
343+
pub fn default_name(&self) -> String {
344+
let s = match self {
345+
FieldType::RichText => "Text",
346+
FieldType::Number => "Number",
347+
FieldType::DateTime => "Date",
348+
FieldType::SingleSelect => "Single Select",
349+
FieldType::MultiSelect => "Multi Select",
350+
FieldType::Checkbox => "Checkbox",
351+
FieldType::URL => "URL",
352+
FieldType::Checklist => "Checklist",
353+
FieldType::LastEditedTime => "Last modified",
354+
FieldType::CreatedTime => "Created time",
355+
FieldType::Relation => "Relation",
356+
FieldType::Summary => "Summarize",
357+
FieldType::Translate => "Translate",
358+
FieldType::Time => "Time",
359+
FieldType::Media => "Media",
360+
};
361+
s.to_string()
362+
}
363+
364+
pub fn is_ai_field(&self) -> bool {
365+
matches!(self, FieldType::Summary | FieldType::Translate)
366+
}
367+
368+
pub fn is_number(&self) -> bool {
369+
matches!(self, FieldType::Number)
370+
}
371+
372+
pub fn is_text(&self) -> bool {
373+
matches!(self, FieldType::RichText)
374+
}
375+
376+
pub fn is_checkbox(&self) -> bool {
377+
matches!(self, FieldType::Checkbox)
378+
}
379+
380+
pub fn is_date(&self) -> bool {
381+
matches!(self, FieldType::DateTime)
382+
}
383+
384+
pub fn is_single_select(&self) -> bool {
385+
matches!(self, FieldType::SingleSelect)
386+
}
387+
388+
pub fn is_multi_select(&self) -> bool {
389+
matches!(self, FieldType::MultiSelect)
390+
}
391+
392+
pub fn is_last_edited_time(&self) -> bool {
393+
matches!(self, FieldType::LastEditedTime)
394+
}
395+
396+
pub fn is_created_time(&self) -> bool {
397+
matches!(self, FieldType::CreatedTime)
398+
}
399+
400+
pub fn is_url(&self) -> bool {
401+
matches!(self, FieldType::URL)
402+
}
403+
404+
pub fn is_select_option(&self) -> bool {
405+
self.is_single_select() || self.is_multi_select()
406+
}
407+
408+
pub fn is_checklist(&self) -> bool {
409+
matches!(self, FieldType::Checklist)
410+
}
411+
412+
pub fn is_relation(&self) -> bool {
413+
matches!(self, FieldType::Relation)
414+
}
415+
416+
pub fn is_time(&self) -> bool {
417+
matches!(self, FieldType::Time)
418+
}
419+
420+
pub fn is_media(&self) -> bool {
421+
matches!(self, FieldType::Media)
422+
}
423+
424+
pub fn can_be_group(&self) -> bool {
425+
self.is_select_option() || self.is_checkbox() || self.is_url()
426+
}
427+
428+
pub fn is_auto_update(&self) -> bool {
429+
self.is_last_edited_time()
430+
}
431+
}
432+
300433
impl From<i64> for FieldType {
301434
fn from(index: i64) -> Self {
302435
match index {
@@ -323,6 +456,29 @@ impl From<i64> for FieldType {
323456
}
324457
}
325458

459+
pub fn default_type_option_data_from_type(field_type: FieldType) -> TypeOptionData {
460+
match field_type {
461+
FieldType::RichText => RichTextTypeOption.into(),
462+
FieldType::Number => NumberTypeOption::default().into(),
463+
FieldType::DateTime => DateTypeOption::default().into(),
464+
FieldType::LastEditedTime | FieldType::CreatedTime => TimestampTypeOption {
465+
field_type: field_type.into(),
466+
..Default::default()
467+
}
468+
.into(),
469+
FieldType::SingleSelect => SingleSelectTypeOption::default().into(),
470+
FieldType::MultiSelect => MultiSelectTypeOption::default().into(),
471+
FieldType::Checkbox => CheckboxTypeOption.into(),
472+
FieldType::URL => URLTypeOption::default().into(),
473+
FieldType::Time => TimeTypeOption.into(),
474+
FieldType::Media => MediaTypeOption::default().into(),
475+
FieldType::Checklist => ChecklistTypeOption.into(),
476+
FieldType::Relation => RelationTypeOption::default().into(),
477+
FieldType::Summary => SummarizationTypeOption::default().into(),
478+
FieldType::Translate => TranslateTypeOption::default().into(),
479+
}
480+
}
481+
326482
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
327483
#[repr(u8)]
328484
pub enum FileUploadType {

collab-database/src/fields/field.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use serde::{Deserialize, Serialize};
22

33
use collab::preclude::{Any, Map, MapExt, MapRef, ReadTxn, TransactionMut, YrsValue};
44

5+
use crate::database::gen_field_id;
6+
use crate::entity::{default_type_option_data_from_type, FieldType};
57
use crate::fields::{TypeOptionData, TypeOptions, TypeOptionsUpdate};
68
use crate::{impl_bool_update, impl_i64_update, impl_str_update};
79

@@ -37,6 +39,17 @@ impl Field {
3739
self
3840
}
3941

42+
pub fn from_field_type(name: &str, field_type: FieldType, is_primary: bool) -> Self {
43+
let new_field = Self {
44+
id: gen_field_id(),
45+
name: name.to_string(),
46+
field_type: field_type.into(),
47+
is_primary,
48+
..Default::default()
49+
};
50+
new_field.with_type_option_data(field_type, default_type_option_data_from_type(field_type))
51+
}
52+
4053
pub fn get_type_option<T: From<TypeOptionData>>(&self, type_id: impl ToString) -> Option<T> {
4154
let type_option_data = self.type_options.get(&type_id.to_string())?.clone();
4255
Some(T::from(type_option_data))

0 commit comments

Comments
 (0)