Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
feat: 修改日期能够正常的弹出格式框和设置对应格式的日期时间
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-f committed Nov 25, 2020
1 parent 6244457 commit 67c44e7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
74 changes: 67 additions & 7 deletions src/controllers/cellDatePickerCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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();
},
}

Expand Down
6 changes: 6 additions & 0 deletions src/controllers/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/updateCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/global/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 67c44e7

Please sign in to comment.