Skip to content

Commit

Permalink
add schedule widget
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Oct 1, 2023
1 parent 376b9d5 commit ccb30b6
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ nav.level.grid-2 {
font-family: $family-monospace;
}

.cm-tag {
height: 1.5em !important;
}

.svelte-select {
--chevron-color: hsl(229deg, 53%, 53%);
--font-size: 14px;
Expand Down
4 changes: 3 additions & 1 deletion src/lib/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import _ from "lodash";
import { editorState, initialEditorState } from "../store";
import { autocompletion, completeFromList, ifIn } from "@codemirror/autocomplete";
import { MergeView } from "@codemirror/merge";
import { schedulePlugin } from "./transaction_tag";

export { editorState } from "../store";

Expand Down Expand Up @@ -87,7 +88,8 @@ export function createEditor(
redoDepth: redoDepth(viewUpdate.state)
})
);
})
}),
schedulePlugin
],
doc: content,
parent: dom
Expand Down
9 changes: 7 additions & 2 deletions src/lib/transaction_sequence.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import _ from "lodash";
import type { Transaction, TransactionSchedule, TransactionSequence } from "./utils";
import {
prefixMinutesSeconds,
type Transaction,
type TransactionSchedule,
type TransactionSequence
} from "./utils";
import dayjs from "dayjs";
import { parse, type CronExprs } from "@datasert/cronjs-parser";
import { getFutureMatches } from "@datasert/cronjs-matcher";
Expand Down Expand Up @@ -83,7 +88,7 @@ function enrich(ts: TransactionSequence) {
let cron: CronExprs;
try {
if (ts.period != "") {
cron = parse("0 0 " + ts.period, { hasSeconds: false });
cron = parse(prefixMinutesSeconds(ts.period), { hasSeconds: false });
periodAvailable = true;
} else {
periodAvailable = false;
Expand Down
76 changes: 76 additions & 0 deletions src/lib/transaction_tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import dayjs from "dayjs";
import { parse } from "@datasert/cronjs-parser";
import { getFutureMatches } from "@datasert/cronjs-matcher";
import { WidgetType, MatchDecorator } from "@codemirror/view";
import {
type DecorationSet,
ViewUpdate,
ViewPlugin,
EditorView,
Decoration
} from "@codemirror/view";
import _ from "lodash";
import { prefixMinutesSeconds } from "./utils";

class SchedulePreview extends WidgetType {
constructor(readonly period: string) {
super();
}

eq(other: SchedulePreview) {
return this.period === other.period;
}

toDOM() {
let text = "";
try {
const cron = parse(prefixMinutesSeconds(this.period), { hasSeconds: false });
const schedules = getFutureMatches(cron, {
matchCount: 3,
timezone: dayjs.tz.guess()
});
text = _.chain(schedules)
.map((schedule) => dayjs(schedule).format("DD MMM YYYY"))
.join(", ")
.value();

if (_.isEmpty(schedules)) {
text = "Invalid";
}
} catch (e) {
text = "Invalid";
}
const wrapper = document.createElement("span");
wrapper.innerHTML = text;
wrapper.className = "cm-tag tag ml-2";
return wrapper;
}
}

const periodDecorator = new MatchDecorator({
regexp: /;\s*Period: (([^ ]+ ){2,}[^ ]+)$/gi,
decorate: (add, from, to, match) => {
const period = match[1];
const start = to,
end = to;
const preview = new SchedulePreview(period);
add(start, end, Decoration.widget({ widget: preview, side: 1 }));
}
});

export const schedulePlugin = ViewPlugin.fromClass(
class ScheduleView {
decorator: MatchDecorator;
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorator = periodDecorator;
this.decorations = this.decorator.createDeco(view);
}
update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged) {
this.decorations = this.decorator.updateDeco(update, this.decorations);
}
}
},
{ decorations: (v) => v.decorations }
);
7 changes: 7 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,3 +919,10 @@ export function monthDays(month: string) {
}
return { days, monthStart, monthEnd };
}

export function prefixMinutesSeconds(cronExpression: string) {
return cronExpression
.split("|")
.map((cron) => "0 0 " + cron)
.join("|");
}

0 comments on commit ccb30b6

Please sign in to comment.