@atlaskit/editor-plugin-date 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/cjs/actions.js +105 -0
- package/dist/cjs/commands.js +114 -1
- package/dist/cjs/index.js +9 -1
- package/dist/cjs/nodeviews/date.js +44 -0
- package/dist/cjs/plugin.js +197 -0
- package/dist/cjs/pm-plugins/keymap.js +41 -0
- package/dist/cjs/pm-plugins/main.js +39 -0
- package/dist/cjs/pm-plugins/plugin-key.js +8 -0
- package/dist/cjs/pm-plugins/types.js +5 -0
- package/dist/cjs/pm-plugins/utils.js +70 -0
- package/dist/cjs/ui/DatePicker/date-picker-input.js +253 -0
- package/dist/cjs/ui/DatePicker/index.js +170 -0
- package/dist/cjs/utils/formatParse.js +88 -0
- package/dist/cjs/utils/internal.js +176 -0
- package/dist/es2019/actions.js +93 -0
- package/dist/es2019/commands.js +113 -1
- package/dist/es2019/index.js +1 -1
- package/dist/es2019/nodeviews/date.js +47 -0
- package/dist/es2019/plugin.js +183 -0
- package/dist/es2019/pm-plugins/keymap.js +34 -0
- package/dist/es2019/pm-plugins/main.js +35 -0
- package/dist/es2019/pm-plugins/plugin-key.js +2 -0
- package/dist/es2019/pm-plugins/types.js +1 -0
- package/dist/es2019/pm-plugins/utils.js +70 -0
- package/dist/es2019/ui/DatePicker/date-picker-input.js +242 -0
- package/dist/es2019/ui/DatePicker/index.js +154 -0
- package/dist/es2019/utils/formatParse.js +83 -0
- package/dist/es2019/utils/internal.js +151 -0
- package/dist/esm/actions.js +99 -0
- package/dist/esm/commands.js +112 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/nodeviews/date.js +37 -0
- package/dist/esm/plugin.js +185 -0
- package/dist/esm/pm-plugins/keymap.js +34 -0
- package/dist/esm/pm-plugins/main.js +34 -0
- package/dist/esm/pm-plugins/plugin-key.js +2 -0
- package/dist/esm/pm-plugins/types.js +1 -0
- package/dist/esm/pm-plugins/utils.js +61 -0
- package/dist/esm/ui/DatePicker/date-picker-input.js +247 -0
- package/dist/esm/ui/DatePicker/index.js +164 -0
- package/dist/esm/utils/formatParse.js +79 -0
- package/dist/esm/utils/internal.js +165 -0
- package/dist/types/actions.d.ts +49 -0
- package/dist/types/commands.d.ts +12 -10
- package/dist/types/index.d.ts +1 -1
- package/dist/types/nodeviews/date.d.ts +3 -0
- package/dist/types/plugin.d.ts +3 -0
- package/dist/types/pm-plugins/keymap.d.ts +3 -0
- package/dist/types/pm-plugins/main.d.ts +6 -0
- package/dist/types/pm-plugins/plugin-key.d.ts +3 -0
- package/dist/types/pm-plugins/types.d.ts +12 -0
- package/dist/types/pm-plugins/utils.d.ts +5 -0
- package/dist/types/types.d.ts +10 -2
- package/dist/types/ui/DatePicker/date-picker-input.d.ts +25 -0
- package/dist/types/ui/DatePicker/index.d.ts +36 -0
- package/dist/types/utils/formatParse.d.ts +27 -0
- package/dist/types/utils/internal.d.ts +32 -0
- package/dist/types-ts4.5/actions.d.ts +60 -0
- package/dist/types-ts4.5/commands.d.ts +14 -10
- package/dist/types-ts4.5/index.d.ts +1 -1
- package/dist/types-ts4.5/nodeviews/date.d.ts +3 -0
- package/dist/types-ts4.5/plugin.d.ts +3 -0
- package/dist/types-ts4.5/pm-plugins/keymap.d.ts +3 -0
- package/dist/types-ts4.5/pm-plugins/main.d.ts +6 -0
- package/dist/types-ts4.5/pm-plugins/plugin-key.d.ts +3 -0
- package/dist/types-ts4.5/pm-plugins/types.d.ts +12 -0
- package/dist/types-ts4.5/pm-plugins/utils.d.ts +5 -0
- package/dist/types-ts4.5/types.d.ts +10 -2
- package/dist/types-ts4.5/ui/DatePicker/date-picker-input.d.ts +25 -0
- package/dist/types-ts4.5/ui/DatePicker/index.d.ts +36 -0
- package/dist/types-ts4.5/utils/formatParse.d.ts +27 -0
- package/dist/types-ts4.5/utils/internal.d.ts +32 -0
- package/package.json +29 -4
- package/report.api.md +5 -2
- package/tmp/api-report-tmp.d.ts +5 -2
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
|
+
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
+
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
|
|
4
|
+
import _inherits from "@babel/runtime/helpers/inherits";
|
|
5
|
+
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
|
6
|
+
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
|
7
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
8
|
+
import _taggedTemplateLiteral from "@babel/runtime/helpers/taggedTemplateLiteral";
|
|
9
|
+
var _templateObject;
|
|
10
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
11
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
12
|
+
/** @jsx jsx */
|
|
13
|
+
import React from 'react';
|
|
14
|
+
import { css, jsx } from '@emotion/react';
|
|
15
|
+
import { defineMessages, injectIntl } from 'react-intl-next';
|
|
16
|
+
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
|
|
17
|
+
import { ErrorMessage } from '@atlaskit/form';
|
|
18
|
+
import TextField from '@atlaskit/textfield';
|
|
19
|
+
import { formatDateType, parseDateType } from '../../utils/formatParse';
|
|
20
|
+
import { adjustDate, findDateSegmentByPosition, isDatePossiblyValid } from '../../utils/internal';
|
|
21
|
+
|
|
22
|
+
// eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage
|
|
23
|
+
var dateTextFieldWrapper = css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n padding: 22px;\n padding-bottom: ", ";\n"])), "var(--ds-space-150, 12px)");
|
|
24
|
+
var messages = defineMessages({
|
|
25
|
+
invalidDateError: {
|
|
26
|
+
id: 'fabric.editor.invalidDateError',
|
|
27
|
+
defaultMessage: 'Enter a valid date',
|
|
28
|
+
description: 'Error message when the date typed in is invalid, requesting they inputs a new date'
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// eslint-disable-next-line @repo/internal/react/no-class-components
|
|
33
|
+
var DatePickerInput = /*#__PURE__*/function (_React$Component) {
|
|
34
|
+
_inherits(DatePickerInput, _React$Component);
|
|
35
|
+
var _super = _createSuper(DatePickerInput);
|
|
36
|
+
function DatePickerInput(props) {
|
|
37
|
+
var _this;
|
|
38
|
+
_classCallCheck(this, DatePickerInput);
|
|
39
|
+
_this = _super.call(this, props);
|
|
40
|
+
/**
|
|
41
|
+
* Focus the input textfield
|
|
42
|
+
*/
|
|
43
|
+
_defineProperty(_assertThisInitialized(_this), "focusInput", function () {
|
|
44
|
+
if (!_this.inputRef) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
// Defer to prevent editor scrolling to top (See FS-3227, also ED-2992)
|
|
48
|
+
_this.autofocusTimeout = setTimeout(function () {
|
|
49
|
+
var _this$inputRef;
|
|
50
|
+
(_this$inputRef = _this.inputRef) === null || _this$inputRef === void 0 || _this$inputRef.focus();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
/**
|
|
54
|
+
* Select all the input text
|
|
55
|
+
*/
|
|
56
|
+
_defineProperty(_assertThisInitialized(_this), "selectInput", function () {
|
|
57
|
+
if (!_this.inputRef) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// Defer to prevent editor scrolling to top (See FS-3227, also ED-2992)
|
|
61
|
+
_this.autoSelectAllTimeout = setTimeout(function () {
|
|
62
|
+
var _this$inputRef2;
|
|
63
|
+
(_this$inputRef2 = _this.inputRef) === null || _this$inputRef2 === void 0 || _this$inputRef2.select();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
_defineProperty(_assertThisInitialized(_this), "handleInputRef", function (ref) {
|
|
67
|
+
var _this$props = _this.props,
|
|
68
|
+
autoFocus = _this$props.autoFocus,
|
|
69
|
+
autoSelectAll = _this$props.autoSelectAll;
|
|
70
|
+
if (ref) {
|
|
71
|
+
_this.inputRef = ref;
|
|
72
|
+
}
|
|
73
|
+
if (ref && autoFocus) {
|
|
74
|
+
_this.focusInput();
|
|
75
|
+
}
|
|
76
|
+
if (autoSelectAll) {
|
|
77
|
+
_this.selectInput();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
_defineProperty(_assertThisInitialized(_this), "handleChange", function (evt) {
|
|
81
|
+
var textFieldValue = evt.target.value;
|
|
82
|
+
var _this$props2 = _this.props,
|
|
83
|
+
locale = _this$props2.locale,
|
|
84
|
+
dispatchAnalyticsEvent = _this$props2.dispatchAnalyticsEvent;
|
|
85
|
+
var newDate = parseDateType(textFieldValue, locale);
|
|
86
|
+
if (newDate !== undefined && newDate !== null) {
|
|
87
|
+
_this.setState({
|
|
88
|
+
inputText: textFieldValue
|
|
89
|
+
});
|
|
90
|
+
_this.props.onNewDate(newDate);
|
|
91
|
+
if (dispatchAnalyticsEvent) {
|
|
92
|
+
dispatchAnalyticsEvent({
|
|
93
|
+
eventType: EVENT_TYPE.TRACK,
|
|
94
|
+
action: ACTION.TYPING_FINISHED,
|
|
95
|
+
actionSubject: ACTION_SUBJECT.DATE
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
// if invalid, just update state text (to rerender textfield)
|
|
100
|
+
_this.setState({
|
|
101
|
+
inputText: textFieldValue
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
_defineProperty(_assertThisInitialized(_this), "handleKeyPress", function (event) {
|
|
106
|
+
var _this$props3 = _this.props,
|
|
107
|
+
locale = _this$props3.locale,
|
|
108
|
+
dispatchAnalyticsEvent = _this$props3.dispatchAnalyticsEvent;
|
|
109
|
+
var textFieldValue = event.target.value;
|
|
110
|
+
|
|
111
|
+
// Fire event on every keypress (textfield not necessarily empty)
|
|
112
|
+
if (dispatchAnalyticsEvent && event.key !== 'Enter' && event.key !== 'Backspace') {
|
|
113
|
+
dispatchAnalyticsEvent({
|
|
114
|
+
eventType: EVENT_TYPE.TRACK,
|
|
115
|
+
action: ACTION.TYPING_STARTED,
|
|
116
|
+
actionSubject: ACTION_SUBJECT.DATE
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (event.key !== 'Enter') {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (textFieldValue === '') {
|
|
123
|
+
_this.props.onEmptySubmit();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
var newDate = parseDateType(textFieldValue, locale);
|
|
127
|
+
_this.props.onSubmitDate(newDate);
|
|
128
|
+
});
|
|
129
|
+
// arrow keys are only triggered by onKeyDown, not onKeyPress
|
|
130
|
+
_defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (event) {
|
|
131
|
+
var _this$inputRef3;
|
|
132
|
+
var dateString = event.target.value;
|
|
133
|
+
var locale = _this.props.locale;
|
|
134
|
+
var adjustment;
|
|
135
|
+
if (event.key === 'ArrowUp') {
|
|
136
|
+
adjustment = 1;
|
|
137
|
+
} else if (event.key === 'ArrowDown') {
|
|
138
|
+
adjustment = -1;
|
|
139
|
+
}
|
|
140
|
+
if (adjustment === undefined) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
var dispatchAnalyticsEvent = _this.props.dispatchAnalyticsEvent;
|
|
144
|
+
var cursorPos = (_this$inputRef3 = _this.inputRef) === null || _this$inputRef3 === void 0 ? void 0 : _this$inputRef3.selectionStart;
|
|
145
|
+
if (cursorPos === null || cursorPos === undefined) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
var activeSegment = findDateSegmentByPosition(cursorPos, dateString, locale);
|
|
149
|
+
if (activeSegment === undefined) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
var dateSegment;
|
|
153
|
+
switch (activeSegment) {
|
|
154
|
+
case 'day':
|
|
155
|
+
dateSegment = ACTION_SUBJECT_ID.DATE_DAY;
|
|
156
|
+
break;
|
|
157
|
+
case 'month':
|
|
158
|
+
dateSegment = ACTION_SUBJECT_ID.DATE_MONTH;
|
|
159
|
+
break;
|
|
160
|
+
default:
|
|
161
|
+
dateSegment = ACTION_SUBJECT_ID.DATE_YEAR;
|
|
162
|
+
}
|
|
163
|
+
if (dispatchAnalyticsEvent) {
|
|
164
|
+
dispatchAnalyticsEvent({
|
|
165
|
+
eventType: EVENT_TYPE.TRACK,
|
|
166
|
+
action: adjustment > 0 ? ACTION.INCREMENTED : ACTION.DECREMENTED,
|
|
167
|
+
actionSubject: ACTION_SUBJECT.DATE_SEGMENT,
|
|
168
|
+
attributes: {
|
|
169
|
+
dateSegment: dateSegment
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
var oldDateType = parseDateType(dateString, locale);
|
|
174
|
+
if (oldDateType === undefined || oldDateType === null) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
var newDateType = adjustDate(oldDateType, activeSegment, adjustment);
|
|
178
|
+
_this.setState({
|
|
179
|
+
inputText: formatDateType(newDateType, locale)
|
|
180
|
+
});
|
|
181
|
+
_this.props.onNewDate(newDateType);
|
|
182
|
+
_this.setInputSelectionPos = Math.min(cursorPos, dateString.length);
|
|
183
|
+
event.preventDefault();
|
|
184
|
+
});
|
|
185
|
+
var date = props.date;
|
|
186
|
+
_this.setInputSelectionPos = undefined;
|
|
187
|
+
var inputText = formatDateType(date, _this.props.locale);
|
|
188
|
+
_this.state = {
|
|
189
|
+
inputText: inputText
|
|
190
|
+
};
|
|
191
|
+
return _this;
|
|
192
|
+
}
|
|
193
|
+
_createClass(DatePickerInput, [{
|
|
194
|
+
key: "render",
|
|
195
|
+
value: function render() {
|
|
196
|
+
var _this$props4 = this.props,
|
|
197
|
+
locale = _this$props4.locale,
|
|
198
|
+
formatMessage = _this$props4.intl.formatMessage;
|
|
199
|
+
var inputText = this.state.inputText;
|
|
200
|
+
var possiblyValid = isDatePossiblyValid(inputText);
|
|
201
|
+
var attemptedDateParse = parseDateType(inputText, locale);
|
|
202
|
+
|
|
203
|
+
// Don't display an error for an empty input.
|
|
204
|
+
var displayError = (attemptedDateParse === null || !possiblyValid) && inputText !== '';
|
|
205
|
+
return jsx("div", {
|
|
206
|
+
css: dateTextFieldWrapper
|
|
207
|
+
}, jsx(TextField, {
|
|
208
|
+
name: "datetextfield",
|
|
209
|
+
value: inputText,
|
|
210
|
+
ref: this.handleInputRef,
|
|
211
|
+
onChange: this.handleChange,
|
|
212
|
+
onKeyPress: this.handleKeyPress,
|
|
213
|
+
onKeyDown: this.handleKeyDown,
|
|
214
|
+
spellCheck: false,
|
|
215
|
+
autoComplete: "off",
|
|
216
|
+
isInvalid: displayError
|
|
217
|
+
}), displayError && jsx(ErrorMessage, null, formatMessage(messages.invalidDateError)));
|
|
218
|
+
}
|
|
219
|
+
}, {
|
|
220
|
+
key: "componentDidUpdate",
|
|
221
|
+
value: function componentDidUpdate() {
|
|
222
|
+
var setInputSelectionPos = this.setInputSelectionPos;
|
|
223
|
+
if (this.inputRef && setInputSelectionPos !== undefined) {
|
|
224
|
+
this.inputRef.setSelectionRange(setInputSelectionPos, setInputSelectionPos);
|
|
225
|
+
this.setInputSelectionPos = undefined;
|
|
226
|
+
}
|
|
227
|
+
if (this.inputRef && this.props.autoFocus) {
|
|
228
|
+
// TODO: Check if input already has focus
|
|
229
|
+
this.focusInput();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Don't select all text here - would seleect text on each keystroke
|
|
233
|
+
}
|
|
234
|
+
}, {
|
|
235
|
+
key: "componentWillUnmount",
|
|
236
|
+
value: function componentWillUnmount() {
|
|
237
|
+
if (this.autofocusTimeout !== undefined) {
|
|
238
|
+
clearTimeout(this.autofocusTimeout);
|
|
239
|
+
}
|
|
240
|
+
if (this.autoSelectAllTimeout !== undefined) {
|
|
241
|
+
clearTimeout(this.autoSelectAllTimeout);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}]);
|
|
245
|
+
return DatePickerInput;
|
|
246
|
+
}(React.Component);
|
|
247
|
+
export default injectIntl(DatePickerInput);
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
|
+
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
+
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
|
|
4
|
+
import _inherits from "@babel/runtime/helpers/inherits";
|
|
5
|
+
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
|
6
|
+
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
|
7
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
8
|
+
import _taggedTemplateLiteral from "@babel/runtime/helpers/taggedTemplateLiteral";
|
|
9
|
+
var _templateObject;
|
|
10
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
11
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
12
|
+
/** @jsx jsx */
|
|
13
|
+
import React from 'react';
|
|
14
|
+
import { css, jsx } from '@emotion/react';
|
|
15
|
+
import ReactDOM from 'react-dom';
|
|
16
|
+
import { injectIntl } from 'react-intl-next';
|
|
17
|
+
import Calendar from '@atlaskit/calendar';
|
|
18
|
+
import { INPUT_METHOD } from '@atlaskit/editor-common/analytics';
|
|
19
|
+
import { Popup, withOuterListeners } from '@atlaskit/editor-common/ui';
|
|
20
|
+
import { timestampToIsoFormat, timestampToUTCDate } from '@atlaskit/editor-common/utils';
|
|
21
|
+
import { akEditorFloatingDialogZIndex } from '@atlaskit/editor-shared-styles';
|
|
22
|
+
import { N0, N60A } from '@atlaskit/theme/colors';
|
|
23
|
+
import { borderRadius } from '@atlaskit/theme/constants';
|
|
24
|
+
var PopupWithListeners = withOuterListeners(Popup);
|
|
25
|
+
import DatePickerInput from './date-picker-input';
|
|
26
|
+
|
|
27
|
+
// eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage
|
|
28
|
+
var popupContentWrapper = css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n padding: ", ";\n border-radius: ", "px;\n box-shadow: ", ";\n background-color: ", ";\n"])), "var(--ds-space-025, 2px)", borderRadius(), "var(--ds-shadow-overlay, ".concat("0 4px 8px -2px ".concat(N60A, ", 0 0 1px ").concat(N60A), ")"), "var(--ds-surface-overlay, ".concat(N0, ")"));
|
|
29
|
+
// eslint-disable-next-line @repo/internal/react/no-class-components
|
|
30
|
+
var DatePicker = /*#__PURE__*/function (_React$Component) {
|
|
31
|
+
_inherits(DatePicker, _React$Component);
|
|
32
|
+
var _super = _createSuper(DatePicker);
|
|
33
|
+
function DatePicker(props) {
|
|
34
|
+
var _this;
|
|
35
|
+
_classCallCheck(this, DatePicker);
|
|
36
|
+
_this = _super.call(this, props);
|
|
37
|
+
_defineProperty(_assertThisInitialized(_this), "handleNewDate", function (date) {
|
|
38
|
+
_this.props.onTextChanged(date);
|
|
39
|
+
_this.setState({
|
|
40
|
+
latestValidDate: date
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
_defineProperty(_assertThisInitialized(_this), "handleKeyboardSubmitDate", function (date) {
|
|
44
|
+
_this.props.onSelect(date, INPUT_METHOD.KEYBOARD);
|
|
45
|
+
});
|
|
46
|
+
_defineProperty(_assertThisInitialized(_this), "handleEmptySubmitDate", function () {
|
|
47
|
+
_this.props.onDelete();
|
|
48
|
+
});
|
|
49
|
+
_defineProperty(_assertThisInitialized(_this), "handleOnChange", function (_ref) {
|
|
50
|
+
var day = _ref.day,
|
|
51
|
+
month = _ref.month,
|
|
52
|
+
year = _ref.year;
|
|
53
|
+
var date = {
|
|
54
|
+
day: day,
|
|
55
|
+
month: month,
|
|
56
|
+
year: year
|
|
57
|
+
};
|
|
58
|
+
_this.setState({
|
|
59
|
+
latestValidDate: date
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
_defineProperty(_assertThisInitialized(_this), "closeDatePickerWithAnalytics", function () {
|
|
63
|
+
_this.props.closeDatePickerWithAnalytics({
|
|
64
|
+
date: _this.state.latestValidDate
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
_defineProperty(_assertThisInitialized(_this), "handleRef", function (ref) {
|
|
68
|
+
var elm = ref && ReactDOM.findDOMNode(ref);
|
|
69
|
+
if (elm) {
|
|
70
|
+
elm.focus();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
var timestamp = props.element.getAttribute('timestamp');
|
|
74
|
+
if (timestamp) {
|
|
75
|
+
// Warning: The 'Date' return type of timestampToUTCDate() is not a JS date, it's more similar
|
|
76
|
+
// to the DateType type
|
|
77
|
+
var _timestampToUTCDate = timestampToUTCDate(timestamp),
|
|
78
|
+
day = _timestampToUTCDate.day,
|
|
79
|
+
month = _timestampToUTCDate.month,
|
|
80
|
+
year = _timestampToUTCDate.year;
|
|
81
|
+
var _date = {
|
|
82
|
+
day: day,
|
|
83
|
+
month: month,
|
|
84
|
+
year: year
|
|
85
|
+
};
|
|
86
|
+
_this.state = {
|
|
87
|
+
selected: [timestampToIsoFormat(timestamp)],
|
|
88
|
+
date: _date,
|
|
89
|
+
latestValidDate: _date
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return _this;
|
|
93
|
+
}
|
|
94
|
+
_createClass(DatePicker, [{
|
|
95
|
+
key: "render",
|
|
96
|
+
value: function render() {
|
|
97
|
+
var _this$props = this.props,
|
|
98
|
+
element = _this$props.element,
|
|
99
|
+
_onSelect = _this$props.onSelect,
|
|
100
|
+
mountTo = _this$props.mountTo,
|
|
101
|
+
boundariesElement = _this$props.boundariesElement,
|
|
102
|
+
scrollableElement = _this$props.scrollableElement,
|
|
103
|
+
intl = _this$props.intl,
|
|
104
|
+
dispatchAnalyticsEvent = _this$props.dispatchAnalyticsEvent,
|
|
105
|
+
isNew = _this$props.isNew,
|
|
106
|
+
autoFocus = _this$props.autoFocus,
|
|
107
|
+
weekStartDay = _this$props.weekStartDay;
|
|
108
|
+
var timestamp = element.getAttribute('timestamp');
|
|
109
|
+
if (this.state === null) {
|
|
110
|
+
// Without this, you can blow up the page by slowing down cpu, opening date, typing after date
|
|
111
|
+
// then clicking on date lozenge and typing quickly before it opens
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
var _this$state = this.state,
|
|
115
|
+
date = _this$state.date,
|
|
116
|
+
selected = _this$state.selected,
|
|
117
|
+
latestValidDate = _this$state.latestValidDate;
|
|
118
|
+
var day = latestValidDate.day,
|
|
119
|
+
month = latestValidDate.month,
|
|
120
|
+
year = latestValidDate.year;
|
|
121
|
+
if (!timestamp) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
return jsx(PopupWithListeners, {
|
|
125
|
+
target: element,
|
|
126
|
+
offset: [0, 8],
|
|
127
|
+
fitHeight: 327,
|
|
128
|
+
fitWidth: 340,
|
|
129
|
+
handleClickOutside: this.closeDatePickerWithAnalytics,
|
|
130
|
+
handleEscapeKeydown: this.closeDatePickerWithAnalytics,
|
|
131
|
+
zIndex: akEditorFloatingDialogZIndex,
|
|
132
|
+
mountTo: mountTo,
|
|
133
|
+
boundariesElement: boundariesElement,
|
|
134
|
+
scrollableElement: scrollableElement,
|
|
135
|
+
ariaLabel: null
|
|
136
|
+
}, jsx("div", {
|
|
137
|
+
css: popupContentWrapper
|
|
138
|
+
}, jsx(DatePickerInput, {
|
|
139
|
+
date: date,
|
|
140
|
+
onNewDate: this.handleNewDate,
|
|
141
|
+
onSubmitDate: this.handleKeyboardSubmitDate,
|
|
142
|
+
onEmptySubmit: this.handleEmptySubmitDate,
|
|
143
|
+
locale: intl.locale,
|
|
144
|
+
dispatchAnalyticsEvent: dispatchAnalyticsEvent,
|
|
145
|
+
autoFocus: autoFocus,
|
|
146
|
+
autoSelectAll: isNew
|
|
147
|
+
}), jsx(Calendar, {
|
|
148
|
+
onChange: this.handleOnChange,
|
|
149
|
+
onSelect: function onSelect(date) {
|
|
150
|
+
return _onSelect(date, INPUT_METHOD.PICKER);
|
|
151
|
+
},
|
|
152
|
+
day: day,
|
|
153
|
+
month: month,
|
|
154
|
+
year: year,
|
|
155
|
+
selected: selected,
|
|
156
|
+
ref: this.handleRef,
|
|
157
|
+
weekStartDay: weekStartDay,
|
|
158
|
+
testId: 'datepicker'
|
|
159
|
+
})));
|
|
160
|
+
}
|
|
161
|
+
}]);
|
|
162
|
+
return DatePicker;
|
|
163
|
+
}(React.Component);
|
|
164
|
+
export default injectIntl(DatePicker);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { createLocalizationProvider } from '@atlaskit/locale';
|
|
2
|
+
/**
|
|
3
|
+
* Attempt to parse a string representing a date in a particular locale to a date object
|
|
4
|
+
* @param dateString The string representing the date in the given locale, eg '02/12/2000'
|
|
5
|
+
* @param l10n The localisation provider created by createLocalizationProvider
|
|
6
|
+
* @returns Editor DateType when can parse, null when can't parse or invalid
|
|
7
|
+
*/
|
|
8
|
+
export function parseDateType(dateString, locale) {
|
|
9
|
+
try {
|
|
10
|
+
var l10n = createLocalizationProvider(locale);
|
|
11
|
+
var date = l10n.parseDate(dateString);
|
|
12
|
+
|
|
13
|
+
// If date is invalid
|
|
14
|
+
if (isNaN(date.getTime())) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
var year = date.getFullYear();
|
|
18
|
+
if (year < 1000 || year > 9999) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
var dateObj = {
|
|
22
|
+
day: date.getDate(),
|
|
23
|
+
month: date.getMonth() + 1,
|
|
24
|
+
year: year
|
|
25
|
+
};
|
|
26
|
+
return dateObj;
|
|
27
|
+
} catch (e) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Convert an EditorDateType to a date string string formatted for a particular locale
|
|
34
|
+
* @param date The date object
|
|
35
|
+
* @param locale The locale code string (eg. "en-AU")
|
|
36
|
+
* @returns Date string, eg "25/5/20"
|
|
37
|
+
*/
|
|
38
|
+
export function formatDateType(date, locale) {
|
|
39
|
+
var day = date.day,
|
|
40
|
+
month = date.month,
|
|
41
|
+
year = date.year;
|
|
42
|
+
var l10n = createLocalizationProvider(locale);
|
|
43
|
+
|
|
44
|
+
// The JS Date api represents month as a number between 0-11 :)
|
|
45
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
|
46
|
+
var dateObj = new Date(year, month - 1, day);
|
|
47
|
+
return l10n.formatDate(dateObj);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Convert an Editor DateType to a JavaScript Date object
|
|
52
|
+
* @param date Editor DateType
|
|
53
|
+
* @returns JavaScript Date object
|
|
54
|
+
*/
|
|
55
|
+
export function dateTypeToDate(date) {
|
|
56
|
+
var day = date.day,
|
|
57
|
+
month = date.month,
|
|
58
|
+
year = date.year;
|
|
59
|
+
// The JS Date api represents month as a number between 0-11 :)
|
|
60
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
|
61
|
+
var dateObj = new Date(year, month - 1, day);
|
|
62
|
+
return dateObj;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Convert a JavaScript Date to an editor DateType
|
|
67
|
+
* @param date JavaScript Date object
|
|
68
|
+
* @returns Editor DateType
|
|
69
|
+
*/
|
|
70
|
+
export function dateToDateType(date) {
|
|
71
|
+
var dateObj = {
|
|
72
|
+
day: date.getDate(),
|
|
73
|
+
// The JS Date api represents month as a number between 0-11 :)
|
|
74
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
|
75
|
+
month: date.getMonth() + 1,
|
|
76
|
+
year: date.getFullYear()
|
|
77
|
+
};
|
|
78
|
+
return dateObj;
|
|
79
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
2
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
3
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
4
|
+
import addDays from 'date-fns/addDays';
|
|
5
|
+
import addMonths from 'date-fns/addMonths';
|
|
6
|
+
import addYears from 'date-fns/addYears';
|
|
7
|
+
import { dateToDateType, dateTypeToDate, formatDateType } from './formatParse';
|
|
8
|
+
function isDigit(c) {
|
|
9
|
+
if (c === undefined) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return c >= '0' && c <= '9';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Check if cursor is in first segment of a date.
|
|
17
|
+
* @param cursorPos Cursor pos, with 0 referring to the left of first char
|
|
18
|
+
* @param date Date string in any locale
|
|
19
|
+
*/
|
|
20
|
+
function isCursorInFirstDateSegment(cursorPos, date) {
|
|
21
|
+
var posCounter = cursorPos - 1;
|
|
22
|
+
var isAdjacent = true;
|
|
23
|
+
// The date without any non-digit characters on the end
|
|
24
|
+
var strippedDate = date.replace(/[^0-9]+$/g, '');
|
|
25
|
+
while (posCounter >= 0 && isAdjacent) {
|
|
26
|
+
var c = strippedDate[posCounter];
|
|
27
|
+
if (!isDigit(c)) {
|
|
28
|
+
isAdjacent = false;
|
|
29
|
+
}
|
|
30
|
+
posCounter -= 1;
|
|
31
|
+
}
|
|
32
|
+
return isAdjacent;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Check if cursor is in last segment of a date.
|
|
37
|
+
* @param cursorPos Cursor pos, with 0 referring to the left of first char
|
|
38
|
+
* @param date Date string in any locale
|
|
39
|
+
*/
|
|
40
|
+
function isCursorInLastDateSegment(cursorPos, date) {
|
|
41
|
+
var posCounter = cursorPos;
|
|
42
|
+
var isAdjacent = true;
|
|
43
|
+
// The date without any non-digit characters on the end
|
|
44
|
+
var strippedDate = date.replace(/[^0-9]+$/g, '');
|
|
45
|
+
while (posCounter < strippedDate.length && isAdjacent) {
|
|
46
|
+
var c = strippedDate[posCounter];
|
|
47
|
+
if (!isDigit(c)) {
|
|
48
|
+
isAdjacent = false;
|
|
49
|
+
}
|
|
50
|
+
posCounter += 1;
|
|
51
|
+
}
|
|
52
|
+
return isAdjacent;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Inconclusively check if a date string is valid - a value of false means it is definitely
|
|
57
|
+
* invalid, a value of true means it might be valid.
|
|
58
|
+
* @param date Date string to be parsed
|
|
59
|
+
*/
|
|
60
|
+
export function isDatePossiblyValid(date) {
|
|
61
|
+
var _iterator = _createForOfIteratorHelper(date),
|
|
62
|
+
_step;
|
|
63
|
+
try {
|
|
64
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
65
|
+
var c = _step.value;
|
|
66
|
+
var isNumber = c >= '0' && c <= '9';
|
|
67
|
+
var isValidPunctuation = '. ,/'.indexOf(c) !== -1;
|
|
68
|
+
if (!(isNumber || isValidPunctuation)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
} catch (err) {
|
|
73
|
+
_iterator.e(err);
|
|
74
|
+
} finally {
|
|
75
|
+
_iterator.f();
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Find the segment of a date a position refers to. Eg: pos 2 in 29/03/2020 is in
|
|
82
|
+
* the day segment.
|
|
83
|
+
* @param position Cursor position, with 0 referring to the left of the first char
|
|
84
|
+
* @param date The localised date string
|
|
85
|
+
* @param locale The language to interpret the date string in
|
|
86
|
+
*/
|
|
87
|
+
export function findDateSegmentByPosition(position, date, locale) {
|
|
88
|
+
if (position > date.length) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
var placeholder = getLocaleDatePlaceholder(locale);
|
|
92
|
+
if (!placeholder) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// The placeholder without any non-digit characters on the end
|
|
97
|
+
var strippedPlaceholder = placeholder.replace(/[^ymd]+$/g, '');
|
|
98
|
+
var keyToSegment = {
|
|
99
|
+
d: 'day',
|
|
100
|
+
m: 'month',
|
|
101
|
+
y: 'year'
|
|
102
|
+
};
|
|
103
|
+
var firstSegment = keyToSegment[strippedPlaceholder[0]];
|
|
104
|
+
var lastSegment = keyToSegment[strippedPlaceholder[strippedPlaceholder.length - 1]];
|
|
105
|
+
var allPossibleSegments = ['day', 'month', 'year'];
|
|
106
|
+
var middleSegment = allPossibleSegments.filter(function (s) {
|
|
107
|
+
return s !== firstSegment && s !== lastSegment;
|
|
108
|
+
})[0];
|
|
109
|
+
if (isCursorInFirstDateSegment(position, date)) {
|
|
110
|
+
return firstSegment;
|
|
111
|
+
}
|
|
112
|
+
if (isCursorInLastDateSegment(position, date)) {
|
|
113
|
+
return lastSegment;
|
|
114
|
+
}
|
|
115
|
+
return middleSegment;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Generate a placeholder date string for a given locale
|
|
120
|
+
* eg: locale 'hu-HU' -> 'yyyy. mm. dd.'
|
|
121
|
+
* @param locale A locale string supported by Intl.DateTimeFormat
|
|
122
|
+
* @returns A placeholder string. d=1 or 2 digit day, dd=zero padded
|
|
123
|
+
* day, same for month but letter m, yyyy=year
|
|
124
|
+
*/
|
|
125
|
+
export function getLocaleDatePlaceholder(locale) {
|
|
126
|
+
var uniqueDateType = {
|
|
127
|
+
day: 7,
|
|
128
|
+
month: 1,
|
|
129
|
+
year: 1992
|
|
130
|
+
};
|
|
131
|
+
var localisedDateString = formatDateType(uniqueDateType, locale);
|
|
132
|
+
var shortDateFormat = localisedDateString.replace(/\d+/g, function (str) {
|
|
133
|
+
if (!str) {
|
|
134
|
+
return '';
|
|
135
|
+
}
|
|
136
|
+
var num = parseInt(str);
|
|
137
|
+
switch (num % 100) {
|
|
138
|
+
case 92:
|
|
139
|
+
return str.replace(/.{1}/g, 'y');
|
|
140
|
+
case 1:
|
|
141
|
+
return str.length === 1 ? 'm' : 'mm';
|
|
142
|
+
case 7:
|
|
143
|
+
return str.length === 1 ? 'd' : 'dd';
|
|
144
|
+
}
|
|
145
|
+
return '';
|
|
146
|
+
});
|
|
147
|
+
return shortDateFormat;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Adjust date segment up or down. Eg. If day is the active segment and adjustment is -1,
|
|
152
|
+
* reduce the day by one.
|
|
153
|
+
* @param date Valid datetype
|
|
154
|
+
* @param activeSegment which part of the date is selected/being adjusted
|
|
155
|
+
* @param adjustment how many units the segment is being adjusted (can be pos or neg, usually 1 or -1)
|
|
156
|
+
*/
|
|
157
|
+
export function adjustDate(date, activeSegment, adjustment) {
|
|
158
|
+
var originalDate = dateTypeToDate(date);
|
|
159
|
+
var newDate = activeSegment === 'day' ? addDays(originalDate, adjustment) : activeSegment === 'month' ? addMonths(originalDate, adjustment) : addYears(originalDate, adjustment);
|
|
160
|
+
return dateToDateType(newDate);
|
|
161
|
+
}
|
|
162
|
+
export function isToday(date) {
|
|
163
|
+
var today = new Date();
|
|
164
|
+
return date !== undefined && today.getDate() === date.day && date.month === today.getMonth() + 1 && date.year === today.getFullYear();
|
|
165
|
+
}
|