dartlangeditorflutterflutter-appsflutter-examplesflutter-packageflutter-widgetquillquill-deltaquilljsreactquillrich-textrich-text-editorwysiwygwysiwyg-editor
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.3 KiB
75 lines
2.3 KiB
4 years ago
|
import 'dart:math' as math;
|
||
|
|
||
|
import '../models/quill_delta.dart';
|
||
|
|
||
|
// Diff between two texts - old text and new text
|
||
|
class Diff {
|
||
|
Diff(this.start, this.deleted, this.inserted);
|
||
|
|
||
|
// Start index in old text at which changes begin.
|
||
|
final int start;
|
||
|
|
||
|
/// The deleted text
|
||
|
final String deleted;
|
||
|
|
||
|
// The inserted text
|
||
|
final String inserted;
|
||
|
|
||
|
@override
|
||
|
String toString() {
|
||
|
return 'Diff[$start, "$deleted", "$inserted"]';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* Get diff operation between old text and new text */
|
||
|
Diff getDiff(String oldText, String newText, int cursorPosition) {
|
||
|
var end = oldText.length;
|
||
|
final delta = newText.length - end;
|
||
|
for (final limit = math.max(0, cursorPosition - delta);
|
||
|
end > limit && oldText[end - 1] == newText[end + delta - 1];
|
||
|
end--) {}
|
||
|
var start = 0;
|
||
|
for (final startLimit = cursorPosition - math.max(0, delta);
|
||
|
start < startLimit && oldText[start] == newText[start];
|
||
|
start++) {}
|
||
|
final deleted = (start >= end) ? '' : oldText.substring(start, end);
|
||
|
final inserted = newText.substring(start, end + delta);
|
||
|
return Diff(start, deleted, inserted);
|
||
|
}
|
||
|
|
||
|
int getPositionDelta(Delta user, Delta actual) {
|
||
|
if (actual.isEmpty) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
final userItr = DeltaIterator(user);
|
||
|
final actualItr = DeltaIterator(actual);
|
||
|
var diff = 0;
|
||
|
while (userItr.hasNext || actualItr.hasNext) {
|
||
|
final length = math.min(userItr.peekLength(), actualItr.peekLength());
|
||
4 years ago
|
final userOperation = userItr.next(length);
|
||
4 years ago
|
final actualOperation = actualItr.next(length);
|
||
|
if (userOperation.length != actualOperation.length) {
|
||
4 years ago
|
throw 'userOp ${userOperation.length} does not match actualOp '
|
||
|
'${actualOperation.length}';
|
||
4 years ago
|
}
|
||
|
if (userOperation.key == actualOperation.key) {
|
||
|
continue;
|
||
|
} else if (userOperation.isInsert && actualOperation.isRetain) {
|
||
|
diff -= userOperation.length!;
|
||
|
} else if (userOperation.isDelete && actualOperation.isRetain) {
|
||
|
diff += userOperation.length!;
|
||
|
} else if (userOperation.isRetain && actualOperation.isInsert) {
|
||
|
String? operationTxt = '';
|
||
|
if (actualOperation.data is String) {
|
||
|
operationTxt = actualOperation.data as String?;
|
||
|
}
|
||
|
if (operationTxt!.startsWith('\n')) {
|
||
|
continue;
|
||
|
}
|
||
|
diff += actualOperation.length!;
|
||
|
}
|
||
|
}
|
||
|
return diff;
|
||
|
}
|