Skip to content

Open source CapCut alternative.

TypeScript

feat(ios): turn selected words into list lines

David Lee715b2c0b3cd
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/NoteRichText.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/NoteRichText.swift
index 77e3429f..ea32fe24 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/NoteRichText.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/NoteRichText.swift
@@ -306,13 +306,71 @@ nonisolated public enum NoteRichText {
// MARK: Changing blocks
/// Turn lines into another kind of block. Lists keep their nesting when
- /// they stay lists.
+ /// they stay lists. A list made from several lines takes only the
+ /// written ones, so it numbers 1, 2, 3 and never around empty items; a
+ /// blank line alone still turns.
public static func setting(_ kind: NoteBlockKind, lines: ClosedRange<Int>, of blocks: [NoteBlock]) -> [NoteBlock] {
var out = blocks
for i in lines where out.indices.contains(i) {
out[i].indent = kind.isList && out[i].kind.isList ? out[i].indent : 0
out[i].kind = kind
}
+ let blank = lines.filter { out.indices.contains($0) && out[$0].text.isEmpty }
+ guard kind.isList, blank.count < lines.count else { return out }
+ for i in blank.reversed() { out.remove(at: i) }
+ return out
+ }
+
+ /// Cut the words between two offsets out of their plain lines onto lines
+ /// of their own, so a list made from a piece of a line takes that piece:
+ /// "eggs" selected in "milk eggs bread" leaves "milk", "eggs", "bread".
+ /// Spaces at a cut go with it; a heading, quote or list line stays whole.
+ /// Returns the blocks and where the words now sit.
+ public static func cuttingOut(from start: Int, to end: Int, in blocks: [NoteBlock]) -> (blocks: [NoteBlock], range: Range<Int>) {
+ guard start < end else { return (blocks, start..<end) }
+ var out = blocks
+ let a = position(of: start, in: blocks), b = position(of: end, in: blocks)
+
+ // The end first, so the start's line still holds.
+ var endLine = b.line, endContent = b.content
+ if let cut = cut(&out, line: b.line, at: b.content) { endContent = cut.before }
+ var startLine = a.line, startContent = a.content
+ if let cut = cut(&out, line: a.line, at: a.content) {
+ startLine += 1
+ startContent = 0
+ endLine += 1
+ if b.line == a.line { endContent -= cut.after }
+ }
+ return (out, offset(line: startLine, content: startContent, in: out)..<offset(line: endLine, content: endContent, in: out))
+ }
+
+ /// Split a plain line at a point in its words, dropping the spaces
+ /// around the cut: `before` is where the first half's words end, `after`
+ /// where the second half's began. Nil when the point sits at the line's
+ /// edge, and the line stays whole.
+ private static func cut(_ blocks: inout [NoteBlock], line: Int, at content: Int) -> (before: Int, after: Int)? {
+ guard blocks.indices.contains(line), blocks[line].kind == .paragraph else { return nil }
+ let chars = Array(blocks[line].text)
+ var before = content, after = content
+ while before > 0, chars[before - 1].isWhitespace { before -= 1 }
+ while after < chars.count, chars[after].isWhitespace { after += 1 }
+ guard before > 0, after < chars.count else { return nil }
+ let runs = blocks[line].runs
+ blocks[line].runs = slice(runs, 0..<before)
+ blocks.insert(NoteBlock(.paragraph, runs: slice(runs, after..<chars.count)), at: line + 1)
+ return (before, after)
+ }
+
+ /// The runs covering a range of a line's characters.
+ private static func slice(_ runs: [NoteRun], _ range: Range<Int>) -> [NoteRun] {
+ var out: [NoteRun] = []
+ var index = 0
+ for run in runs {
+ let chars = Array(run.text)
+ let from = max(range.lowerBound - index, 0), to = min(range.upperBound - index, chars.count)
+ if from < to { out.append(NoteRun(String(chars[from..<to]), run.marks)) }
+ index += chars.count
+ }
return out
}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/NoteBodyEditor.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/NoteBodyEditor.swift
index e8118752..16376ea9 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/NoteBodyEditor.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/NoteBodyEditor.swift
@@ -95,6 +95,14 @@ final class NoteBodyEditing {
// MARK: Formatting
func setKind(_ kind: NoteBlockKind) {
+ // Selected words become the list on lines of their own.
+ if kind.isList {
+ let range = selectedOffsets()
+ let cut = NoteRichText.cuttingOut(from: range.lowerBound, to: range.upperBound, in: blocks)
+ blocks = cut.blocks
+ redraw()
+ select(cut.range)
+ }
changeLines { NoteRichText.setting(kind, lines: $1, of: $0) }
}
@@ -150,11 +158,16 @@ final class NoteBodyEditing {
let range = selectedOffsets()
let start = NoteRichText.position(of: range.lowerBound, in: blocks)
let end = NoteRichText.position(of: range.upperBound, in: blocks)
+ let before = blocks
blocks = change(blocks, start.line...end.line)
redraw()
+ // Lines the change dropped come out of the selection's end; a dropped
+ // last line leaves it at the end of the line above.
+ let dropped = before.count - blocks.count
+ let endGone = dropped > 0 && before[end.line].text.isEmpty
select(
NoteRichText.offset(line: start.line, content: start.content, in: blocks)
- ..< NoteRichText.offset(line: end.line, content: end.content, in: blocks)
+ ..< NoteRichText.offset(line: end.line - dropped, content: endGone ? .max : end.content, in: blocks)
)
publish()
}
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/NoteMarkdownTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/NoteMarkdownTests.swift
index 804210bc..b2f0cb9c 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/NoteMarkdownTests.swift
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/NoteMarkdownTests.swift
@@ -289,5 +289,27 @@ import Testing
#expect(NoteMarkdown.serialize(nested) == "1. hello world\n 1. next")
let heading = NoteRichText.setting(.heading(2), lines: 1...1, of: nested)
#expect(NoteMarkdown.serialize(heading) == "1. hello world\n## next")
+ // A list made from several lines takes the written ones; a blank
+ // line alone still turns.
+ let spaced = NoteMarkdown.parse("one\n\ntwo\n\nafter")
+ #expect(NoteMarkdown.serialize(NoteRichText.setting(.number, lines: 0...2, of: spaced)) == "1. one\n2. two\n\nafter")
+ #expect(NoteMarkdown.serialize(NoteRichText.setting(.bullet, lines: 1...1, of: spaced)) == "one\n- \ntwo\n\nafter")
+ }
+
+ @Test func selectedWordsCutOntoTheirOwnLines() {
+ func cut(_ body: String, _ words: String) -> (String, String) {
+ let blocks = NoteMarkdown.parse(body)
+ let text = String(NoteRichText.attributedText(blocks).characters)
+ let start = text.distance(from: text.startIndex, to: text.range(of: words)!.lowerBound)
+ let result = NoteRichText.cuttingOut(from: start, to: start + words.count, in: blocks)
+ let drawn = Array(NoteRichText.attributedText(result.blocks).characters)
+ return (NoteMarkdown.serialize(result.blocks), String(drawn[result.range]))
+ }
+ #expect(cut("milk **eggs** bread", "eggs") == ("milk\n**eggs**\nbread", "eggs"))
+ #expect(cut("I quit. I'll find one.", " I'll") == ("I quit.\nI'll\nfind one.", "I'll"))
+ #expect(cut("one two\nthree four", "two\nthree") == ("one\ntwo\nthree\nfour", "two\nthree"))
+ // A whole line, or a piece of a heading, stays as it is.
+ #expect(cut("milk eggs", "milk eggs") == ("milk eggs", "milk eggs"))
+ #expect(cut("# milk eggs", "eggs") == ("# milk eggs", "eggs"))
}
}