Skip to content

Commit

Permalink
More ergonomic table-input HTML (#6516)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Apr 15, 2023
1 parent dd2038e commit b484f06
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions source/features/table-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ function addTable({delegateTarget: square}: DelegateEvent<MouseEvent, HTMLButton
const field = square.form!.querySelector('textarea.js-comment-field')!;
const cursorPosition = field.selectionStart;

const columns = Number(square.dataset.x);
const rows = Number(square.dataset.y);
const row = columns === 1
// One HTML line per row
? '<tr><td>\n'

// <tr> on its own line
// "1 space" indents without causing unwanted Markdown code blocks that 4 spaces would cause
: '<tr>\n' + ' <td>\n'.repeat(columns);
field.focus();
const table
= '<table>\n'
+ ('<tr>\n'
+ '\t<td>\n'.repeat(Number(square.dataset.x))
).repeat(Number(square.dataset.y))
+ '</table>';
const table = '<table>\n' + row.repeat(rows) + '</table>';
textFieldEdit.insert(field, smartBlockWrap(table, field));

// Move caret to first cell
Expand Down

0 comments on commit b484f06

Please sign in to comment.