@atlaskit/editor-plugin-date
Version:
Date plugin for @atlaskit/editor-core
189 lines (180 loc) • 6.34 kB
JavaScript
import addDays from 'date-fns/addDays';
import addMonths from 'date-fns/addMonths';
import addYears from 'date-fns/addYears';
// eslint-disable-next-line import/no-namespace
import * as locales from 'date-fns/locale';
import { dateToDateType, dateTypeToDate, formatDateType } from './formatParse';
function isDigit(c) {
if (c === undefined) {
return false;
}
return c >= '0' && c <= '9';
}
/**
* Check if cursor is in first segment of a date.
* @param cursorPos Cursor pos, with 0 referring to the left of first char
* @param date Date string in any locale
*/
function isCursorInFirstDateSegment(cursorPos, date) {
let posCounter = cursorPos - 1;
let isAdjacent = true;
// The date without any non-digit characters on the end
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
const strippedDate = date.replace(/[^0-9]+$/g, '');
while (posCounter >= 0 && isAdjacent) {
const c = strippedDate[posCounter];
if (!isDigit(c)) {
isAdjacent = false;
}
posCounter -= 1;
}
return isAdjacent;
}
/**
* Check if cursor is in last segment of a date.
* @param cursorPos Cursor pos, with 0 referring to the left of first char
* @param date Date string in any locale
*/
function isCursorInLastDateSegment(cursorPos, date) {
let posCounter = cursorPos;
let isAdjacent = true;
// The date without any non-digit characters on the end
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
const strippedDate = date.replace(/[^0-9]+$/g, '');
while (posCounter < strippedDate.length && isAdjacent) {
const c = strippedDate[posCounter];
if (!isDigit(c)) {
isAdjacent = false;
}
posCounter += 1;
}
return isAdjacent;
}
/**
* Inconclusively check if a date string is valid - a value of false means it is definitely
* invalid, a value of true means it might be valid.
* @param date Date string to be parsed
*/
export function isDatePossiblyValid(date) {
for (const c of date) {
const isNumber = c >= '0' && c <= '9';
const isValidPunctuation = '. ,/-'.indexOf(c) !== -1;
if (!(isNumber || isValidPunctuation)) {
return false;
}
}
return true;
}
/**
* Find the segment of a date a position refers to. Eg: pos 2 in 29/03/2020 is in
* the day segment.
* @param position Cursor position, with 0 referring to the left of the first char
* @param date The localised date string
* @param locale The language to interpret the date string in
*/
export function findDateSegmentByPosition(position, date, locale) {
if (position > date.length) {
return undefined;
}
const placeholder = getLocaleDatePlaceholder(locale);
if (!placeholder) {
return undefined;
}
// The placeholder without any non-digit characters on the end
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
const strippedPlaceholder = placeholder.replace(/[^ymd]+$/g, '');
const keyToSegment = {
d: 'day',
m: 'month',
y: 'year'
};
const firstSegment = keyToSegment[strippedPlaceholder[0]];
const lastSegment = keyToSegment[strippedPlaceholder[strippedPlaceholder.length - 1]];
const allPossibleSegments = ['day', 'month', 'year'];
const middleSegment = allPossibleSegments.filter(s => s !== firstSegment && s !== lastSegment)[0];
if (isCursorInFirstDateSegment(position, date)) {
return firstSegment;
}
if (isCursorInLastDateSegment(position, date)) {
return lastSegment;
}
return middleSegment;
}
/**
* Generate a placeholder date string for a given locale
* eg: locale 'hu-HU' -> 'yyyy. mm. dd.'
* @param locale A locale string supported by Intl.DateTimeFormat
* @returns A placeholder string. d=1 or 2 digit day, dd=zero padded
* day, same for month but letter m, yyyy=year
*/
export function getLocaleDatePlaceholder(locale) {
const uniqueDateType = {
day: 7,
month: 1,
year: 1992
};
const localisedDateString = formatDateType(uniqueDateType, locale);
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
const shortDateFormat = localisedDateString.replace(/\d+/g, str => {
if (!str) {
return '';
}
// Ignored via go/ees005
// eslint-disable-next-line no-var
var num = parseInt(str);
switch (num % 100) {
case 92:
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
return str.replace(/.{1}/g, 'y');
case 1:
return str.length === 1 ? 'm' : 'mm';
case 7:
return str.length === 1 ? 'd' : 'dd';
}
return '';
});
return shortDateFormat;
}
/**
* Adjust date segment up or down. Eg. If day is the active segment and adjustment is -1,
* reduce the day by one.
* @param date Valid datetype
* @param activeSegment which part of the date is selected/being adjusted
* @param adjustment how many units the segment is being adjusted (can be pos or neg, usually 1 or -1)
*/
export function adjustDate(date, activeSegment, adjustment) {
const originalDate = dateTypeToDate(date);
const newDate = activeSegment === 'day' ? addDays(originalDate, adjustment) : activeSegment === 'month' ? addMonths(originalDate, adjustment) : addYears(originalDate, adjustment);
return dateToDateType(newDate);
}
export function isToday(date) {
const today = new Date();
return date !== undefined && today.getDate() === date.day && date.month === today.getMonth() + 1 && date.year === today.getFullYear();
}
/**
* Get the corresponding date-fns locale for a given locale string. Ideally we could rely on Intl
* but firefox does not support getting the first day of the week from it. We have to handle silly
* use cases like 'nl-NL' where the language code is the same as the region code because someone
* decided to format them in this non-standard way
* @param localeString Locale string, eg. 'en-AU'
* @returns Locale or undefined if not found
* @example
* getDFLocale('en-AU') // returns enAU locale
*/
export function getDFLocale(localeString) {
let dfLocaleString = localeString;
const [lang, region] = localeString.split('-');
if (region) {
if (lang.toLocaleLowerCase() === region.toLocaleLowerCase()) {
dfLocaleString = lang;
} else {
dfLocaleString = `${lang}${region}`;
}
}
return locales[dfLocaleString];
}