Skip to content

Commit 4e7ff46

Browse files
CatHood0CatHood0
andauthored
Fix: Block Attributes are not displayed if the editor is empty (singerdmx#2210)
* Fix: list blocks is not showed if the editor is empty * Fix: placeholder line is being checked by the SpellCheckerService * Chore: dart format * Improved comments * Chore: another dart format --------- Co-authored-by: CatHood0 <[email protected]>
1 parent eb548c6 commit 4e7ff46

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

lib/src/delta/delta_diff.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ Diff getDiff(String oldText, String newText, int cursorPosition) {
3939
end > limit && oldText[end - 1] == newText[end + delta - 1];
4040
end--) {}
4141
var start = 0;
42+
//TODO: we need to improve this part because this loop has a lot of unsafe index operations
4243
for (final startLimit = cursorPosition - math.max(0, delta);
43-
start < startLimit && oldText[start] == newText[start];
44+
start < startLimit &&
45+
(start > oldText.length - 1 ? '' : oldText[start]) ==
46+
(start > newText.length - 1 ? '' : newText[start]);
4447
start++) {}
4548
final deleted = (start >= end) ? '' : oldText.substring(start, end);
46-
final inserted = newText.substring(start, end + delta);
49+
// we need to make the check if the start is major than the end because if we directly get the
50+
// new inserted text without checking first, this will always throw an error since this is an unsafe op
51+
final inserted =
52+
(start >= end + delta) ? '' : newText.substring(start, end + delta);
4753
return Diff(
4854
start: start,
4955
deleted: deleted,

lib/src/editor/raw_editor/raw_editor_state.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'dart:async' show StreamSubscription;
2-
import 'dart:convert' show jsonDecode;
2+
import 'dart:convert' show jsonDecode, jsonEncode;
33
import 'dart:math' as math;
44
import 'dart:ui' as ui hide TextStyle;
55

@@ -404,9 +404,22 @@ class QuillRawEditorState extends EditorState
404404
var doc = controller.document;
405405
if (doc.isEmpty() && widget.configurations.placeholder != null) {
406406
final raw = widget.configurations.placeholder?.replaceAll(r'"', '\\"');
407+
// get current block attributes applied to the first line even if it
408+
// is empty
409+
final blockAttributesWithoutContent =
410+
doc.root.children.firstOrNull?.toDelta().first.attributes;
411+
// check if it has code block attribute to add '//' to give to the users
412+
// the feeling of this is really a block of code
413+
final isCodeBlock =
414+
blockAttributesWithoutContent?.containsKey('code-block') ?? false;
415+
// we add the block attributes at the same time as the placeholder to allow the editor to display them without removing
416+
// the placeholder (this is really awkward when everything is empty)
417+
final blockAttrInsertion = blockAttributesWithoutContent == null
418+
? ''
419+
: ',{"insert":"\\n","attributes":${jsonEncode(blockAttributesWithoutContent)}}';
407420
doc = Document.fromJson(
408421
jsonDecode(
409-
'[{"attributes":{"placeholder":true},"insert":"$raw\\n"}]',
422+
'[{"attributes":{"placeholder":true},"insert":"${isCodeBlock ? '// ' : ''}$raw${blockAttrInsertion.isEmpty ? '\\n' : ''}"}$blockAttrInsertion]',
410423
),
411424
);
412425
}

lib/src/editor/widgets/text/text_line.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ class _TextLineState extends State<TextLine> {
130130
super.dispose();
131131
}
132132

133+
/// Check if this line contains the placeholder attribute
134+
bool get isPlaceholderLine =>
135+
widget.line.toDelta().first.attributes?.containsKey('placeholder') ??
136+
false;
137+
133138
@override
134139
Widget build(BuildContext context) {
135140
assert(debugCheckHasMediaQuery(context));
@@ -326,6 +331,16 @@ class _TextLineState extends State<TextLine> {
326331

327332
textStyle = _applyCustomAttributes(textStyle, widget.line.style.attributes);
328333

334+
if (isPlaceholderLine) {
335+
final oldStyle = textStyle;
336+
textStyle = defaultStyles.placeHolder!.style;
337+
textStyle = textStyle.merge(oldStyle.copyWith(
338+
color: textStyle.color,
339+
backgroundColor: textStyle.backgroundColor,
340+
background: textStyle.background,
341+
));
342+
}
343+
329344
return textStyle;
330345
}
331346

@@ -408,7 +423,8 @@ class _TextLineState extends State<TextLine> {
408423
!widget.readOnly &&
409424
!widget.line.style.attributes.containsKey('code-block') &&
410425
!widget.line.style.attributes.containsKey('placeholder') &&
411-
!kIsWeb) {
426+
!kIsWeb &&
427+
!isPlaceholderLine) {
412428
final service = SpellCheckerServiceProvider.instance;
413429
final spellcheckedSpans = service.checkSpelling(textNode.value);
414430
if (spellcheckedSpans != null && spellcheckedSpans.isNotEmpty) {

0 commit comments

Comments
 (0)