pull/1719/head
Mike Allen 1 year ago committed by X Code
parent ca06eebbd7
commit c1e5d91004
  1. 25
      lib/src/models/documents/document.dart
  2. 2
      lib/src/widgets/quill/quill_controller.dart
  3. 20
      lib/src/widgets/raw_editor/raw_editor_state.dart

@ -114,14 +114,32 @@ class Document {
/// Returns an instance of [Delta] actually composed into this document. /// Returns an instance of [Delta] actually composed into this document.
Delta replace(int index, int len, Object? data) { Delta replace(int index, int len, Object? data) {
assert(index >= 0); assert(index >= 0);
assert(data is String || data is Embeddable); assert(data is String || data is Embeddable || data is Delta);
var delta = Delta();
if (data is Delta) {
// move to insertion point and add the inserted content
if (index > 0) {
delta.retain(index);
}
// remove any text we are replacing
if (len > 0) {
delta.delete(len);
}
// add the pasted content
for (final op in data.operations) {
delta.push(op);
}
compose(delta, ChangeSource.local);
} else {
final dataIsNotEmpty = (data is String) ? data.isNotEmpty : true; final dataIsNotEmpty = (data is String) ? data.isNotEmpty : true;
assert(dataIsNotEmpty || len > 0); assert(dataIsNotEmpty || len > 0);
var delta = Delta();
// We have to insert before applying delete rules // We have to insert before applying delete rules
// Otherwise delete would be operating on stale document snapshot. // Otherwise delete would be operating on stale document snapshot.
if (dataIsNotEmpty) { if (dataIsNotEmpty) {
@ -132,6 +150,7 @@ class Document {
final deleteDelta = delete(index, len); final deleteDelta = delete(index, len);
delta = delta.compose(deleteDelta); delta = delta.compose(deleteDelta);
} }
}
return delta; return delta;
} }

@ -291,7 +291,7 @@ class QuillController extends ChangeNotifier {
bool ignoreFocus = false, bool ignoreFocus = false,
bool shouldNotifyListeners = true, bool shouldNotifyListeners = true,
}) { }) {
assert(data is String || data is Embeddable); assert(data is String || data is Embeddable || data is Delta);
if (onReplaceText != null && !onReplaceText!(index, len, data)) { if (onReplaceText != null && !onReplaceText!(index, len, data)) {
return; return;

@ -221,22 +221,12 @@ class QuillRawEditorState extends EditorState
final htmlBody = html_parser.parse(html).body?.outerHtml; final htmlBody = html_parser.parse(html).body?.outerHtml;
final deltaFromClipboard = Document.fromHtml(htmlBody ?? html); final deltaFromClipboard = Document.fromHtml(htmlBody ?? html);
var newDelta = Delta(); controller.replaceText(
newDelta = newDelta.compose(deltaFromClipboard); textEditingValue.selection.start,
if (!controller.document.isEmpty()) { textEditingValue.selection.end - textEditingValue.selection.start,
newDelta = newDelta.compose(controller.document.toDelta()); deltaFromClipboard,
}
controller
..setContents(
newDelta,
)
..updateSelection(
TextSelection.collapsed( TextSelection.collapsed(
offset: controller.document.length, offset: textEditingValue.selection.end));
),
ChangeSource.local,
);
bringIntoView(textEditingValue.selection.extent); bringIntoView(textEditingValue.selection.extent);

Loading…
Cancel
Save