Preserve cursor with remote changes in kanban

pull/1/head
yflory 5 years ago
parent 6b81bba3a5
commit 63ea882545

@ -390,6 +390,75 @@ define([
$container.addClass('cp-app-readonly'); $container.addClass('cp-app-readonly');
}); });
var getSelectedElement = function () {
var node = document.getSelection().anchorNode;
return (node.nodeType === 3 ? node.parentNode : node);
};
var getCursor = function () {
if (!kanban || !kanban.inEditMode) { return; }
try {
var el = getSelectedElement();
var input = $(el).is('input') ? el : $(el).find('input')[0];
var $item = $(el).closest('.kanban-item');
var pos = kanban.findElementPosition($item[0]);
var board = $item.closest('.kanban-board').attr('data-id');
var val = ($(input).val && $(input).val()) || '';
var start = input.selectionStart;
var end = input.selectionEnd;
var boardEl = kanban.options.boards.find(function (b) {
return b.id === board;
});
var oldVal = boardEl.item[pos] || {};
return {
board: board,
pos: pos,
value: val,
start: start,
end: end,
oldValue: oldVal.title
};
} catch (e) {
return {};
}
};
var restoreCursor = function (data) {
try {
var boardEl = kanban.options.boards.find(function (b) {
return b.id === data.board;
});
if (!boardEl) { return; }
// An item was added: add a new item
if (!data.oldValue) {
var $board = $('.kanban-board[data-id="'+data.board+'"');
$board.find('.kanban-title-button.fa-plus').click();
var $newInput = $board.find('.kanban-item:last-child input');
$newInput.val(data.value);
$newInput[0].selectionStart = data.start;
$newInput[0].selectionEnd = data.end;
return;
}
// An item was edited: click on the correct item
var newVal = boardEl.item[data.pos];
if (!newVal || newVal.title !== data.oldValue) { return; }
var $el = $('.kanban-board[data-id="' + data.board + '"]')
.find('.kanban-item:nth-child('+(data.pos + 1)+')');
$el.find('.kanban-item-text').click();
var $input = $el.find('input');
if ($input.length) {
$input.val(data.value);
$input[0].selectionStart = data.start;
$input[0].selectionEnd = data.end;
}
} catch (e) {
return;
}
};
framework.onContentUpdate(function (newContent) { framework.onContentUpdate(function (newContent) {
// Init if needed // Init if needed
if (!kanban) { if (!kanban) {
@ -404,11 +473,12 @@ define([
var remoteContent = newContent.content; var remoteContent = newContent.content;
if (Sortify(currentContent) !== Sortify(remoteContent)) { if (Sortify(currentContent) !== Sortify(remoteContent)) {
// reinit kanban (TODO: optimize to diff only) var cursor = getCursor();
verbose("Content is different.. Applying content"); verbose("Content is different.. Applying content");
kanban.setBoards(remoteContent); kanban.setBoards(remoteContent);
kanban.inEditMode = false; kanban.inEditMode = false;
addRemoveItemButton(framework, kanban); addRemoveItemButton(framework, kanban);
restoreCursor(cursor);
} }
}); });

Loading…
Cancel
Save