From 3ce25db3bccbebf37d90b1a7e34cad1548dc7c5e Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 11 Mar 2024 19:59:55 +0100 Subject: [PATCH 01/14] Fix date rendering by adding `` --- modules/timeutil/datetime.go | 15 ++++----- templates/devtest/gitea-ui.tmpl | 10 ++++++ web_src/js/webcomponents/GiteaLocaleDate.js | 37 +++++++++++++++++++++ web_src/js/webcomponents/webcomponents.js | 1 + 4 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 web_src/js/webcomponents/GiteaLocaleDate.js diff --git a/modules/timeutil/datetime.go b/modules/timeutil/datetime.go index 62b94f7cf481c..6f914b4c67752 100644 --- a/modules/timeutil/datetime.go +++ b/modules/timeutil/datetime.go @@ -52,17 +52,16 @@ func DateTime(format string, datetime any, extraAttrs ...string) template.HTML { attrs := make([]string, 0, 10+len(extraAttrs)) attrs = append(attrs, extraAttrs...) attrs = append(attrs, `data-tooltip-content`, `data-tooltip-interactive="true"`) - attrs = append(attrs, `format="datetime"`, `weekday=""`, `year="numeric"`) + attrs = append(attrs, `weekday=""`, `year="numeric"`) switch format { - case "short": - attrs = append(attrs, `month="short"`, `day="numeric"`) - case "long": - attrs = append(attrs, `month="long"`, `day="numeric"`) - case "full": - attrs = append(attrs, `month="short"`, `day="numeric"`, `hour="numeric"`, `minute="numeric"`, `second="numeric"`) + case "short", "long": // date only, render using + attrs = append(attrs, `month="`+format+`"`, `day="numeric"`) + return template.HTML(fmt.Sprintf(``, strings.Join(attrs, " "), datetimeEscaped)) + case "full": // full date including time + attrs = append(attrs, `format="datetime"`, `month="short"`, `day="numeric"`, `hour="numeric"`, `minute="numeric"`, `second="numeric"`) + return template.HTML(fmt.Sprintf(`%s`, strings.Join(attrs, " "), datetimeEscaped, textEscaped)) default: panic(fmt.Sprintf("Unsupported format %s", format)) } - return template.HTML(fmt.Sprintf(`%s`, strings.Join(attrs, " "), datetimeEscaped, textEscaped)) } diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index ccf188609c91d..929485eb7f58a 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -110,6 +110,16 @@
+
+

GiteaLocaleDate

+
+
+
+
+
+
relative-time:
+
+

LocaleNumber

{{ctx.Locale.PrettyNumber 1}}
diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaLocaleDate.js new file mode 100644 index 0000000000000..fa33bb16b4c96 --- /dev/null +++ b/web_src/js/webcomponents/GiteaLocaleDate.js @@ -0,0 +1,37 @@ +window.customElements.define('gitea-locale-date', class extends HTMLElement { + static observedAttributes = ['date', 'year', 'month', 'weekday', 'day']; + + update = () => { + const year = this.getAttribute('year') ?? 'numeric'; + const month = this.getAttribute('month') ?? 'short'; + const weekday = this.getAttribute('weekday') ?? ''; + const day = this.getAttribute('day') ?? 'numeric'; + const lang = this.closest('[lang]')?.getAttribute('lang') || + this.ownerDocument.documentElement.getAttribute('lang') || + ''; + const date = new Date(this.getAttribute('date')); + + // apply negative timezone offset because `new Date()` above assumes that `yyyy-mm-dd` is + // a UTC date, so the local date will have a offset towards the user's timezone. + // Ref: https://stackoverflow.com/a/14569783/808699 + const correctedDate = new Date(date.getTime() - date.getTimezoneOffset() * -60000); + + this.textContent = correctedDate.toLocaleString(lang ?? [], { + ...(year && {year}), + ...(month && {month}), + ...(weekday && {weekday}), + ...(day && {day}), + }); + }; + + attributeChangedCallback(_name, oldValue, newValue) { + if (oldValue === newValue || !this.initialized) return; + this.update(); + } + + connectedCallback() { + this.initialized = false; + this.update(); + this.initialized = true; + } +}); diff --git a/web_src/js/webcomponents/webcomponents.js b/web_src/js/webcomponents/webcomponents.js index 916a588db64bf..b942f58ffc089 100644 --- a/web_src/js/webcomponents/webcomponents.js +++ b/web_src/js/webcomponents/webcomponents.js @@ -3,3 +3,4 @@ import './polyfill.js'; import '@github/relative-time-element'; import './GiteaOriginUrl.js'; +import './GiteaLocaleDate.js'; From a27ce9898636332c50c651f2deb2fa6a60b7ded8 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 11 Mar 2024 17:51:33 -0400 Subject: [PATCH 02/14] add shadowRoot and fallback text --- modules/timeutil/datetime.go | 2 +- web_src/js/webcomponents/GiteaLocaleDate.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/timeutil/datetime.go b/modules/timeutil/datetime.go index 6f914b4c67752..8d520140c064b 100644 --- a/modules/timeutil/datetime.go +++ b/modules/timeutil/datetime.go @@ -57,7 +57,7 @@ func DateTime(format string, datetime any, extraAttrs ...string) template.HTML { switch format { case "short", "long": // date only, render using attrs = append(attrs, `month="`+format+`"`, `day="numeric"`) - return template.HTML(fmt.Sprintf(``, strings.Join(attrs, " "), datetimeEscaped)) + return template.HTML(fmt.Sprintf(`%s`, strings.Join(attrs, " "), datetimeEscaped, textEscaped)) case "full": // full date including time attrs = append(attrs, `format="datetime"`, `month="short"`, `day="numeric"`, `hour="numeric"`, `minute="numeric"`, `second="numeric"`) return template.HTML(fmt.Sprintf(`%s`, strings.Join(attrs, " "), datetimeEscaped, textEscaped)) diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaLocaleDate.js index fa33bb16b4c96..a1494daf420b7 100644 --- a/web_src/js/webcomponents/GiteaLocaleDate.js +++ b/web_src/js/webcomponents/GiteaLocaleDate.js @@ -16,7 +16,8 @@ window.customElements.define('gitea-locale-date', class extends HTMLElement { // Ref: https://stackoverflow.com/a/14569783/808699 const correctedDate = new Date(date.getTime() - date.getTimezoneOffset() * -60000); - this.textContent = correctedDate.toLocaleString(lang ?? [], { + if (!this.shadowRoot) this.attachShadow({mode: 'open'}); + this.shadowRoot.textContent = correctedDate.toLocaleString(lang ?? [], { ...(year && {year}), ...(month && {month}), ...(weekday && {weekday}), From ef17866b88be91270440e6df003f3e7ef383ae9b Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 11 Mar 2024 17:56:30 -0400 Subject: [PATCH 03/14] update comment --- web_src/js/webcomponents/GiteaLocaleDate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaLocaleDate.js index a1494daf420b7..d18a9ceedc518 100644 --- a/web_src/js/webcomponents/GiteaLocaleDate.js +++ b/web_src/js/webcomponents/GiteaLocaleDate.js @@ -12,7 +12,7 @@ window.customElements.define('gitea-locale-date', class extends HTMLElement { const date = new Date(this.getAttribute('date')); // apply negative timezone offset because `new Date()` above assumes that `yyyy-mm-dd` is - // a UTC date, so the local date will have a offset towards the user's timezone. + // a UTC date, so the local date will have a offset towards UTC which we reverse here. // Ref: https://stackoverflow.com/a/14569783/808699 const correctedDate = new Date(date.getTime() - date.getTimezoneOffset() * -60000); From 870e3b70b83023cb221f03efc401e271919c21d2 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 11 Mar 2024 23:11:08 +0100 Subject: [PATCH 04/14] partial test fix --- modules/timeutil/datetime_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/timeutil/datetime_test.go b/modules/timeutil/datetime_test.go index 26494b84754f9..0b3370d5c3a06 100644 --- a/modules/timeutil/datetime_test.go +++ b/modules/timeutil/datetime_test.go @@ -27,16 +27,16 @@ func TestDateTime(t *testing.T) { assert.EqualValues(t, "-", DateTime("short", TimeStamp(0))) actual := DateTime("short", "invalid") - assert.EqualValues(t, `invalid`, actual) + assert.EqualValues(t, `invalid`, actual) actual = DateTime("short", refTimeStr) - assert.EqualValues(t, `2018-01-01T00:00:00Z`, actual) + assert.EqualValues(t, `2018-01-01T00:00:00Z`, actual) actual = DateTime("short", refTime) - assert.EqualValues(t, `2018-01-01`, actual) + assert.EqualValues(t, `2018-01-01`, actual) actual = DateTime("short", refTimeStamp) - assert.EqualValues(t, `2017-12-31`, actual) + assert.EqualValues(t, `2017-12-31`, actual) actual = DateTime("full", refTimeStamp) assert.EqualValues(t, `2017-12-31 19:00:00 -05:00`, actual) From bc709344e3a21194c2a1c02f68baaea0f1b10f40 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 11 Mar 2024 23:28:09 +0100 Subject: [PATCH 05/14] fix tests --- modules/timeutil/datetime_test.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/timeutil/datetime_test.go b/modules/timeutil/datetime_test.go index 0b3370d5c3a06..3dff7bb68b687 100644 --- a/modules/timeutil/datetime_test.go +++ b/modules/timeutil/datetime_test.go @@ -18,6 +18,7 @@ func TestDateTime(t *testing.T) { defer test.MockVariableValue(&setting.DefaultUILocation, testTz)() refTimeStr := "2018-01-01T00:00:00Z" + refDateStr := "2018-01-01" refTime, _ := time.Parse(time.RFC3339, refTimeStr) refTimeStamp := TimeStamp(refTime.Unix()) @@ -27,17 +28,20 @@ func TestDateTime(t *testing.T) { assert.EqualValues(t, "-", DateTime("short", TimeStamp(0))) actual := DateTime("short", "invalid") - assert.EqualValues(t, `invalid`, actual) + assert.EqualValues(t, `invalid`, actual) actual = DateTime("short", refTimeStr) - assert.EqualValues(t, `2018-01-01T00:00:00Z`, actual) + assert.EqualValues(t, `2018-01-01T00:00:00Z`, actual) actual = DateTime("short", refTime) - assert.EqualValues(t, `2018-01-01`, actual) + assert.EqualValues(t, `2018-01-01`, actual) + + actual = DateTime("short", refDateStr) + assert.EqualValues(t, `2018-01-01`, actual) actual = DateTime("short", refTimeStamp) - assert.EqualValues(t, `2017-12-31`, actual) + assert.EqualValues(t, `2017-12-31`, actual) actual = DateTime("full", refTimeStamp) - assert.EqualValues(t, `2017-12-31 19:00:00 -05:00`, actual) + assert.EqualValues(t, `2017-12-31 19:00:00 -05:00`, actual) } From ceb95fed30f56ba3efeff8ae849b3ee48dda790d Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 11 Mar 2024 18:37:53 -0400 Subject: [PATCH 06/14] default all js attrs to empty --- templates/devtest/gitea-ui.tmpl | 11 +++++------ web_src/js/webcomponents/GiteaLocaleDate.js | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index 929485eb7f58a..675bfd06bd331 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -112,12 +112,11 @@

GiteaLocaleDate

-
-
-
-
-
-
relative-time:
+
+
+
+
+
relative-time:
diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaLocaleDate.js index d18a9ceedc518..ec9f2c98fed93 100644 --- a/web_src/js/webcomponents/GiteaLocaleDate.js +++ b/web_src/js/webcomponents/GiteaLocaleDate.js @@ -2,10 +2,10 @@ window.customElements.define('gitea-locale-date', class extends HTMLElement { static observedAttributes = ['date', 'year', 'month', 'weekday', 'day']; update = () => { - const year = this.getAttribute('year') ?? 'numeric'; - const month = this.getAttribute('month') ?? 'short'; + const year = this.getAttribute('year') ?? ''; + const month = this.getAttribute('month') ?? ''; const weekday = this.getAttribute('weekday') ?? ''; - const day = this.getAttribute('day') ?? 'numeric'; + const day = this.getAttribute('day') ?? ''; const lang = this.closest('[lang]')?.getAttribute('lang') || this.ownerDocument.documentElement.getAttribute('lang') || ''; From 44da6fb94126bd695065269bad5d916d80cc4c94 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 11 Mar 2024 18:43:49 -0400 Subject: [PATCH 07/14] extract only yyyy-mm-dd in js --- templates/devtest/gitea-ui.tmpl | 1 + web_src/js/webcomponents/GiteaLocaleDate.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index 675bfd06bd331..05e64a48ea585 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -116,6 +116,7 @@
+
relative-time:
diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaLocaleDate.js index ec9f2c98fed93..2ef78c2c3cd8b 100644 --- a/web_src/js/webcomponents/GiteaLocaleDate.js +++ b/web_src/js/webcomponents/GiteaLocaleDate.js @@ -9,7 +9,9 @@ window.customElements.define('gitea-locale-date', class extends HTMLElement { const lang = this.closest('[lang]')?.getAttribute('lang') || this.ownerDocument.documentElement.getAttribute('lang') || ''; - const date = new Date(this.getAttribute('date')); + + // only extract the `yyyy-mm-dd` part + const date = new Date(this.getAttribute('date').substring(0, 10)); // apply negative timezone offset because `new Date()` above assumes that `yyyy-mm-dd` is // a UTC date, so the local date will have a offset towards UTC which we reverse here. From c7589d8fb50e712dd2f5e9550dd85d3e043128a4 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 11 Mar 2024 18:48:20 -0400 Subject: [PATCH 08/14] fix closing tags --- templates/devtest/gitea-ui.tmpl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index 05e64a48ea585..c48bdf29e5ac4 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -112,11 +112,11 @@

GiteaLocaleDate

-
-
-
-
-
+
+
+
+
+
relative-time:
From 48c739231c55688e0666e8777a319cca0073a7b0 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 11 Mar 2024 23:50:48 +0100 Subject: [PATCH 09/14] swap check --- web_src/js/webcomponents/GiteaLocaleDate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaLocaleDate.js index 2ef78c2c3cd8b..61702c63f26f5 100644 --- a/web_src/js/webcomponents/GiteaLocaleDate.js +++ b/web_src/js/webcomponents/GiteaLocaleDate.js @@ -28,7 +28,7 @@ window.customElements.define('gitea-locale-date', class extends HTMLElement { }; attributeChangedCallback(_name, oldValue, newValue) { - if (oldValue === newValue || !this.initialized) return; + if (!this.initialized || oldValue === newValue) return; this.update(); } From cd9c7fb558251243bee528cb68728e2dc081906e Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 12 Mar 2024 19:22:45 +0100 Subject: [PATCH 10/14] Update web_src/js/webcomponents/GiteaLocaleDate.js --- web_src/js/webcomponents/GiteaLocaleDate.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaLocaleDate.js index 61702c63f26f5..5ebf2f66c671c 100644 --- a/web_src/js/webcomponents/GiteaLocaleDate.js +++ b/web_src/js/webcomponents/GiteaLocaleDate.js @@ -10,7 +10,9 @@ window.customElements.define('gitea-locale-date', class extends HTMLElement { this.ownerDocument.documentElement.getAttribute('lang') || ''; - // only extract the `yyyy-mm-dd` part + // only extract the `yyyy-mm-dd` part. When converting to Date, the date will be in UTC and when rendered + // as locale date, will have a offset towards UTC added. We should eventually use `Temporal.PlainDate` here + // to avoid needing to remove this offset: https://tc39.es/proposal-temporal/docs/plaindate.html const date = new Date(this.getAttribute('date').substring(0, 10)); // apply negative timezone offset because `new Date()` above assumes that `yyyy-mm-dd` is From d75a670f1a7ba969d688a7a19a25a79fdc31c50b Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 12 Mar 2024 19:30:37 +0100 Subject: [PATCH 11/14] comment fix --- web_src/js/webcomponents/GiteaLocaleDate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaLocaleDate.js index 5ebf2f66c671c..86d4c61ebc5fa 100644 --- a/web_src/js/webcomponents/GiteaLocaleDate.js +++ b/web_src/js/webcomponents/GiteaLocaleDate.js @@ -10,7 +10,7 @@ window.customElements.define('gitea-locale-date', class extends HTMLElement { this.ownerDocument.documentElement.getAttribute('lang') || ''; - // only extract the `yyyy-mm-dd` part. When converting to Date, the date will be in UTC and when rendered + // only extract the `yyyy-mm-dd` part. When converting to Date, it will become midnight UTC and when rendered // as locale date, will have a offset towards UTC added. We should eventually use `Temporal.PlainDate` here // to avoid needing to remove this offset: https://tc39.es/proposal-temporal/docs/plaindate.html const date = new Date(this.getAttribute('date').substring(0, 10)); From 0b98d59843d717afa4ce9d6dd4b683e02b9e82db Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 12 Mar 2024 19:33:05 +0100 Subject: [PATCH 12/14] merge comments --- web_src/js/webcomponents/GiteaLocaleDate.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaLocaleDate.js index 86d4c61ebc5fa..2a526ae301353 100644 --- a/web_src/js/webcomponents/GiteaLocaleDate.js +++ b/web_src/js/webcomponents/GiteaLocaleDate.js @@ -11,13 +11,11 @@ window.customElements.define('gitea-locale-date', class extends HTMLElement { ''; // only extract the `yyyy-mm-dd` part. When converting to Date, it will become midnight UTC and when rendered - // as locale date, will have a offset towards UTC added. We should eventually use `Temporal.PlainDate` here - // to avoid needing to remove this offset: https://tc39.es/proposal-temporal/docs/plaindate.html + // as localized date, will have a offset towards UTC, which we remove to shift the timestamp to midnight in the + // localized date. We should eventually use `Temporal.PlainDate` which will make the correction unnecessary. + // - https://stackoverflow.com/a/14569783/808699 + // - https://tc39.es/proposal-temporal/docs/plaindate.html const date = new Date(this.getAttribute('date').substring(0, 10)); - - // apply negative timezone offset because `new Date()` above assumes that `yyyy-mm-dd` is - // a UTC date, so the local date will have a offset towards UTC which we reverse here. - // Ref: https://stackoverflow.com/a/14569783/808699 const correctedDate = new Date(date.getTime() - date.getTimezoneOffset() * -60000); if (!this.shadowRoot) this.attachShadow({mode: 'open'}); From f82351aa95efe34e4d4c86c4614c1b52645beef1 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 12 Mar 2024 22:02:05 +0100 Subject: [PATCH 13/14] rename to gitea-absolute-date --- modules/timeutil/datetime.go | 4 ++-- modules/timeutil/datetime_test.go | 10 +++++----- templates/devtest/gitea-ui.tmpl | 14 +++++++------- .../{GiteaLocaleDate.js => GiteaAbsoluteDate.js} | 2 +- web_src/js/webcomponents/webcomponents.js | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) rename web_src/js/webcomponents/{GiteaLocaleDate.js => GiteaAbsoluteDate.js} (95%) diff --git a/modules/timeutil/datetime.go b/modules/timeutil/datetime.go index 8d520140c064b..1001b6f0e1d6c 100644 --- a/modules/timeutil/datetime.go +++ b/modules/timeutil/datetime.go @@ -55,9 +55,9 @@ func DateTime(format string, datetime any, extraAttrs ...string) template.HTML { attrs = append(attrs, `weekday=""`, `year="numeric"`) switch format { - case "short", "long": // date only, render using + case "short", "long": // date only, render using attrs = append(attrs, `month="`+format+`"`, `day="numeric"`) - return template.HTML(fmt.Sprintf(`%s`, strings.Join(attrs, " "), datetimeEscaped, textEscaped)) + return template.HTML(fmt.Sprintf(`%s`, strings.Join(attrs, " "), datetimeEscaped, textEscaped)) case "full": // full date including time attrs = append(attrs, `format="datetime"`, `month="short"`, `day="numeric"`, `hour="numeric"`, `minute="numeric"`, `second="numeric"`) return template.HTML(fmt.Sprintf(`%s`, strings.Join(attrs, " "), datetimeEscaped, textEscaped)) diff --git a/modules/timeutil/datetime_test.go b/modules/timeutil/datetime_test.go index 3dff7bb68b687..5af8682dcf03e 100644 --- a/modules/timeutil/datetime_test.go +++ b/modules/timeutil/datetime_test.go @@ -28,19 +28,19 @@ func TestDateTime(t *testing.T) { assert.EqualValues(t, "-", DateTime("short", TimeStamp(0))) actual := DateTime("short", "invalid") - assert.EqualValues(t, `invalid`, actual) + assert.EqualValues(t, `invalid`, actual) actual = DateTime("short", refTimeStr) - assert.EqualValues(t, `2018-01-01T00:00:00Z`, actual) + assert.EqualValues(t, `2018-01-01T00:00:00Z`, actual) actual = DateTime("short", refTime) - assert.EqualValues(t, `2018-01-01`, actual) + assert.EqualValues(t, `2018-01-01`, actual) actual = DateTime("short", refDateStr) - assert.EqualValues(t, `2018-01-01`, actual) + assert.EqualValues(t, `2018-01-01`, actual) actual = DateTime("short", refTimeStamp) - assert.EqualValues(t, `2017-12-31`, actual) + assert.EqualValues(t, `2017-12-31`, actual) actual = DateTime("full", refTimeStamp) assert.EqualValues(t, `2017-12-31 19:00:00 -05:00`, actual) diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index c48bdf29e5ac4..e551572b96c1b 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -111,13 +111,13 @@
-

GiteaLocaleDate

-
-
-
-
-
-
relative-time:
+

GiteaAbsoluteDate

+
+
+
+
+
+
relative-time:
diff --git a/web_src/js/webcomponents/GiteaLocaleDate.js b/web_src/js/webcomponents/GiteaAbsoluteDate.js similarity index 95% rename from web_src/js/webcomponents/GiteaLocaleDate.js rename to web_src/js/webcomponents/GiteaAbsoluteDate.js index 2a526ae301353..660aa99d07036 100644 --- a/web_src/js/webcomponents/GiteaLocaleDate.js +++ b/web_src/js/webcomponents/GiteaAbsoluteDate.js @@ -1,4 +1,4 @@ -window.customElements.define('gitea-locale-date', class extends HTMLElement { +window.customElements.define('gitea-absolute-date', class extends HTMLElement { static observedAttributes = ['date', 'year', 'month', 'weekday', 'day']; update = () => { diff --git a/web_src/js/webcomponents/webcomponents.js b/web_src/js/webcomponents/webcomponents.js index b942f58ffc089..03348d895fe4a 100644 --- a/web_src/js/webcomponents/webcomponents.js +++ b/web_src/js/webcomponents/webcomponents.js @@ -3,4 +3,4 @@ import './polyfill.js'; import '@github/relative-time-element'; import './GiteaOriginUrl.js'; -import './GiteaLocaleDate.js'; +import './GiteaAbsoluteDate.js'; From 86a46fb4c15ce11e093dbc55a17c84e9caa7f1b4 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 12 Mar 2024 22:31:06 +0100 Subject: [PATCH 14/14] remove unnecessary tooltip attributes --- modules/timeutil/datetime.go | 5 ++--- modules/timeutil/datetime_test.go | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/modules/timeutil/datetime.go b/modules/timeutil/datetime.go index 1001b6f0e1d6c..50c8d44f132be 100644 --- a/modules/timeutil/datetime.go +++ b/modules/timeutil/datetime.go @@ -51,15 +51,14 @@ func DateTime(format string, datetime any, extraAttrs ...string) template.HTML { attrs := make([]string, 0, 10+len(extraAttrs)) attrs = append(attrs, extraAttrs...) - attrs = append(attrs, `data-tooltip-content`, `data-tooltip-interactive="true"`) attrs = append(attrs, `weekday=""`, `year="numeric"`) switch format { - case "short", "long": // date only, render using + case "short", "long": // date only attrs = append(attrs, `month="`+format+`"`, `day="numeric"`) return template.HTML(fmt.Sprintf(`%s`, strings.Join(attrs, " "), datetimeEscaped, textEscaped)) case "full": // full date including time - attrs = append(attrs, `format="datetime"`, `month="short"`, `day="numeric"`, `hour="numeric"`, `minute="numeric"`, `second="numeric"`) + attrs = append(attrs, `format="datetime"`, `month="short"`, `day="numeric"`, `hour="numeric"`, `minute="numeric"`, `second="numeric"`, `data-tooltip-content`, `data-tooltip-interactive="true"`) return template.HTML(fmt.Sprintf(`%s`, strings.Join(attrs, " "), datetimeEscaped, textEscaped)) default: panic(fmt.Sprintf("Unsupported format %s", format)) diff --git a/modules/timeutil/datetime_test.go b/modules/timeutil/datetime_test.go index 5af8682dcf03e..39aecbc43bdf6 100644 --- a/modules/timeutil/datetime_test.go +++ b/modules/timeutil/datetime_test.go @@ -28,20 +28,20 @@ func TestDateTime(t *testing.T) { assert.EqualValues(t, "-", DateTime("short", TimeStamp(0))) actual := DateTime("short", "invalid") - assert.EqualValues(t, `invalid`, actual) + assert.EqualValues(t, `invalid`, actual) actual = DateTime("short", refTimeStr) - assert.EqualValues(t, `2018-01-01T00:00:00Z`, actual) + assert.EqualValues(t, `2018-01-01T00:00:00Z`, actual) actual = DateTime("short", refTime) - assert.EqualValues(t, `2018-01-01`, actual) + assert.EqualValues(t, `2018-01-01`, actual) actual = DateTime("short", refDateStr) - assert.EqualValues(t, `2018-01-01`, actual) + assert.EqualValues(t, `2018-01-01`, actual) actual = DateTime("short", refTimeStamp) - assert.EqualValues(t, `2017-12-31`, actual) + assert.EqualValues(t, `2017-12-31`, actual) actual = DateTime("full", refTimeStamp) - assert.EqualValues(t, `2017-12-31 19:00:00 -05:00`, actual) + assert.EqualValues(t, `2017-12-31 19:00:00 -05:00`, actual) }