copySelection method

Future<void> copySelection()

Copies the current selection to the system clipboard as HTML and Text.

Implementation

Future<void> copySelection() async {
  final selection = _editorWidgetState?.selection;
  final blockIndex = _editorWidgetState?.focusedBlockIndex;

  if (selection == null || selection.isCollapsed || blockIndex == null) {
    return;
  }

  final html = _documentController.getSelectedHtml(blockIndex, selection);
  final plainText =
      _documentController.getSelectedPlainText(blockIndex, selection);

  final item = DataWriterItem();
  if (html.isNotEmpty) {
    item.add(Formats.htmlText(html));
  }
  item.add(Formats.plainText(plainText));

  await SystemClipboard.instance?.write([item]);
  await _updatePasteState(); // Refresh canPaste state immediately
  onMessage?.call('Copied to clipboard');
}