PlotDirector/PlotLine/wwwroot/js/context-calendar-model.js
2026-12-01 13:17:32 +00:00

13 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Date-only UTC arithmetic avoids DST and the JavaScript 0–99 year constructor trap. */
(function (host) {
'use strict';
const date=value=>{if(!/^\d{4}-\d{2}-\d{2}$/.test(value||'')||value.startsWith('0000'))return null;const d=new Date(value+'T00:00:00Z');return Number.isFinite(+d)&&d.toISOString().slice(0,10)===value?d:null;};
const iso=d=>d.toISOString().slice(0,10);
function shift(month,step){const d=date(month+'-01');if(!d)return null;d.setUTCMonth(d.getUTCMonth()+step);return d.getUTCFullYear()>=1&&d.getUTCFullYear()<=9999?iso(d).slice(0,7):null;}
function grid(month){const first=date(month+'-01');if(!first)return [];const d=new Date(+first);d.setUTCDate(1-(first.getUTCDay()+6)%7);const next=new Date(+first);next.setUTCMonth(next.getUTCMonth()+1);next.setUTCDate(0);const count=Math.ceil(((first.getUTCDay()+6)%7+next.getUTCDate())/7)*7;return Array.from({length:count},()=>{const value=d.getUTCFullYear()>=1&&d.getUTCFullYear()<=9999?iso(d):null;d.setUTCDate(d.getUTCDate()+1);return value;});}
function chapters(scenes,day){if(!day)return [];const result=new Map();for(const s of scenes){if(s.date!==day)continue;const key=s.bookID+'/'+s.chapterID;if(!result.has(key))result.set(key,{...s,scenes:[]});result.get(key).scenes.push(s);}return [...result.values()];}
function manuscript(scenes,position){const current=scenes.find(s=>s.bookID===position.bookId&&s.sceneID===position.sceneId);if(current?.date)return {date:current.date,source:'scene'};const chapter=position.chapterId??current?.chapterID;const dated=scenes.filter(s=>s.bookID===position.bookId&&s.chapterID===chapter&&s.date).sort((a,b)=>a.sceneNumber-b.sceneNumber);return {date:dated[0]?.date||null,source:dated.length?'chapter (first dated scene)':'undated'};}
const api={date,iso,shift,grid,chapters,manuscript,elapsed:(from,to)=>date(from)&&date(to)?Math.round((date(to)-date(from))/86400000):null};
if(typeof module!=='undefined'&&module.exports)module.exports=api;else host.PlotDirectorCalendar=api;
})(globalThis);