Skip to content

Commit

Permalink
modified: main.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
joycode-hub committed Jul 24, 2024
1 parent dbb5f86 commit 77d9b01
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ interface DateFormat {
formatToUser: string;
formatToPicker: string;
}
interface VisibleText {
from: number;
to: number;
text: string;
}

class PickerButtonWidget extends WidgetType {
toDOM(): HTMLElement {
Expand Down Expand Up @@ -94,28 +99,26 @@ class DatepickerCMPlugin implements PluginValue {
regex: /\d{4}[-\/\\.]{1}\d{1,2}[-\/\\.]{1}\d{1,2}/g,
formatToUser: "YYYY-MM-DD",
formatToPicker: "YYYY-MM-DD",

},
{
regex: /\d{1,2}[-\/\\.]{1}\d{1,2}[-\/\\.]{1}\d{4}/g,
formatToUser: "DD-MM-YYYY",
formatToPicker: "YYYY-MM-DD"

}
]

private getAllDates(view: EditorView): DateMatch[] {
const textView = view.state.doc.toString();
let visibleText: VisibleText[] = [];
visibleText = view.visibleRanges.map(r => { return { from: r.from, to: r.to, text: view.state.doc.sliceString(r.from, r.to) } });
let matchingDate: RegExpExecArray | null;
let dateMatches: DateMatch[] = [];

for (const format of this.formats) {
while ((matchingDate = format.regex.exec(textView ?? "")) !== null) {

if (dateMatches.some((match) => match.from === matchingDate?.index)) {
continue;
for (const vt of visibleText) {
for (const format of this.formats) {
while ((matchingDate = format.regex.exec(vt.text ?? "")) !== null) {
if (dateMatches.some((match) => match.from === (matchingDate?.index! + vt.from))) continue;
dateMatches.push({ from: matchingDate.index + vt.from, to: matchingDate.index + matchingDate[0].length + vt.from, value: matchingDate[0], format: format });
}
dateMatches.push({ from: matchingDate.index, to: matchingDate.index + matchingDate[0].length, value: matchingDate[0], format: format });
}
}
return dateMatches;
Expand All @@ -137,7 +140,7 @@ class DatepickerCMPlugin implements PluginValue {
private previousDateMatch: DateMatch;
dates: DateMatch[] = [];

private justReplaced = false;// flag to prevent datepicker opening after just replacing after delay on changing tab
private justReplaced = false;// flag to prevent datepicker opening after just replacing after delay on changing active leaf
openDatepicker(view: EditorView, match: DateMatch) {
const dateToPicker = moment(match.value, [
"YYYY-MM-DD hh:mm A"
Expand Down Expand Up @@ -181,6 +184,12 @@ class DatepickerCMPlugin implements PluginValue {
}
update(update: ViewUpdate) {
this.view = update.view;

this.dates = this.getAllDates(update.view);

if (DatepickerPlugin.settings.showButtons)
this.decorations = pickerButtons(this.dates);

/*
CM fires two update events for selection change,
I use the below code section to ignore the second one
Expand All @@ -191,13 +200,6 @@ class DatepickerCMPlugin implements PluginValue {
update.state.selection.main.to === update.startState.selection.main.to
) return;

this.dates = this.getAllDates(update.view);

if (update.docChanged || update.viewportChanged) {
if (DatepickerPlugin.settings.showButtons)
this.decorations = pickerButtons(this.dates);
}

if (update.state.selection.main.from !== update.state.selection.main.to) {
Datepicker.closeAll();
return;
Expand Down Expand Up @@ -244,7 +246,6 @@ class DatepickerCMPlugin implements PluginValue {
}
}


destroy() {
Datepicker.closeAll();
this.scrollEventAbortController.abort();
Expand Down

0 comments on commit 77d9b01

Please sign in to comment.