fluffychat/lib/pangea/choreographer/text_editing/pangea_text_controller.dart
wcjord dec473d579
Writing assistance (#5598)
* feat: wa working full stack

* feat: writing assistance made anew

* docs: migrate copilot docs to .github/instructions/ format

- Create choreographer.instructions.md (applyTo: lib/pangea/choreographer/**)
- Create events-and-tokens.instructions.md (applyTo: lib/pangea/events/**,lib/pangea/extensions/**)
- Create modules.instructions.md (applyTo: lib/pangea/**) — full module map
- Track copilot-instructions.md (remove .gitignore rule)
- Add documentation reference table to copilot-instructions.md

Content sourced from docs/copilot/ on writing-assistance branch.

* docs: remove old docs/copilot/ (migrated to .github/instructions/)

* docs: update choreographer + modules docs for writing-assistance audit

- Mark IT (Interactive Translation) as deprecated throughout
- Document new ReplacementTypeEnum taxonomy (grammar, surface, word-choice categories)
- Add AssistanceStateEnum, AutocorrectPopup, feedback rerun flow
- Mark SpanDataRepo/span_details as dead code
- Mark SpanChoiceTypeEnum.bestCorrection/bestAnswer as deprecated
- Add new files to modules listing (autocorrect_popup, start_igc_button, etc.)
- Update API endpoints table with active/deprecated/dead status

* formatting, replace deprecated withOpacity calls

* fix linter issues from deprecated types

* use better error color

* move cloing of overlays into choreographer

* reduce duplicate code on igc_controller, update UI on feedback

* couple of adjustments

* display prompt in span card by type

* fix error in tests

* translations

* simplify span card feedback

---------

Co-authored-by: ggurdin <ggurdin@gmail.com>
2026-02-09 15:55:18 -05:00

225 lines
6.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/pangea/choreographer/choreo_constants.dart';
import 'package:fluffychat/pangea/choreographer/igc/autocorrect_span.dart';
import 'package:fluffychat/pangea/choreographer/igc/pangea_match_model.dart';
import 'package:fluffychat/pangea/choreographer/igc/pangea_match_state_model.dart';
import 'package:fluffychat/pangea/choreographer/igc/pangea_match_status_enum.dart';
import 'package:fluffychat/pangea/choreographer/igc/replacement_type_enum.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:fluffychat/pangea/subscription/controllers/subscription_controller.dart';
import 'package:fluffychat/widgets/matrix.dart';
import '../choreographer.dart';
import 'edit_type_enum.dart';
class PangeaTextController extends TextEditingController {
final Choreographer choreographer;
EditTypeEnum editType = EditTypeEnum.keyboard;
String _currentText = '';
PangeaTextController({
required this.choreographer,
}) {
addListener(_onTextChanged);
}
bool get exceededMaxLength => text.length >= ChoreoConstants.maxLength;
TextStyle _underlineStyle(Color color) => TextStyle(
decoration: TextDecoration.underline,
decorationColor: color,
decorationThickness: 5,
);
Color _underlineColor(PangeaMatch match) {
// Automatic corrections use primary color
if (match.status == PangeaMatchStatusEnum.automatic) {
return AppConfig.primaryColor.withAlpha(180);
}
// Use type-based coloring
return match.match.type.underlineColor();
}
TextStyle _textStyle(
PangeaMatch match,
TextStyle? existingStyle,
bool isOpenMatch,
) {
double opacityFactor = 1.0;
if (!isOpenMatch) {
opacityFactor = 0.2;
}
final alpha = (255 * opacityFactor).round();
final style = _underlineStyle(_underlineColor(match).withAlpha(alpha));
return existingStyle?.merge(style) ?? style;
}
void setSystemText(String newText, EditTypeEnum type) {
editType = type;
text = newText;
}
void _onTextChanged() {
final diff = text.characters.length - _currentText.characters.length;
if (diff > 1 && editType == EditTypeEnum.keyboard) {
final pastedText = text.characters
.skip(_currentText.characters.length)
.take(diff)
.join();
choreographer.onPaste(pastedText);
}
_currentText = text;
}
void _onUndo(PangeaMatchState match) {
try {
choreographer.igcController.updateMatch(
match,
PangeaMatchStatusEnum.undo,
);
} catch (e, s) {
ErrorHandler.logError(
e: e,
s: s,
level: SentryLevel.warning,
data: {
"match": match.toJson(),
},
);
MatrixState.pAnyState.closeOverlay();
choreographer.clearMatches(e);
}
}
@override
TextSpan buildTextSpan({
required BuildContext context,
TextStyle? style,
required bool withComposing,
}) {
final subscription =
MatrixState.pangeaController.subscriptionController.subscriptionStatus;
if (subscription == SubscriptionStatus.shouldShowPaywall) {
return _buildPaywallSpan(style);
}
if (choreographer.igcController.currentText == null) {
return TextSpan(text: text, style: style);
}
final parts = text.split(choreographer.igcController.currentText!);
if (parts.length != 2) {
return TextSpan(text: text, style: style);
}
return TextSpan(
style: style,
children: [
..._buildTokenSpan(defaultStyle: style),
TextSpan(text: parts[1], style: style),
],
);
}
TextSpan _buildPaywallSpan(TextStyle? style) => TextSpan(
text: text,
style: style?.merge(
_underlineStyle(const Color.fromARGB(187, 132, 96, 224)),
),
);
InlineSpan _buildMatchSpan(
PangeaMatchState match,
TextStyle style,
) {
final span = choreographer.igcController.currentText!.characters
.getRange(
match.updatedMatch.match.offset,
match.updatedMatch.match.offset + match.updatedMatch.match.length,
)
.toString();
if (match.updatedMatch.status == PangeaMatchStatusEnum.automatic) {
final originalText = match.originalMatch.match.fullText.characters
.getRange(
match.originalMatch.match.offset,
match.originalMatch.match.offset + match.originalMatch.match.length,
)
.toString();
return AutocorrectSpan(
transformTargetId:
"autocorrection_${match.updatedMatch.match.offset}_${match.updatedMatch.match.length}",
currentText: span,
originalText: originalText,
onUndo: () => _onUndo(match),
style: style,
);
} else {
return TextSpan(
text: span,
style: style,
);
}
}
/// Returns a list of [TextSpan]s used to display the text in the input field
/// with the appropriate styling for each error match.
List<InlineSpan> _buildTokenSpan({
TextStyle? defaultStyle,
}) {
final textSpanMatches = [
...choreographer.igcController.openMatches,
...choreographer.igcController.recentAutomaticCorrections,
]..sort(
(a, b) =>
a.updatedMatch.match.offset.compareTo(b.updatedMatch.match.offset),
);
final currentText = choreographer.igcController.currentText!;
final spans = <InlineSpan>[];
int cursor = 0;
for (final match in textSpanMatches) {
if (cursor < match.updatedMatch.match.offset) {
final text = currentText.characters
.getRange(cursor, match.updatedMatch.match.offset)
.toString();
spans.add(TextSpan(text: text, style: defaultStyle));
}
final openMatch =
choreographer.igcController.currentlyOpenMatch?.updatedMatch.match;
final style = _textStyle(
match.updatedMatch,
defaultStyle,
openMatch != null &&
openMatch.offset == match.updatedMatch.match.offset &&
openMatch.length == match.updatedMatch.match.length,
);
spans.add(_buildMatchSpan(match, style));
cursor =
match.updatedMatch.match.offset + match.updatedMatch.match.length;
}
if (cursor < currentText.characters.length) {
spans.add(
TextSpan(
text: currentText.characters
.getRange(cursor, currentText.characters.length)
.toString(),
style: defaultStyle,
),
);
}
return spans;
}
}