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

Fix paste text mess up style and Add support copy/cut block text. #1321

Merged
merged 2 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [7.2.17]
- Fix paste text mess up style.
- Add support copy/cut block text.

# [7.2.16]
- Allow for custom context menu.

Expand Down
25 changes: 16 additions & 9 deletions lib/src/models/documents/nodes/line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -405,25 +405,32 @@ class Line extends Container<Leaf?> {
var node = data.node as Leaf?;
if (node != null) {
var pos = 0;
if (node is Text || node.value is Embeddable) {
pos = node.length - data.offset;
result.add(OffsetValue(
beg, node is Text ? node.style : node.value as Embeddable));
pos = node.length - data.offset;
if (node is Text && node.style.isNotEmpty) {
result.add(OffsetValue(beg, node.style, node.length));
} else if (node.value is Embeddable) {
result.add(OffsetValue(beg, node.value as Embeddable, node.length));
}
while (!node!.isLast && pos < local) {
node = node.next as Leaf;
if (node is Text || node.value is Embeddable) {
result.add(OffsetValue(
pos + beg, node is Text ? node.style : node.value as Embeddable));
pos += node.length;
if (node is Text && node.style.isNotEmpty) {
result.add(OffsetValue(pos + beg, node.style, node.length));
} else if (node.value is Embeddable) {
result.add(
OffsetValue(pos + beg, node.value as Embeddable, node.length));
}
pos += node.length;
}
}

if (style.isNotEmpty) {
result.add(OffsetValue(beg, style));
}

final remaining = len - local;
if (remaining > 0 && nextLine != null) {
final rest = nextLine!
.collectAllIndividualStylesAndEmbed(0, remaining, beg: local);
.collectAllIndividualStylesAndEmbed(0, remaining, beg: local + beg);
result.addAll(rest);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/src/models/documents/style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Style {

bool get isInline => isNotEmpty && values.every((item) => item.isInline);

bool get isBlock =>
isNotEmpty && values.every((item) => item.scope == AttributeScope.BLOCK);

bool get isIgnored =>
isNotEmpty && values.every((item) => item.scope == AttributeScope.IGNORE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math' as math;
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

import '../../../flutter_quill.dart';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use '../../../flutter_quill.dart'

import '../../models/documents/document.dart';
import '../../models/documents/nodes/embeddable.dart';
import '../../models/documents/nodes/leaf.dart';
Expand Down Expand Up @@ -51,12 +52,15 @@ mixin RawEditorStateSelectionDelegateMixin on EditorState
if (styleAndEmbed is Embeddable) {
widget.controller.replaceText(pos + offset, 0, styleAndEmbed, null);
} else {
widget.controller.formatTextStyle(
pos + offset,
i == pasteStyleAndEmbed.length - 1
? pastePlainText.length - offset
: pasteStyleAndEmbed[i + 1].offset,
styleAndEmbed);
final style = styleAndEmbed as Style;
if (style.isInline) {
widget.controller.formatTextStyle(
pos + offset, pasteStyleAndEmbed[i].length!, style);
} else if (style.isBlock) {
style.values.forEach((attribute) {
widget.controller.document.format(pos + offset, 0, attribute);
});
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_quill
description: A rich text editor supporting mobile and web (Demo App @ bulletjournal.us)
version: 7.2.16
version: 7.2.17
#author: bulletjournal
homepage: https://bulletjournal.us/home/index.html
repository: https://github.com/singerdmx/flutter-quill
Expand Down
Loading