Skip to content

Commit a0042a6

Browse files
committed
- Fixes deselection after applying heading style
- Adds removing special chars when todo applying in selected paragraph
1 parent d7e2646 commit a0042a6

File tree

1 file changed

+54
-71
lines changed

1 file changed

+54
-71
lines changed

FSNotes/Helpers/TextFormatter.swift

Lines changed: 54 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ public class TextFormatter {
500500
}
501501

502502
public func header(_ string: String) {
503+
let fullSelection = selectedRange.length > 0
503504
guard let pRange = getParagraphRange() else { return }
504505

505506
#if os(iOS)
@@ -532,7 +533,13 @@ public class TextFormatter {
532533
}
533534

534535
let diff = paragraph.contains("\n") ? 1 : 0
535-
let selectRange = NSRange(location: pRange.location + paragraph.count - diff, length: 0)
536+
537+
var selectRange = NSRange(location: pRange.location + paragraph.count - diff, length: 0)
538+
539+
if fullSelection {
540+
selectRange = NSRange(location: pRange.location, length: paragraph.count - diff)
541+
}
542+
536543
insertText(paragraph, replacementRange: pRange, selectRange: selectRange)
537544
#endif
538545
}
@@ -793,6 +800,25 @@ public class TextFormatter {
793800

794801
var result = String()
795802
for line in lines {
803+
804+
// Removes extra chars identified as list items start
805+
806+
var line = line
807+
808+
let digitRegex = try! NSRegularExpression(pattern: "^([0-9]+\\. )")
809+
let digitRegexResult = digitRegex.firstMatch(in: line, range: NSRange(0..<line.count))
810+
811+
let charRegex = try! NSRegularExpression(pattern: "^([-*–+]+ )")
812+
let charRegexResult = charRegex.firstMatch(in: line, range: NSRange(0..<line.count))
813+
814+
if let result = digitRegexResult {
815+
let qty = result.range.length
816+
line = String(line.dropFirst(qty))
817+
} else if let result = charRegexResult, !line.contains("- [") {
818+
let qty = result.range.length
819+
line = String(line.dropFirst(qty))
820+
}
821+
796822
if addPrefixes {
797823
let task = addCompleted ? "- [x] " : "- [ ] "
798824
var empty = String()
@@ -883,7 +909,6 @@ public class TextFormatter {
883909
} else {
884910
guard let attributedText = AttributedBox.getUnChecked() else { return }
885911

886-
887912
// Toggle render if exist in current paragraph
888913
var rangeFound = false
889914
let attributedParagraph = self.storage.attributedSubstring(from: paragraphRange)
@@ -1292,7 +1317,9 @@ public class TextFormatter {
12921317
public func list() {
12931318
guard let pRange = getParagraphRange() else { return }
12941319

1295-
let string = getAttributedString().attributedSubstring(from: pRange).string
1320+
let attributedString = getAttributedString().attributedSubstring(from: pRange)
1321+
let mutable = NSMutableAttributedString(attributedString: attributedString)
1322+
let string = mutable.unLoadCheckboxes().string
12961323

12971324
guard string.isContainsLetters else {
12981325
insertText("- ")
@@ -1343,7 +1370,9 @@ public class TextFormatter {
13431370
public func orderedList() {
13441371
guard let pRange = getParagraphRange() else { return }
13451372

1346-
let string = getAttributedString().attributedSubstring(from: pRange).string
1373+
let attributedString = getAttributedString().attributedSubstring(from: pRange)
1374+
let mutable = NSMutableAttributedString(attributedString: attributedString)
1375+
let string = mutable.unLoadCheckboxes().string
13471376

13481377
guard string.isContainsLetters else {
13491378
insertText("1. ")
@@ -1406,75 +1435,25 @@ public class TextFormatter {
14061435
}
14071436

14081437
private func cleanListItem(line: String) -> String {
1409-
var cleanLine = String()
1410-
var prefixFound = false
1411-
1412-
var numberCheck = false
1413-
var spaceCheck = false
1414-
var dotCheck = false
1415-
1416-
var skipped = String()
1417-
1418-
for char in line {
1419-
if numberCheck {
1420-
if char.isNumber {
1421-
skipped.append(char)
1422-
continue
1423-
} else {
1424-
numberCheck = false
1425-
dotCheck = true
1426-
}
1427-
}
1428-
1429-
if dotCheck {
1430-
if char == "." {
1431-
skipped.append(char)
1432-
spaceCheck = true
1433-
} else {
1434-
cleanLine.append(skipped)
1435-
cleanLine.append(char)
1436-
skipped = ""
1437-
}
1438-
1439-
dotCheck = false
1440-
continue
1441-
}
1442-
1443-
if spaceCheck {
1444-
if char.isWhitespace {
1445-
} else {
1446-
cleanLine.append(skipped)
1447-
cleanLine.append(char)
1448-
}
1449-
1450-
spaceCheck = false
1451-
skipped = ""
1452-
continue
1453-
}
1454-
1455-
if char.isWhitespace && !prefixFound {
1456-
cleanLine.append(char)
1457-
} else if !prefixFound {
1458-
if char.isNumber {
1459-
numberCheck = true
1460-
skipped.append(char)
1461-
} else if char == "-" {
1462-
spaceCheck = true
1463-
skipped.append(char)
1464-
} else {
1465-
cleanLine.append(char)
1466-
}
1467-
prefixFound = true
1468-
} else {
1469-
cleanLine.append(char)
1470-
}
1471-
}
1472-
1473-
if skipped.count > 0 {
1474-
cleanLine.append(skipped)
1438+
var line = line
1439+
1440+
let digitRegex = try! NSRegularExpression(pattern: "^([0-9]+\\. )")
1441+
let digitRegexResult = digitRegex.firstMatch(in: line, range: NSRange(0..<line.count))
1442+
1443+
let charRegex = try! NSRegularExpression(pattern: "^([-*–+]+ )")
1444+
let charRegexResult = charRegex.firstMatch(in: line, range: NSRange(0..<line.count))
1445+
1446+
if line.starts(with: "- [ ] ") || line.starts(with: "- [x] ") {
1447+
line = String(line.dropFirst(6))
1448+
} else if let result = digitRegexResult {
1449+
let qty = result.range.length
1450+
line = String(line.dropFirst(qty))
1451+
} else if let result = charRegexResult, !line.contains("- [") {
1452+
let qty = result.range.length
1453+
line = String(line.dropFirst(qty))
14751454
}
14761455

1477-
return cleanLine
1456+
return line
14781457
}
14791458

14801459
private func parseTodo(line: String) -> (Bool, Bool, String) {
@@ -1514,6 +1493,10 @@ public class TextFormatter {
15141493
}
15151494

15161495
private func hasPrefix(line: String, numbers: Bool) -> Bool {
1496+
if line.starts(with: "- [ ] ") || line.starts(with: "- [x] ") {
1497+
return false
1498+
}
1499+
15171500
var checkNumberDot = false
15181501

15191502
for char in line {

0 commit comments

Comments
 (0)