Skip to content

Commit 57965bb

Browse files
committedDec 23, 2020
Initial commit
0 parents  commit 57965bb

18 files changed

+2243
-0
lines changed
 

‎.eslintrc.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
root: true,
3+
parser: "@typescript-eslint/parser",
4+
plugins: ["@typescript-eslint"],
5+
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
6+
rules: {
7+
"@typescript-eslint/no-unused-vars": [
8+
2,
9+
{ args: "all", argsIgnorePattern: "^_" },
10+
],
11+
},
12+
};

‎.github/FUNDING.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github: [liamcain]
2+
custom: ["https://paypal.me/hiliam", "https://buymeacoffee.com/liamcain"]

‎.github/workflows/main.yml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Build plugin
2+
3+
on:
4+
push:
5+
# Sequence of patterns matched against refs/tags
6+
tags:
7+
- "*" # Push events to matching any tag format, i.e. 1.0, 20.15.10
8+
9+
env:
10+
PLUGIN_NAME: calendar
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Use Node.js
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: "14.x" # You might need to adjust this value to your own version
22+
- name: Build
23+
id: build
24+
run: |
25+
npm install -g yarn
26+
yarn
27+
yarn run build --if-present
28+
mkdir ${{ env.PLUGIN_NAME }}
29+
cp main.js manifest.json ${{ env.PLUGIN_NAME }}
30+
zip -r ${{ env.PLUGIN_NAME }}.zip ${{ env.PLUGIN_NAME }}
31+
ls
32+
echo "::set-output name=tag_name::$(git tag --sort version:refname | tail -n 1)"
33+
- name: Create Release
34+
id: create_release
35+
uses: actions/create-release@v1
36+
env:
37+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38+
VERSION: ${{ github.ref }}
39+
with:
40+
tag_name: ${{ github.ref }}
41+
release_name: ${{ github.ref }}
42+
draft: false
43+
prerelease: false
44+
- name: Upload zip file
45+
id: upload-zip
46+
uses: actions/upload-release-asset@v1
47+
env:
48+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49+
with:
50+
upload_url: ${{ steps.create_release.outputs.upload_url }}
51+
asset_path: ./${{ env.PLUGIN_NAME }}.zip
52+
asset_name: ${{ env.PLUGIN_NAME }}-${{ steps.build.outputs.tag_name }}.zip
53+
asset_content_type: application/zip
54+
- name: Upload main.js
55+
id: upload-main
56+
uses: actions/upload-release-asset@v1
57+
env:
58+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
59+
with:
60+
upload_url: ${{ steps.create_release.outputs.upload_url }}
61+
asset_path: ./main.js
62+
asset_name: main.js
63+
asset_content_type: text/javascript
64+
- name: Upload manifest.json
65+
id: upload-manifest
66+
uses: actions/upload-release-asset@v1
67+
env:
68+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
69+
with:
70+
upload_url: ${{ steps.create_release.outputs.upload_url }}
71+
asset_path: ./manifest.json
72+
asset_name: manifest.json
73+
asset_content_type: application/json

‎.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
dist/
3+
data.json
4+
main.js
5+
*.code-workspace

‎.prettierrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"svelteSortOrder": "scripts-markup-styles",
3+
"svelteStrictMode": true,
4+
"svelteBracketNewLine": true,
5+
"svelteAllowShorthand": true,
6+
"svelteIndentScriptAndStyle": true
7+
}

‎package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "obsidian-calendar-ui",
3+
"version": "0.0.1",
4+
"description": "Calendar UI that powers obsidian-calendar-plugin",
5+
"author": "liamcain",
6+
"main": "dist/index.js",
7+
"module": "dist/index.mjs",
8+
"svelte": "src/index.js",
9+
"license": "MIT",
10+
"scripts": {
11+
"lint": "svelte-check && eslint . --ext .ts",
12+
"build": "npm run lint && rollup -c"
13+
},
14+
"dependencies": {
15+
"obsidian-daily-notes-interface": "0.4.0",
16+
"svelte": "3.31.0",
17+
"tslib": "2.0.3"
18+
},
19+
"devDependencies": {
20+
"@rollup/plugin-commonjs": "17.0.0",
21+
"@rollup/plugin-node-resolve": "11.0.0",
22+
"@rollup/plugin-typescript": "8.0.0",
23+
"@testing-library/svelte": "3.0.0",
24+
"@tsconfig/svelte": "1.0.10",
25+
"@types/moment": "2.13.0",
26+
"@types/node": "14.14.13",
27+
"@typescript-eslint/eslint-plugin": "4.9.1",
28+
"@typescript-eslint/parser": "4.9.1",
29+
"eslint": "7.15.0",
30+
"moment": "2.29.1",
31+
"rollup": "2.34.2",
32+
"rollup-plugin-svelte": "7.0.0",
33+
"svelte-check": "1.1.22",
34+
"svelte-preprocess": "4.6.1",
35+
"typescript": "4.1.3"
36+
}
37+
}

‎rollup.config.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import svelte from "rollup-plugin-svelte";
2+
import resolve from "@rollup/plugin-node-resolve";
3+
import commonjs from "@rollup/plugin-commonjs";
4+
import typescript from "@rollup/plugin-typescript";
5+
import autoPreprocess from "svelte-preprocess";
6+
7+
import pkg from "./package.json";
8+
9+
export default {
10+
input: "src/index.ts",
11+
output: [
12+
{ file: pkg.module, format: "es", name: "obsidian-calendar-ui" },
13+
{ file: pkg.main, format: "umd", name: "obsidian-calendar-ui" },
14+
],
15+
external: ["fs", "os", "path"],
16+
plugins: [
17+
svelte({
18+
emitCss: false,
19+
preprocess: autoPreprocess(),
20+
}),
21+
typescript(),
22+
resolve({
23+
browser: true,
24+
dedupe: ["svelte"],
25+
}),
26+
commonjs({
27+
include: "node_modules/**",
28+
}),
29+
],
30+
};

‎src/Arrow.svelte

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<script lang="ts">
2+
export let onClick: () => void;
3+
export let tooltip: string;
4+
export let direction: "left" | "right";
5+
</script>
6+
7+
<div
8+
class="arrow"
9+
class:right="{direction === 'right'}"
10+
on:click="{onClick}"
11+
aria-label="{tooltip}"
12+
>
13+
<svg
14+
focusable="false"
15+
role="img"
16+
xmlns="http://www.w3.org/2000/svg"
17+
viewBox="0 0 320 512"
18+
><path
19+
fill="currentColor"
20+
d="M34.52 239.03L228.87 44.69c9.37-9.37 24.57-9.37 33.94 0l22.67 22.67c9.36 9.36 9.37 24.52.04 33.9L131.49 256l154.02 154.75c9.34 9.38 9.32 24.54-.04 33.9l-22.67 22.67c-9.37 9.37-24.57 9.37-33.94 0L34.52 272.97c-9.37-9.37-9.37-24.57 0-33.94z"
21+
></path></svg>
22+
</div>
23+
24+
<style>
25+
.arrow {
26+
align-items: center;
27+
cursor: pointer;
28+
display: flex;
29+
justify-content: center;
30+
width: 24px;
31+
}
32+
33+
.right {
34+
transform: rotate(180deg);
35+
}
36+
37+
.arrow svg {
38+
color: var(--color-arrow);
39+
height: 16px;
40+
width: 16px;
41+
}
42+
</style>

‎src/Calendar.svelte

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<script lang="ts">
2+
import type { Moment } from "moment";
3+
import { derived, Readable } from "svelte/store";
4+
5+
import Arrow from "./Arrow.svelte";
6+
import Day from "./Day.svelte";
7+
import WeekNum from "./WeekNum.svelte";
8+
import { displayedMonth, metadata } from "./stores";
9+
import {
10+
IMonth,
11+
getDaysOfWeek,
12+
getMonth,
13+
getStartOfWeek,
14+
isWeekend,
15+
} from "./utils";
16+
17+
export let today: Moment = window.moment();
18+
export let showWeekNums: boolean = false;
19+
20+
export let onHoverDay: (date: Moment, targetEl: EventTarget) => void;
21+
export let onHoverWeek: (date: Moment, targetEl: EventTarget) => void;
22+
export let onClickDay: (date: Moment, isMetaPressed: boolean) => void;
23+
export let onClickWeek: (date: Moment, isMetaPressed: boolean) => void;
24+
25+
export let localeData: any;
26+
export let dependencies: [Readable<any>, ...Array<Readable<any>>];
27+
28+
const dependencyStore = derived(dependencies, (values) => values);
29+
30+
let month: IMonth;
31+
let daysOfWeek: string[];
32+
33+
// Get the word 'Today' but localized to the current language
34+
const todayDisplayStr = today.calendar().split(/\d|\s/)[0];
35+
36+
$: month = getMonth($displayedMonth, localeData);
37+
$: daysOfWeek = getDaysOfWeek(localeData);
38+
</script>
39+
40+
<svelte:options immutable />
41+
<div id="calendar-container" class="container">
42+
<div class="nav">
43+
<h3 class="title" on:click="{displayedMonth.reset}">
44+
<span class="month">{$displayedMonth.format('MMM')}</span>
45+
<span class="year">{$displayedMonth.format('YYYY')}</span>
46+
</h3>
47+
<div class="right-nav">
48+
<Arrow
49+
direction="left"
50+
onClick="{displayedMonth.decrement}"
51+
tooltip="Previous Month"
52+
/>
53+
<div class="reset-button" on:click="{displayedMonth.reset}">
54+
{todayDisplayStr}
55+
</div>
56+
<Arrow
57+
direction="right"
58+
onClick="{displayedMonth.increment}"
59+
tooltip="Next Month"
60+
/>
61+
</div>
62+
</div>
63+
<table class="calendar">
64+
<colgroup>
65+
{#if showWeekNums}
66+
<col />
67+
{/if}
68+
{#each month[1].days as date}
69+
<col class:weekend="{isWeekend(date)}" />
70+
{/each}
71+
</colgroup>
72+
<thead>
73+
<tr>
74+
{#if showWeekNums}
75+
<th>W</th>
76+
{/if}
77+
{#each daysOfWeek as dayOfWeek}
78+
<th>{dayOfWeek}</th>
79+
{/each}
80+
</tr>
81+
</thead>
82+
<tbody>
83+
{#each month as week}
84+
<tr>
85+
{#if showWeekNums}
86+
<WeekNum
87+
{...week}
88+
onClick="{onClickWeek}"
89+
onHover="{onHoverWeek}"
90+
metadata="{metadata.getWeek(getStartOfWeek(week.days), $dependencyStore)}"
91+
/>
92+
{/if}
93+
{#each week.days as date (date.format())}
94+
<Day
95+
today="{today}"
96+
date="{date}"
97+
onClick="{onClickDay}"
98+
onHover="{onHoverDay}"
99+
metadata="{metadata.getDay(date, $dependencyStore)}"
100+
/>
101+
{/each}
102+
</tr>
103+
{/each}
104+
</tbody>
105+
</table>
106+
</div>
107+
108+
<style>
109+
.container {
110+
--color-background-heading: transparent;
111+
--color-background-day: transparent;
112+
--color-background-weeknum: transparent;
113+
--color-background-weekend: transparent;
114+
115+
--color-dot: var(--text-muted);
116+
--color-arrow: var(--text-muted);
117+
--color-button: var(--text-muted);
118+
119+
--color-text-title: var(--text-normal);
120+
--color-text-heading: var(--text-muted);
121+
--color-text-day: var(--text-normal);
122+
--color-text-today: var(--interactive-accent);
123+
--color-text-weeknum: var(--text-muted);
124+
}
125+
126+
.container {
127+
padding: 0 8px;
128+
}
129+
130+
th {
131+
text-align: center;
132+
}
133+
134+
.nav {
135+
align-items: center;
136+
display: flex;
137+
margin: 0.6em 0 1em;
138+
padding: 0 8px;
139+
width: 100%;
140+
}
141+
142+
.title {
143+
color: var(--color-text-title);
144+
font-size: 1.5em;
145+
margin: 0;
146+
}
147+
148+
.month {
149+
font-weight: 500;
150+
text-transform: capitalize;
151+
}
152+
153+
.year {
154+
color: var(--interactive-accent);
155+
}
156+
157+
.right-nav {
158+
display: flex;
159+
justify-content: center;
160+
margin-left: auto;
161+
}
162+
163+
.reset-button {
164+
border-radius: 4px;
165+
color: var(--text-muted);
166+
font-size: 0.7em;
167+
font-weight: 600;
168+
letter-spacing: 1px;
169+
margin: 0 4px;
170+
padding: 0px 4px;
171+
text-transform: uppercase;
172+
}
173+
174+
.weekend {
175+
background-color: var(--color-background-weekend);
176+
}
177+
178+
.calendar {
179+
border-collapse: collapse;
180+
width: 100%;
181+
}
182+
183+
th {
184+
background-color: var(--color-background-heading);
185+
color: var(--color-text-heading);
186+
font-size: 0.6em;
187+
letter-spacing: 1px;
188+
padding: 4px 8px;
189+
text-transform: uppercase;
190+
}
191+
</style>

‎src/CalendarSource.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Moment } from "moment";
2+
3+
export interface IDot {
4+
color: string;
5+
isFilled: boolean;
6+
}
7+
8+
export interface IDayMetadata {
9+
classes?: string[];
10+
dataAttributes?: string[];
11+
dots: Promise<IDot[]>;
12+
}
13+
14+
export interface IWeekMetadata {
15+
classes?: string[];
16+
dataAttributes?: string[];
17+
dots: Promise<IDot[]>;
18+
}
19+
20+
export abstract class CalendarSource {
21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22+
abstract getDailyMetadata(date: Moment, ...args: any[]): IDayMetadata;
23+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24+
abstract getWeeklyMetadata(date: Moment, ...args: any[]): IDayMetadata;
25+
}

‎src/Day.svelte

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<script lang="ts">
2+
import type { Moment } from "moment";
3+
4+
import Dot from "./Dot.svelte";
5+
import type { IDayMetadata } from "./CalendarSource";
6+
import { displayedMonth } from "./stores";
7+
import { isMetaPressed } from "./utils";
8+
9+
export let date: Moment;
10+
export let onHover: (date: Moment, targetEl: EventTarget) => void;
11+
export let onClick: (date: Moment, isMetaPressed: boolean) => void;
12+
export let today: Moment;
13+
export let metadata: IDayMetadata;
14+
</script>
15+
16+
<svelte:options immutable />
17+
<td>
18+
<div
19+
class="{`day ${metadata.classes.join(' ')}`}"
20+
class:adjacent-month="{!date.isSame($displayedMonth, 'month')}"
21+
class:today="{date.isSame(today, 'day')}"
22+
on:click="{(e) => {
23+
onClick(date, isMetaPressed(e));
24+
}}"
25+
on:pointerover="{(e) => {
26+
if (isMetaPressed(e)) {
27+
onHover(date, e.target);
28+
}
29+
}}"
30+
>
31+
{date.format('D')}
32+
33+
<div class="dot-container">
34+
{#await metadata.dots then dots}
35+
{#each dots as dot}
36+
<Dot {...dot} />
37+
{/each}
38+
{/await}
39+
</div>
40+
</div>
41+
</td>
42+
43+
<style>
44+
.day {
45+
background-color: var(--color-background-day);
46+
border-radius: 4px;
47+
color: var(--color-text-day);
48+
cursor: pointer;
49+
font-size: 0.8em;
50+
height: 100%;
51+
padding: 4px;
52+
position: relative;
53+
text-align: center;
54+
transition: background-color 0.1s ease-in, color 0.1s ease-in;
55+
vertical-align: baseline;
56+
}
57+
.day:hover {
58+
background-color: var(--interactive-hover);
59+
}
60+
61+
.day.active:hover {
62+
background-color: var(--interactive-accent-hover);
63+
}
64+
65+
.adjacent-month {
66+
opacity: 0.25;
67+
}
68+
69+
.today {
70+
color: var(--color-text-today);
71+
}
72+
73+
.active,
74+
.active.today {
75+
color: var(--text-on-accent);
76+
background-color: var(--interactive-accent);
77+
}
78+
79+
.dot-container {
80+
display: flex;
81+
flex-wrap: wrap;
82+
justify-content: center;
83+
line-height: 6px;
84+
min-height: 6px;
85+
}
86+
</style>

‎src/Dot.svelte

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<script lang="ts">
2+
export let isFilled: boolean;
3+
export let isActive: boolean;
4+
</script>
5+
6+
{#if isFilled}
7+
<svg
8+
class="dot"
9+
class:active="{isActive}"
10+
viewBox="0 0 6 6"
11+
xmlns="http://www.w3.org/2000/svg"
12+
>
13+
<circle cx="3" cy="3" r="2"></circle>
14+
</svg>
15+
{:else}
16+
<svg
17+
class="hollow"
18+
class:active="{isActive}"
19+
viewBox="0 0 6 6"
20+
xmlns="http://www.w3.org/2000/svg"
21+
>
22+
<circle cx="3" cy="3" r="2"></circle>
23+
</svg>
24+
{/if}
25+
26+
<style>
27+
.dot,
28+
.hollow {
29+
display: inline-block;
30+
fill: var(--color-dot);
31+
height: 6px;
32+
width: 6px;
33+
margin: 0 1px;
34+
}
35+
36+
.active.dot {
37+
fill: var(--text-on-accent);
38+
}
39+
40+
.hollow {
41+
fill: none;
42+
stroke: var(--color-dot);
43+
}
44+
45+
.active.hollow {
46+
stroke: var(--text-on-accent);
47+
}
48+
</style>

‎src/WeekNum.svelte

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<script lang="ts">
2+
import type { Moment } from "moment";
3+
4+
import Dot from "./Dot.svelte";
5+
import type { IWeekMetadata } from "./CalendarSource";
6+
import { getStartOfWeek, isMetaPressed } from "./utils";
7+
8+
export let weekNum: number;
9+
export let days: Moment[];
10+
export let metadata: IWeekMetadata;
11+
12+
export let onHover: (date: Moment, targetEl: EventTarget) => void;
13+
export let onClick: (date: Moment, isMetaPressed: boolean) => void;
14+
15+
let startOfWeek: Moment;
16+
let isActive: boolean;
17+
18+
// $: isActive = $activeFile && weeklyNote === $activeFile;
19+
$: startOfWeek = getStartOfWeek(days);
20+
</script>
21+
22+
<svelte:options immutable />
23+
<td>
24+
<div
25+
class="week-num"
26+
class:active="{isActive}"
27+
on:click="{(e) => {
28+
onClick(startOfWeek, isMetaPressed(e));
29+
}}"
30+
on:pointerover="{(e) => {
31+
if (isMetaPressed(e)) {
32+
onHover(startOfWeek, e.target);
33+
}
34+
}}"
35+
>
36+
{weekNum}
37+
<div class="dot-container">
38+
{#await metadata.dots then dots}
39+
{#each dots as dot}
40+
<Dot {...dot} />
41+
{/each}
42+
{/await}
43+
</div>
44+
</div>
45+
</td>
46+
47+
<style>
48+
.week-num {
49+
background-color: var(--color-background-weeknum);
50+
border-radius: 4px;
51+
color: var(--color-text-weeknum);
52+
cursor: pointer;
53+
font-size: 0.65em;
54+
height: 100%;
55+
padding: 4px;
56+
text-align: center;
57+
transition: background-color 0.1s ease-in, color 0.1s ease-in;
58+
vertical-align: baseline;
59+
}
60+
61+
td {
62+
border-right: 1px solid var(--background-modifier-border);
63+
}
64+
65+
.week-num:hover {
66+
background-color: var(--interactive-hover);
67+
}
68+
69+
.week-num.active:hover {
70+
background-color: var(--interactive-accent-hover);
71+
}
72+
73+
.active {
74+
color: var(--text-on-accent);
75+
background-color: var(--interactive-accent);
76+
}
77+
78+
.dot-container {
79+
display: flex;
80+
flex-wrap: wrap;
81+
justify-content: center;
82+
line-height: 6px;
83+
min-height: 6px;
84+
}
85+
</style>

‎src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Calendar from "./Calendar.svelte";
2+
3+
export default Calendar;

‎src/stores.ts

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import type { Moment } from "moment";
2+
import { writable } from "svelte/store";
3+
import { getDateUID } from "obsidian-daily-notes-interface";
4+
5+
import type {
6+
CalendarSource,
7+
IDayMetadata,
8+
IWeekMetadata,
9+
} from "./CalendarSource";
10+
11+
export class MetadataCache {
12+
private dailyCache: Record<string, IDayMetadata>;
13+
private weeklyCache: Record<string, IWeekMetadata>;
14+
private source: CalendarSource;
15+
16+
constructor() {
17+
this.dailyCache = {};
18+
this.weeklyCache = {};
19+
}
20+
21+
addSource(source: CalendarSource): void {
22+
this.source = source;
23+
}
24+
25+
setDay(date: Moment, metadata: IDayMetadata): void {
26+
const key = getDateUID(date);
27+
this.weeklyCache = Object.assign(
28+
{},
29+
{
30+
[key]: metadata,
31+
}
32+
);
33+
}
34+
35+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
36+
getDay(date: Moment, ..._args: any[]): IDayMetadata {
37+
const dateStr = getDateUID(date);
38+
let value = this.dailyCache[dateStr];
39+
if (value) {
40+
return value;
41+
}
42+
value = this.source.getDailyMetadata(date);
43+
this.setDay(date, value);
44+
return value;
45+
}
46+
47+
setWeek(date: Moment, metadata: IDayMetadata): void {
48+
const key = getDateUID(date);
49+
this.weeklyCache = Object.assign(
50+
{},
51+
{
52+
[key]: metadata,
53+
}
54+
);
55+
}
56+
57+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
58+
getWeek(date: Moment, ..._args: any[]): IDayMetadata {
59+
const dateStr = getDateUID(date);
60+
let value = this.weeklyCache[dateStr];
61+
if (value) {
62+
return value;
63+
}
64+
value = this.source.getWeeklyMetadata(date);
65+
this.setWeek(date, value);
66+
return value;
67+
}
68+
69+
reset(): void {
70+
this.dailyCache = {};
71+
this.weeklyCache = {};
72+
}
73+
}
74+
75+
function createDisplayedMonthStore() {
76+
const store = writable<Moment>(null);
77+
return {
78+
reset: () => store.set(window.moment()),
79+
increment: () => store.update((month) => month.clone().add(1, "months")),
80+
decrement: () =>
81+
store.update((month) => month.clone().subtract(1, "months")),
82+
...store,
83+
};
84+
}
85+
86+
export const displayedMonth = createDisplayedMonthStore();
87+
export const metadata = new MetadataCache();

‎src/utils.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import type { Moment } from "moment";
2+
import * as os from "os";
3+
4+
export interface IWeek {
5+
days: Moment[];
6+
weekNum: number;
7+
}
8+
9+
export type IMonth = IWeek[];
10+
11+
export function clamp(
12+
num: number,
13+
lowerBound: number,
14+
upperBound: number
15+
): number {
16+
return Math.min(Math.max(lowerBound, num), upperBound);
17+
}
18+
19+
export function getWordCount(text: string): number {
20+
const wordChars = /(?:[',.0-9;A-Z_a-z\xAA\xB2\xB3\xB5\xB9\xBA\xBC-\xBE\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0560-\u0589\u05D0-\u05EA\u05EF-\u05F2\u060C\u060D\u0620-\u064A\u0660-\u0669\u066C\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07C0-\u07EA\u07F4\u07F5\u07F8\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u0860-\u086A\u08A0-\u08B4\u08B6-\u08BD\u0904-\u0939\u093D\u0950\u0958-\u0961\u0966-\u096F\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09E6-\u09F1\u09F4-\u09F9\u09FC\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A66-\u0A6F\u0A72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u103F-\u1049\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u1090-\u1099\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1369-\u137C\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u17E0-\u17E9\u17F0-\u17F9\u1810-\u1819\u1820-\u1878\u1880-\u1884\u1887-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19DA\u1A00-\u1A16\u1A20-\u1A54\u1A80-\u1A89\u1A90-\u1A99\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B50-\u1B59\u1B83-\u1BA0\u1BAE-\u1BE5\u1C00-\u1C23\u1C40-\u1C49\u1C4D-\u1C7D\u1C80-\u1C88\u1C90-\u1CBA\u1CBD-\u1CBF\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2018\u2019\u2024\u203F\u2040\u2044\u2054\u2070\u2071\u2074-\u2079\u207F-\u2089\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2150-\u2189\u2460-\u249B\u24EA-\u24FF\u2776-\u2793\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2CFD\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312F\u3131-\u318E\u3192-\u3195\u31A0-\u31BA\u31F0-\u31FF\u3220-\u3229\u3248-\u324F\u3251-\u325F\u3280-\u3289\u32B1-\u32BF\u3400-\u4DB5\u4E00-\u9FEF\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA7B9\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA830-\uA835\uA840-\uA873\uA882-\uA8B3\uA8D0-\uA8D9\uA8F2-\uA8F7\uA8FB\uA8FD\uA8FE\uA900-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF-\uA9D9\uA9E0-\uA9E4\uA9E6-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA50-\uAA59\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\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uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46\uDD50-\uDD59\uDD60-\uDD65\uDD67\uDD68\uDD6A-\uDD89\uDD98\uDDA0-\uDDA9\uDEE0-\uDEF2]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD81C-\uD820\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE60-\uDE69\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF50-\uDF59\uDF5B-\uDF61\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDE40-\uDE96\uDF00-\uDF44\uDF50\uDF93-\uDF9F\uDFE0\uDFE1]|\uD821[\uDC00-\uDFF1]|\uD822[\uDC00-\uDEF2]|\uD82C[\uDC00-\uDD1E\uDD70-\uDEFB]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD834[\uDEE0-\uDEF3\uDF60-\uDF78]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB\uDFCE-\uDFFF]|\uD83A[\uDC00-\uDCC4\uDCC7-\uDCCF\uDD00-\uDD43\uDD50-\uDD59]|\uD83B[\uDC71-\uDCAB\uDCAD-\uDCAF\uDCB1-\uDCB4\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD83C[\uDD00-\uDD0C]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0]|\uD87E[\uDC00-\uDE1D]|[\u00AD\u2010\u2011]|\u002D(?!\u002D))+/g;
21+
return (text.match(wordChars) || []).length;
22+
}
23+
24+
function isMacOS() {
25+
return os.platform() === "darwin";
26+
}
27+
28+
export function isMetaPressed(e: MouseEvent): boolean {
29+
return isMacOS() ? e.metaKey : e.ctrlKey;
30+
}
31+
32+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33+
export function getDaysOfWeek(..._args: any[]): string[] {
34+
return window.moment.weekdaysShort(true);
35+
}
36+
37+
export function isWeekend(date: Moment): boolean {
38+
return date.isoWeekday() === 6 || date.isoWeekday() === 7;
39+
}
40+
41+
export function getStartOfWeek(days: Moment[]): Moment {
42+
return days[0].weekday(0);
43+
}
44+
45+
/**
46+
* Generate a 2D array of daily information to power
47+
* the calendar view.
48+
*/
49+
export function getMonth(
50+
displayedMonth: Moment,
51+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
52+
..._upstreamChanges: any[]
53+
): IMonth {
54+
const month = [];
55+
let week: IWeek;
56+
57+
const startOfMonth = displayedMonth.clone().date(1);
58+
const startOffset = startOfMonth.weekday();
59+
let date: Moment = startOfMonth.clone().subtract(startOffset, "days");
60+
61+
for (let _day = 0; _day < 42; _day++) {
62+
if (_day % 7 === 0) {
63+
week = {
64+
days: [],
65+
weekNum: date.week(),
66+
};
67+
month.push(week);
68+
}
69+
70+
week.days.push(date);
71+
date = date.clone().add(1, "days");
72+
}
73+
74+
return month;
75+
}

‎tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "@tsconfig/svelte/tsconfig.json",
3+
4+
"include": ["src/**/*"],
5+
"exclude": ["node_modules/*"],
6+
"compilerOptions": {
7+
"types": ["node", "svelte"],
8+
"baseUrl": ".",
9+
"paths": {
10+
"src": ["src/*"]
11+
}
12+
}
13+
}

‎yarn.lock

+1,422
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.