(view: EditorView)
| 222 | } |
| 223 | |
| 224 | toDOM(view: EditorView): HTMLElement { |
| 225 | this.view = view |
| 226 | const root = document.createElement('div') |
| 227 | root.className = 'cm-table-widget' |
| 228 | root.setAttribute('contenteditable', 'false') |
| 229 | this.dom = root |
| 230 | |
| 231 | const wrapper = document.createElement('div') |
| 232 | wrapper.className = 'cm-table-wrapper' |
| 233 | |
| 234 | const table = document.createElement('table') |
| 235 | |
| 236 | // Colgroup carries persisted column widths and is what the resize handles |
| 237 | // drive. Built for every table so a drag can populate it. (#294) |
| 238 | const colgroup = document.createElement('colgroup') |
| 239 | const colWidths = this.model.colWidths ?? [] |
| 240 | this.cols = [] |
| 241 | let anyWidth = false |
| 242 | for (let c = 0; c < this.model.headers.length; c++) { |
| 243 | const colEl = document.createElement('col') |
| 244 | const w = colWidths[c] |
| 245 | if (typeof w === 'number' && w > 0) { |
| 246 | colEl.style.width = `${w}px` |
| 247 | anyWidth = true |
| 248 | } |
| 249 | colgroup.append(colEl) |
| 250 | this.cols.push(colEl) |
| 251 | } |
| 252 | table.append(colgroup) |
| 253 | if (anyWidth) table.classList.add('cm-table-fixed') |
| 254 | |
| 255 | const thead = document.createElement('thead') |
| 256 | const headRow = document.createElement('tr') |
| 257 | this.model.headers.forEach((text, col) => { |
| 258 | const th = this.buildCell('th', -1, col, text) |
| 259 | this.attachColResizeHandle(th, col) |
| 260 | headRow.append(th) |
| 261 | }) |
| 262 | thead.append(headRow) |
| 263 | table.append(thead) |
| 264 | |
| 265 | const tbody = document.createElement('tbody') |
| 266 | this.model.rows.forEach((row, r) => { |
| 267 | const tr = document.createElement('tr') |
| 268 | row.forEach((text, col) => tr.append(this.buildCell('td', r, col, text))) |
| 269 | tbody.append(tr) |
| 270 | }) |
| 271 | table.append(tbody) |
| 272 | wrapper.append(table) |
| 273 | |
| 274 | // Add-row / add-column buttons (appear on hover via CSS). |
| 275 | const addCol = document.createElement('button') |
| 276 | addCol.type = 'button' |
| 277 | addCol.className = 'cm-table-add cm-table-add-col' |
| 278 | addCol.textContent = '+' |
| 279 | addCol.title = 'Add column' |
| 280 | addCol.addEventListener('mousedown', (e) => e.preventDefault()) |
| 281 | addCol.addEventListener('click', (e) => { |
nothing calls this directly
no test coverage detected