Skip to content

Commit 7071db6

Browse files
authored
Merge pull request #519 from AppFlowy-IO/feat/grid_column_drag
chore: enable drag to expand field's width
2 parents 4e3d267 + 3d41cb0 commit 7071db6

File tree

6 files changed

+53
-45
lines changed

6 files changed

+53
-45
lines changed

frontend/app_flowy/lib/workspace/application/grid/field/field_cell_bloc.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ class FieldCellBloc extends Bloc<FieldCellEvent, FieldCellState> {
2424
_startListening();
2525
},
2626
didReceiveFieldUpdate: (field) {
27-
emit(state.copyWith(field: field));
27+
emit(state.copyWith(field: cellContext.field));
2828
},
29-
updateWidth: (offset) {
30-
final defaultWidth = state.field.width.toDouble();
31-
final width = defaultWidth + offset;
32-
if (width > defaultWidth && width < 300) {
33-
_fieldService.updateField(width: width);
29+
startUpdateWidth: (offset) {
30+
final width = state.width + offset;
31+
emit(state.copyWith(width: width));
32+
},
33+
endUpdateWidth: () {
34+
if (state.width != state.field.width.toDouble()) {
35+
_fieldService.updateField(width: state.width);
3436
}
3537
},
3638
);
@@ -61,18 +63,21 @@ class FieldCellBloc extends Bloc<FieldCellEvent, FieldCellState> {
6163
class FieldCellEvent with _$FieldCellEvent {
6264
const factory FieldCellEvent.initial() = _InitialCell;
6365
const factory FieldCellEvent.didReceiveFieldUpdate(Field field) = _DidReceiveFieldUpdate;
64-
const factory FieldCellEvent.updateWidth(double offset) = _UpdateWidth;
66+
const factory FieldCellEvent.startUpdateWidth(double offset) = _StartUpdateWidth;
67+
const factory FieldCellEvent.endUpdateWidth() = _EndUpdateWidth;
6568
}
6669

6770
@freezed
6871
class FieldCellState with _$FieldCellState {
6972
const factory FieldCellState({
7073
required String gridId,
7174
required Field field,
75+
required double width,
7276
}) = _FieldCellState;
7377

7478
factory FieldCellState.initial(GridFieldCellContext cellContext) => FieldCellState(
7579
gridId: cellContext.gridId,
7680
field: cellContext.field,
81+
width: cellContext.field.width.toDouble(),
7782
);
7883
}

frontend/app_flowy/lib/workspace/application/grid/row/row_bloc.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RowBloc extends Bloc<RowEvent, RowState> {
3030
_rowService.createRow();
3131
},
3232
didReceiveCellDatas: (_DidReceiveCellDatas value) async {
33-
final fields = value.gridCellMap.values.map((e) => CellSnapshot(e.field)).toList();
33+
final fields = value.gridCellMap.values.map((e) => GridCellEquatable(e.field)).toList();
3434
final snapshots = UnmodifiableListView(fields);
3535
emit(state.copyWith(
3636
gridCellMap: value.gridCellMap,
@@ -74,26 +74,27 @@ class RowState with _$RowState {
7474
const factory RowState({
7575
required GridRow rowData,
7676
required GridCellMap gridCellMap,
77-
required UnmodifiableListView<CellSnapshot> snapshots,
77+
required UnmodifiableListView<GridCellEquatable> snapshots,
7878
GridRowChangeReason? changeReason,
7979
}) = _RowState;
8080

8181
factory RowState.initial(GridRow rowData, GridCellMap cellDataMap) => RowState(
8282
rowData: rowData,
8383
gridCellMap: cellDataMap,
84-
snapshots: UnmodifiableListView(cellDataMap.values.map((e) => CellSnapshot(e.field)).toList()),
84+
snapshots: UnmodifiableListView(cellDataMap.values.map((e) => GridCellEquatable(e.field)).toList()),
8585
);
8686
}
8787

88-
class CellSnapshot extends Equatable {
88+
class GridCellEquatable extends Equatable {
8989
final Field _field;
9090

91-
const CellSnapshot(Field field) : _field = field;
91+
const GridCellEquatable(Field field) : _field = field;
9292

9393
@override
9494
List<Object?> get props => [
9595
_field.id,
9696
_field.fieldType,
9797
_field.visibility,
98+
_field.width,
9899
];
99100
}

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/header/field_cell.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:flowy_infra/theme.dart';
66
import 'package:flowy_infra_ui/style_widget/button.dart';
77
import 'package:flowy_infra_ui/style_widget/hover.dart';
88
import 'package:flowy_infra_ui/style_widget/text.dart';
9-
import 'package:flowy_sdk/log.dart';
109
import 'package:flowy_sdk/protobuf/flowy-grid-data-model/grid.pb.dart' show Field;
1110
import 'package:flutter/material.dart';
1211
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -24,6 +23,7 @@ class GridFieldCell extends StatelessWidget {
2423
return BlocProvider(
2524
create: (context) => FieldCellBloc(cellContext: cellContext)..add(const FieldCellEvent.initial()),
2625
child: BlocBuilder<FieldCellBloc, FieldCellState>(
26+
// buildWhen: (p, c) => p.field != c.field,
2727
builder: (context, state) {
2828
final button = FieldCellButton(
2929
field: state.field,
@@ -38,7 +38,7 @@ class GridFieldCell extends StatelessWidget {
3838
);
3939

4040
return _GridHeaderCellContainer(
41-
width: state.field.width.toDouble(),
41+
width: state.width,
4242
child: Stack(
4343
alignment: Alignment.centerRight,
4444
fit: StackFit.expand,
@@ -60,13 +60,14 @@ class GridFieldCell extends StatelessWidget {
6060

6161
void _showFieldEditor(BuildContext context) {
6262
final state = context.read<FieldCellBloc>().state;
63+
final field = state.field;
6364

6465
FieldEditor(
6566
gridId: state.gridId,
66-
fieldName: state.field.name,
67+
fieldName: field.name,
6768
contextLoader: FieldContextLoader(
6869
gridId: state.gridId,
69-
field: state.field,
70+
field: field,
7071
),
7172
).show(context);
7273
}
@@ -84,7 +85,8 @@ class _GridHeaderCellContainer extends StatelessWidget {
8485
@override
8586
Widget build(BuildContext context) {
8687
final theme = context.watch<AppTheme>();
87-
final borderSide = BorderSide(color: theme.shader4, width: 0.4);
88+
final borderSide = BorderSide(color: theme.shader5, width: 1.0);
89+
8890
final decoration = BoxDecoration(
8991
border: Border(
9092
top: borderSide,
@@ -113,21 +115,19 @@ class _DragToExpandLine extends StatelessWidget {
113115
onTap: () {},
114116
child: GestureDetector(
115117
behavior: HitTestBehavior.opaque,
116-
onHorizontalDragCancel: () {},
117118
onHorizontalDragUpdate: (value) {
118-
// context.read<FieldCellBloc>().add(FieldCellEvent.updateWidth(value.delta.dx));
119-
Log.info(value);
119+
context.read<FieldCellBloc>().add(FieldCellEvent.startUpdateWidth(value.delta.dx));
120120
},
121121
onHorizontalDragEnd: (end) {
122-
Log.info(end);
122+
context.read<FieldCellBloc>().add(const FieldCellEvent.endUpdateWidth());
123123
},
124124
child: FlowyHover(
125125
style: HoverStyle(
126126
hoverColor: theme.main1,
127127
borderRadius: BorderRadius.zero,
128-
contentMargin: const EdgeInsets.only(left: 5),
128+
contentMargin: const EdgeInsets.only(left: 6),
129129
),
130-
child: const SizedBox(width: 2),
130+
child: const SizedBox(width: 4),
131131
),
132132
),
133133
);

frontend/app_flowy/packages/flowy_infra_ui/lib/style_widget/button.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'package:flowy_infra/size.dart';
21
import 'package:flowy_infra_ui/style_widget/hover.dart';
32
import 'package:flowy_infra_ui/style_widget/text.dart';
43
import 'package:flowy_infra_ui/widget/spacing.dart';
@@ -28,7 +27,7 @@ class FlowyButton extends StatelessWidget {
2827
return InkWell(
2928
onTap: onTap,
3029
child: FlowyHover(
31-
style: HoverStyle(borderRadius: Corners.s6Border, hoverColor: hoverColor),
30+
style: HoverStyle(borderRadius: BorderRadius.zero, hoverColor: hoverColor),
3231
setSelected: () => isSelected,
3332
builder: (context, onHover) => _render(),
3433
),

frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl TypeOptionBuilder for RichTextTypeOptionBuilder {
2727
#[derive(Debug, Clone, Default, Serialize, Deserialize, ProtoBuf)]
2828
pub struct RichTextTypeOption {
2929
#[pb(index = 1)]
30-
data: String, //It's not used.
30+
data: String, //It's not used yet
3131
}
3232
impl_type_option!(RichTextTypeOption, FieldType::RichText);
3333

frontend/rust-lib/flowy-grid/src/services/field/type_options/url_type_option.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl TypeOptionBuilder for URLTypeOptionBuilder {
3030
#[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf)]
3131
pub struct URLTypeOption {
3232
#[pb(index = 1)]
33-
data: String, //It's not used.
33+
data: String, //It's not used yet.
3434
}
3535
impl_type_option!(URLTypeOption, FieldType::URL);
3636

@@ -56,28 +56,31 @@ impl CellDataOperation<EncodedCellData<URLCellData>, String> for URLTypeOption {
5656
C: Into<CellContentChangeset>,
5757
{
5858
let changeset = changeset.into();
59-
let mut cell_data = URLCellData {
60-
url: "".to_string(),
59+
let mut url = "".to_string();
60+
if let Ok(Some(m)) = URL_REGEX.find(&changeset) {
61+
url = auto_append_scheme(m.as_str());
62+
}
63+
URLCellData {
64+
url,
6165
content: changeset.to_string(),
62-
};
66+
}
67+
.to_json()
68+
}
69+
}
6370

64-
if let Ok(Some(m)) = URL_REGEX.find(&changeset) {
65-
// Only support https scheme by now
66-
match url::Url::parse(m.as_str()) {
67-
Ok(url) => {
68-
if url.scheme() == "https" {
69-
cell_data.url = url.into();
70-
} else {
71-
cell_data.url = format!("https://{}", m.as_str());
72-
}
73-
}
74-
Err(_) => {
75-
cell_data.url = format!("https://{}", m.as_str());
76-
}
71+
fn auto_append_scheme(s: &str) -> String {
72+
// Only support https scheme by now
73+
match url::Url::parse(s) {
74+
Ok(url) => {
75+
if url.scheme() == "https" {
76+
url.into()
77+
} else {
78+
format!("https://{}", s)
7779
}
7880
}
79-
80-
cell_data.to_json()
81+
Err(_) => {
82+
format!("https://{}", s)
83+
}
8184
}
8285
}
8386

0 commit comments

Comments
 (0)