Content deleted Content added
add more docs |
fixes |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1:
/**
* COMMENTS IN LOCAL TIME
Line 30 ⟶ 29:
function convertMonthToNumber(month) {
return new Date(`${month} 1, 2001`).getMonth();
}
function getDates(time) {
const [, oldHour, oldMinute, oldDay, oldMonth, oldYear] = time;
// Today
const today = new Date();
// Yesterday
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
// Tomorrow
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
// Set the date entered.
const newTime = new Date();
newTime.setUTCFullYear(oldYear, convertMonthToNumber(oldMonth), oldDay);
newTime.setUTCHours(oldHour);
newTime.setUTCMinutes(oldMinute);
return { time: newTime, today, tomorrow, yesterday };
}
Line 86 ⟶ 111:
adjustTime(originalTimestamp, search) {
const { time, today, tomorrow, yesterday } = getDates(
originalTimestamp.match(search)
);
// A string matching the date pattern was found, but it cannot be
Line 113 ⟶ 120:
return [originalTimestamp, ''];
}
const date = this.determineDateText({
time,
today,
tomorrow,
yesterday,
});
const { ampm, hour } = this.getHour(time);
const minute = addLeadingZero(time.getMinutes());
const finalTime = `${hour}:${minute}${ampm}`;
// Determine the time offset.
Line 119 ⟶ 137:
utcValue >= 0 ? `+${utcValue}` : `−${Math.abs(utcValue.toFixed(1))}`;
const returnDate = this.LocalComments.timeFirst
? `${finalTime}, ${date} ${utcPart}`
: `${date}, ${finalTime} ${utcPart}`;
return returnDate;
}
Line 239 ⟶ 161:
this.language.December,
][number];
}
createDateText({ day, month, time, today, year }) {
// Calculate day of week
const dayNames = [
this.language.Sunday,
this.language.Monday,
this.language.Tuesday,
this.language.Wednesday,
this.language.Thursday,
this.language.Friday,
this.language.Saturday,
];
const dayOfTheWeek = dayNames[time.getDay()];
let descriptiveDifference = '';
let last = '';
// Create a relative descriptive difference
if (this.LocalComments.dateDifference) {
({ descriptiveDifference, last } = this.createRelativeDate(
today,
time
));
}
const monthName = this.convertNumberToMonth(time.getMonth());
// Format the date according to user preferences
let formattedDate = '';
switch (this.LocalComments.dateFormat.toLowerCase()) {
case 'dmy':
formattedDate = `${day} ${monthName} ${year}`;
break;
case 'mdy':
formattedDate = `${monthName} ${day}, ${year}`;
break;
default:
formattedDate = `${year}-${month}-${addLeadingZero(day)}`;
}
const formattedDayOfTheWeek = this.LocalComments.dayOfWeek
? `, ${last}${dayOfTheWeek}`
: '';
return `${formattedDate}${formattedDayOfTheWeek}${descriptiveDifference}`;
}
Line 263 ⟶ 233:
*/
let daysAgo = Math.abs(Math.round(millisecondsAgo / 1000 / 60 / 60 / 24));
millisecondsAgo,
});
//
// it's better than the previous method that used 1 January + delta days.
// That was usually quite off because it mapped the second delta month to
// February, which has only 28 days. This method is usually not more than
// one day off, except perhaps over very distant dates.
/**
Line 331 ⟶ 286:
const descriptiveParts = [];
// There is years text to add.
if (yearsAgo > 0) {
descriptiveParts.push(
)}`
}
// There is months text to add.
if (monthsAgo > 0) {
descriptiveParts.push(
)}`
}
// There is days text to add.
if (daysAgo > 0) {
descriptiveParts.push(
)}`
}
Line 367 ⟶ 325:
last,
};
}
determineDateText({ time, today, tomorrow, yesterday }) {
// Set the date bits to output.
const year = time.getFullYear();
const month = addLeadingZero(time.getMonth() + 1);
const day = time.getDate();
// Return 'today' or 'yesterday' if that is the case
if (
year === today.getFullYear() &&
month === addLeadingZero(today.getMonth() + 1) &&
day === today.getDate()
) {
return this.language.Today;
}
if (
year === yesterday.getFullYear() &&
month === addLeadingZero(yesterday.getMonth() + 1) &&
day === yesterday.getDate()
) {
return this.language.Yesterday;
}
if (
year === tomorrow.getFullYear() &&
month === addLeadingZero(tomorrow.getMonth() + 1) &&
day === tomorrow.getDate()
) {
return this.language.Tomorrow;
}
return this.createDateText({ day, month, time, today, year });
}
getHour(time) {
let ampm;
let hour = Number.parseInt(time.getHours(), 10);
if (this.LocalComments.twentyFourHours) {
ampm = '';
hour = addLeadingZero(hour);
} else {
// Output am or pm depending on the date.
ampm = hour <= 11 ? ' am' : ' pm';
if (hour > 12) {
hour -= 12;
} else if (hour === 0) {
hour = 12;
}
}
return { ampm, hour };
}
relativeText({ daysAgo, millisecondsAgo }) {
let differenceWord = '';
let last = '';
// The date is in the past.
if (millisecondsAgo >= 0) {
differenceWord = this.language.ago;
if (daysAgo <= 7) {
last = `${this.language.last} `;
}
// The date is in the future.
} else {
differenceWord = this.language['from now'];
if (daysAgo <= 7) {
last = `${this.language.this} `;
}
}
return { differenceWord, last };
}
Line 376 ⟶ 413:
// Check if this is a text node.
if (node.nodeType === 3) {
// Don't continue if this text node's parent tag is one of these.
if (['CODE', 'PRE'].includes(node.parentNode.nodeName)) {
return;
}
Line 387 ⟶ 421:
const matches = value.match(search);
if (matches) {
// Only act on the first timestamp we found in this node. This is for
//
//
const [match] = matches;
const position = value.search(search);
const stringLength = match.toString().length;
// Grab the text content before and after the matching timestamp,
// which we'll then wrap in their own SPAN nodes.
const beforeMatch = value.slice(0, position);
const afterMatch = value.slice(position + stringLength);
const returnDate = this.adjustTime(match.toString(), search);
// Create the code to display the new local comments content.
const $span = $(
`<span class="localcomments" style="font-size: 95%;" title="${match}">${returnDate}</span>`
);
// Replace the existing text node in the page with our new local
// comments node.
$(node).replaceWith($span);
// Replace the text content that appears before the timestamp.
if (beforeMatch) {
$span.before(
`<span class="before-localcomments">${beforeMatch}</span>`
);
}
// Replace the text content that appears after the timestamp.
$span.after(
`<span class="after-localcomments">${afterMatch}</span>`
);
}
}
} else {
Line 464 ⟶ 495:
this.replaceText(
document.querySelector('.mw-body-content .mw-parser-output'),
/(\d{1,2}):(\d{2}), (\d{1,2}) ([A-Z][a-z]+) (\d{4}) \(UTC\)/
);
|