Revert example

pull/1280/head
Cierra_Runis 2 years ago
parent 89e9ac06ed
commit 450762419b
No known key found for this signature in database
GPG Key ID: 6A879A1FDB3BF78
  1. 136
      example/lib/pages/home_page.dart
  2. 44
      example/lib/widgets/time_stamp_embed_widget.dart

@ -14,6 +14,7 @@ import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import '../universal_ui/universal_ui.dart'; import '../universal_ui/universal_ui.dart';
import '../widgets/time_stamp_embed_widget.dart';
import 'read_only_page.dart'; import 'read_only_page.dart';
enum _SelectionType { enum _SelectionType {
@ -80,9 +81,24 @@ class _HomePageState extends State<HomePage> {
), ),
actions: [ actions: [
IconButton( IconButton(
onPressed: () => _addEditNote(context), onPressed: () => _insertTimeStamp(
icon: const Icon(Icons.note_add), _controller!,
DateTime.now().toString(),
),
icon: const Icon(Icons.add_alarm_rounded),
), ),
IconButton(
onPressed: () => showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(_controller!.document.toPlainText([
...FlutterQuillEmbeds.builders(),
TimeStampEmbedBuilderWidget()
])),
),
),
icon: const Icon(Icons.text_fields_rounded),
)
], ],
), ),
drawer: Container( drawer: Container(
@ -188,7 +204,7 @@ class _HomePageState extends State<HomePage> {
), ),
embedBuilders: [ embedBuilders: [
...FlutterQuillEmbeds.builders(), ...FlutterQuillEmbeds.builders(),
NotesEmbedBuilder(addEditNote: _addEditNote) TimeStampEmbedBuilderWidget()
], ],
); );
if (kIsWeb) { if (kIsWeb) {
@ -220,7 +236,7 @@ class _HomePageState extends State<HomePage> {
), ),
embedBuilders: [ embedBuilders: [
...defaultEmbedBuildersWeb, ...defaultEmbedBuildersWeb,
NotesEmbedBuilder(addEditNote: _addEditNote), TimeStampEmbedBuilderWidget()
]); ]);
} }
var toolbar = QuillToolbar.basic( var toolbar = QuillToolbar.basic(
@ -433,99 +449,41 @@ class _HomePageState extends State<HomePage> {
return file.path.toString(); return file.path.toString();
} }
Future<void> _addEditNote(BuildContext context, {Document? document}) async { static void _insertTimeStamp(QuillController controller, String string) {
final isEditing = document != null; controller.document.insert(controller.selection.extentOffset, '\n');
final quillEditorController = QuillController( controller.updateSelection(
document: document ?? Document(), TextSelection.collapsed(
selection: const TextSelection.collapsed(offset: 0), offset: controller.selection.extentOffset + 1,
);
await showDialog(
context: context,
builder: (context) => AlertDialog(
titlePadding: const EdgeInsets.only(left: 16, top: 8),
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('${isEditing ? 'Edit' : 'Add'} note'),
IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: const Icon(Icons.close),
)
],
),
content: QuillEditor.basic(
controller: quillEditorController,
readOnly: false,
),
), ),
ChangeSource.LOCAL,
); );
if (quillEditorController.document.isEmpty()) return; controller.document.insert(
controller.selection.extentOffset,
final block = BlockEmbed.custom( TimeStampEmbed(string),
NotesBlockEmbed.fromDocument(quillEditorController.document),
); );
final controller = _controller!;
final index = controller.selection.baseOffset;
final length = controller.selection.extentOffset - index;
if (isEditing) {
final offset =
getEmbedNode(controller, controller.selection.start).offset;
controller.replaceText(
offset, 1, block, TextSelection.collapsed(offset: offset));
} else {
controller.replaceText(index, length, block, null);
}
}
}
class NotesEmbedBuilder extends EmbedBuilder { controller.updateSelection(
NotesEmbedBuilder({required this.addEditNote}); TextSelection.collapsed(
offset: controller.selection.extentOffset + 1,
Future<void> Function(BuildContext context, {Document? document}) addEditNote; ),
ChangeSource.LOCAL,
);
@override controller.document.insert(controller.selection.extentOffset, ' ');
String get key => 'notes'; controller.updateSelection(
TextSelection.collapsed(
offset: controller.selection.extentOffset + 1,
),
ChangeSource.LOCAL,
);
@override controller.document.insert(controller.selection.extentOffset, '\n');
Widget build( controller.updateSelection(
BuildContext context, TextSelection.collapsed(
QuillController controller, offset: controller.selection.extentOffset + 1,
Embed node,
bool readOnly,
bool inline,
TextStyle textStyle,
) {
final notes = NotesBlockEmbed(node.value.data).document;
return Material(
color: Colors.transparent,
child: ListTile(
title: Text(
notes.toPlainText().replaceAll('\n', ' '),
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
leading: const Icon(Icons.notes),
onTap: () => addEditNote(context, document: notes),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
side: const BorderSide(color: Colors.grey),
),
), ),
ChangeSource.LOCAL,
); );
} }
} }
class NotesBlockEmbed extends CustomBlockEmbed {
const NotesBlockEmbed(String value) : super(noteType, value);
static const String noteType = 'notes';
static NotesBlockEmbed fromDocument(Document document) =>
NotesBlockEmbed(jsonEncode(document.toDelta().toJson()));
Document get document => Document.fromJson(jsonDecode(data));
}

@ -0,0 +1,44 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_quill/flutter_quill.dart' hide Text;
class TimeStampEmbed extends Embeddable {
const TimeStampEmbed(
String value,
) : super(timeStampType, value);
static const String timeStampType = 'timeStamp';
static TimeStampEmbed fromDocument(Document document) =>
TimeStampEmbed(jsonEncode(document.toDelta().toJson()));
Document get document => Document.fromJson(jsonDecode(data));
}
class TimeStampEmbedBuilderWidget extends EmbedBuilder {
@override
String get key => 'timeStamp';
@override
String toPlainText(Embed embed) {
return embed.value.data;
}
@override
Widget build(
BuildContext context,
QuillController controller,
Embed node,
bool readOnly,
bool inline,
TextStyle textStyle,
) {
return Row(
children: [
const Icon(Icons.access_time_rounded),
Text(node.value.data as String),
],
);
}
}
Loading…
Cancel
Save