Skip to content

Commit

Permalink
feat: editor state update node (#405)
Browse files Browse the repository at this point in the history
* feat: default text direction property

Introduced default text direction property on editor style. which
accepts rtl|ltr|auto for controlling default fallback direction.
Before this we had something controlled by Directionality but it
had two problems:
1. It didn't accept auto text direction. We had to introduce our
own Directionality component.
2. Directionality also affects layout direction, for example
setting Directionality to RTL will cause the handlers and other
things move to right side of the screen. While this is good but
there are cases which we don't want to change whole layout direction
but only the text default direction to for example auto.

* fix: flutter analyze

* fix: exception on prev node text direction null

The code was throwing excenption on changing current node text
direction to auto when the previous node had text direction attribute
with null value.
Also changed to only use direct previous node text direction and
direct parent for inheriting text direction when the current node
text direction is auto and undeterminable (e.g. empty text).

* feat: editor state update node

Use update node when the purpose is only adding new attribute to
node. format node causes extra unnessesary delete operation.
  • Loading branch information
zoli authored Aug 29, 2023
1 parent e2fc87f commit cda17c7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions lib/src/editor/command/text_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,38 @@ extension TextTransforms on EditorState {
return apply(transaction);
}

/// update the node attributes at the given selection.
///
/// If the [Selection] is not passed in, use the current selection.
Future<void> updateNode(
Selection? selection,
Node Function(
Node node,
) nodeBuilder,
) async {
selection ??= this.selection;
selection = selection?.normalized;

if (selection == null) {
return;
}

final nodes = getNodesInSelection(selection);
if (nodes.isEmpty) {
return;
}

final transaction = this.transaction;

for (final node in nodes) {
transaction
..updateNode(node, nodeBuilder(node).attributes)
..afterSelection = transaction.beforeSelection;
}

return apply(transaction);
}

/// Insert text at the given index of the given [Node] or the [Path].
///
/// [Path] and [Node] are mutually exclusive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class _TextDirectionToolbarItem extends ToolbarItem {
isHighlight: isHighlight,
highlightColor: highlightColor,
tooltip: tooltip,
onPressed: () => editorState.formatNode(
onPressed: () => editorState.updateNode(
selection,
(node) => node.copyWith(
attributes: {
Expand Down

0 comments on commit cda17c7

Please sign in to comment.