Skip to content

Commit

Permalink
Added code to merge and render merged cells
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed May 27, 2024
1 parent 04ceea3 commit bc31554
Show file tree
Hide file tree
Showing 20 changed files with 283 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,16 @@ class CommandsExampleViewController: ExamplesBaseViewController {
var selectedCells: [GridCell]? = nil
var selectedGrid: GridView? = nil

var selectedTableCells: [TableCell]? = nil
var selectedTable: TableView? = nil

@objc
func mergeCells(sender: UIButton) {
if let cells = selectedCells {
selectedGrid?.merge(cells: cells)
if let cells = selectedTableCells {
selectedTable?.merge(cells: cells)
}
selectedCells = nil
selectedGrid = nil
selectedTableCells = nil
selectedTable = nil
}

@objc
Expand Down Expand Up @@ -599,7 +602,15 @@ extension CommandsExampleViewController: TableViewDelegate {
}

func tableView(_ tableView: TableView, didSelectCells cells: [TableCell]) {

selectedTable = tableView
selectedTableCells = cells
mergeButton?.isEnabled = tableView.isCellSelectionMergeable(cells)

if cells.count == 1, cells[0].isSplittable {
splitButton?.isEnabled = true
} else {
splitButton?.isEnabled = false
}
}

func tableView(_ tableView: TableView, didUnselectCells cells: [TableCell]) {
Expand Down
20 changes: 17 additions & 3 deletions Proton/Sources/Swift/Table/Core/Table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import UIKit

protocol TableDelegate: AnyObject {
var viewport: CGRect { get }
var cellsInViewport: [TableCell] { get }
func table(_ table: Table, shouldChangeColumnWidth proposedWidth: CGFloat, for columnIndex: Int) -> Bool
}

Expand Down Expand Up @@ -86,6 +87,10 @@ class Table {
return cellStore.cellAt(rowIndex: rowIndex, columnIndex: columnIndex)
}

func heightForCell(_ cell: TableCell) -> CGFloat {
cell.rowSpan.reduce(0) { $0 + rowHeights[$1].currentHeight }
}

func calculateTableDimensions(basedOn size: CGSize) {
let viewportWidth = viewport?.width ?? size.width
var cumulativeX: CGFloat = 0
Expand Down Expand Up @@ -236,6 +241,7 @@ class Table {
}

private func merge(cell: TableCell, other: TableCell) {
// syncTextForCellsInViewport()
guard let _ = cellStore.cells.firstIndex(where: { $0.id == cell.id }),
let otherIndex = cellStore.cells.firstIndex(where: { $0.id == other.id }) else {
return
Expand All @@ -244,9 +250,11 @@ class Table {
cell.rowSpan = Array(Set(cell.rowSpan).union(other.rowSpan)).sorted()
cell.columnSpan = Array(Set(cell.columnSpan).union(other.columnSpan)).sorted()

// TODO: fix
// cell.editor.replaceCharacters(in: cell.editor.textEndRange, with: " ")
// cell.editor.replaceCharacters(in: cell.editor.textEndRange, with: other.editor.attributedText)
let updatedText = NSMutableAttributedString(attributedString: cell.attributedText ?? NSAttributedString())
updatedText.append(NSAttributedString(string: " "))
updatedText.append(other.attributedText ?? NSAttributedString())

cell.attributedText = updatedText

cellStore.deleteCellAt(index: otherIndex)
}
Expand Down Expand Up @@ -417,6 +425,12 @@ class Table {

}

func syncTextForCellsInViewport() {
delegate?.cellsInViewport.forEach {
$0.attributedText = $0.editor?.attributedText
}
}

func collapseRow(at index: Int) {
rowHeights[index].isCollapsed = true
}
Expand Down
1 change: 0 additions & 1 deletion Proton/Sources/Swift/Table/Core/TableCellStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class TableCellStore {
let cell = cells[index]
cell.removeContentView()
cells.remove(at: index)

}

func addCells(_ newCells: [TableCell]) {
Expand Down
13 changes: 11 additions & 2 deletions Proton/Sources/Swift/Table/TableCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ public class TableCell {
/// Additional attributes that can be stored on Cell to identify various aspects like Header, Numbered etc.
public var additionalAttributes: [String: Any] = [:]

public var attributedText: NSAttributedString?
private var _attributedText: NSAttributedString?
public var attributedText: NSAttributedString? {
get { contentView == nil ? _attributedText : editor?.attributedText }
set {
_attributedText = newValue
editor?.attributedText = newValue ?? NSAttributedString()
}
}

/// Row indexes spanned by the cell. In case of a merged cell, this will contain all the rows= indexes which are merged.

/// Row indexes spanned by the cell. In case of a merged cell, this will contain all the rows= indexes which are merged.
public internal(set) var rowSpan: [Int]
Expand Down Expand Up @@ -105,7 +114,7 @@ public class TableCell {
contentView?.frame = frame
//TODO: get rid of editorInitializer in favor of delegate callback for editor
if let editor = contentView?.editor {
editor.attributedText = attributedText ?? editorInitializer().attributedText
editor.attributedText = _attributedText ?? editorInitializer().attributedText
onEditorInitialized?(self, editor)
}
contentView?.applyStyle(style)
Expand Down
12 changes: 12 additions & 0 deletions Proton/Sources/Swift/Table/TableCellContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public class TableCellContentView: UIView {
self.gridStyle = containerCell.gridStyle
super.init(frame: containerCell.frame)

if containerCell.attributedText == nil {
containerCell.attributedText = editor.attributedText
}

self.layoutMargins = .zero

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(contentViewTapped))
Expand Down Expand Up @@ -185,4 +189,12 @@ extension TableCellContentView: EditorViewDelegate {
backgroundColor = color
delegate?.cell(containerCell, didChangeBackgroundColor: color, oldColor: oldColor)
}

// public func editor(_ editor: EditorView, didChangeTextAt range: NSRange) {
// containerCell.attributedText = editor.attributedText
// }
//
// public func editor(_ editor: EditorView, didSetAttributedText attributedText: NSAttributedString, isDeferred: Bool) {
// containerCell.attributedText = attributedText
// }
}
8 changes: 7 additions & 1 deletion Proton/Sources/Swift/Table/TableContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,11 @@ extension TableContentView: TableCellDelegate {
return
}

table.rowHeights[row].currentHeight = bounds.height
// Only increase the height if combined height of merged cell, if it is, is less than the height of editor
// TODO: handle the case where height is reduced. Same as above
if table.heightForCell(cell) <= bounds.height {
table.rowHeights[row].currentHeight = bounds.height
}
//TODO: only update affected row/column onwards
table.calculateTableDimensions(basedOn: self.bounds.size)
recalculateCellBounds(cell: cell)
Expand Down Expand Up @@ -542,6 +546,8 @@ extension TableContentView: UIGestureRecognizerDelegate {
extension TableContentView: TableDelegate {
var viewport: CGRect { bounds }

var cellsInViewport: [TableCell] { tableContentViewDelegate?.cellsInViewport ?? [] }

func table(_ table: Table, shouldChangeColumnWidth proposedWidth: CGFloat, for columnIndex: Int) -> Bool {
tableContentViewDelegate?.tableContentView(self, shouldChangeColumnWidth: proposedWidth, for: columnIndex) ?? true
}
Expand Down
Loading

0 comments on commit bc31554

Please sign in to comment.