diff --git a/lib/src/editor/command/text_commands.dart b/lib/src/editor/command/text_commands.dart index da05e17e6..8b3b77cb7 100644 --- a/lib/src/editor/command/text_commands.dart +++ b/lib/src/editor/command/text_commands.dart @@ -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 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. diff --git a/lib/src/editor/toolbar/desktop/items/text_direction_toolbar_items.dart b/lib/src/editor/toolbar/desktop/items/text_direction_toolbar_items.dart index 67317fd95..7017567be 100644 --- a/lib/src/editor/toolbar/desktop/items/text_direction_toolbar_items.dart +++ b/lib/src/editor/toolbar/desktop/items/text_direction_toolbar_items.dart @@ -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: {