Skip to content

Commit

Permalink
fix(*): 调整service.updateAnnuals方法,调整工具mergeAnnualMarks方法
Browse files Browse the repository at this point in the history
  • Loading branch information
lspriv committed Feb 20, 2024
1 parent 595c724 commit 7354b14
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/basic/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See File LICENSE for detail or copy at https://opensource.org/licenses/MIT
* @Description: 年度面板绘制
* @Author: lspriv
* @LastEditTime: 2024-02-20 13:21:21
* @LastEditTime: 2024-02-20 15:25:00
*/
import { CalendarHandler } from '../interface/component';
import { WxCalendar, getAnnualMarkKey, isToday, inMonthDate, sortWeeks } from '../interface/calendar';
Expand Down Expand Up @@ -800,6 +800,7 @@ export class YearPrinter extends CalendarHandler {
*/
public update(idxs?: number[]) {
idxs = idxs || Array.from({ length: CALENDAR_PANELS }, (_, i) => i);
idxs = [...new Set(idxs)];
for (const idx of idxs) {
const canvas = this._canvas_[idx];
const year = this._instance_._panel_.getFullYear(idx);
Expand Down
23 changes: 14 additions & 9 deletions src/basic/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See File LICENSE for detail or copy at https://opensource.org/licenses/MIT
* @Description: 插件服务
* @Author: lspriv
* @LastEditTime: 2024-02-20 13:38:01
* @LastEditTime: 2024-02-20 15:24:12
*/
import { nextTick } from './tools';
import { camelToSnake, notEmptyObject } from '../utils/shared';
Expand Down Expand Up @@ -233,7 +233,7 @@ export class PluginService<T extends PluginConstructor[] = PluginConstructor[]>
const result = plugin.PLUGIN_TRACK_YEAR?.(year);
if (result) {
if (result.subinfo) record.subinfo = result.subinfo;
if (result.marks) record.marks = result.marks;
if (result.marks?.size) record.marks = mergeAnnualMarks(record.marks, result.marks);
}
});
return notEmptyObject(record) ? record : null;
Expand Down Expand Up @@ -291,7 +291,7 @@ export class PluginService<T extends PluginConstructor[] = PluginConstructor[]>
const ydx = years.findIndex(y => y.year === year.year);
if (ydx >= 0) {
if (year.result.subinfo) sets[`years[${ydx}].subinfo`] = year.result.subinfo;
if (year.result.marks) {
if (year.result.marks?.size) {
this.component._years_[ydx].marks = mergeAnnualMarks(this.component._years_[ydx].marks, year.result.marks)!;
this.component._printer_.update([ydx]);
}
Expand Down Expand Up @@ -348,17 +348,22 @@ export class PluginService<T extends PluginConstructor[] = PluginConstructor[]>
/**
* 刷新年度面板
*/
public async updateAnnuals(annuals?: Array<AnnualResult>) {
public async updateAnnuals(annuals?: Array<number>) {
if (!annuals?.length) return;
const years = this.component.data.years;
const sets: Partial<CalendarData> = {};
const ydxs: number[] = [];
for (const annual of annuals) {
const ydx = years.findIndex(y => y.year === annual.year);
for (let i = annuals.length; i--; ) {
const ydx = years.findIndex(y => y.year === annuals[i]);
if (ydx >= 0) {
if (annual.result.subinfo) sets[`years[${ydx}].subinfo`] = annual.result.subinfo;
if (annual.result.marks) {
this.component._years_[ydx].marks = mergeAnnualMarks(this.component._years_[ydx].marks, annual.result.marks)!;
const year = years[ydx];
const result = this.walkForYear(year);
if (result) {
if (result.subinfo) sets[`years[${ydx}].subinfo`] = result.subinfo;
if (result.marks?.size) {
this.component._years_[ydx].marks = mergeAnnualMarks(this.component._years_[ydx].marks, result.marks)!;
ydxs.push(ydx);
}
}
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/interface/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See File LICENSE for detail or copy at https://opensource.org/licenses/MIT
* @Description: 日期处理
* @Author: lspriv
* @LastEditTime: 2024-02-20 14:12:58
* @LastEditTime: 2024-02-20 15:20:40
*/
import { WEEKS } from '../basic/constants';
import { Nullable, isDate, isFunction, isNumber, isString } from '../utils/shared';
Expand Down Expand Up @@ -105,17 +105,14 @@ export const getAnnualMarkKey = (day: Pick<CalendarDay, 'month' | 'day'>) => `${
* 合并两个年面板标记
*/
export const mergeAnnualMarks = (m1?: WcAnnualMarks, m2?: WcAnnualMarks) => {
if (!m1 || !m2) return m1 || m2;

if (!m2) return m1;
m1 = m1 || new Map();
const entries = m2.entries();
for (const [key, mark] of entries) {
const m = m1.get(key);
if (m) {
mark.rwtype && (m.rwtype = mark.rwtype);
mark.sub && (m.sub = mark.sub);
} else {
m1.set(key, mark);
}
const m = m1.get(key) || {};
mark.rwtype && (m.rwtype = mark.rwtype);
mark.sub && (m.sub = mark.sub);
m1.set(key, m);
}
return m1;
};
Expand Down

0 comments on commit 7354b14

Please sign in to comment.