Skip to content

Commit c36e348

Browse files
committed
Make copied row appear immediately below current row
1 parent 4af03ab commit c36e348

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

src/editors/table.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,14 @@ export class TableEditor extends ArrayEditor {
336336
return false
337337
}
338338

339-
const i = e.currentTarget.getAttribute('data-i') * 1
340-
const newval = this.getValue().filter((row, j) => j !== i) /* If this is the one we're deleting */
341-
this.setValue(newval)
339+
const j = e.currentTarget.getAttribute('data-i') * 1
340+
const value = this.getValue()
341+
342+
value.splice(j, 1)
343+
344+
this.setValue(value)
342345
this.onChange(true)
343-
this.jsoneditor.trigger('deleteRow', this.rows[i])
346+
this.jsoneditor.trigger('deleteRow', this.rows[j])
344347
})
345348
controlsHolder.appendChild(this.rows[i].delete_button)
346349
}
@@ -352,19 +355,15 @@ export class TableEditor extends ArrayEditor {
352355
this.rows[i].copy_button.addEventListener('click', e => {
353356
e.preventDefault()
354357
e.stopPropagation()
355-
const i = e.currentTarget.getAttribute('data-i') * 1
358+
359+
const j = e.currentTarget.getAttribute('data-i') * 1
356360
const value = this.getValue()
357361

358-
value.forEach((row, j) => {
359-
if (j === i) {
360-
value.push(row)
361-
}
362-
})
362+
value.splice(j + 1, 0, value[j])
363363

364364
this.setValue(value)
365-
this.refreshValue(true)
366365
this.onChange(true)
367-
this.jsoneditor.trigger('copyRow', value[value.length - 1])
366+
this.jsoneditor.trigger('copyRow', this.rows[j + 1])
368367
})
369368
controlsHolder.appendChild(this.rows[i].copy_button)
370369
}
@@ -376,17 +375,15 @@ export class TableEditor extends ArrayEditor {
376375
this.rows[i].moveup_button.addEventListener('click', e => {
377376
e.preventDefault()
378377
e.stopPropagation()
379-
const i = e.currentTarget.getAttribute('data-i') * 1
380378

381-
if (i <= 0) return
382-
const rows = this.getValue()
383-
const tmp = rows[i - 1]
384-
rows[i - 1] = rows[i]
385-
rows[i] = tmp
379+
const j = e.currentTarget.getAttribute('data-i') * 1
380+
const value = this.getValue()
386381

387-
this.setValue(rows)
382+
value.splice(j - 1, 0, value.splice(j, 1)[0])
383+
384+
this.setValue(value)
388385
this.onChange(true)
389-
this.jsoneditor.trigger('moveRow', this.rows[i - 1])
386+
this.jsoneditor.trigger('moveRow', this.rows[j - 1])
390387
})
391388
controlsHolder.appendChild(this.rows[i].moveup_button)
392389
}
@@ -398,17 +395,15 @@ export class TableEditor extends ArrayEditor {
398395
this.rows[i].movedown_button.addEventListener('click', e => {
399396
e.preventDefault()
400397
e.stopPropagation()
401-
const i = e.currentTarget.getAttribute('data-i') * 1
402-
const rows = this.getValue()
403-
if (i >= rows.length - 1) return
404398

405-
const tmp = rows[i + 1]
406-
rows[i + 1] = rows[i]
407-
rows[i] = tmp
399+
const j = e.currentTarget.getAttribute('data-i') * 1
400+
const value = this.getValue()
408401

409-
this.setValue(rows)
402+
value.splice(j + 1, 0, value.splice(j, 1)[0])
403+
404+
this.setValue(value)
410405
this.onChange(true)
411-
this.jsoneditor.trigger('moveRow', this.rows[i + 1])
406+
this.jsoneditor.trigger('moveRow', this.rows[j + 1])
412407
})
413408
controlsHolder.appendChild(this.rows[i].movedown_button)
414409
}

tests/codeceptjs/editors/array_test.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,44 @@ Scenario('should trigger array (table) editing triggers', async (I) => {
1212
I.amOnPage('table-move-events.html');
1313
I.seeElement('[data-schemapath="root.0"]');
1414
I.seeElement('[data-schemapath="root.1"]');
15+
I.seeElement('[data-schemapath="root.2"]');
16+
I.seeElement('[data-schemapath="root.3"]');
17+
I.seeElement('[data-schemapath="root.4"]');
1518
I.click('.get-value');
1619
value = await I.grabValueFrom('.debug');
17-
assert.equal(value, '["A","B"]');
20+
assert.equal(value, '["A","B","C","D","E"]');
1821

1922
I.amAcceptingPopups();
2023
I.click('//button[contains(@class, "json-editor-btn-moveup") and @data-i="1"]');
2124
I.seeInPopup('moveRow');
2225
I.acceptPopup();
2326
I.click('.get-value');
2427
value = await I.grabValueFrom('.debug');
25-
assert.equal(value, '["B","A"]');
28+
assert.equal(value, '["B","A","C","D","E"]');
2629

2730
I.amAcceptingPopups();
28-
I.click('//button[contains(@class, "json-editor-btn-movedown") and @data-i="0"]');
31+
I.click('//button[contains(@class, "json-editor-btn-movedown") and @data-i="1"]');
2932
I.seeInPopup('moveRow');
3033
I.acceptPopup();
3134
I.click('.get-value');
3235
value = await I.grabValueFrom('.debug');
33-
assert.equal(value, '["A","B"]');
36+
assert.equal(value, '["B","C","A","D","E"]');
3437

3538
I.amAcceptingPopups();
36-
I.click('//button[contains(@class, "json-editor-btn-copy") and @data-i="1"]');
39+
I.click('//button[contains(@class, "json-editor-btn-copy") and @data-i="2"]');
3740
I.seeInPopup('copyRow');
3841
I.acceptPopup();
3942
I.click('.get-value');
4043
value = await I.grabValueFrom('.debug');
41-
assert.equal(value, '["A","B","B"]');
44+
assert.equal(value, '["B","C","A","A","D","E"]');
4245

4346
I.amAcceptingPopups();
4447
I.click('.json-editor-btntype-add');
4548
I.seeInPopup('addRow');
4649
I.acceptPopup();
4750
I.click('.get-value');
4851
value = await I.grabValueFrom('.debug');
49-
assert.equal(value, '["A","B","B",""]');
52+
assert.equal(value, '["B","C","A","A","D","E",""]');
5053

5154
// This test will fail when using Puppeteer due to the way Puppeteer handles popups.
5255
// Puppeteer apparently only sees the text in the last popup, so it doesn't see the
@@ -62,7 +65,7 @@ Scenario('should trigger array (table) editing triggers', async (I) => {
6265
I.acceptPopup();
6366
I.click('.get-value');
6467
value = await I.grabValueFrom('.debug');
65-
assert.equal(value, '["A","B","B"]');
68+
assert.equal(value, '["B","C","A","A","D","E"]');
6669

6770
// This test will fail when using Puppeteer due to the way Puppeteer handles popups.
6871
I.amAcceptingPopups();

tests/pages/table-move-events.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
document.querySelector('.get-value').addEventListener('click', function () {
3030
debug.value = JSON.stringify(editor.getValue());
3131
});
32-
editor.setValue(["A","B"]);
32+
editor.setValue(["A","B","C","D","E"]);
3333
editor.on('copyRow', function () {
3434
alert('copyRow');
3535
console.log('copyRow');

0 commit comments

Comments
 (0)