Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style recognition fixes #1416

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions lib/src/models/documents/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,28 @@ class Document {
/// included in the result.
Style collectStyle(int index, int len) {
final res = queryChild(index);
// -1 because the cursor is at the part of the line that is not visible
// Bug: When the caret is in the middle of the paragraph
// and at the end of the format string, it will display the wrong state
// of the format button
final isLinkStyle =
res.node?.style.attributes[Attribute.link.key]?.value == true;
// In this case, we have an exception, this is a link.
// When node is a link we will not -1
return (res.node as Line).collectStyle(
len == 0 && res.node != null && !isLinkStyle
? res.offset - 1
: res.offset,
len);
Style rangeStyle;
if (len > 0) {
return (res.node as Line).collectStyle(res.offset, len);
}
if (res.offset == 0) {
rangeStyle = (res.node as Line).collectStyle(res.offset, len);
return rangeStyle.removeAll({
for (final attr in rangeStyle.values)
if (attr.isInline) attr
});
}
rangeStyle = (res.node as Line).collectStyle(res.offset - 1, len);
final linkAttribute = rangeStyle.attributes[Attribute.link.key];
if ((linkAttribute != null) &&
(linkAttribute.value !=
(res.node as Line)
.collectStyle(res.offset, len)
.attributes[Attribute.link.key]
?.value)) {
return rangeStyle.removeAll({linkAttribute});
}
return rangeStyle;
}

/// Returns all styles and Embed for each node within selection
Expand Down
13 changes: 4 additions & 9 deletions lib/src/models/documents/nodes/line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,13 @@ class Line extends Container<Leaf?> {
final excluded = <Attribute>{};

void _handle(Style style) {
if (result.isEmpty) {
excluded.addAll(style.values);
} else {
for (final attr in result.values) {
if (!style.containsKey(attr.key)) {
excluded.add(attr);
}
for (final attr in result.values) {
if (!style.containsKey(attr.key) ||
(style.attributes[attr.key] != attr.value)) {
excluded.add(attr);
}
}
final remaining = style.removeAll(excluded);
result = result.removeAll(excluded);
result = result.mergeAll(remaining);
}

final data = queryChild(offset, true);
Expand Down