Skip to content

Commit b8beb55

Browse files
authored
fix(cat-voices): prevent reseting cursor position in rich text editor (#2590)
* fix: don't trim markdown * chore: cleanup * fix: prevent cursor reset
1 parent 30e4086 commit b8beb55

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

catalyst_voices/apps/voices/lib/common/codecs/markdown_codec.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import 'package:markdown_quill/markdown_quill.dart';
1414
// models/shared package
1515
const markdown = MarkdownCodec();
1616

17-
final _mdDocument = md.Document();
18-
final _mdToDelta = MarkdownToDelta(markdownDocument: _mdDocument);
1917
final _deltaToMd = DeltaToMarkdown(
2018
customContentHandler: DeltaToMarkdown.escapeSpecialCharactersRelaxed,
2119
);
20+
final _mdDocument = md.Document();
21+
final _mdToDelta = MarkdownToDelta(markdownDocument: _mdDocument);
2222

2323
final class MarkdownCodec extends Codec<MarkdownData, Delta> {
2424
const MarkdownCodec();

catalyst_voices/apps/voices/lib/widgets/document_builder/value/multiline_text_entry_markdown_widget.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ class _MultilineTextEntryMarkdownWidgetState extends State<MultilineTextEntryMar
7070
),
7171
VoicesRichText(
7272
controller: _controller,
73-
enabled: widget.isEditMode,
7473
focusNode: _focus,
7574
scrollController: _scrollController,
75+
enabled: widget.isEditMode,
7676
minHeight: 160,
7777
charsLimit: _maxLength,
7878
onChanged: _onChanged,
@@ -144,8 +144,10 @@ class _MultilineTextEntryMarkdownWidgetState extends State<MultilineTextEntryMar
144144
widget.onChanged([change]);
145145
}
146146

147-
void _onChanged(MarkdownData? markdownData) {
148-
_onChangedDebouncer.run(() => _dispatchChange(markdownData));
147+
void _onChanged(MarkdownData? _) {
148+
// Get markdown data from the controller, the method argument might
149+
// already not be what latest controller holds due to debouncer delay.
150+
_onChangedDebouncer.run(() => _dispatchChange(_controller.markdownData));
149151
}
150152

151153
void _toggleEditMode() {

catalyst_voices/apps/voices/lib/widgets/rich_text/voices_rich_text.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,16 @@ final class VoicesRichTextController extends quill.QuillController {
136136
} else if (newMarkdownData.data.isEmpty) {
137137
clear();
138138
} else {
139+
// current selection
140+
final currentSelection = selection;
141+
142+
// update document (resets selection)
139143
final delta = markdown.encoder.convert(newMarkdownData);
140-
document = quill.Document.fromDelta(delta);
144+
final newDocument = quill.Document.fromDelta(delta);
145+
document = newDocument;
146+
147+
// revert selection
148+
updateSelection(currentSelection, quill.ChangeSource.local);
141149
}
142150
}
143151

0 commit comments

Comments
 (0)