Skip to main content

Overview

EditorState is the core state management class for AppFlowy Editor. It holds the document, selection, and provides access to editor services like undo/redo, selection management, and rendering. All document mutations should be applied through transactions to support collaborative editing and undo/redo functionality.

Constructor

EditorState({
  required Document document,
  Duration minHistoryItemDuration = const Duration(milliseconds: 50),
  int? maxHistoryItemSize,
})

Named Constructors

// Create a blank editor with optional initial text
EditorState.blank({bool withInitialText = true})

Parameters

document
Document
required
The document to render and edit.
minHistoryItemDuration
Duration
default:"Duration(milliseconds: 50)"
Minimum duration before sealing a history item for undo/redo.
maxHistoryItemSize
int
default:"200"
Maximum number of history items to keep in the undo stack.

Properties

document
Document
The document being edited.
selection
Selection?
Current selection in the editor. Returns null when nothing is selected.
selectionNotifier
PropertyValueNotifier<Selection?>
Notifier for observing selection changes.
editable
bool
Whether the editor is in editable mode.
editableNotifier
ValueNotifier<bool>
Notifier for observing editable state changes.
selectionType
SelectionType?
Type of the current selection (inline or block).
selectionUpdateReason
SelectionUpdateReason
Reason for the last selection update (uiEvent, transaction, remote, etc.).
remoteSelections
PropertyValueNotifier<List<RemoteSelection>>
Selections from other users in collaborative editing.
transactionStream
Stream<EditorTransactionValue>
Stream of transaction events (before/after apply).
transaction
Transaction
Creates a new transaction for the current document.
undoManager
UndoManager
Manages undo/redo history.
service
EditorService
Access to editor services (selection, scroll, rendering).
scrollService
AppFlowyScrollService?
Scroll service for programmatic scrolling.
selectionService
AppFlowySelectionService
Selection service for managing text selection.
renderer
BlockComponentRendererService
Block component renderer service.
editorStyle
EditorStyle
Visual style configuration for the editor.
disableAutoScroll
bool
Disable automatic scrolling behavior.
autoScrollEdgeOffset
double
Edge offset for auto-scroll trigger.
toggledStyle
UnmodifiableMapView<String, dynamic>
Format styles toggled for the next character insertion (bold, italic, etc.).
documentRules
List<DocumentRule>
Rules applied to document transactions.

Methods

apply

Future<void> apply(
  Transaction transaction, {
  bool isRemote = false,
  ApplyOptions options = const ApplyOptions(recordUndo: true),
  bool withUpdateSelection = true,
  bool skipHistoryDebounce = false,
})
Applies a transaction to update the document.
transaction
Transaction
required
The transaction to apply.
isRemote
bool
default:"false"
Whether this transaction comes from a remote user.
options
ApplyOptions
default:"ApplyOptions(recordUndo: true)"
Options controlling undo/redo recording.
withUpdateSelection
bool
default:"true"
Whether to update selection after applying.
skipHistoryDebounce
bool
default:"false"
Skip the debounce and immediately seal the history item.

updateSelectionWithReason

Future<void> updateSelectionWithReason(
  Selection? selection, {
  SelectionUpdateReason reason = SelectionUpdateReason.transaction,
  Map? extraInfo,
  SelectionType? customSelectionType,
})
Updates the editor selection with a specific reason.

getNodesInSelection

List<Node> getNodesInSelection(Selection selection)
Returns all nodes within the given selection, ordered based on selection direction.

getSelectedNodes

List<Node> getSelectedNodes({
  Selection? selection,
  bool withCopy = true,
})
Returns the selected nodes, optionally as deep copies.

getNodeAtPath

Node? getNodeAtPath(Path path)
Returns the node at the specified path, or null if not found.

selectionRects

List<Rect> selectionRects()
Returns the rectangles of the current selection in global coordinates.

reload

void reload()
Force rebuilds the editor UI.

updateToggledStyle

void updateToggledStyle(String key, dynamic value)
Updates a toggled format style for the next character insertion.

dispose

void dispose()
Cleans up resources. Must be called when the editor state is no longer needed.

Example

// Create editor state
final editorState = EditorState.blank();

// Apply a transaction
final transaction = editorState.transaction;
transaction.insertText(
  editorState.document.first!,
  0,
  'Hello, World!',
);
await editorState.apply(transaction);

// Listen to selection changes
editorState.selectionNotifier.addListener(() {
  final selection = editorState.selection;
  print('Selection changed: $selection');
});

// Update selection
await editorState.updateSelectionWithReason(
  Selection.single(path: [0], startOffset: 0, endOffset: 5),
  reason: SelectionUpdateReason.uiEvent,
);

// Clean up
editorState.dispose();

See Also

Build docs developers (and LLMs) love