mirror of
https://github.com/Ellpeck/ObsidianSimpleTimeTracker.git
synced 2024-11-16 07:23:12 +01:00
Use fixed format for editable timestamps
This commit is contained in:
parent
0612a4bd21
commit
3d9e6163e7
4 changed files with 123 additions and 37 deletions
|
@ -1,5 +1,6 @@
|
||||||
export const defaultSettings: SimpleTimeTrackerSettings = {
|
export const defaultSettings: SimpleTimeTrackerSettings = {
|
||||||
timestampFormat: "YY-MM-DD HH:mm:ss",
|
timestampFormat: "YY-MM-DD HH:mm:ss",
|
||||||
|
editableTimestampFormat: "YYYY-MM-DD HH:mm:ss",
|
||||||
csvDelimiter: ",",
|
csvDelimiter: ",",
|
||||||
fineGrainedDurations: true
|
fineGrainedDurations: true
|
||||||
};
|
};
|
||||||
|
@ -7,6 +8,7 @@ export const defaultSettings: SimpleTimeTrackerSettings = {
|
||||||
export interface SimpleTimeTrackerSettings {
|
export interface SimpleTimeTrackerSettings {
|
||||||
|
|
||||||
timestampFormat: string;
|
timestampFormat: string;
|
||||||
|
editableTimestampFormat: string;
|
||||||
csvDelimiter: string;
|
csvDelimiter: string;
|
||||||
fineGrainedDurations: boolean;
|
fineGrainedDurations: boolean;
|
||||||
|
|
||||||
|
|
|
@ -202,8 +202,12 @@ function formatTimestamp(timestamp: number, settings: SimpleTimeTrackerSettings)
|
||||||
return moment.unix(timestamp).format(settings.timestampFormat);
|
return moment.unix(timestamp).format(settings.timestampFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
function unFormatTimestamp(formatted: string, settings: SimpleTimeTrackerSettings): number {
|
function formatEditableTimestamp(timestamp: number, settings: SimpleTimeTrackerSettings) {
|
||||||
return moment(formatted, settings.timestampFormat).unix();
|
return moment.unix(timestamp).format(settings.editableTimestampFormat);
|
||||||
|
}
|
||||||
|
|
||||||
|
function unformatEditableTimestamp(formatted: string, settings: SimpleTimeTrackerSettings): number {
|
||||||
|
return moment(formatted, settings.editableTimestampFormat).unix();
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDuration(totalTime: number, settings: SimpleTimeTrackerSettings): string {
|
function formatDuration(totalTime: number, settings: SimpleTimeTrackerSettings): string {
|
||||||
|
@ -294,7 +298,7 @@ class EditableField {
|
||||||
this.box.inputEl.show();
|
this.box.inputEl.show();
|
||||||
}
|
}
|
||||||
endEdit(): string {
|
endEdit(): string {
|
||||||
let value = this.box.getValue()
|
const value = this.box.getValue();
|
||||||
this.label.setText(value);
|
this.label.setText(value);
|
||||||
this.box.inputEl.hide();
|
this.box.inputEl.hide();
|
||||||
this.label.hidden = false;
|
this.label.hidden = false;
|
||||||
|
@ -302,16 +306,38 @@ class EditableField {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class EditableTimestampField extends EditableField {
|
||||||
|
settings: SimpleTimeTrackerSettings;
|
||||||
|
constructor(row: HTMLTableRowElement, indent: number, value: string, settings: SimpleTimeTrackerSettings) {
|
||||||
|
const timestamp = Number(value);
|
||||||
|
value = timestamp > 0 ? formatTimestamp(timestamp, settings) : "";
|
||||||
|
super(row, indent, value);
|
||||||
|
this.settings = settings;
|
||||||
|
}
|
||||||
|
beginEdit(value: string) {
|
||||||
|
value = formatEditableTimestamp(Number(value), this.settings);
|
||||||
|
super.beginEdit(value);
|
||||||
|
}
|
||||||
|
endEdit(): string {
|
||||||
|
const value = this.box.getValue();
|
||||||
|
const timestamp = unformatEditableTimestamp(value, this.settings);
|
||||||
|
const displayValue = formatTimestamp(timestamp, this.settings);
|
||||||
|
this.label.setText(displayValue);
|
||||||
|
this.box.inputEl.hide();
|
||||||
|
this.label.hidden = false;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
getTimestamp(): number {
|
||||||
|
return unformatEditableTimestamp(this.box.getValue(), this.settings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableElement, newSegmentNameBox: TextComponent, running: boolean, file: string, getSectionInfo: () => MarkdownSectionInformation, settings: SimpleTimeTrackerSettings, indent: number) {
|
function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableElement, newSegmentNameBox: TextComponent, running: boolean, file: string, getSectionInfo: () => MarkdownSectionInformation, settings: SimpleTimeTrackerSettings, indent: number) {
|
||||||
let row = table.createEl("tr");
|
let row = table.createEl("tr");
|
||||||
|
|
||||||
let nameField = new EditableField(row, indent, entry.name);
|
let nameField = new EditableField(row, indent, entry.name);
|
||||||
|
let startField = new EditableTimestampField(row, indent, String(entry.startTime), settings);
|
||||||
let startValue = entry.startTime ? formatTimestamp(entry.startTime, settings) : "";
|
let endField = new EditableTimestampField(row, indent, String(entry.endTime), settings);
|
||||||
let startField = new EditableField(row, indent, startValue);
|
|
||||||
|
|
||||||
let endValue = entry.endTime ? formatTimestamp(entry.endTime, settings) : "";
|
|
||||||
let endField = new EditableField(row, indent, endValue);
|
|
||||||
|
|
||||||
row.createEl("td", { text: entry.endTime || entry.subEntries ? formatDuration(getDuration(entry), settings) : "" });
|
row.createEl("td", { text: entry.endTime || entry.subEntries ? formatDuration(getDuration(entry), settings) : "" });
|
||||||
|
|
||||||
|
@ -333,14 +359,16 @@ function addEditableTableRow(tracker: Tracker, entry: Entry, table: HTMLTableEle
|
||||||
.onClick(async () => {
|
.onClick(async () => {
|
||||||
if (nameField.editing()) {
|
if (nameField.editing()) {
|
||||||
entry.name = nameField.endEdit();
|
entry.name = nameField.endEdit();
|
||||||
entry.startTime = unFormatTimestamp(startField.endEdit(), settings);
|
startField.endEdit();
|
||||||
entry.endTime = unFormatTimestamp(endField.endEdit(), settings);
|
entry.startTime = startField.getTimestamp();
|
||||||
|
endField.endEdit();
|
||||||
|
entry.endTime = endField.getTimestamp();
|
||||||
await saveTracker(tracker, this.app, file, getSectionInfo());
|
await saveTracker(tracker, this.app, file, getSectionInfo());
|
||||||
editButton.setIcon("lucide-pencil");
|
editButton.setIcon("lucide-pencil");
|
||||||
} else {
|
} else {
|
||||||
nameField.beginEdit(entry.name);
|
nameField.beginEdit(entry.name);
|
||||||
startField.beginEdit(formatTimestamp(entry.startTime, settings));
|
startField.beginEdit(String(entry.startTime));
|
||||||
endField.beginEdit(formatTimestamp(entry.endTime, settings));
|
endField.beginEdit(String(entry.endTime));
|
||||||
editButton.setIcon("lucide-check");
|
editButton.setIcon("lucide-check");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue