Skip to content

Commit 381c8c7

Browse files
committed
Fix for issues #6, #7 and #8
- Cell alignment support - New checkbox to ensure a cell padding of one space - Ignore empty rows on the bottom
1 parent f38014a commit 381c8c7

File tree

3 files changed

+159
-38
lines changed

3 files changed

+159
-38
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ <h2> Your table </h2>
8484
<option value="double">Double border</option>
8585
<option value="ascii">ASCII characters</option>
8686
</select></p>
87+
<p><input type="checkbox" id="spacePadding" checked="checked" onclick="genPTT();"> Ensure a padding of one space in each cell.</input></p>
8788
<pre id="ptt-wrapper"></pre>
8889
<hr>
8990

js/main.js

Lines changed: 129 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
$("#table-wrapper").handsontable({
66
colHeaders: false,
77
contextMenu: true,
8-
afterChange: genPTT
8+
afterChange: genPTT,
9+
afterSetCellMeta: genPTT
910
});
1011
}
1112

@@ -58,52 +59,64 @@ function genPTT(){
5859
bottom_right: '┘'
5960
}
6061
}
62+
var spacePadding = document.getElementById("spacePadding").checked
6163

62-
var arr = extractData();
63-
var widths = getWidths(arr);
64+
var data = extractData(spacePadding);
65+
var widths = getWidths(data, spacePadding);
66+
var heights = getHeights(data, spacePadding);
6467
var str = "";
65-
var i, j, k, m, entry, row, pseudoRows, plen;
68+
var i, j, k, m, entry, item, offsets, end;
6669
var typeOption = document.getElementById('type').value;
6770

6871
// top
6972
str += generateSeparationLine(widths, style, 'top_left', 'top_center', 'top_right');
7073

7174
// rows
72-
for (k = 0; k < arr.length; k++) {
73-
74-
row = arr[k];
75-
pseudoRows = [];
76-
77-
for (i = 0; i < widths.length; i++) {
78-
entry = arr[k][i];
79-
if (! entry) continue;
80-
entry = entry.split('\n');
81-
plen = pseudoRows.length;
82-
for (j = 0; j < entry.length - plen; j++) {
83-
pseudoRows.push([]);
84-
}
85-
for (j = 0; j < entry.length; j++) {
86-
pseudoRows[j][i] = entry[j];
75+
for (i = 0; i < heights.length; i++) {
76+
offsets = [];
77+
for (j = 0; j < widths.length; j++) {
78+
if('bottom' == data['arr'][i][j]['vAlign']) {
79+
offsets[j] = data['arr'][i][j]['pseudoRows'].length - heights[i];
80+
} else if ('middle' == data['arr'][i][j]['vAlign']) {
81+
offsets[j] = Math.ceil((data['arr'][i][j]['pseudoRows'].length - heights[i]) / 2);
82+
} else {
83+
offsets[j] = 0;
8784
}
8885
}
8986

90-
for (m = 0; m < pseudoRows.length; m++) {
87+
for (m = 0; m < heights[i]; m++) {
9188
str += style['vertical'];
92-
for (i = 0; i < widths.length; i++) {
93-
entry = pseudoRows[m][i] || '';
89+
for (j = 0; j < widths.length; j++) {
90+
item = data['arr'][i][j];
91+
if(item['empty']) {
92+
entry = '';
93+
} else {
94+
entry = item['pseudoRows'][m + offsets[j]] || '';
95+
}
96+
if('right' == data['arr'][i][j]['hAlign']) {
97+
end = widths[j] - entry.length;
98+
} else if ('center' == data['arr'][i][j]['hAlign']) {
99+
end = Math.floor((widths[j] - entry.length) / 2);
100+
} else {
101+
end = 0;
102+
}
103+
for (k = 0; k < end; k++) {
104+
str += ' ';
105+
}
94106
str += entry;
95-
for (j = entry.length; j < widths[i]; j++) {
107+
end = widths[j] - entry.length - end;
108+
for (k = 0; k < end; k++) {
96109
str += ' ';
97110
}
98-
if (i < widths.length-1) {
111+
if (j < widths.length-1) {
99112
str += style['vertical'];
100113
}
101114
}
102115
str += style['vertical'];
103116
str += '\n';
104117
}
105118

106-
if (('grid' == typeOption && k < arr.length-1) || ('header' == typeOption && k == 0 && k < arr.length-1)) {
119+
if (('grid' == typeOption && i < heights.length-1) || ('header' == typeOption && i == 0 && i < heights.length-1)) {
107120
str += generateSeparationLine(widths, style, 'middle_left', 'middle_center', 'middle_right');
108121
}
109122
}
@@ -113,31 +126,109 @@ function genPTT(){
113126
$('#ptt-wrapper').text(str);
114127
}
115128

116-
function extractData(){
117-
return $('#table-wrapper').handsontable('getData');
129+
function extractData(spacePadding){
130+
var i, j, k, item, lines, w, meta, vAlign, hAlign;
131+
var result = [];
132+
var table = $('#table-wrapper');
133+
var arr = table.handsontable('getData');
134+
for(i = 0; i < arr.length; i++) {
135+
result.push([]);
136+
for (j = 0; j < arr[i].length; j++) {
137+
item = arr[i][j];
138+
if (! item) {
139+
result[i][j] = {empty: true};
140+
} else {
141+
w = 0;
142+
lines = item.split('\n');
143+
for (k = 0; k < lines.length; k++) {
144+
if(spacePadding) {
145+
if(lines[k].indexOf(' ', 0) !== 0) {
146+
lines[k] = ' ' + lines[k];
147+
}
148+
if(lines[k].indexOf(' ', lines[k].length - 1) === -1) {
149+
lines[k] = lines[k] + ' ';
150+
}
151+
}
152+
if (lines[k].length > w) {
153+
w = lines[k].length;
154+
}
155+
}
156+
meta = table.handsontable('getCellMeta', i, j);
157+
hAlign = 'left';
158+
vAlign = 'top';
159+
if(meta['className']) {
160+
if(meta['className'].indexOf('htCenter') > -1) {
161+
hAlign = 'center';
162+
} else if(meta['className'].indexOf('htRight') > -1) {
163+
hAlign = 'right';
164+
} else if(meta['className'].indexOf('htJustify') > -1) {
165+
hAlign = 'justify';
166+
}
167+
if(meta['className'].indexOf('htMiddle') > -1) {
168+
vAlign = 'middle';
169+
} else if(meta['className'].indexOf('htBottom') > -1) {
170+
vAlign = 'bottom';
171+
}
172+
}
173+
result[i][j] = {empty: false, pseudoRows: lines, maxWidth: w, vAlign: vAlign, hAlign: hAlign};
174+
}
175+
}
176+
}
177+
return {arr: result, vLen: i, hLen: j};
118178
}
119179

120-
function getWidths(arr){
180+
function getWidths(data, spacePadding){
121181
var widths = [];
122-
var i, j, k, w, item, lines;
182+
var i, j, w, item;
183+
var hasContent = false;
123184

124-
for(i = 0; i < arr.length; i++) {
125-
for (j = 0; j < arr[i].length; j++) {
126-
w = widths[j] || 0;
127-
item = arr[i][j];
128-
if (! item) continue;
129-
lines = item.split('\n');
130-
for (k = 0; k < lines.length; k++) {
131-
if (lines[k].length > w) {
132-
w = lines[k].length;
185+
for (j = data['hLen'] - 1; j >= 0; j--) {
186+
w = 0;
187+
for (i = 0; i < data['vLen']; i++) {
188+
item = data['arr'][i][j];
189+
if (!item['empty']) {
190+
if (item['maxWidth'] > w) {
191+
w = item['maxWidth'];
133192
}
134193
}
194+
}
195+
if(hasContent || w > 0) {
196+
if(spacePadding && w == 0) {
197+
w = 1;
198+
}
135199
widths[j] = w;
200+
hasContent = true;
136201
}
137202
}
138203
return widths;
139204
}
140205

206+
function getHeights(data, spacePadding){
207+
var heights = [];
208+
var i, j, h, item;
209+
var hasContent = false;
210+
211+
for (i = data['vLen'] - 1; i >= 0; i--) {
212+
h = 0;
213+
for (j = 0; j < data['hLen']; j++) {
214+
item = data['arr'][i][j];
215+
if (!item['empty']) {
216+
if (item['pseudoRows'].length > h) {
217+
h = item['pseudoRows'].length;
218+
}
219+
}
220+
}
221+
if(hasContent || h > 0) {
222+
if(spacePadding && h == 0) {
223+
h = 1;
224+
}
225+
heights[i] = h;
226+
hasContent = true;
227+
}
228+
}
229+
return heights;
230+
}
231+
141232
function generateSeparationLine(widths, style, leftKey, centerKey, rightKey){
142233
var str = "";
143234
str += style[leftKey];

user_manual/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Edit the grid and fill it with the the data you want to represent as text.
99

1010
![An example table filled with data](table_example.png)
1111

12+
With the context menu in each cell, the text alignment (vertical and horizontal) can be set.
13+
Those properties are reflected in the output.
14+
1215
## Type option
1316

1417
With the type menu you can influence the type of table you want to obtain.
@@ -83,6 +86,32 @@ __ASCII characters__:
8386
| 3 | Carrie |
8487
+----+--------+
8588

89+
## Cell padding
90+
91+
There is a checkbox to configure if additional spaces should be added to ensure a cell padding of one space in each cell.
92+
93+
__Checked__ (default):
94+
95+
┌────┬────────┐
96+
│ Id │ Name │
97+
├────┼────────┤
98+
│ 1 │ Alice │
99+
│ 2 │ Bob │
100+
│ 3 │ Carrie │
101+
└────┴────────┘
102+
103+
__Not checked__ (default):
104+
105+
┌──┬──────┐
106+
│Id│Name │
107+
├──┼──────┤
108+
│1 │Alice │
109+
├──┼──────┤
110+
│2 │Bob │
111+
├──┼──────┤
112+
│3 │Carrie│
113+
└──┴──────┘
114+
86115
## Output
87116

88117
The output is displayed right below the table.

0 commit comments

Comments
 (0)