Feat: custom behavior while copying or cutting a embeddable

pull/2067/head
CatHood0 9 months ago
parent 1e545ca298
commit 2b49d6ca27
  1. 3
      lib/flutter_quill.dart
  2. 14
      lib/src/document/nodes/line.dart
  3. 13
      lib/src/editor_toolbar_controller_shared/copy_cut_service/copy_cut_service.dart
  4. 19
      lib/src/editor_toolbar_controller_shared/copy_cut_service/copy_cut_service_provider.dart
  5. 14
      lib/src/editor_toolbar_controller_shared/copy_cut_service/default_copy_cut_service.dart

@ -28,6 +28,9 @@ export 'src/editor/style_widgets/style_widgets.dart';
export 'src/editor/widgets/cursor.dart'; export 'src/editor/widgets/cursor.dart';
export 'src/editor/widgets/default_styles.dart'; export 'src/editor/widgets/default_styles.dart';
export 'src/editor/widgets/link.dart'; export 'src/editor/widgets/link.dart';
export 'src/editor_toolbar_controller_shared/copy_cut_service/copy_cut_service.dart';
export 'src/editor_toolbar_controller_shared/copy_cut_service/copy_cut_service_provider.dart';
export 'src/editor_toolbar_controller_shared/copy_cut_service/default_copy_cut_service.dart';
export 'src/editor_toolbar_controller_shared/quill_configurations.dart'; export 'src/editor_toolbar_controller_shared/quill_configurations.dart';
export 'src/editor_toolbar_shared/quill_configurations_ext.dart'; export 'src/editor_toolbar_shared/quill_configurations_ext.dart';
export 'src/toolbar/base_toolbar.dart'; export 'src/toolbar/base_toolbar.dart';

@ -5,6 +5,7 @@ import 'package:collection/collection.dart';
import '../../../../quill_delta.dart'; import '../../../../quill_delta.dart';
import '../../common/structs/offset_value.dart'; import '../../common/structs/offset_value.dart';
import '../../editor/embed/embed_editor_builder.dart'; import '../../editor/embed/embed_editor_builder.dart';
import '../../editor_toolbar_controller_shared/copy_cut_service/copy_cut_service_provider.dart';
import '../attribute.dart'; import '../attribute.dart';
import '../style.dart'; import '../style.dart';
import 'block.dart'; import 'block.dart';
@ -520,7 +521,18 @@ base class Line extends QuillContainer<Leaf?> {
int _getNodeText(Leaf node, StringBuffer buffer, int offset, int remaining) { int _getNodeText(Leaf node, StringBuffer buffer, int offset, int remaining) {
final text = node.toPlainText(); final text = node.toPlainText();
if (text == Embed.kObjectReplacementCharacter) { if (text == Embed.kObjectReplacementCharacter) {
buffer.write(Embed.kObjectReplacementCharacter); final embed = node.value as Embeddable;
final provider = CopyCutServiceProvider.instance;
// By default getCopyCutAction just return the same operation
// returning Embed.kObjectReplacementCharacter for the buffer
final action = provider.getCopyCutAction(embed.type);
final data = action?.call(embed.data);
if(data is String && data != Embed.kObjectReplacementCharacter){
buffer.write(data);
return remaining;
} else{
buffer.write(action?.call(data));
}
return remaining - node.length; return remaining - node.length;
} }

@ -0,0 +1,13 @@
import 'package:flutter/foundation.dart';
typedef CopyCutAction = Object? Function(dynamic data);
/// An abstraction to make it easy to provide different implementations
/// For copy or cut actions from a Line (just for embeddable blocks)
@immutable
abstract class CopyCutService {
/// Get the CopyCutAction by the type
/// of the embeddable (this type is decided by
/// the property type of that class)
CopyCutAction? getCopyCutAction(String type);
}

@ -0,0 +1,19 @@
import 'package:flutter/foundation.dart' show immutable;
import 'copy_cut_service.dart';
import 'default_copy_cut_service.dart';
@immutable
class CopyCutServiceProvider {
const CopyCutServiceProvider._();
static CopyCutService _instance = DefaultCopyCutService();
static CopyCutService get instance => _instance;
static void setInstance(CopyCutService service) {
_instance = service;
}
static void setInstanceToDefault() {
_instance = DefaultCopyCutService();
}
}

@ -0,0 +1,14 @@
import '../../document/nodes/leaf.dart';
import 'copy_cut_service.dart';
/// Default implementation for [CopyCutService]
///
/// This implementation always return the default embed character
/// replacemenet ([\uFFFC]) to work with the embeds from the internal
/// flutter quill plugins
class DefaultCopyCutService extends CopyCutService {
@override
CopyCutAction? getCopyCutAction(String type) {
return (data) => Embed.kObjectReplacementCharacter;
}
}
Loading…
Cancel
Save