Copy image with its style

pull/635/head
X Code 3 years ago
parent 56d617cb64
commit 7fbda46988
  1. 5
      lib/src/models/documents/nodes/line.dart
  2. 3
      lib/src/utils/string.dart
  3. 9
      lib/src/widgets/controller.dart
  4. 25
      lib/src/widgets/embeds/default_embed_builder.dart
  5. 11
      lib/src/widgets/raw_editor.dart

@ -129,7 +129,10 @@ class Line extends Container<Leaf?> {
final isLineFormat = (index + local == thisLength) && local == 1;
if (isLineFormat) {
assert(style.values.every((attr) => attr.scope == AttributeScope.BLOCK),
assert(
style.values.every((attr) =>
attr.scope == AttributeScope.BLOCK ||
attr.scope == AttributeScope.IGNORE),
'It is not allowed to apply inline attributes to line itself.');
_format(style);
} else {

@ -17,8 +17,7 @@ Map<String, String> parseKeyValuePairs(String s, Set<String> targetKeys) {
return result;
}
String replaceStyleString(String? s, double width, double height) {
s ??= '';
String replaceStyleString(String s, double width, double height) {
final result = <String, String>{};
final pairs = s.split(';');
for (final pair in pairs) {

@ -334,12 +334,13 @@ class QuillController extends ChangeNotifier {
return document.querySegmentLeafNode(offset).item2;
}
/// Clipboard for image url
String? _copiedImageUrl;
/// Clipboard for image url and its corresponding style
/// item1 is url and item2 is style string
Tuple2<String, String>? _copiedImageUrl;
String? getCopiedImageUrl() => _copiedImageUrl;
Tuple2<String, String>? get copiedImageUrl => _copiedImageUrl;
set copiedImageUrl(String? value) {
set copiedImageUrl(Tuple2<String, String>? value) {
_copiedImageUrl = value;
Clipboard.setData(const ClipboardData(text: ''));
}

@ -69,21 +69,11 @@ Widget defaultEmbedBuilder(BuildContext context, QuillController controller,
context: context,
builder: (context) {
final _screenSize = MediaQuery.of(context).size;
final _style = controller
.getAllSelectionStyles()
.firstWhere(
(s) => s.attributes
.containsKey(Attribute.style.key),
orElse: () => Style());
return ImageResizer(
onImageResize: (w, h) {
final res = _getImageNode(controller);
final attr = replaceStyleString(
_style.attributes[Attribute.style.key]
?.value,
w,
h);
_getImageStyleString(controller), w, h);
controller.formatText(
res.item1, 1, StyleAttribute(attr));
},
@ -101,7 +91,8 @@ Widget defaultEmbedBuilder(BuildContext context, QuillController controller,
onPressed: () {
final imageNode = _getImageNode(controller).item2;
final imageUrl = imageNode.value.data;
controller.copiedImageUrl = imageUrl;
controller.copiedImageUrl =
Tuple2(imageUrl, _getImageStyleString(controller));
Navigator.pop(context);
},
);
@ -151,6 +142,16 @@ Widget defaultEmbedBuilder(BuildContext context, QuillController controller,
}
}
String _getImageStyleString(QuillController controller) {
final String? s = controller
.getAllSelectionStyles()
.firstWhere((s) => s.attributes.containsKey(Attribute.style.key),
orElse: () => Style())
.attributes[Attribute.style.key]
?.value;
return s ?? '';
}
Tuple2<int, leaf.Embed> _getImageNode(QuillController controller) {
var offset = controller.selection.start;
var imageNode = controller.queryNode(offset);

@ -920,11 +920,16 @@ class RawEditorState extends EditorState
@override
Future<void> pasteText(SelectionChangedCause cause) async {
if (widget.controller.getCopiedImageUrl() != null) {
if (widget.controller.copiedImageUrl != null) {
final index = textEditingValue.selection.baseOffset;
final length = textEditingValue.selection.extentOffset - index;
widget.controller.replaceText(index, length,
BlockEmbed.image(widget.controller.getCopiedImageUrl()!), null);
final copied = widget.controller.copiedImageUrl!;
widget.controller
.replaceText(index, length, BlockEmbed.image(copied.item1), null);
if (copied.item2.isNotEmpty) {
widget.controller
.formatText(index + 1, 1, StyleAttribute(copied.item2));
}
widget.controller.copiedImageUrl = null;
await Clipboard.setData(const ClipboardData(text: ''));
return;

Loading…
Cancel
Save