mirror of
https://github.com/alibaba/flutter-go.git
synced 2025-05-20 06:16:20 +08:00
41 lines
1.4 KiB
Dart
41 lines
1.4 KiB
Dart
// Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
/// Utility functions for Zefyr.
|
|
library zefyr.util;
|
|
|
|
import 'dart:math' as math;
|
|
|
|
import 'package:quill_delta/quill_delta.dart';
|
|
|
|
export 'src/fast_diff.dart';
|
|
|
|
int getPositionDelta(Delta user, Delta actual) {
|
|
final userIter = new DeltaIterator(user);
|
|
final actualIter = new DeltaIterator(actual);
|
|
int diff = 0;
|
|
while (userIter.hasNext || actualIter.hasNext) {
|
|
num length = math.min(userIter.peekLength(), actualIter.peekLength());
|
|
final userOp = userIter.next(length);
|
|
final actualOp = actualIter.next(length);
|
|
assert(userOp.length == actualOp.length);
|
|
if (userOp.key == actualOp.key) continue;
|
|
if (userOp.isInsert && actualOp.isRetain) {
|
|
diff -= userOp.length;
|
|
} else if (userOp.isDelete && actualOp.isRetain) {
|
|
diff += userOp.length;
|
|
} else if (userOp.isRetain && actualOp.isInsert) {
|
|
if (actualOp.data.startsWith('\n') ) {
|
|
// At this point user input reached its end (retain). If a heuristic
|
|
// rule inserts a new line we should keep cursor on it's original position.
|
|
continue;
|
|
}
|
|
diff += actualOp.length;
|
|
} else {
|
|
// TODO: this likely needs to cover more edge cases.
|
|
}
|
|
}
|
|
return diff;
|
|
}
|