remove dead drawTableBorders code and fix table view positioning
This commit is contained in:
parent
77bf5a113c
commit
063063895a
|
|
@ -167,76 +167,6 @@ class MarkdownLayoutManager: NSLayoutManager {
|
|||
path.fill()
|
||||
}
|
||||
|
||||
private func drawTableBorders(glyphRange: NSRange, columns: Int, origin: NSPoint, container: NSTextContainer) {
|
||||
guard columns > 0 else { return }
|
||||
var rect = boundingRect(forGlyphRange: glyphRange, in: container)
|
||||
rect.origin.x = origin.x + 4
|
||||
rect.origin.y += origin.y
|
||||
rect.size.width = container.containerSize.width - 8
|
||||
|
||||
let outerPath = NSBezierPath(rect: rect)
|
||||
outerPath.lineWidth = 1
|
||||
Theme.current.surface2.setStroke()
|
||||
outerPath.stroke()
|
||||
|
||||
guard let ts = textStorage else { return }
|
||||
let charRange = characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
|
||||
let text = ts.string as NSString
|
||||
let tableText = text.substring(with: charRange)
|
||||
let lines = tableText.components(separatedBy: "\n").filter { !$0.isEmpty }
|
||||
|
||||
var charOffset = charRange.location
|
||||
for (i, line) in lines.enumerated() {
|
||||
let lineLen = (line as NSString).length
|
||||
if i > 0 {
|
||||
let lineGlyphRange = self.glyphRange(forCharacterRange: NSRange(location: charOffset, length: 1), actualCharacterRange: nil)
|
||||
let lineRect = boundingRect(forGlyphRange: lineGlyphRange, in: container)
|
||||
let y = lineRect.origin.y + origin.y
|
||||
let rowLine = NSBezierPath()
|
||||
rowLine.move(to: NSPoint(x: rect.origin.x, y: y))
|
||||
rowLine.line(to: NSPoint(x: rect.origin.x + rect.size.width, y: y))
|
||||
rowLine.lineWidth = 0.5
|
||||
Theme.current.surface2.setStroke()
|
||||
rowLine.stroke()
|
||||
}
|
||||
charOffset += lineLen + 1
|
||||
}
|
||||
|
||||
if let firstLine = lines.first {
|
||||
let nsFirstLine = firstLine as NSString
|
||||
var pipeOffsets: [Int] = []
|
||||
for i in 0..<nsFirstLine.length {
|
||||
if nsFirstLine.character(at: i) == UInt16(UnicodeScalar("|").value) {
|
||||
pipeOffsets.append(i)
|
||||
}
|
||||
}
|
||||
if pipeOffsets.count > 2 {
|
||||
for pi in 1..<(pipeOffsets.count - 1) {
|
||||
let charPos = charRange.location + pipeOffsets[pi]
|
||||
let pipeGlyph = self.glyphRange(forCharacterRange: NSRange(location: charPos, length: 1), actualCharacterRange: nil)
|
||||
let pipeRect = boundingRect(forGlyphRange: pipeGlyph, in: container)
|
||||
let x = pipeRect.origin.x + origin.x + pipeRect.size.width / 2
|
||||
let colLine = NSBezierPath()
|
||||
colLine.move(to: NSPoint(x: x, y: rect.origin.y))
|
||||
colLine.line(to: NSPoint(x: x, y: rect.origin.y + rect.size.height))
|
||||
colLine.lineWidth = 0.5
|
||||
Theme.current.surface2.setStroke()
|
||||
colLine.stroke()
|
||||
}
|
||||
}
|
||||
|
||||
if lines.count > 1 {
|
||||
let firstLineRange = NSRange(location: charRange.location, length: nsFirstLine.length)
|
||||
let headerGlyphRange = self.glyphRange(forCharacterRange: firstLineRange, actualCharacterRange: nil)
|
||||
var headerRect = boundingRect(forGlyphRange: headerGlyphRange, in: container)
|
||||
headerRect.origin.x = rect.origin.x
|
||||
headerRect.origin.y += origin.y
|
||||
headerRect.size.width = rect.size.width
|
||||
Theme.current.surface0.setFill()
|
||||
headerRect.fill()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Interactive Table Component
|
||||
|
|
@ -1604,19 +1534,21 @@ struct EditorTextView: NSViewRepresentable {
|
|||
}
|
||||
embeddedTableViews.removeAll()
|
||||
|
||||
let origin = tv.textContainerOrigin
|
||||
let text = tv.string as NSString
|
||||
for block in lm.blockRanges {
|
||||
guard case .tableBlock = block.kind else { continue }
|
||||
guard let parsed = parseMarkdownTable(from: text, range: block.range) else { continue }
|
||||
|
||||
let glyphRange = lm.glyphRange(forCharacterRange: block.range, actualCharacterRange: nil)
|
||||
var rect = lm.boundingRect(forGlyphRange: glyphRange, in: tc)
|
||||
rect.origin.x += tv.textContainerInset.width
|
||||
rect.origin.y += tv.textContainerInset.height
|
||||
let rect = lm.boundingRect(forGlyphRange: glyphRange, in: tc)
|
||||
|
||||
let tableX = origin.x + 4
|
||||
let tableY = rect.origin.y + origin.y
|
||||
let tableWidth = tc.containerSize.width - 8
|
||||
|
||||
let tableView = MarkdownTableView(table: parsed, width: tableWidth)
|
||||
tableView.frame.origin = NSPoint(x: rect.origin.x + 4, y: rect.origin.y)
|
||||
tableView.frame.origin = NSPoint(x: tableX, y: tableY)
|
||||
tableView.textView = tv
|
||||
|
||||
tableView.onTableChanged = { [weak self] updatedTable in
|
||||
|
|
|
|||
Loading…
Reference in New Issue