Skip to content

Commit

Permalink
Merge pull request #63 from ZhgChgLi/fix/harry/list-indent
Browse files Browse the repository at this point in the history
[Fix] list item indent with fancy way
  • Loading branch information
zhgchgli0718 authored May 28, 2024
2 parents 7525275 + dfacf29 commit d71980e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,14 @@ struct MarkupNSAttributedStringVisitor: MarkupVisitor {
// We don't set NSTextList to NSParagraphStyle directly, because NSTextList have abnormal extra spaces.
// ref: https://stackoverflow.com/questions/66714650/nstextlist-formatting

var level = 0
var parentMarkup: Markup? = markup.parentMarkup as? ListMarkup
while (parentMarkup != nil) {
if parentMarkup is ListMarkup {
level += 1
}
parentMarkup = parentMarkup?.parentMarkup
}
let indent = String(repeating: "\t", count: level - 1)

if let parentMarkup = markup.parentMarkup as? ListMarkup {
let thisAttributedString: NSMutableAttributedString
if parentMarkup.styleList.type.isOrder() {
let siblingListItems = markup.parentMarkup?.childMarkups.filter({ $0 is ListItemMarkup }) ?? []
let position = (siblingListItems.firstIndex(where: { $0 === markup }) ?? 0) + parentMarkup.styleList.startingItemNumber
thisAttributedString = NSMutableAttributedString(attributedString: makeString(in: markup, string:indent+parentMarkup.styleList.marker(forItemNumber: position)))
thisAttributedString = NSMutableAttributedString(attributedString: makeString(in: markup, string:parentMarkup.styleList.marker(forItemNumber: position)))
} else {
thisAttributedString = NSMutableAttributedString(attributedString: makeString(in: markup, string:indent+parentMarkup.styleList.marker(forItemNumber: parentMarkup.styleList.startingItemNumber)))
}

// Since we use \t as an indentation character, the list text cannot contain \t, otherwise it will cause formatting issues, so we replace \t with spaces directly.
var tabRange = attributedString.mutableString.range(of: "\t", options: .caseInsensitive)
while tabRange.location != NSNotFound {
attributedString.replaceCharacters(in: tabRange, with: " ") // the default width of a \t character is typically equivalent to 8 spaces
tabRange = attributedString.mutableString.range(of: "\t", options: .caseInsensitive)
thisAttributedString = NSMutableAttributedString(attributedString: makeString(in: markup, string:parentMarkup.styleList.marker(forItemNumber: parentMarkup.styleList.startingItemNumber)))
}

attributedString.insert(thisAttributedString, at: 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,27 @@ struct HTMLElementMarkupComponentMarkupStyleVisitor: MarkupVisitor {

func visit(_ markup: ListMarkup) -> Result {
var style = defaultVisit(components.value(markup: markup)) ?? MarkupStyle()

var level = 1
var parentMarkup: Markup? = markup.parentMarkup as? ListMarkup
while (parentMarkup != nil) {
if parentMarkup is ListMarkup {
level += 1
}
parentMarkup = parentMarkup?.parentMarkup
}


let headIndent = (style.font.size ?? 16) * markup.styleList.headIndentMultiply
let indent = (style.font.size ?? 16) * markup.styleList.indentMultiply

style.paragraphStyle.headIndent = CGFloat(level) * (style.paragraphStyle.headIndent ?? indent) + headIndent + (indent * CGFloat(level))
let dotIndent: CGFloat = (markup.styleList.type.isOrder()) ? (indent) : (0) // for 1. -> "."

if style.paragraphStyle.tabStops == nil {
var tabStops: [NSTextTab] = [.init(textAlignment: .left, location: headIndent)]
for i in 1...level {
let dotWidth: CGFloat
if markup.styleList.type.isOrder() {
dotWidth = indent * CGFloat(i)
} else {
dotWidth = 0
}
tabStops.append(.init(textAlignment: .left, location: CGFloat(i) * indent + headIndent + dotWidth))
var parentIndent: CGFloat = 0
var parentMarkup: Markup? = markup.parentMarkup
while (parentMarkup != nil) {
if let thisParentMarkup = parentMarkup as? ListMarkup {
parentIndent += visit(markup: thisParentMarkup)?.paragraphStyle.headIndent ?? 0
}
style.paragraphStyle.tabStops = tabStops
parentMarkup = parentMarkup?.parentMarkup
}

var tabStops: [NSTextTab] = [.init(textAlignment: .left, location: headIndent + parentIndent)]
tabStops.append(.init(textAlignment: .left, location: headIndent + parentIndent + dotIndent + indent))


style.paragraphStyle.tabStops = style.paragraphStyle.tabStops ?? tabStops
style.paragraphStyle.headIndent = style.paragraphStyle.headIndent ?? style.paragraphStyle.tabStops?.last?.location
return style
}

Expand Down

0 comments on commit d71980e

Please sign in to comment.