Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix order list numbering #988

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 28 additions & 42 deletions lib/src/widgets/style_widgets/number_point.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,40 @@ class QuillNumberPoint extends StatelessWidget {

@override
Widget build(BuildContext context) {
var s = index.toString();
int? level = 0;
if (!attrs.containsKey(Attribute.indent.key) &&
!indentLevelCounts.containsKey(1)) {
indentLevelCounts.clear();
return Container(
alignment: AlignmentDirectional.topEnd,
width: width,
padding: EdgeInsetsDirectional.only(end: padding),
child: Text(withDot ? '$s.' : s, style: style),
);
}
if (attrs.containsKey(Attribute.indent.key)) {
level = attrs[Attribute.indent.key]!.value;
} else {
// first level but is back from previous indent level
// supposed to be "2."
indentLevelCounts[0] = 1;
}
if (indentLevelCounts.containsKey(level! + 1)) {
// last visited level is done, going up
indentLevelCounts.remove(level + 1);
}
final count = (indentLevelCounts[level] ?? 0) + 1;
indentLevelCounts[level] = count;

s = count.toString();
if (level % 3 == 1) {
// a. b. c. d. e. ...
s = _toExcelSheetColumnTitle(count);
} else if (level % 3 == 2) {
// i. ii. iii. ...
s = _intToRoman(count);
}
// level % 3 == 0 goes back to 1. 2. 3.
final olString = _getOlString();

return Container(
alignment: AlignmentDirectional.topEnd,
width: width,
padding: EdgeInsetsDirectional.only(end: padding),
child: Text(withDot ? '$s.' : s, style: style),
child: Text('$olString${withDot ? '.' : ''}', style: style),
);
}

String _toExcelSheetColumnTitle(int n) {
String _getOlString() {
final int indentLevel = attrs[Attribute.indent.key]?.value ?? 0;

if (indentLevelCounts.containsKey(indentLevel + 1)) {
// last visited level is done, going up
indentLevelCounts.remove(indentLevel + 1);
}

final count = (indentLevelCounts[indentLevel] ?? 0) + 1;
indentLevelCounts[indentLevel] = count;

final numberingMode = indentLevel % 3;
if (numberingMode == 1) {
// a. b. c.
return _intToAlpha(count);
} else if (numberingMode == 2) {
// i. ii. iii.
return _intToRoman(count);
}

return count.toString();
}

String _intToAlpha(int n) {
final result = StringBuffer();
while (n > 0) {
n--;
Expand All @@ -93,14 +82,11 @@ class QuillNumberPoint extends StatelessWidget {

final builder = StringBuffer();
for (var a = 0; a < arabianRomanNumbers.length; a++) {
final times = (num / arabianRomanNumbers[a])
.truncate(); // equals 1 only when arabianRomanNumbers[a] = num
final times = (num / arabianRomanNumbers[a]).truncate(); // equals 1 only when arabianRomanNumbers[a] = num
// executes n times where n is the number of times you have to add
// the current roman number value to reach current num.
builder.write(romanNumbers[a] * times);
num -= times *
arabianRomanNumbers[
a]; // subtract previous roman number value from num
num -= times * arabianRomanNumbers[a]; // subtract previous roman number value from num
}

return builder.toString().toLowerCase();
Expand Down