popAllEditable method
Pop all editable commands and combine them with current input.
Notification modes (task-notification) are left in the queue. Returns null if no editable commands in queue.
Implementation
PopAllEditableResult? popAllEditable({
required String currentInput,
required int currentCursorOffset,
}) {
if (_commandQueue.isEmpty) return null;
final editable = <QueuedCommand>[];
final nonEditable = <QueuedCommand>[];
for (final cmd in _commandQueue) {
if (isQueuedCommandEditable(cmd)) {
editable.add(cmd);
} else {
nonEditable.add(cmd);
}
}
if (editable.isEmpty) return null;
// Extract text from queued commands
final queuedTexts = editable
.map((cmd) => _extractTextFromValue(cmd.value))
.toList();
final allParts = [
...queuedTexts,
currentInput,
].where((s) => s.isNotEmpty).toList();
final newInput = allParts.join('\n');
// Calculate cursor offset
final cursorOffset =
queuedTexts.join('\n').length + 1 + currentCursorOffset;
// Extract images from queued commands
final images = <PastedContent>[];
int nextImageId = DateTime.now().millisecondsSinceEpoch;
for (final cmd in editable) {
// Preserve original PastedContent from pastedContents map.
if (cmd.pastedContents != null) {
for (final content in cmd.pastedContents!.values) {
if (content.type == 'image') {
images.add(content);
}
}
}
final cmdImages = _extractImagesFromValue(cmd.value, nextImageId);
images.addAll(cmdImages);
nextImageId += cmdImages.length;
}
for (final command in editable) {
final content = command.value is StringCommandValue
? (command.value as StringCommandValue).value
: null;
_logOperation(QueueOperation.popAll, content);
}
// Replace queue with only the non-editable commands
_commandQueue
..clear()
..addAll(nonEditable);
_notifySubscribers();
return PopAllEditableResult(
text: newInput,
cursorOffset: cursorOffset,
images: images,
);
}