-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: moved table plugin to appflowy-editor
- Loading branch information
Showing
23 changed files
with
2,157 additions
and
70 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,186 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:appflowy_editor/src/render/table/util.dart'; | ||
|
||
addCol(Node tableNode, Transaction transaction) { | ||
List<Node> cellNodes = []; | ||
final int rowsLen = tableNode.attributes['rowsLen'], | ||
colsLen = tableNode.attributes['colsLen']; | ||
|
||
var lastCellNode = getCellNode(tableNode, colsLen - 1, rowsLen - 1)!; | ||
for (var i = 0; i < rowsLen; i++) { | ||
final node = Node( | ||
type: kTableCellType, | ||
attributes: { | ||
'position': {'col': colsLen, 'row': i} | ||
}, | ||
); | ||
node.insert(TextNode.empty()); | ||
|
||
cellNodes.add(newCellNode(tableNode, node)); | ||
} | ||
|
||
// TODO(zoli): this calls notifyListener rowsLen+1 times. isn't there a better | ||
// way? | ||
transaction.insertNodes(lastCellNode.path.next, cellNodes); | ||
transaction.updateNode(tableNode, {'colsLen': colsLen + 1}); | ||
} | ||
|
||
addRow(Node tableNode, Transaction transaction) { | ||
final int rowsLen = tableNode.attributes['rowsLen'], | ||
colsLen = tableNode.attributes['colsLen']; | ||
for (var i = 0; i < colsLen; i++) { | ||
final node = Node( | ||
type: kTableCellType, | ||
attributes: { | ||
'position': {'col': i, 'row': rowsLen} | ||
}, | ||
); | ||
node.insert(TextNode.empty()); | ||
|
||
transaction.insertNode(getCellNode(tableNode, i, rowsLen - 1)!.path.next, | ||
newCellNode(tableNode, node)); | ||
} | ||
transaction.updateNode(tableNode, {'rowsLen': rowsLen + 1}); | ||
} | ||
|
||
removeCol(Node tableNode, int col, Transaction transaction) { | ||
final int rowsLen = tableNode.attributes['rowsLen'], | ||
colsLen = tableNode.attributes['colsLen']; | ||
List<Node> nodes = []; | ||
for (var i = 0; i < rowsLen; i++) { | ||
nodes.add(getCellNode(tableNode, col, i)!); | ||
} | ||
transaction.deleteNodes(nodes); | ||
|
||
for (var i = col + 1; i < colsLen; i++) { | ||
for (var j = 0; j < rowsLen; j++) { | ||
transaction.updateNode(getCellNode(tableNode, i, j)!, { | ||
'position': {'col': i - 1, 'row': j} | ||
}); | ||
} | ||
} | ||
|
||
transaction.updateNode(tableNode, {'colsLen': colsLen - 1}); | ||
} | ||
|
||
removeRow(Node tableNode, int row, Transaction transaction) { | ||
final int rowsLen = tableNode.attributes['rowsLen'], | ||
colsLen = tableNode.attributes['colsLen']; | ||
List<Node> nodes = []; | ||
for (var i = 0; i < colsLen; i++) { | ||
nodes.add(getCellNode(tableNode, i, row)!); | ||
} | ||
transaction.deleteNodes(nodes); | ||
|
||
for (var i = row + 1; i < rowsLen; i++) { | ||
for (var j = 0; j < colsLen; j++) { | ||
transaction.updateNode(getCellNode(tableNode, j, i)!, { | ||
'position': {'col': j, 'row': i - 1} | ||
}); | ||
} | ||
} | ||
|
||
transaction.updateNode(tableNode, {'rowsLen': rowsLen - 1}); | ||
} | ||
|
||
duplicateCol(Node tableNode, int col, Transaction transaction) { | ||
final int rowsLen = tableNode.attributes['rowsLen'], | ||
colsLen = tableNode.attributes['colsLen']; | ||
List<Node> nodes = []; | ||
for (var i = 0; i < rowsLen; i++) { | ||
final node = getCellNode(tableNode, col, i)!; | ||
nodes.add(node.copyWith(attributes: { | ||
'position': {'col': col + 1, 'row': i} | ||
})); | ||
} | ||
transaction.insertNodes( | ||
getCellNode(tableNode, col, rowsLen - 1)!.path.next, nodes); | ||
|
||
for (var i = col + 1; i < colsLen; i++) { | ||
for (var j = 0; j < rowsLen; j++) { | ||
transaction.updateNode(getCellNode(tableNode, i, j)!, { | ||
'position': {'col': i + 1, 'row': j} | ||
}); | ||
} | ||
} | ||
|
||
transaction.updateNode(tableNode, {'colsLen': colsLen + 1}); | ||
} | ||
|
||
duplicateRow(Node tableNode, int row, Transaction transaction) { | ||
final int rowsLen = tableNode.attributes['rowsLen'], | ||
colsLen = tableNode.attributes['colsLen']; | ||
for (var i = 0; i < colsLen; i++) { | ||
final node = getCellNode(tableNode, i, row)!; | ||
transaction.insertNode( | ||
node.path.next, | ||
node.copyWith(attributes: { | ||
'position': {'row': row + 1, 'col': i} | ||
}), | ||
); | ||
} | ||
|
||
for (var i = row + 1; i < rowsLen; i++) { | ||
for (var j = 0; j < colsLen; j++) { | ||
transaction.updateNode(getCellNode(tableNode, j, i)!, { | ||
'position': {'col': j, 'row': i + 1} | ||
}); | ||
} | ||
} | ||
|
||
transaction.updateNode(tableNode, {'rowsLen': rowsLen + 1}); | ||
} | ||
|
||
setColBgColor(Node tableNode, int col, Transaction transaction, String? color) { | ||
final rowslen = tableNode.attributes['rowsLen']; | ||
for (var i = 0; i < rowslen; i++) { | ||
final node = getCellNode(tableNode, col, i)!; | ||
transaction.updateNode( | ||
node, | ||
{'backgroundColor': color}, | ||
); | ||
} | ||
} | ||
|
||
setRowBgColor(Node tableNode, int row, Transaction transaction, String? color) { | ||
final colsLen = tableNode.attributes['colsLen']; | ||
for (var i = 0; i < colsLen; i++) { | ||
final node = getCellNode(tableNode, i, row)!; | ||
transaction.updateNode( | ||
node, | ||
{'backgroundColor': color}, | ||
); | ||
} | ||
} | ||
|
||
newCellNode(Node tableNode, n) { | ||
final row = n.attributes['position']['row'] as int; | ||
final col = n.attributes['position']['col'] as int; | ||
final int rowsLen = tableNode.attributes['rowsLen']; | ||
final int colsLen = tableNode.attributes['colsLen']; | ||
|
||
if (!n.attributes.containsKey('height')) { | ||
double nodeHeight = double.tryParse( | ||
tableNode.attributes['config']['rowDefaultHeight'].toString())!; | ||
if (row < rowsLen) { | ||
nodeHeight = double.tryParse(getCellNode(tableNode, 0, row)! | ||
.attributes['height'] | ||
.toString()) ?? | ||
nodeHeight; | ||
} | ||
n.updateAttributes({'height': nodeHeight}); | ||
} | ||
|
||
if (!n.attributes.containsKey('width')) { | ||
double nodeWidth = double.tryParse( | ||
tableNode.attributes['config']['colDefaultWidth'].toString())!; | ||
if (col < colsLen) { | ||
nodeWidth = double.tryParse( | ||
getCellNode(tableNode, col, 0)!.attributes['width'].toString()) ?? | ||
nodeWidth; | ||
} | ||
n.updateAttributes({'width': nodeWidth}); | ||
} | ||
|
||
return n; | ||
} |
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,43 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
|
||
class TableActionButton extends StatefulWidget { | ||
const TableActionButton({ | ||
Key? key, | ||
required this.width, | ||
required this.height, | ||
required this.padding, | ||
required this.onPressed, | ||
}) : super(key: key); | ||
|
||
final double width, height; | ||
final EdgeInsetsGeometry padding; | ||
final Function onPressed; | ||
|
||
@override | ||
State<TableActionButton> createState() => _TableActionButtonState(); | ||
} | ||
|
||
class _TableActionButtonState extends State<TableActionButton> { | ||
bool _visible = false; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
padding: widget.padding, | ||
width: widget.width, | ||
height: widget.height, | ||
child: MouseRegion( | ||
onEnter: (_) => setState(() => _visible = true), | ||
onExit: (_) => setState(() => _visible = false), | ||
child: Center( | ||
child: Visibility( | ||
visible: _visible, | ||
child: ActionMenuWidget(items: [ | ||
ActionMenuItem.icon( | ||
iconData: Icons.add, onPressed: () => widget.onPressed()), | ||
]), | ||
), | ||
))); | ||
} | ||
} |
Oops, something went wrong.