User:Mxn/CommentsInLocalTime/sandbox.js: Difference between revisions

Content deleted Content added
No edit summary
Overshot a bit
 
(20 intermediate revisions by the same user not shown)
Line 35:
* If a property is a function, it is called to retrieve the formatted
* timestamp string. The function must accept one argument, a `Date` object.
*
* If no `options` is specified, the timestamp adheres to the user’s date
* format and timezone preferences.
*/
outputFormats: {
Line 41 ⟶ 44:
* The tooltip provides a more specific timestamp to distinguish
* comments in rapid succession.
*
* See <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#options>.
*/
relative: {
Line 49 ⟶ 54:
* Absolute dates are helpful for more distant dates, so that the user
* doesn’t have to do math in their head.
*
* See <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#date-time_component_options>
* and <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#style_shortcuts>.
*/
// absolute: {
// dateStyle: "fulllong",
// timeStyle: "short",
// },
},
Line 61 ⟶ 69:
* If an element of this array is an object its `type` and `options` may be:
*
* `type` | `options`
* ------------|----------
* `relative` | `Intl.RelativeTimeFormat` options
* `absolute` | `Intl.AbsoluteTimeFormat` options
* `iso8601mediawiki` | —
* `iso8601` | —
*
* See:
* <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#options>
* <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#date-time_component_options>
* <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#style_shortcuts>
*
* If an element of this array is a function, it is called to retrieve the
Line 116 ⟶ 130:
*/
codeTags: ["code", "input", "pre", "textarea"],
/**
* An object mapping the date format user options provided by this MediaWiki
* installation to corresponding Moment.js format strings. The user can
* choose a preferred date format in
* [[Special:Preferences#mw-prefsection-rendering-dateformat]]. See
* [[mw:Manual:Date formatting]]. These formats determine the default
* timestamp display format.
*
* These formats come from
* <https://doc.wikimedia.org/mediawiki-core/1.34.0/php/MessagesEn_8php.html#a2fc93ea5327f655d3ed306e221ee33f0>.
* When customizing these formats for a different wiki’s content language,
* consult the language’s corresponding message file’s `$dateFormats`
* variable. Use only the messages with the “both” suffix, and remove that
* suffix from each key. The MediaWiki date format syntax is described in
* <https://doc.wikimedia.org/mediawiki-core/1.34.0/php/classLanguage.html#a94f84f82d7f954c4cb2e191d22c6e6a6>
* and [[mw:Help:Extension:ParserFunctions##time]]. The Moment.js syntax is
* described in <https://momentjs.com/docs/#/parsing/string-format/>.
*
* @todo Automatically convert MediaWiki date format syntax to Moment.js
* date format syntax.
*/
formatOptions: {
mdy: "HH:mm, MMMM D, YYYY", // H:i, F j, Y
dmy: "HH:mm, D MMMM YYYY", // H:i, j F Y
ymd: "HH:mm, YYYY MMMM D", // H:i, Y F j
"ISO 8601": "YYYY-MM-DDTHH:mm:ss", // xnY-xnm-xnd"T"xnH:xni:xns
},
/**
* Expected format or formats of the timestamps in existing wikitext. If
* very different formats have been used over the course of the wiki’s
* history, specify an array of formats.
*
* This option expects parsing format strings
* <http://momentjs.com/docs/#/parsing/string-format/>.
*/
parseFormat: "H:m, D MMM YYYY",
/**
* Regular expression matching all the timestamps inserted by this MediaWiki
* installation over the years. This regular expression shouldincludes morethe or lessnamed
* capturing groups `hours`, `minutes`, `day`, `month`, `year`, and
* agree with the parseFormat option.
* `timezone`.
*
* Until 2005:
Line 165 ⟶ 142:
* 08:51, 23 November 2015 (UTC)
*/
parseRegExp: /(?<hours>\d\d):(?<minutes>\d\d), (?<day>\d\d?) (?<month>(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*) (?<year>\d{4}) \((?<timezone>UTC)\)/,
/**
Line 209 ⟶ 186:
var prefixNode;
while ((prefixNode = iter.nextNode())) {
var then;
var dateNode;
var result = LocalComments.parseRegExp.exec(prefixNode.data);
if (!result) continue;{
// Split out the timestamp into a separate text node.
dateNode = prefixNode.splitText(result.index);
// Split out the timestamp into a separate text node.
var dateNodesuffixNode = prefixNodedateNode.splitText(result[0].indexlength);
var suffixNode = dateNode.splitText(result[0].length);
// Determine the represented time.
var components = result.groups;
// Determine the represented time.
var monthIndex = mw.config.get("wgMonthNames").slice(1).indexOf(components.month);
var then = moment.utc(result[0], LocalComments.parseFormat);
if (!then.isValid()) {
// Many Wikipedias started out with English as the default
// localization, so fall back to English.
if (monthIndex === -1) {
then = moment.utc(result[0], "H:m, D MMM YYYY", "en");
monthIndex = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"].indexOf(components.month);
}
if (monthIndex === -1) {
monthIndex = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"].indexOf(components.month);
}
if (monthIndex !== -1) {
var offsetHours = components.hours - LocalComments.utcOffset;
var minuteOffset = (LocalComments.utcOffset - Math.round(LocalComments.utcOffset)) % 60;
var offsetMinutes = components.minutes - minuteOffset;
then = new Date(Date.UTC(components.year, monthIndex, components.day, offsetHours, offsetMinutes));
}
}
Line 231 ⟶ 220:
// [[User talk:Mxn/CommentsInLocalTime.js#Interface-protected edit request on 18 November 2022]]
var timeElt = $("<time />");
if (then.isValid!isNaN(then)) {
then.utcOffset(-LocalComments.utcOffset);
// MediaWiki core styles .explain[title] the same way as
// abbr[title], guiding the user to the tooltip.
timeElt.addClass("localcomments explain");
timeElt.dateTime =attr("datetime", then.toISOString());
}
if (dateNode) $(dateNode).wrap(timeElt);
}
}
Line 254 ⟶ 242:
var value;
var unit;
var seconds = (nowthen - thennow) / 1000; // convert ms to s
value = seconds;
unit = "seconds";
Line 301 ⟶ 289:
var lang = mw.config.get("wgPageViewLanguage");
var formatter = mw.loader.require("mediawiki.DateFormatter");
var format;
switch (fmt.type) {
case "absolute":
formatif = new Intl.DateTimeFormat(lang, fmt.options); {
var absolute = new Intl.DateTimeFormat(lang, fmt.options);
return format.format(then);
return absolute.format(then);
}
return formatter.formatTimeAndDate(then);
case "relative":
formatif (fmt.options) {
var relative = new Intl.RelativeTimeFormat(lang, fmt.options);
var component = relativeDateComponent(then);
return formatrelative.format(component.value, component.unit);
}
return formatter.formatRelativeTimeOrDate(then);
case "iso8601":
return thenformatter.toISOStringformatIso(then);
}
}
Line 336 ⟶ 330:
// Replace the text.
var componenttext = relativeDateComponentformatDate(then, {type: "relative"});
var text;
if (component.unit === "weeks") {
text = formatDate(then, {
type: "relative",
options: LocalComments.outputFormats.relative,
});
} else {
text = formatDate(then, {
type: "absolute",
options: LocalComments.outputFormats.absolute,
});
}
if (text) {
$(elt).text(text);
Line 354 ⟶ 336:
// Register for periodic updates.
var component = relativeDateComponent(then);
$(elt).attr("data-localcomments-unit", component.unit);
}
Line 386 ⟶ 369:
}
wrapTimestamps();
mw.loader.using("moment", function () {
mw.loader.using("mediawiki.DateFormatter", function () {
wrapTimestamps();
formatTimestamps();
});