Skip to content

Commit 44f8559

Browse files
authored
Merge pull request #65 from anbui1510/cursor/week-grid-freeze-align-2256
fix(F-213): hết lệch hàng cột Bác sĩ vs lưới ngày (bỏ clone trái)
2 parents 6cec8e1 + 2215815 commit 44f8559

3 files changed

Lines changed: 135 additions & 142 deletions

File tree

‎src/components/SheetToolbar.jsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export function SheetToolbar({
280280
const settings = bridge.current.hot?.getSettings?.() || {};
281281
return {
282282
rows: Number(settings.fixedRowsTop) || 0,
283-
cols: Number(settings.fixedColumnsStart ?? settings.fixedColumnsLeft) || 0
283+
cols: Number(settings.stickyColumnsStart ?? settings.fixedColumnsStart ?? settings.fixedColumnsLeft) || 0
284284
};
285285
};
286286

@@ -305,7 +305,9 @@ export function SheetToolbar({
305305
const current = readFreeze();
306306
const rows = Math.max(0, Math.min(patch.rows ?? current.rows, hot.countRows()));
307307
const cols = Math.max(0, Math.min(patch.cols ?? current.cols, hot.countCols()));
308-
hot.updateSettings({ fixedRowsTop: rows, fixedColumnsStart: cols });
308+
// Lịch tuần ghim cột bằng sticky trên master (stickyColumnsStart), không bật clone trái.
309+
const colKey = hot.getSettings().stickyColumnsStart != null ? 'stickyColumnsStart' : 'fixedColumnsStart';
310+
hot.updateSettings({ fixedRowsTop: rows, [colKey]: cols });
309311
onFreeze?.({ rows, cols });
310312
setMenu(null);
311313
};

‎src/components/SheetView.jsx‎

Lines changed: 64 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,18 @@ function findTaskColor(taskList, raw, roomNames) {
4949
}
5050

5151
function syncWeekFrame(hot) {
52-
if (!hot?.rootElement) return;
52+
if (!hot?.rootElement || hot.isDestroyed) return;
5353
let body = 0;
5454
for (let index = 0; index < hot.countRows(); index += 1) body += hot.getRowHeight(index) || 30;
5555
let cols = 0;
5656
for (let index = 0; index < hot.countCols(); index += 1) cols += hot.getColWidth(index) || 0;
5757
const zoom = parseFloat(hot.rootElement.style.zoom) || 1;
5858
const thead = hot.rootElement.querySelector('.ht_master .htCore thead');
5959
const measured = thead ? Math.ceil(thead.getBoundingClientRect().height / zoom) : 0;
60-
const leftClone = hot.rootElement.querySelector('.ht_clone_left, .ht_clone_inline_start');
61-
const leftCloneWidth = leftClone ? Math.ceil(leftClone.getBoundingClientRect().width / zoom) : 0;
62-
// When rowHeaders are off, left clone is only frozen data cols — do not add a fake gutter.
63-
const gutter = WEEK_ROW_HEADERS ? Math.max(leftCloneWidth, WEEK_ROW_HEADER_WIDTH) : 0;
6460
const table = hot.rootElement.querySelector('.ht_master .htCore');
6561
const tableWidth = table ? Math.ceil(table.getBoundingClientRect().width / zoom) : 0;
6662
const height = body + Math.max(measured, 48) + 4;
67-
const width = Math.max(cols + gutter + 4, tableWidth);
63+
const width = Math.max(cols + 4, tableWidth);
6864
const current = hot.getSettings();
6965
const next = {};
7066
if (current.height !== height) next.height = height;
@@ -76,17 +72,14 @@ const NAME_COL_WIDTH = 78;
7672
const BALANCE_COL_WIDTH = 48;
7773
const SESSION_COL_WIDTH = 96;
7874
const NOTE_COL_WIDTH = 220;
79-
/** Week sheet: no Handsontable row-index gutter (1…n); freeze starts at Bác sĩ. */
80-
const WEEK_ROW_HEADERS = false;
81-
const WEEK_ROW_HEADER_WIDTH = WEEK_ROW_HEADERS ? 50 : 0;
82-
const WEEK_DEFAULT_FREEZE_COLS = 1; // cột BÁC SĨ
83-
/** Nested col headers: hàng thứ/ngày + hàng buổi — luôn ghim khi cuộn khung sheet. */
84-
const WEEK_PIN_HEADER_ROWS = 2;
75+
/** Cột BÁC SĨ luôn ghim; sheet chưa chọn ghim cột thì ghim BÁC SĨ + BÙ + PHÉP. */
76+
const WEEK_MIN_PIN_COLS = 1;
77+
const WEEK_DEFAULT_PIN_COLS = FIXED_COLS;
8578

8679
function dayColumnWidth(viewport, sessionCount, zoom) {
8780
const scale = zoom > 0 ? zoom : 1;
8881
const count = Math.max(1, sessionCount);
89-
const available = viewport / scale - WEEK_ROW_HEADER_WIDTH - NAME_COL_WIDTH - BALANCE_COL_WIDTH * 2;
82+
const available = viewport / scale - NAME_COL_WIDTH - BALANCE_COL_WIDTH * 2;
9083
return Math.max(64, Math.floor(available / count));
9184
}
9285

@@ -216,104 +209,47 @@ function weekScrollParents(root) {
216209
return [...nodes];
217210
}
218211

212+
/**
213+
* Không dùng ht_clone_left: clone là bảng riêng → chiều cao hàng lệch master (bug lệch BS).
214+
* Ghim cột bằng ô trên cùng một bảng master: translateX(--week-pin-x) theo cuộn .sheet-scroll.
215+
*/
216+
function weekPinCount(hot) {
217+
return Math.max(0, Number(hot?.getSettings?.().stickyColumnsStart) || 0);
218+
}
219+
220+
function weekPinOffset(hot, col) {
221+
let left = 0;
222+
for (let index = 0; index < col; index += 1) left += hot.getColWidth(index) || 0;
223+
return left;
224+
}
225+
226+
/** Gắn class ghim; HOT reset ô mỗi render nên gọi từ renderer / afterGetColHeader. */
227+
function pinWeekCell(hot, cell, col, span = 1) {
228+
if (!cell) return;
229+
const count = weekPinCount(hot);
230+
cell.classList.remove('week-pin-col', 'week-pin-edge');
231+
cell.style.removeProperty('left');
232+
if (col < 0 || count <= 0 || col >= count) return;
233+
cell.classList.add('week-pin-col');
234+
if (col + Math.max(1, span) >= count) cell.classList.add('week-pin-edge');
235+
}
236+
219237
function stickWeekFreeze(hot) {
220238
const root = hot?.rootElement;
221239
const scroller = root?.closest('.sheet-scroll');
222-
const left = root?.querySelector('.ht_clone_inline_start, .ht_clone_left');
223240
const top = root?.querySelector('.ht_clone_top');
224-
const corner = root?.querySelector('.ht_clone_top_inline_start_corner, .ht_clone_top_left_corner');
225-
if (!root || !scroller?.classList.contains('is-open') || !left || !top || !corner) return;
241+
if (!root || hot.isDestroyed || !scroller?.classList.contains('is-open')) return;
226242
const zoom = parseFloat(root.style.zoom) || 1;
227243
const rootRect = root.getBoundingClientRect();
228244
const pinBox = scroller.getBoundingClientRect();
229-
const shiftX = Math.round(Math.max(0, Math.min((pinBox.left - rootRect.left) / zoom, root.offsetWidth - left.offsetWidth)));
245+
const pinCols = weekPinCount(hot);
246+
const maxX = Math.max(0, weekPinOffset(hot, hot.countCols()) - weekPinOffset(hot, pinCols));
247+
const shiftX = Math.round(Math.max(0, Math.min((pinBox.left - rootRect.left) / zoom, maxX)));
230248
const shiftY = Math.round(Math.max(0, (pinBox.top - rootRect.top) / zoom));
231-
left.style.transform = shiftX ? `translate3d(${shiftX}px, 0, 0)` : '';
232-
top.style.transform = shiftY ? `translate3d(0, ${shiftY}px, 0)` : '';
233-
const cornerX = shiftX ? `${shiftX}px` : '0';
234-
const cornerY = shiftY ? `${shiftY}px` : '0';
235-
corner.style.transform = (shiftX || shiftY) ? `translate3d(${cornerX}, ${cornerY}, 0)` : '';
249+
root.style.setProperty('--week-pin-x', `${shiftX}px`);
250+
if (top) top.style.transform = shiftY ? `translate3d(0, ${shiftY}px, 0)` : '';
236251
root.classList.toggle('is-pin-x', shiftX > 0);
237252
root.classList.toggle('is-pin-y', shiftY > 0);
238-
root.dataset.weekPinHeaders = String(WEEK_PIN_HEADER_ROWS);
239-
}
240-
241-
function applyHotRowHeight(rowEl, heightPx) {
242-
if (!rowEl || heightPx <= 0) return;
243-
const next = `${Math.round(heightPx)}px`;
244-
rowEl.style.height = next;
245-
rowEl.querySelectorAll('th, td').forEach((cell) => {
246-
cell.style.height = next;
247-
cell.style.boxSizing = 'border-box';
248-
});
249-
}
250-
251-
function syncHotRowHeights(hot, sourceRows, targetRows, rowOffset = 0) {
252-
const plugin = hot?.getPlugin?.('manualRowResize');
253-
sourceRows.forEach((source, index) => {
254-
const target = targetRows[index];
255-
if (!target) return;
256-
const row = rowOffset + index;
257-
const fromHot = Number(hot.getRowHeight?.(row)) || 0;
258-
const measured = Math.ceil(source.getBoundingClientRect().height);
259-
const peer = Math.ceil(target.getBoundingClientRect().height);
260-
const height = Math.max(fromHot, measured, peer, 30);
261-
if (plugin?.setManualSize && hot.getRowHeight(row) !== height) {
262-
plugin.setManualSize(row, height);
263-
}
264-
applyHotRowHeight(source, height);
265-
applyHotRowHeight(target, height);
266-
});
267-
}
268-
269-
function attachWeekBodyScrollSync(hot) {
270-
const root = hot?.rootElement;
271-
if (!root) return () => {};
272-
const masterHolder = root.querySelector('.ht_master .wtHolder');
273-
const leftHolder = root.querySelector('.ht_clone_left .wtHolder, .ht_clone_inline_start .wtHolder');
274-
if (!masterHolder || !leftHolder) return () => {};
275-
let frame = 0;
276-
const onScroll = () => {
277-
cancelAnimationFrame(frame);
278-
frame = requestAnimationFrame(() => {
279-
if (leftHolder.scrollTop !== masterHolder.scrollTop) {
280-
leftHolder.scrollTop = masterHolder.scrollTop;
281-
}
282-
});
283-
};
284-
masterHolder.addEventListener('scroll', onScroll, { passive: true });
285-
return () => {
286-
cancelAnimationFrame(frame);
287-
masterHolder.removeEventListener('scroll', onScroll);
288-
};
289-
}
290-
291-
function alignFrozenPanes(hot) {
292-
const root = hot?.rootElement;
293-
if (!root) return;
294-
stickWeekFreeze(hot);
295-
const master = root.querySelector('.ht_master .htCore');
296-
const leftPaneCore = root.querySelector('.ht_clone_left .htCore, .ht_clone_inline_start .htCore');
297-
const topPaneCore = root.querySelector('.ht_clone_top .htCore');
298-
const corner = root.querySelector('.ht_clone_top_left_corner .htCore, .ht_clone_top_inline_start_corner .htCore');
299-
const leftPane = root.querySelector('.ht_clone_left, .ht_clone_inline_start');
300-
const paneWidth = leftPane ? leftPane.getBoundingClientRect().width : 0;
301-
root.style.setProperty('--freeze-pane', `${Math.round(paneWidth)}px`);
302-
if (!master || !leftPaneCore) return;
303-
304-
const masterHead = [...master.querySelectorAll('thead tr')];
305-
syncHotRowHeights(hot, masterHead, [...(corner?.querySelectorAll('thead tr') || [])]);
306-
syncHotRowHeights(hot, masterHead, [...(topPaneCore?.querySelectorAll('thead tr') || [])]);
307-
308-
const masterBody = [...master.querySelectorAll('tbody tr')];
309-
const leftBody = [...leftPaneCore.querySelectorAll('tbody tr')];
310-
syncHotRowHeights(hot, masterBody, leftBody, 0);
311-
312-
const masterHolder = root.querySelector('.ht_master .wtHolder');
313-
const leftHolder = root.querySelector('.ht_clone_left .wtHolder, .ht_clone_inline_start .wtHolder');
314-
if (masterHolder && leftHolder && leftHolder.scrollTop !== masterHolder.scrollTop) {
315-
leftHolder.scrollTop = masterHolder.scrollTop;
316-
}
317253
}
318254

319255
export function SheetView({
@@ -614,11 +550,11 @@ export function SheetView({
614550
const noteCol = FIXED_COLS + 7 * sessions.length;
615551
const weekWidth = noteCol + 1;
616552
const freezeRows = Math.max(0, Number(scheduleData.freeze?.week?.rows) || 0);
617-
// Default / minimum: freeze cột BÁC SĨ (index 0). Stored 0 vẫn nâng lên 1.
618-
const freezeCols = Math.max(
619-
WEEK_DEFAULT_FREEZE_COLS,
620-
Number(scheduleData.freeze?.week?.cols) || WEEK_DEFAULT_FREEZE_COLS
621-
);
553+
// Stored 0 vẫn nâng lên 1 (BÁC SĨ).
554+
const storedFreezeCols = scheduleData.freeze?.week?.cols;
555+
const freezeCols = storedFreezeCols == null
556+
? WEEK_DEFAULT_PIN_COLS
557+
: Math.max(WEEK_MIN_PIN_COLS, Number(storedFreezeCols) || 0);
622558
const alertLine = alertRows.length
623559
? buildSessionAlertRow(alertRows, {
624560
days, sessions, weekWidth, noteCol, leaveEntries, studyEntries, grid, staffList, taskList
@@ -715,13 +651,15 @@ export function SheetView({
715651
const hot = new window.Handsontable(containerRef.current, {
716652
data: weekData,
717653
nestedHeaders: [topHeaders, subHeaders],
718-
rowHeaders: WEEK_ROW_HEADERS,
654+
// rowHeaders hoặc fixedColumnsStart > 0 làm HOT dựng lại clone trái (lệch hàng với master).
655+
rowHeaders: false,
719656
height: wrappedHeights.reduce((sum, row) => sum + row, 0) + 56,
720-
width: colWidths.reduce((sum, col) => sum + col, 0) + WEEK_ROW_HEADER_WIDTH + 4,
657+
width: colWidths.reduce((sum, col) => sum + col, 0) + 4,
721658
licenseKey: HANDSONTABLE_LICENSE_KEY,
722659
stretchH: 'none',
723660
fixedRowsTop: freezeRows,
724-
fixedColumnsStart: freezeCols,
661+
fixedColumnsStart: 0,
662+
stickyColumnsStart: freezeCols,
725663
wordWrap: false,
726664
autoRowSize: false,
727665
autoColumnSize: false,
@@ -796,6 +734,7 @@ export function SheetView({
796734
td.style.fontWeight = '';
797735
td.classList.remove('week-warning', 'week-task', 'week-cath-name');
798736
td.removeAttribute('title');
737+
pinWeekCell(instance, td, col);
799738
if (liveAlert) {
800739
const text = String(instance.getDataAtCell(row, col) || '').trim();
801740
if (text) td.classList.add('week-warning');
@@ -851,6 +790,7 @@ export function SheetView({
851790
TH.style.display = 'none';
852791
TH.textContent = '';
853792
TH.classList.remove('ht-span-gap');
793+
pinWeekCell(this, TH, col, TH.colSpan || 1);
854794
return;
855795
}
856796
TH.rowSpan = 2;
@@ -861,17 +801,20 @@ export function SheetView({
861801
span.className = 'col-title col-title--merged';
862802
span.textContent = label;
863803
TH.appendChild(span);
804+
pinWeekCell(this, TH, col, TH.colSpan || 1);
864805
return;
865806
}
866807
if (headerLevel === 0 && col >= FIXED_COLS && col < noteCol && (col - FIXED_COLS) % sessions.length === 0) {
867808
const day = weekDaysRef.current[Math.floor((col - FIXED_COLS) / sessions.length)];
868-
if (!day?.dateStr || String(day.dateStr).includes('NaN')) return;
869-
TH.classList.add('ht-day-head');
870-
TH.innerHTML = `<div class="day-head">${day.dayName} (${day.dateStr})</div>`;
809+
if (day?.dateStr && !String(day.dateStr).includes('NaN')) {
810+
TH.classList.add('ht-day-head');
811+
TH.innerHTML = `<div class="day-head">${day.dayName} (${day.dateStr})</div>`;
812+
}
871813
}
872814
if (headerLevel === 1 && col >= FIXED_COLS && col < noteCol) {
873815
TH.classList.add('ht-session-head');
874816
}
817+
pinWeekCell(this, TH, col, TH.colSpan || 1);
875818
},
876819
afterChange(changes, source) {
877820
if (readOnlyRef.current || source === 'loadData' || !changes) return;
@@ -925,7 +868,7 @@ export function SheetView({
925868
if (changed) this.render();
926869
requestAnimationFrame(() => {
927870
syncWeekFrame(this);
928-
alignFrozenPanes(this);
871+
stickWeekFreeze(this);
929872
});
930873
},
931874
afterRowResize() {
@@ -934,16 +877,12 @@ export function SheetView({
934877
weekSizeRef.current.rows = heights;
935878
requestAnimationFrame(() => {
936879
syncWeekFrame(this);
937-
alignFrozenPanes(this);
880+
stickWeekFreeze(this);
938881
});
939882
},
940883
afterViewRender() {
941-
if (this.__alignFrame) return;
942-
const hot = this;
943-
this.__alignFrame = requestAnimationFrame(() => {
944-
hot.__alignFrame = 0;
945-
alignFrozenPanes(hot);
946-
});
884+
// HOT xóa transform của clone top mỗi lần vẽ — ghim lại ngay, không đợi frame sau.
885+
stickWeekFreeze(this);
947886
},
948887
afterSelectionEnd() {
949888
bridge.current.hot = this;
@@ -992,29 +931,24 @@ export function SheetView({
992931
let pinFrame = 0;
993932
const onFrameScroll = () => {
994933
cancelAnimationFrame(pinFrame);
995-
pinFrame = requestAnimationFrame(() => {
996-
stickWeekFreeze(hot);
997-
alignFrozenPanes(hot);
998-
});
934+
pinFrame = requestAnimationFrame(() => stickWeekFreeze(hot));
999935
};
1000936
const scrollers = frame ? weekScrollParents(frame) : [];
1001937
scrollers.forEach((node) => node.addEventListener('scroll', onFrameScroll, { passive: true }));
1002938
requestAnimationFrame(() => {
1003939
refitColumns();
1004940
syncWeekFrame(hot);
1005-
alignFrozenPanes(hot);
941+
stickWeekFreeze(hot);
1006942
});
1007943
const detachDrag = attachCellDragSwap(hot, {
1008944
describe: (row, col) => describeRef.current(row, col),
1009945
onPatchStyles: (prev, next) => {
1010946
pushFormatUndo(hot, prev, next, (map) => patchRef.current(map));
1011947
}
1012948
});
1013-
const detachBodyScroll = attachWeekBodyScrollSync(hot);
1014949
return () => {
1015950
detachDrop();
1016951
detachDrag();
1017-
detachBodyScroll();
1018952
stopFit();
1019953
cancelAnimationFrame(fitFrame);
1020954
observer.disconnect();
@@ -1047,8 +981,10 @@ export function SheetView({
1047981
}, weekSizeRef.current.rows);
1048982
const changed = applyWeekRowHeights(hot, heights, weekSizeRef.current.rows);
1049983
const align = () => {
984+
if (hot.isDestroyed) return;
1050985
if (changed) hot.render();
1051-
alignFrozenPanes(hot);
986+
syncWeekFrame(hot);
987+
stickWeekFreeze(hot);
1052988
};
1053989
requestAnimationFrame(() => requestAnimationFrame(align));
1054990
// eslint-disable-next-line react-hooks/exhaustive-deps

0 commit comments

Comments
 (0)