Add a clock widget

It's a simple clock that shows the current time

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
This commit is contained in:
Yarden Shoham 2024-05-18 22:40:33 +03:00
parent 0bbb4573b2
commit 4a27ec3271
8 changed files with 62 additions and 0 deletions

View File

@ -11,6 +11,7 @@
* Weather * Weather
* Bookmarks * Bookmarks
* Latest YouTube videos from specific channels * Latest YouTube videos from specific channels
* Clock
* Calendar * Calendar
* Stocks * Stocks
* iframe * iframe

View File

@ -17,6 +17,7 @@
- [Repository](#repository) - [Repository](#repository)
- [Bookmarks](#bookmarks) - [Bookmarks](#bookmarks)
- [Calendar](#calendar) - [Calendar](#calendar)
- [Clock](#clock)
- [Stocks](#stocks) - [Stocks](#stocks)
- [Twitch Channels](#twitch-channels) - [Twitch Channels](#twitch-channels)
- [Twitch Top Games](#twitch-top-games) - [Twitch Top Games](#twitch-top-games)
@ -34,6 +35,7 @@ pages:
columns: columns:
- size: small - size: small
widgets: widgets:
- type: clock
- type: calendar - type: calendar
- type: rss - type: rss
@ -963,6 +965,19 @@ Whether to open the link in the same tab or a new one.
Whether to hide the colored arrow on each link. Whether to hide the colored arrow on each link.
### Clock
Display a clock showing the current time.
Example:
```yaml
- type: clock
```
Preview:
![](images/clock-widget-preview.png)
### Calendar ### Calendar
Display a calendar. Display a calendar.

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -341,6 +341,20 @@ function afterContentReady(callback) {
contentReadyCallbacks.push(callback); contentReadyCallbacks.push(callback);
} }
function updateClocks(elements, formatter) {
const currentDate = new Date();
for (const elem of elements) {
elem.textContent = formatter.format(currentDate);
}
}
function setupClocks() {
const clockFormatter = new Intl.DateTimeFormat(undefined, {minute: "numeric", hour: "numeric"});
const elements = document.getElementsByClassName("glance-clock");
updateClocks(elements, clockFormatter)
setInterval(() => {updateClocks(elements, clockFormatter)}, 1000);
}
async function setupPage() { async function setupPage() {
const pageElement = document.getElementById("page"); const pageElement = document.getElementById("page");
const pageContentElement = document.getElementById("page-content"); const pageContentElement = document.getElementById("page-content");
@ -349,6 +363,7 @@ async function setupPage() {
pageContentElement.innerHTML = pageContent; pageContentElement.innerHTML = pageContent;
try { try {
setupClocks()
setupCarousels(); setupCarousels();
setupCollapsibleLists(); setupCollapsibleLists();
setupCollapsibleGrids(); setupCollapsibleGrids();

View File

@ -15,6 +15,7 @@ var (
PageTemplate = compileTemplate("page.html", "document.html", "page-style-overrides.gotmpl") PageTemplate = compileTemplate("page.html", "document.html", "page-style-overrides.gotmpl")
PageContentTemplate = compileTemplate("content.html") PageContentTemplate = compileTemplate("content.html")
CalendarTemplate = compileTemplate("calendar.html", "widget-base.html") CalendarTemplate = compileTemplate("calendar.html", "widget-base.html")
ClockTemplate = compileTemplate("clock.html", "widget-base.html")
BookmarksTemplate = compileTemplate("bookmarks.html", "widget-base.html") BookmarksTemplate = compileTemplate("bookmarks.html", "widget-base.html")
IFrameTemplate = compileTemplate("iframe.html", "widget-base.html") IFrameTemplate = compileTemplate("iframe.html", "widget-base.html")
WeatherTemplate = compileTemplate("weather.html", "widget-base.html") WeatherTemplate = compileTemplate("weather.html", "widget-base.html")

View File

@ -0,0 +1,5 @@
{{ template "widget-base.html" . }}
{{ define "widget-content" }}
<div class="size-h2 color-highlight text-center glance-clock"></div>
{{ end }}

23
internal/widget/clock.go Normal file
View File

@ -0,0 +1,23 @@
package widget
import (
"context"
"html/template"
"github.com/glanceapp/glance/internal/assets"
)
type Clock struct {
widgetBase `yaml:",inline"`
}
func (widget *Clock) Initialize() error {
widget.withTitle("Clock").withError(nil)
return nil
}
func (widget *Clock) Update(ctx context.Context) {}
func (widget *Clock) Render() template.HTML {
return widget.render(widget, assets.ClockTemplate)
}

View File

@ -19,6 +19,8 @@ func New(widgetType string) (Widget, error) {
switch widgetType { switch widgetType {
case "calendar": case "calendar":
return &Calendar{}, nil return &Calendar{}, nil
case "clock":
return &Clock{}, nil
case "weather": case "weather":
return &Weather{}, nil return &Weather{}, nil
case "bookmarks": case "bookmarks":