Skip to content

Commit 2876334

Browse files
committed
[WIP] Mobile toolbar - Bottom Dialog Block Option [DONE]
1 parent dc6061e commit 2876334

File tree

3 files changed

+51
-76
lines changed

3 files changed

+51
-76
lines changed

lib/src/widgets/controller.dart

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'dart:math' as math;
22

33
import 'package:flutter/cupertino.dart';
4+
import 'package:flutter/services.dart';
45
import 'package:flutter_quill/models/documents/nodes/leaf.dart';
6+
import 'package:flutter_quill/src/models/documents/nodes/block.dart';
57
import 'package:flutter_quill/src/models/documents/nodes/line.dart';
68
import 'package:flutter_quill/src/widgets/sym_widgets/sym_title_widgets/sym_title_kalpataru.dart';
79
import 'package:tuple/tuple.dart';
@@ -267,19 +269,21 @@ class QuillController extends ChangeNotifier {
267269
extentOffset: math.min(selection.extentOffset, end));
268270
}
269271

270-
void deleteCurrentLine() {
271-
final line = document.getLineFromTextIndex(selection.baseOffset);
272+
void deleteSelectedLine([int? selectedTextIndex]) {
273+
final line = document
274+
.getLineFromTextIndex(selectedTextIndex ?? selection.baseOffset);
272275

273-
final isEmbed = (line.children.first as Leaf).value is Embeddable;
274-
final textIndex = line.offset;
276+
final isEmbed = line.children.isNotEmpty
277+
? (line.children.first as Leaf).value is Embeddable
278+
: false;
279+
final textIndex = selectedTextIndex ?? line.offset;
275280
int textLength;
276281
if (!isEmbed) {
277282
textLength = line.length + 1;
278283
} else {
279284
textLength = 1;
280285
}
281286

282-
/* SOMEHOW IT DOESN'T WORK LIKE ON THE SymMenuBlockOption */
283287
if (textLength < document.length) {
284288
document.delete(textIndex, line.length);
285289

@@ -298,9 +302,39 @@ class QuillController extends ChangeNotifier {
298302
ChangeSource.LOCAL);
299303
}
300304
} else {
301-
print('replaceText: ${line.length}');
302305
replaceText(
303306
0, line.length - 1, '\n', const TextSelection.collapsed(offset: 0));
304307
}
305308
}
309+
310+
Future<void> copyPlainTextSelectedLine([int? selectedTextIndex]) async {
311+
final text = document
312+
.getTextInLineFromTextIndex(selectedTextIndex ?? selection.baseOffset);
313+
await Clipboard.setData(ClipboardData(text: text));
314+
}
315+
316+
void duplicateSelectedLine([int? selectedTextIndex]) {
317+
final line = document
318+
.getLineFromTextIndex(selectedTextIndex ?? selection.baseOffset);
319+
320+
final selectedBlock = line.parent is Block ? line.parent as Block : null;
321+
var newLineIndex = line.documentOffset + line.length;
322+
323+
if (line.nextLine == null) {
324+
newLineIndex--;
325+
}
326+
327+
document.duplicateLine(newLineIndex, line,
328+
selectedBlock?.style.attributes.entries.first.value);
329+
}
330+
331+
void turnSelectedLineInto(Attribute? attribute, [int? selectedTextIndex]) {
332+
final textIndex = selectedTextIndex ?? selection.baseOffset;
333+
for (final attr in Attribute.blockKeysExceptIndent) {
334+
if (attr != attribute) {
335+
document.format(textIndex, 0, Attribute.clone(attr, null));
336+
}
337+
}
338+
document.format(textIndex, 0, attribute);
339+
}
306340
}

lib/src/widgets/sym_widgets/sym_menu_block_option.dart

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -74,77 +74,16 @@ class _SymMenuBlockOptionState extends State<SymMenuBlockOption> {
7474
widget.renderEditableTextLine.setLineSelected(true);
7575
final textIndex = widget.textIndex;
7676
actionListener = MenuBlockOptionActionListener(onDelete: () {
77-
int textLength;
78-
79-
/*
80-
Applying linebreak length (+ 1) on the text length
81-
but embeddable (image) considered only contain
82-
a linebreak
83-
*/
84-
if (!widget.isEmbeddable) {
85-
textLength = widget.controller.document
86-
.getTextInLineFromTextIndex(textIndex)
87-
.length +
88-
1;
89-
} else {
90-
textLength = 1;
91-
}
92-
93-
if (textLength != widget.controller.document.length) {
94-
widget.controller.document.delete(textIndex, textLength);
95-
96-
final lastCursorIndex = widget.controller.selection.baseOffset;
97-
98-
if (lastCursorIndex > textIndex) {
99-
var newCursorIndex = lastCursorIndex - textLength;
100-
101-
if (newCursorIndex < 0) {
102-
newCursorIndex = 0;
103-
}
104-
105-
widget.controller.updateSelection(
106-
TextSelection(
107-
baseOffset: newCursorIndex, extentOffset: newCursorIndex),
108-
ChangeSource.LOCAL);
109-
}
110-
} else {
111-
widget.controller.replaceText(
112-
0,
113-
widget.controller.document
114-
.getTextInLineFromTextIndex(textIndex)
115-
.length,
116-
'\n',
117-
const TextSelection(baseOffset: 0, extentOffset: 0));
118-
}
77+
widget.controller.deleteSelectedLine(textIndex);
11978
}, onCopy: () async {
120-
final text =
121-
widget.controller.document.getTextInLineFromTextIndex(textIndex);
122-
await Clipboard.setData(ClipboardData(text: text));
79+
await widget.controller.copyPlainTextSelectedLine(textIndex);
12380
}, onDuplicate: () {
124-
final selectedLine = widget.renderEditableTextLine.line;
125-
126-
final selectedBlock =
127-
selectedLine.parent is Block ? selectedLine.parent as Block : null;
128-
var newLineIndex = selectedLine.documentOffset + selectedLine.length;
129-
130-
if (selectedLine.nextLine == null) {
131-
newLineIndex--;
132-
}
133-
134-
widget.controller.document.duplicateLine(newLineIndex, selectedLine,
135-
selectedBlock?.style.attributes.entries.first.value);
81+
widget.controller.duplicateSelectedLine(textIndex);
13682
});
13783

13884
turnIntoListener = MenuBlockOptionTurnIntoListener(
13985
turnInto: (attribute) {
140-
for (final attr in Attribute.blockKeysExceptIndent) {
141-
if (attr != attribute) {
142-
widget.controller.document
143-
.format(textIndex, 0, Attribute.clone(attr, null));
144-
}
145-
}
146-
147-
widget.controller.document.format(textIndex, 0, attribute);
86+
widget.controller.turnSelectedLineInto(attribute, textIndex);
14887
},
14988
);
15089
}

lib/src/widgets/sym_widgets/sym_toolbar/sym_bottom_menu_block_option.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,23 @@ class SymBottomMenuBlockOption {
122122
if (type is BlockOptionTypeAction) {
123123
switch (type.type) {
124124
case BlockActionItem.DELETE:
125-
controller.deleteCurrentLine();
125+
controller.deleteSelectedLine();
126126
break;
127127
case BlockActionItem.COPY:
128-
// TODO: Handle this case.
128+
controller.copyPlainTextSelectedLine();
129129
break;
130130
case BlockActionItem.DUPLICATE:
131-
// TODO: Handle this case.
131+
controller.duplicateSelectedLine();
132132
break;
133133
case BlockActionItem.INDENT_LEFT:
134-
// TODO: Handle this case.
134+
135135
break;
136136
case BlockActionItem.INDENT_RIGHT:
137-
// TODO: Handle this case.
137+
138138
break;
139139
}
140+
} else if (type is BlockOptionTypeAttribute) {
141+
controller.turnSelectedLineInto(type.attr);
140142
}
141143
});
142144
},

0 commit comments

Comments
 (0)