fix: 保持系统化待办操作后的浏览位置
This commit is contained in:
parent
79fad431df
commit
482825afc2
@ -1099,6 +1099,166 @@
|
||||
</div>
|
||||
</dialog>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
var storageKey = 'caiwu-systemization-todo-grid-scroll';
|
||||
window.CaiWuSystemizationTodoScroll = {save: save, restore: restore, showHighlightedRow: showHighlightedRow};
|
||||
|
||||
function normalizedUrl(value) {
|
||||
var url = new URL(value, window.location.origin);
|
||||
url.searchParams.delete('highlight');
|
||||
var entries = Array.from(url.searchParams.entries()).sort(function (left, right) {
|
||||
return left[0].localeCompare(right[0]) || left[1].localeCompare(right[1]);
|
||||
});
|
||||
url.search = '';
|
||||
entries.forEach(function (entry) {
|
||||
url.searchParams.append(entry[0], entry[1]);
|
||||
});
|
||||
return url.pathname + url.search;
|
||||
}
|
||||
|
||||
function rowById(id) {
|
||||
var value = String(id || '');
|
||||
if (!value) return null;
|
||||
var row = document.getElementById('sys-todo-row-' + value);
|
||||
return row && row.matches('.sys-todo-table tbody tr[data-sys-todo-grid-row-id]')
|
||||
? row
|
||||
: null;
|
||||
}
|
||||
|
||||
function selectedRowId() {
|
||||
var active = document.activeElement;
|
||||
var row = active && active.closest
|
||||
? active.closest('tr[data-sys-todo-grid-row-id]')
|
||||
: null;
|
||||
if (!row) {
|
||||
row = document.querySelector(
|
||||
'.sys-todo-table tr:has(.sys-todo-grid-value.is-editing),'
|
||||
+ ' .sys-todo-table tr:has(.sys-todo-grid-value.is-selected)'
|
||||
);
|
||||
}
|
||||
return row ? row.getAttribute('data-sys-todo-grid-row-id') || '' : '';
|
||||
}
|
||||
|
||||
function save(options) {
|
||||
options = options || {};
|
||||
var tableWrap = document.querySelector('[data-sys-todo-table-wrap]');
|
||||
if (!tableWrap) return;
|
||||
var hasExplicitRow = Object.prototype.hasOwnProperty.call(options, 'rowId');
|
||||
var rowId = hasExplicitRow ? String(options.rowId || '') : selectedRowId();
|
||||
var row = rowById(rowId);
|
||||
var tableRect = tableWrap.getBoundingClientRect();
|
||||
var rowOffset = row
|
||||
? Math.round(row.getBoundingClientRect().top - tableRect.top)
|
||||
: null;
|
||||
|
||||
try {
|
||||
window.sessionStorage.setItem(storageKey, JSON.stringify({
|
||||
url: window.location.href,
|
||||
pageY: window.scrollY,
|
||||
tableX: tableWrap.scrollLeft,
|
||||
tableY: tableWrap.scrollTop,
|
||||
rowId: rowId,
|
||||
rowOffset: rowOffset
|
||||
}));
|
||||
} catch (error) {
|
||||
// The operation still succeeds when session storage is blocked.
|
||||
}
|
||||
}
|
||||
|
||||
function positionRow(row, tableWrap, rowOffset) {
|
||||
if (!row || !tableWrap) return;
|
||||
var tableRect = tableWrap.getBoundingClientRect();
|
||||
var rowRect = row.getBoundingClientRect();
|
||||
var header = tableWrap.querySelector('thead');
|
||||
var headerHeight = header ? header.getBoundingClientRect().height : 0;
|
||||
var minOffset = headerHeight + 4;
|
||||
var maxOffset = Math.max(
|
||||
minOffset,
|
||||
tableWrap.clientHeight - rowRect.height - 4
|
||||
);
|
||||
var desiredOffset = rowOffset === null
|
||||
? Math.max(0, (tableWrap.clientHeight - rowRect.height) / 2)
|
||||
: Number(rowOffset);
|
||||
desiredOffset = Math.max(minOffset, Math.min(maxOffset, desiredOffset));
|
||||
var currentOffset = rowRect.top - tableRect.top;
|
||||
tableWrap.scrollTop += Math.round(currentOffset - desiredOffset);
|
||||
}
|
||||
|
||||
function restore() {
|
||||
var snapshot = null;
|
||||
try {
|
||||
snapshot = JSON.parse(window.sessionStorage.getItem(storageKey) || 'null');
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
if (!snapshot || normalizedUrl(snapshot.url) !== normalizedUrl(window.location.href)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
window.sessionStorage.removeItem(storageKey);
|
||||
} catch (error) {
|
||||
// Continue with the in-memory snapshot.
|
||||
}
|
||||
|
||||
var tableWrap = document.querySelector('[data-sys-todo-table-wrap]');
|
||||
if (!tableWrap) return false;
|
||||
var apply = function () {
|
||||
tableWrap.scrollLeft = Number(snapshot.tableX || 0);
|
||||
tableWrap.scrollTop = Number(snapshot.tableY || 0);
|
||||
window.scrollTo(0, Number(snapshot.pageY || 0));
|
||||
|
||||
var highlighted = document.querySelector(
|
||||
'.sys-todo-table tbody tr.is-highlighted[data-sys-todo-grid-row-id]'
|
||||
);
|
||||
var storedRow = rowById(snapshot.rowId);
|
||||
var target = highlighted || storedRow;
|
||||
if (!target) return;
|
||||
|
||||
var preserveOffset = storedRow === target
|
||||
&& snapshot.rowOffset !== null
|
||||
&& Number.isFinite(Number(snapshot.rowOffset));
|
||||
positionRow(
|
||||
target,
|
||||
tableWrap,
|
||||
preserveOffset ? Number(snapshot.rowOffset) : null
|
||||
);
|
||||
};
|
||||
|
||||
window.requestAnimationFrame(function () {
|
||||
window.requestAnimationFrame(apply);
|
||||
});
|
||||
window.setTimeout(apply, 180);
|
||||
if (document.readyState === 'complete') {
|
||||
window.setTimeout(apply, 0);
|
||||
} else {
|
||||
window.addEventListener('load', apply, {once: true});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function showHighlightedRow() {
|
||||
var tableWrap = document.querySelector('[data-sys-todo-table-wrap]');
|
||||
var highlighted = document.querySelector(
|
||||
'.sys-todo-table tbody tr.is-highlighted[data-sys-todo-grid-row-id]'
|
||||
);
|
||||
if (!tableWrap || !highlighted) return;
|
||||
window.setTimeout(function () {
|
||||
positionRow(highlighted, tableWrap, null);
|
||||
}, 180);
|
||||
}
|
||||
|
||||
try {
|
||||
if (window.history && 'scrollRestoration' in window.history) {
|
||||
window.history.scrollRestoration = 'manual';
|
||||
}
|
||||
} catch (error) {
|
||||
// Browser-managed page scroll restoration is optional.
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
var dialog = document.getElementById('sysTodoDialog');
|
||||
@ -1121,6 +1281,7 @@
|
||||
var imageDelete = imageViewer.querySelector('[data-sys-todo-grid-delete-attachment]');
|
||||
var imageGalleryItems = [];
|
||||
var imageGalleryIndex = 0;
|
||||
var imageSourceRowId = '';
|
||||
var categorySelect = dialog.querySelector('[data-sys-todo-category-select]');
|
||||
var categoryValue = dialog.querySelector('[data-sys-todo-category-value]');
|
||||
var newCategoryWrap = dialog.querySelector('[data-sys-todo-new-category]');
|
||||
@ -1748,15 +1909,17 @@
|
||||
imageOpen.href = item.url;
|
||||
imageDelete.hidden = !item.delete_url;
|
||||
imageDelete.setAttribute('data-delete-url', item.delete_url || '');
|
||||
imageDelete.setAttribute('data-sys-todo-source-row-id', imageSourceRowId);
|
||||
imagePrevious.hidden = imageGalleryItems.length <= 1;
|
||||
imageNext.hidden = imageGalleryItems.length <= 1;
|
||||
}
|
||||
|
||||
function openImageViewerItems(items) {
|
||||
function openImageViewerItems(items, sourceRowId) {
|
||||
imageGalleryItems = Array.isArray(items) && items.length
|
||||
? items
|
||||
: [];
|
||||
imageGalleryIndex = 0;
|
||||
imageSourceRowId = String(sourceRowId || '');
|
||||
if (!imageGalleryItems.length) return;
|
||||
|
||||
showImageViewerItem();
|
||||
@ -1764,8 +1927,8 @@
|
||||
else imageViewer.setAttribute('open', '');
|
||||
}
|
||||
|
||||
function openImageViewer(url, name) {
|
||||
openImageViewerItems([{ url: url, name: name }]);
|
||||
function openImageViewer(url, name, sourceRowId) {
|
||||
openImageViewerItems([{ url: url, name: name }], sourceRowId || currentTodoId);
|
||||
}
|
||||
|
||||
function showPreviousImage() {
|
||||
@ -1791,6 +1954,8 @@
|
||||
imageNext.hidden = true;
|
||||
imageDelete.hidden = true;
|
||||
imageDelete.setAttribute('data-delete-url', '');
|
||||
imageDelete.setAttribute('data-sys-todo-source-row-id', '');
|
||||
imageSourceRowId = '';
|
||||
}
|
||||
|
||||
function openGroupDialog(type, value, count) {
|
||||
@ -2094,7 +2259,11 @@
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
var galleryPayload = decodePayload(gallery.getAttribute('data-sys-todo-image-gallery'));
|
||||
openImageViewerItems(galleryPayload.attachments || []);
|
||||
var galleryRow = gallery.closest('tr[data-sys-todo-grid-row-id]');
|
||||
openImageViewerItems(
|
||||
galleryPayload.attachments || [],
|
||||
galleryRow ? galleryRow.getAttribute('data-sys-todo-grid-row-id') || '' : ''
|
||||
);
|
||||
return;
|
||||
}
|
||||
var image = event.target.closest('[data-sys-todo-image-url]');
|
||||
@ -2102,7 +2271,8 @@
|
||||
event.preventDefault();
|
||||
openImageViewer(
|
||||
image.getAttribute('data-sys-todo-image-url'),
|
||||
image.getAttribute('data-sys-todo-image-name')
|
||||
image.getAttribute('data-sys-todo-image-name'),
|
||||
currentTodoId
|
||||
);
|
||||
return;
|
||||
}
|
||||
@ -2354,13 +2524,20 @@
|
||||
new ResizeObserver(scheduleScrollRailSync).observe(tableWrap);
|
||||
}
|
||||
scheduleScrollRailSync();
|
||||
var highlightedRow = document.querySelector('.sys-todo-table tbody tr.is-highlighted');
|
||||
if (highlightedRow) {
|
||||
window.setTimeout(function () {
|
||||
highlightedRow.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||
highlightedRow.focus({ preventScroll: true });
|
||||
}, 120);
|
||||
}
|
||||
form.addEventListener('submit', function () {
|
||||
if (window.CaiWuSystemizationTodoScroll) {
|
||||
window.CaiWuSystemizationTodoScroll.save({
|
||||
rowId: currentTodoId ? String(currentTodoId) : ''
|
||||
});
|
||||
}
|
||||
});
|
||||
groupForm.addEventListener('submit', function () {
|
||||
if (window.CaiWuSystemizationTodoScroll) {
|
||||
window.CaiWuSystemizationTodoScroll.save({
|
||||
rowId: currentGroupTodoId ? String(currentGroupTodoId) : ''
|
||||
});
|
||||
}
|
||||
});
|
||||
screenshotInput.addEventListener('change', function () {
|
||||
if (screenshotInput.files.length) {
|
||||
uploadNote.textContent = '已选择 ' + screenshotInput.files.length + ' 张:' + selectedFilesLabel() + '。保存后上传。';
|
||||
@ -2398,7 +2575,7 @@
|
||||
var pointerDrag = null;
|
||||
var reloadAfterSave = false;
|
||||
var uploadUrl = '';
|
||||
var scrollStorageKey = 'caiwu-systemization-todo-grid-scroll';
|
||||
var uploadRowId = '';
|
||||
var pasteFields = [
|
||||
'category',
|
||||
'section',
|
||||
@ -2410,28 +2587,9 @@
|
||||
'progress'
|
||||
];
|
||||
|
||||
function scrollUrlKey(value) {
|
||||
var url = new URL(value, window.location.origin);
|
||||
url.searchParams.delete('highlight');
|
||||
var entries = Array.from(url.searchParams.entries()).sort(function (left, right) {
|
||||
return left[0].localeCompare(right[0]) || left[1].localeCompare(right[1]);
|
||||
});
|
||||
url.search = '';
|
||||
entries.forEach(function (entry) {
|
||||
url.searchParams.append(entry[0], entry[1]);
|
||||
});
|
||||
return url.pathname + url.search;
|
||||
}
|
||||
|
||||
function saveScrollSnapshot() {
|
||||
try {
|
||||
window.sessionStorage.setItem(scrollStorageKey, JSON.stringify({
|
||||
url: window.location.href,
|
||||
pageY: window.scrollY,
|
||||
tableX: document.querySelector('[data-sys-todo-table-wrap]').scrollLeft
|
||||
}));
|
||||
} catch (error) {
|
||||
// Deletion still succeeds when session storage is blocked.
|
||||
if (window.CaiWuSystemizationTodoScroll) {
|
||||
window.CaiWuSystemizationTodoScroll.save();
|
||||
}
|
||||
}
|
||||
|
||||
@ -3135,6 +3293,9 @@
|
||||
}
|
||||
|
||||
function openHighlighted(id) {
|
||||
if (window.CaiWuSystemizationTodoScroll) {
|
||||
window.CaiWuSystemizationTodoScroll.save({rowId: ''});
|
||||
}
|
||||
var target = new URL(window.location.href);
|
||||
target.searchParams.set('highlight', id);
|
||||
window.location.href = target.toString();
|
||||
@ -3179,6 +3340,8 @@
|
||||
setGridState('请等待当前修改保存完成后再上传截图。', 'error');
|
||||
return;
|
||||
}
|
||||
var row = button.closest('tr[data-sys-todo-grid-row-id]');
|
||||
uploadRowId = row ? row.getAttribute('data-sys-todo-grid-row-id') || '' : '';
|
||||
uploadUrl = button.getAttribute('data-sys-todo-grid-upload-url');
|
||||
uploadInput.value = '';
|
||||
uploadInput.click();
|
||||
@ -3209,6 +3372,9 @@
|
||||
throw new Error(validation || payload.message || '上传失败。');
|
||||
}
|
||||
setGridState('截图上传完成', 'success');
|
||||
if (window.CaiWuSystemizationTodoScroll) {
|
||||
window.CaiWuSystemizationTodoScroll.save({rowId: uploadRowId});
|
||||
}
|
||||
window.setTimeout(function () { window.location.reload(); }, 180);
|
||||
} catch (error) {
|
||||
setGridState(error.message || '上传失败。', 'error');
|
||||
@ -3234,6 +3400,10 @@
|
||||
if (!response.ok) {
|
||||
throw new Error(payload.message || '删除截图失败。');
|
||||
}
|
||||
var sourceRowId = deleteAttachmentButton.getAttribute('data-sys-todo-source-row-id') || '';
|
||||
if (window.CaiWuSystemizationTodoScroll) {
|
||||
window.CaiWuSystemizationTodoScroll.save({rowId: sourceRowId});
|
||||
}
|
||||
window.location.reload();
|
||||
} catch (error) {
|
||||
setGridState(error.message || '删除截图失败。', 'error');
|
||||
@ -3286,6 +3456,12 @@
|
||||
: '';
|
||||
throw new Error(validation || payload.message || '移动失败。');
|
||||
}
|
||||
var row = button.closest('tr[data-sys-todo-grid-row-id]');
|
||||
if (window.CaiWuSystemizationTodoScroll) {
|
||||
window.CaiWuSystemizationTodoScroll.save({
|
||||
rowId: row ? row.getAttribute('data-sys-todo-grid-row-id') || '' : ''
|
||||
});
|
||||
}
|
||||
window.location.reload();
|
||||
}).catch(function (error) {
|
||||
setGridState(error.message || '移动失败。', 'error');
|
||||
@ -3484,17 +3660,13 @@
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
var storedScroll = JSON.parse(window.sessionStorage.getItem(scrollStorageKey) || 'null');
|
||||
if (storedScroll && scrollUrlKey(storedScroll.url) === scrollUrlKey(window.location.href)) {
|
||||
window.sessionStorage.removeItem(scrollStorageKey);
|
||||
window.setTimeout(function () {
|
||||
document.querySelector('[data-sys-todo-table-wrap]').scrollLeft = Number(storedScroll.tableX || 0);
|
||||
window.scrollTo(0, Number(storedScroll.pageY || 0));
|
||||
}, 160);
|
||||
if (
|
||||
!window.CaiWuSystemizationTodoScroll
|
||||
|| !window.CaiWuSystemizationTodoScroll.restore()
|
||||
) {
|
||||
if (window.CaiWuSystemizationTodoScroll) {
|
||||
window.CaiWuSystemizationTodoScroll.showHighlightedRow();
|
||||
}
|
||||
} catch (error) {
|
||||
// Scroll restoration is optional.
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
||||
@ -85,7 +85,16 @@ class SystemizationTodosTest extends FeatureTestCase
|
||||
->assertSee('tr:hover [data-sys-todo-grid-upload]', false)
|
||||
->assertSee('tr:has(.sys-todo-grid-value.is-selected) [data-sys-todo-grid-upload]', false)
|
||||
->assertSee("deleteForm.addEventListener('submit', saveScrollSnapshot)", false)
|
||||
->assertSee('scrollUrlKey(storedScroll.url) === scrollUrlKey(window.location.href)', false)
|
||||
->assertSee('normalizedUrl(snapshot.url) !== normalizedUrl(window.location.href)', false)
|
||||
->assertSee('tableY: tableWrap.scrollTop', false)
|
||||
->assertSee('tableWrap.scrollTop = Number(snapshot.tableY || 0)', false)
|
||||
->assertSee('rowOffset: rowOffset', false)
|
||||
->assertSee('positionRow(', false)
|
||||
->assertSee("window.CaiWuSystemizationTodoScroll.save({rowId: uploadRowId})", false)
|
||||
->assertSee("window.CaiWuSystemizationTodoScroll.save({rowId: ''})", false)
|
||||
->assertSee("form.addEventListener('submit', function ()", false)
|
||||
->assertSee("groupForm.addEventListener('submit', function ()", false)
|
||||
->assertDontSee("scrollIntoView({ behavior: 'smooth'", false)
|
||||
->assertSee("if (field === 'start_date' || field === 'due_date')", false)
|
||||
->assertSee('sys-todo-grid-date-editor__pending', false)
|
||||
->assertSee('window.CaiWuDatePicker.setup(wrapper)', false)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user