diff --git a/src/controllers/cellDatePickerCtrl.js b/src/controllers/cellDatePickerCtrl.js index bee9a21f0..1ba7d9c50 100644 --- a/src/controllers/cellDatePickerCtrl.js +++ b/src/controllers/cellDatePickerCtrl.js @@ -3,16 +3,48 @@ import formula from '../global/formula'; import Store from '../store'; import flatpickr from 'flatpickr' import dayjs from "dayjs"; +import { update, datenum_local } from '../global/format'; +import { setCellValue, setCellFormat } from '../global/api'; + +const fitFormat = (formatStr) => { + let dateFormat = formatStr.replace(/y/g, 'Y'); + dateFormat = dateFormat.replace(/d/g, 'D'); + dateFormat = dateFormat.replace(/h/g, 'H'); + + dateFormat = dateFormat.replace(/上午\/下午/g, 'A'); + dateFormat = dateFormat.replace(/上午/g, 'A'); + dateFormat = dateFormat.replace(/下午/g, 'A'); + + dateFormat = dateFormat.replace(/AM\/PM/g, 'A'); + dateFormat = dateFormat.replace(/AM/g, 'A'); + dateFormat = dateFormat.replace(/PM/g, 'A'); + dateFormat = dateFormat.replace(/\"/g, ''); + + if (dateFormat.includes('A')) { + dateFormat = dateFormat.replace(/H/g, 'h'); + } + return dateFormat +} const cellDatePickerCtrl = { - cellFocus: function(r, c, value){ + cellFocus: function (r, c, cell) { let row = Store.visibledatarow[r], row_pre = r == 0 ? 0 : Store.visibledatarow[r - 1]; let col = Store.visibledatacolumn[c], col_pre = c == 0 ? 0 : Store.visibledatacolumn[c - 1]; let margeset = menuButton.mergeborer(Store.flowdata, r, c); - if(!!margeset){ + let type = cell.ct.fa || 'YYYY-MM-DD'; + let defaultDate = update('yyyy-MM-dd hh:mm:ss', cell.v); + let dateFormat = fitFormat(type); + let enableTime = false; + let noCalendar = false; + let enableSeconds = false; + let time_24hr = true; + let hasChineseTime = false; + + + if (!!margeset) { row = margeset.row[1]; row_pre = margeset.row[0]; @@ -27,23 +59,51 @@ const cellDatePickerCtrl = { top: row_pre }) - flatpickr('#cellDatePickerBtn',{ - dateFormat: "YYYY-MM-DD", + if (/[上午下午]/.test(type)) { + hasChineseTime = true + } + if (/[Hhms]/.test(dateFormat)) { + enableTime = true; + } + if (!/[YMD]/.test(dateFormat)) { + noCalendar = true; + } + if (/s/.test(dateFormat)) { + enableSeconds = true; + } + if (/A/.test(dateFormat)) { + time_24hr = false; + } + + flatpickr('#luckysheet-input-box', { allowInput: false, - defaultDate: dayjs(value).format('YYYY-MM-DD'), + noCalendar, + enableSeconds, + enableTime, + dateFormat, + time_24hr, + defaultDate, parseDate: (datestr, format) => { return dayjs(datestr).toDate(); }, formatDate: (date, format, locale) => { + if (hasChineseTime) { + return dayjs(date).format(format).replace('AM', '上午').replace('PM', '下午') + } return dayjs(date).format(format); }, onChange: function (selectedDates, dateStr) { + let currentVal = datenum_local(new Date(selectedDates)) $("#luckysheet-rich-text-editor").html(dateStr); - formula.updatecell(Store.luckysheetCellUpdate[0], Store.luckysheetCellUpdate[1]); + setCellValue(r, c, currentVal, { isRefresh: false }) + setCellFormat(r, c, 'ct', cell.ct) + if (!enableTime) { + formula.updatecell(Store.luckysheetCellUpdate[0], Store.luckysheetCellUpdate[1]); + } } }); - $("#cellDatePickerBtn").click(); + $("#luckysheet-input-box").click(); }, } diff --git a/src/controllers/handler.js b/src/controllers/handler.js index 5db4e0f2a..0d4be85c7 100644 --- a/src/controllers/handler.js +++ b/src/controllers/handler.js @@ -1381,6 +1381,12 @@ export default function luckysheetHandler() { col_index = col_location[2]; + let margeset = menuButton.mergeborer(Store.flowdata, row_index, col_index); + if (!!margeset) { + row_index = margeset.row[2]; + col_index = margeset.column[2]; + } + if (pivotTable.isPivotRange(row_index, col_index)) { //数据透视表没有 任何数据 if ((pivotTable.filter == null || pivotTable.filter.length == 0) && (pivotTable.row == null || pivotTable.row.length == 0) && (pivotTable.column == null || pivotTable.column.length == 0) && (pivotTable.values == null || pivotTable.values.length == 0)) { diff --git a/src/controllers/updateCell.js b/src/controllers/updateCell.js index 6268cddbc..26e7bfd93 100644 --- a/src/controllers/updateCell.js +++ b/src/controllers/updateCell.js @@ -247,7 +247,7 @@ export function luckysheetupdateCell(row_index1, col_index1, d, cover, isnotfocu //日期 if(d[row_index1][col_index1] && d[row_index1][col_index1].ct && d[row_index1][col_index1].ct.t == 'd'){ - cellDatePickerCtrl.cellFocus(row_index1, col_index1, d[row_index1][col_index1].m); + cellDatePickerCtrl.cellFocus(row_index1, col_index1, d[row_index1][col_index1]); } formula.rangetosheet = Store.currentSheetIndex; diff --git a/src/global/format.js b/src/global/format.js index a94cd7a06..66170fe0b 100644 --- a/src/global/format.js +++ b/src/global/format.js @@ -1650,7 +1650,7 @@ function datenum(v, date1904) { return (epoch - dnthresh) / (24 * 60 * 60 * 1000); } -function datenum_local(v, date1904) { +export function datenum_local(v, date1904) { var epoch = Date.UTC(v.getFullYear(), v.getMonth(), v.getDate(), v.getHours(), v.getMinutes(), v.getSeconds()); var dnthresh_utc = Date.UTC(1899, 11, 31, 0, 0, 0);