From ac69ab82156ae50c45489c6528afcd3bac1aad81 Mon Sep 17 00:00:00 2001 From: Katrin Khilko Date: Mon, 9 Dec 2024 17:14:15 +0300 Subject: [PATCH] FIO-8541: replaced moment dependency with dayjs --- gulpfile.js | 6 --- package.json | 4 +- src/Element.js | 2 +- src/Webform.js | 6 ++- src/components/datetime/DateTime.js | 15 ++++-- src/components/day/Day.js | 6 +-- src/components/time/Time.js | 2 +- src/utils/calendarUtils.js | 2 +- .../conditionOperators/DateGreaterThan.js | 2 +- src/utils/moment-wrapper.js | 30 +++++++++++ yarn.lock | 52 +++---------------- 11 files changed, 61 insertions(+), 66 deletions(-) create mode 100644 src/utils/moment-wrapper.js diff --git a/gulpfile.js b/gulpfile.js index aad43eaec5..de70f0b689 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -81,14 +81,8 @@ gulp.task('embed-css', () => gulp.src('./dist/formio.embed.min.css').pipe(rename gulp.task('clean:embed-js', () => gulp.src('./dist/formio.embed.js', { read: false, allowEmpty: true }).pipe(clean())); gulp.task('embed-js', () => gulp.src('./dist/formio.embed.min.js').pipe(rename('formio.embed.js')).pipe(gulp.dest('./dist'))); -// Copy over the moment-timezones to the resource folder. -gulp.task('timezones', () => gulp.src('./node_modules/moment-timezone/data/packed/latest.json').pipe(gulp.dest('./resources'))); - // Create a new build. gulp.task('build', gulp.series( - gulp.parallel( - 'timezones' - ), gulp.parallel( 'styles-embed', 'styles-form', diff --git a/package.json b/package.json index a6656d7916..23a1635849 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ "dependencies": { "@formio/bootstrap": "3.0.0-dev.98.17ba6ea", "@formio/choices.js": "^10.2.1", - "@formio/core": "2.1.0-dev.191.8c609ab", + "@formio/core": "2.1.0-dev.201.2792d73", "@formio/text-mask-addons": "^3.8.0-formio.3", "@formio/vanilla-text-mask": "^5.1.1-formio.1", "abortcontroller-polyfill": "^1.7.5", @@ -107,8 +107,6 @@ "jstimezonedetect": "^1.0.7", "jwt-decode": "^3.1.2", "lodash": "^4.17.21", - "moment": "^2.29.4", - "moment-timezone": "^0.5.44", "quill": "^2.0.2", "signature_pad": "^4.2.0", "string-hash": "^1.1.3", diff --git a/src/Element.js b/src/Element.js index f663ae9b44..8c2c17c994 100644 --- a/src/Element.js +++ b/src/Element.js @@ -3,7 +3,7 @@ import { Formio } from './Formio'; import * as FormioUtils from './utils/utils'; import { I18n } from './utils/i18n'; import _ from 'lodash'; -import moment from 'moment'; +import moment from './utils/moment-wrapper'; import maskInput from '@formio/vanilla-text-mask'; /** diff --git a/src/Webform.js b/src/Webform.js index ba65bd5079..9ee9ac9b6a 100644 --- a/src/Webform.js +++ b/src/Webform.js @@ -1,5 +1,6 @@ import _ from "lodash"; -import moment from "moment"; +import dayjs from "dayjs"; +import utc from "dayjs/plugin/utc"; import { compareVersions } from "compare-versions"; import EventEmitter from "./EventEmitter"; import i18nDefaults from "./i18n"; @@ -15,6 +16,7 @@ import { getArrayFromComponentPath, } from "./utils/utils"; import { eachComponent } from "./utils/formUtils"; +dayjs.extend(utc); // We need this here because dragula pulls in CustomEvent class that requires global to exist. if (typeof window !== 'undefined' && typeof window.global === 'undefined') { @@ -1511,7 +1513,7 @@ export default class Webform extends NestedDataComponent { submission.metadata = submission.metadata || {}; _.defaults(submission.metadata, { timezone: _.get(this, "_submission.metadata.timezone", currentTimezone()), - offset: parseInt(_.get(this, "_submission.metadata.offset", moment().utcOffset()), 10), + offset: parseInt(_.get(this, "_submission.metadata.offset", dayjs().utcOffset()), 10), origin: document.location.origin, referrer: document.referrer, browserName: navigator.appName, diff --git a/src/components/datetime/DateTime.js b/src/components/datetime/DateTime.js index 05d222bfc9..b181d93b9c 100644 --- a/src/components/datetime/DateTime.js +++ b/src/components/datetime/DateTime.js @@ -1,8 +1,17 @@ import _ from 'lodash'; -import moment from 'moment'; +import moment from '../../utils/moment-wrapper'; +import dayjs from 'dayjs'; +import utc from "dayjs/plugin/utc"; +import timezone from "dayjs/plugin/timezone"; +import customParseFormat from "dayjs/plugin/customParseFormat"; +import advancedFormat from 'dayjs/plugin/advancedFormat'; import FormioUtils from '../../utils'; import { componentValueTypes, getComponentSavedTypes } from '../../utils/utils'; import Input from '../_classes/input/Input'; +dayjs.extend(utc); +dayjs.extend(timezone); +dayjs.extend(customParseFormat); +dayjs.extend(advancedFormat); export default class DateTimeComponent extends Input { static schema(...extend) { @@ -207,8 +216,8 @@ export default class DateTimeComponent extends Input { } if (Array.isArray(value) && this.component.multiple) { - return value.map(item => _.trim(moment(item).format(format))).join(', '); + return value.map(item => _.trim(dayjs(item).format(format))).join(', '); } - return (value ? _.trim(moment(value).format(format)) : value) || ''; + return (value ? _.trim(dayjs(value).format(format)) : value) || ''; } } diff --git a/src/components/day/Day.js b/src/components/day/Day.js index 5e5ed62727..87b6a2208e 100644 --- a/src/components/day/Day.js +++ b/src/components/day/Day.js @@ -1,5 +1,5 @@ import _ from 'lodash'; -import moment from 'moment'; +import dayjs from 'dayjs'; import Field from '../_classes/field/Field'; import { boolValue, componentValueTypes, getComponentSavedTypes, getLocaleDateFormatInfo } from '../../utils/utils'; @@ -59,10 +59,10 @@ export default class DayComponent extends Field { constructor(component, options, data) { if (component.maxDate && component.maxDate.indexOf('moment(') === -1) { - component.maxDate = moment(component.maxDate, 'YYYY-MM-DD').toISOString(); + component.maxDate = dayjs(component.maxDate, 'YYYY-MM-DD').toISOString(); } if (component.minDate && component.minDate.indexOf('moment(') === -1) { - component.minDate = moment(component.minDate, 'YYYY-MM-DD').toISOString(); + component.minDate = dayjs(component.minDate, 'YYYY-MM-DD').toISOString(); } super(component, options, data); } diff --git a/src/components/time/Time.js b/src/components/time/Time.js index c762985cfa..dfd6546f53 100644 --- a/src/components/time/Time.js +++ b/src/components/time/Time.js @@ -1,4 +1,4 @@ -import moment from 'moment'; +import moment from '../../utils/moment-wrapper'; import TextFieldComponent from '../textfield/TextField'; import { getBrowserInfo } from '../../utils/utils'; diff --git a/src/utils/calendarUtils.js b/src/utils/calendarUtils.js index ff33374646..ae5c79e0f5 100644 --- a/src/utils/calendarUtils.js +++ b/src/utils/calendarUtils.js @@ -1,4 +1,4 @@ -import moment from 'moment'; +import moment from './moment-wrapper'; import _ from 'lodash'; export const CALENDAR_ERROR_MESSAGES = { diff --git a/src/utils/conditionOperators/DateGreaterThan.js b/src/utils/conditionOperators/DateGreaterThan.js index 8324ce7025..8113a2f138 100644 --- a/src/utils/conditionOperators/DateGreaterThan.js +++ b/src/utils/conditionOperators/DateGreaterThan.js @@ -1,5 +1,5 @@ import ConditionOperator from './ConditionOperator'; -import moment from 'moment'; +import moment from '../moment-wrapper'; export default class DateGeaterThan extends ConditionOperator { static get operatorKey() { return 'dateGreaterThan'; diff --git a/src/utils/moment-wrapper.js b/src/utils/moment-wrapper.js new file mode 100644 index 0000000000..65eeb2c979 --- /dev/null +++ b/src/utils/moment-wrapper.js @@ -0,0 +1,30 @@ +import dayjs from "dayjs"; +import utc from "dayjs/plugin/utc"; +import timezone from "dayjs/plugin/timezone" +import customParseFormat from 'dayjs/plugin/customParseFormat'; +import advancedFormat from 'dayjs/plugin/advancedFormat'; +import duration from 'dayjs/plugin/duration'; + +dayjs.extend(utc); +dayjs.extend(timezone); +dayjs.extend(customParseFormat); +dayjs.extend(advancedFormat); +dayjs.extend(duration); + +function moment(...args) { + return dayjs(...args); +} + +moment.utc = () => dayjs.utc(); +moment.tz = (date, timezone) => dayjs.tz(date, timezone); + + +moment.toISOString = () => { + return dayjs.isValid() ? dayjs.toISOString() : "Invalid date"; +} + +moment.format = (formatStr) => { + return dayjs(this).format(formatStr); +}; + +export default moment; diff --git a/yarn.lock b/yarn.lock index a600f82a4e..8521752790 100644 --- a/yarn.lock +++ b/yarn.lock @@ -381,10 +381,10 @@ fuse.js "^6.6.2" redux "^4.2.0" -"@formio/core@2.1.0-dev.191.8c609ab": - version "2.1.0-dev.191.8c609ab" - resolved "https://registry.yarnpkg.com/@formio/core/-/core-2.1.0-dev.191.8c609ab.tgz#2e442888c60786268ca16edc7cd26c38cbd4b773" - integrity sha512-lVj8hqddIwUJiWI6/yWF0NFl/f8QPS3ek4l6h0U1SFMPmeEdWQtcBTMLKi02gHx09kDgXhYocJIbBVVpYyqFnw== +"@formio/core@2.1.0-dev.201.2792d73": + version "2.1.0-dev.201.2792d73" + resolved "https://registry.yarnpkg.com/@formio/core/-/core-2.1.0-dev.201.2792d73.tgz#2cfaabe219a823ee7687b2ec2c7ffc355a1c5301" + integrity sha512-sGrWflkplGOb+y4JPQe1KtiQ9RYNvd1pClQbz0C1o9Gu7k1fjCXxIB2Y5dfpPKA2Xh39GHaDegbjPUrYHn9jew== dependencies: browser-cookies "^1.2.0" core-js "^3.38.0" @@ -396,7 +396,6 @@ inputmask "5.0.9" json-logic-js "^2.0.5" lodash "^4.17.21" - moment "^2.29.4" "@formio/text-mask-addons@^3.8.0-formio.3": version "3.8.0-formio.3" @@ -5524,18 +5523,6 @@ mock-local-storage@^1.1.24: core-js "^3.30.2" global "^4.3.2" -moment-timezone@^0.5.44: - version "0.5.45" - resolved "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.45.tgz#cb685acd56bac10e69d93c536366eb65aa6bcf5c" - integrity sha512-HIWmqA86KcmCAhnMAN0wuDOARV/525R2+lOLotuGFzn4HO+FH+/645z2wx0Dt3iDv6/p61SIvKnDstISainhLQ== - dependencies: - moment "^2.29.4" - -moment@^2.29.4: - version "2.30.1" - resolved "https://registry.npmjs.org/moment/-/moment-2.30.1.tgz#f8c91c07b7a786e30c59926df530b4eac96974ae" - integrity sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how== - mrmime@^2.0.0: version "2.0.0" resolved "https://registry.npmjs.org/mrmime/-/mrmime-2.0.0.tgz#151082a6e06e59a9a39b46b3e14d5cfe92b3abb4" @@ -7324,7 +7311,7 @@ string-replace-loader@^3.1.0: loader-utils "^2.0.0" schema-utils "^3.0.0" -"string-width-cjs@npm:string-width@^4.2.0": +"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0: version "4.2.3" resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -7342,15 +7329,6 @@ string-width@^1.0.1, string-width@^1.0.2: is-fullwidth-code-point "^1.0.0" strip-ansi "^3.0.0" -string-width@^4.1.0, string-width@^4.2.0: - version "4.2.3" - resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" - integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== - dependencies: - emoji-regex "^8.0.0" - is-fullwidth-code-point "^3.0.0" - strip-ansi "^6.0.1" - string-width@^5.0.1, string-width@^5.1.2: version "5.1.2" resolved "https://registry.npmjs.org/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794" @@ -7411,7 +7389,7 @@ stringifier@^1.3.0: traverse "^0.6.6" type-name "^2.0.1" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1": +"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -7432,13 +7410,6 @@ strip-ansi@^4.0.0: dependencies: ansi-regex "^3.0.0" -strip-ansi@^6.0.0, strip-ansi@^6.0.1: - version "6.0.1" - resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" - integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== - dependencies: - ansi-regex "^5.0.1" - strip-ansi@^7.0.1: version "7.1.0" resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45" @@ -8501,7 +8472,7 @@ workerpool@6.2.1: resolved "https://registry.npmjs.org/workerpool/-/workerpool-6.2.1.tgz#46fc150c17d826b86a008e5a4508656777e9c343" integrity sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw== -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== @@ -8527,15 +8498,6 @@ wrap-ansi@^6.2.0: string-width "^4.1.0" strip-ansi "^6.0.0" -wrap-ansi@^7.0.0: - version "7.0.0" - resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" - integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== - dependencies: - ansi-styles "^4.0.0" - string-width "^4.1.0" - strip-ansi "^6.0.0" - wrap-ansi@^8.1.0: version "8.1.0" resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"