Skip to content

Commit

Permalink
Merge pull request obsidian-tasks-group#3059 from obsidian-tasks-grou…
Browse files Browse the repository at this point in the history
…p/more-date-picker-fixes

fix: Improve Edit Task modal calendar button behaviour
  • Loading branch information
claremacrae authored Sep 1, 2024
2 parents e9a94c1 + 6d97202 commit b663a42
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 153 deletions.
7 changes: 7 additions & 0 deletions src/Obsidian/TaskModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ export class TaskModal extends Modal implements IFlatpickrUser {
this.task = task;
this.allTasks = allTasks;
this.onSubmit = (updatedTasks: Task[]) => {
if (this.activeFlatpickrInstance) {
// Ignore onSubmit if the date-picker is open:
// For some unknown reason, making the date-picker lunched from a Button
// caused the modal to be automatically submitted before the user had
// even seen the date picker.
return;
}
updatedTasks.length && onSubmit(updatedTasks);
this.close();
};
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/TaskLineRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class TaskLineRenderer {
span.addEventListener('click', (ev: MouseEvent) => {
ev.preventDefault(); // suppress the default click behavior
ev.stopPropagation(); // suppress further event propagation
promptForDate(li, task, componentDateField, defaultTaskSaver);
promptForDate(span, task, componentDateField, defaultTaskSaver);
});

span.addEventListener('contextmenu', (ev: MouseEvent) => {
Expand Down
27 changes: 14 additions & 13 deletions src/ui/DateEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import flatpickr from 'flatpickr';
import { createEventDispatcher } from 'svelte';
import { setIcon } from 'obsidian';
import { doAutocomplete } from '../DateTime/DateAbbreviations';
import { parseTypedDateForDisplayUsingFutureDate } from '../DateTime/DateTools';
import { labelContentWithAccessKey } from './EditTaskHelpers';
Expand All @@ -19,6 +21,12 @@
const dispatch = createEventDispatcher();
const iconCalendarDays = (node: HTMLElement) => {
// For a more general implementation, see:
// https://github.com/joethei/obsidian-rss/blob/b600e2ead2505d58aa3e4c7898795bbf58fa3cdc/src/view/IconComponent.svelte
setIcon(node, 'calendar-days');
};
$: {
date = doAutocomplete(date);
parsedDate = parseTypedDateForDisplayUsingFutureDate(id, date, forwardOnly);
Expand Down Expand Up @@ -96,20 +104,13 @@
{accesskey}
/>

<!-- Separate the calendar icon from the input to allow typing in the input box -->
<!-- TODO Fix the accessibility warning, ideally by converting this to a proper button -->
<!-- TODO Nicer looking icon, or at least make it a paler shade -->
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
<button
class="tasks-modal-calendar-button"
use:iconCalendarDays
on:click={openDatePicker}
style="cursor: pointer;"
>
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"
/>
</svg>
aria-label="Open date picker"
style="background: none; border: none; padding: 0; cursor: pointer;"
/>

<code class="tasks-modal-parsed-date">{dateSymbol} {@html parsedDate}</code>

Expand Down
10 changes: 4 additions & 6 deletions src/ui/EditTask.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@
min-width: 15em;
}

.tasks-modal-calendar-icon {
.tasks-modal-calendar-button {
grid-column: 3;
width: 24px;
height: 24px;
fill: var(--text-muted);
color: var(--text-muted);
}

.tasks-modal-parsed-date {
Expand Down Expand Up @@ -164,7 +162,7 @@
grid-column: 1;
}

.tasks-modal-calendar-icon {
.tasks-modal-calendar-button {
grid-column: 2;
}

Expand Down Expand Up @@ -194,7 +192,7 @@
grid-column: 1;
}

.tasks-modal-calendar-icon {
.tasks-modal-calendar-button {
grid-column: 1;
}

Expand Down
58 changes: 21 additions & 37 deletions src/ui/Menus/DatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,26 @@ export function promptForDate(
dateFieldToEdit: AllTaskDateFields,
taskSaver: (originalTask: Task, newTasks: Task | Task[]) => Promise<void>,
) {
if (!parentElement) {
console.log('Parent element not found.');
return;
}
const currentValue = task[dateFieldToEdit];
// TODO figure out how Today's date is determined: if Obsidian is left
// running overnight, the flatpickr modal shows the previous day as Today.
const fp = flatpickr(parentElement, {
defaultDate: currentValue ? currentValue.format('YYYY-MM-DD') : new Date(),
enableTime: false, // Optional: Enable time picker
dateFormat: 'Y-m-d', // Adjust the date and time format as needed
locale: {
firstDayOfWeek: 1, // Sets Monday as the first day of the week
},
onClose: async (selectedDates, _dateStr, instance) => {
if (selectedDates.length > 0) {
const date = selectedDates[0];
const newTask = new SetTaskDate(dateFieldToEdit, date).apply(task);
await taskSaver(task, newTask);
}
instance.destroy(); // Proper cleanup
},
});

const input = document.createElement('input');
input.type = 'text'; // Flatpickr can hook into a text input
parentElement.appendChild(input);

// Ensure styles are applied so Flatpickr can render correctly
input.style.minWidth = '200px'; // Ensure there's enough room for Flatpickr

// Delay the initialization of Flatpickr to ensure DOM is ready
setTimeout(() => {
const currentValue = task[dateFieldToEdit];
// TODO figure out how Today's date is determined: if Obsidian is left
// running overnight, the flatpickr modal shows the previous day as Today.
const fp = flatpickr(input, {
defaultDate: currentValue ? currentValue.format('YYYY-MM-DD') : new Date(),
enableTime: false, // Optional: Enable time picker
dateFormat: 'Y-m-d', // Adjust the date and time format as needed
locale: {
firstDayOfWeek: 1, // Sets Monday as the first day of the week
},
onClose: async (selectedDates, _dateStr, instance) => {
if (selectedDates.length > 0) {
const date = selectedDates[0];
const newTask = new SetTaskDate(dateFieldToEdit, date).apply(task);
await taskSaver(task, newTask);
}
instance.destroy(); // Proper cleanup
input.remove(); // Remove the elements after selection
},
});

// Open the calendar programmatically
fp.open();
}, 0);
// Open the calendar programmatically
fp.open();
}
3 changes: 3 additions & 0 deletions tests/__mocks__/obsidian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,6 @@ export function prepareSimpleSearch(query: string): (text: string) => SearchResu
return caseInsensitiveSubstringSearch(query, text);
};
}

type IconName = string;
export function setIcon(_parent: HTMLElement, _iconId: IconName): void {}
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,10 @@
class="tasks-modal-date-input"
placeholder="Try 'Mon' or 'tm' then space"
accesskey="d" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
📅
<i>no due date</i>
Expand All @@ -116,14 +112,10 @@
class="tasks-modal-date-input"
placeholder="Try 'Mon' or 'tm' then space"
accesskey="s" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
<i>no scheduled date</i>
Expand All @@ -139,14 +131,10 @@
class="tasks-modal-date-input"
placeholder="Try 'Mon' or 'tm' then space"
accesskey="a" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
🛫
<i>no start date</i>
Expand Down Expand Up @@ -245,14 +233,10 @@
class="tasks-modal-date-input"
placeholder="Try 'Mon' or 'tm' then space"
accesskey="c" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
<i>no created date</i>
Expand All @@ -268,14 +252,10 @@
class="tasks-modal-date-input"
placeholder="Try 'Mon' or 'tm' then space"
accesskey="x" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
<i>no done date</i>
Expand All @@ -291,14 +271,10 @@
class="tasks-modal-date-input"
placeholder="Try 'Mon' or 'tm' then space"
accesskey="-" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
<i>no cancelled date</i>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,30 @@
</code>
<label for="due">Due</label>
<input id="due" type="text" class="tasks-modal-date-input" placeholder="Try 'Mon' or 'tm' then space" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
📅
<i>no due date</i>
</code>
<label for="scheduled">Scheduled</label>
<input id="scheduled" type="text" class="tasks-modal-date-input" placeholder="Try 'Mon' or 'tm' then space" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
<i>no scheduled date</i>
</code>
<label for="start">Start</label>
<input id="start" type="text" class="tasks-modal-date-input" placeholder="Try 'Mon' or 'tm' then space" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
🛫
<i>no start date</i>
Expand Down Expand Up @@ -165,42 +153,30 @@
</select>
<label for="created">Created</label>
<input id="created" type="text" class="tasks-modal-date-input" placeholder="Try 'Mon' or 'tm' then space" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
<i>no created date</i>
</code>
<label for="done">Done</label>
<input id="done" type="text" class="tasks-modal-date-input" placeholder="Try 'Mon' or 'tm' then space" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
<i>no done date</i>
</code>
<label for="cancelled">Cancelled</label>
<input id="cancelled" type="text" class="tasks-modal-date-input" placeholder="Try 'Mon' or 'tm' then space" />
<svg
class="tasks-modal-calendar-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
style="cursor: pointer">
<path
d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11zM7 11h5v5H7z"></path>
</svg>
<button
class="tasks-modal-calendar-button"
aria-label="Open date picker"
style="background: none; padding: 0px; cursor: pointer"></button>
<code class="tasks-modal-parsed-date">
<i>no cancelled date</i>
Expand Down

0 comments on commit b663a42

Please sign in to comment.