Skip to content

Commit

Permalink
Fix: Add shortcut for Markdown todo and document in README.md
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
minthemiddle committed Sep 19, 2024
1 parent cb90bc6 commit 8fa3feb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The app supports the following formatting shortcuts:
- Bold: `cmd+b` (Mac) / `ctrl+b` (Windows)
- Italic: `cmd+i` (Mac) / `ctrl+i` (Windows)
- Link: `cmd+k` (Mac) / `ctrl+k` (Windows)
- Todo: `cmd+l` (Mac) / `ctrl+l` (Windows)

Note: These shortcuts add formatting to the selected text or at the cursor position.
They do not toggle formatting; applying a shortcut multiple times adds multiple formatting marks.
Expand Down
25 changes: 24 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ thoughtInputEl.addEventListener('keydown', handleKeyDown);
thoughtInputEl.addEventListener('keydown', handleBoldShortcut);
thoughtInputEl.addEventListener('keydown', handleItalicShortcut);
thoughtInputEl.addEventListener('keydown', handleLinkShortcut);
thoughtInputEl.addEventListener('keydown', handleTodoShortcut);

function handleKeyDown(event) {
// Check if the user hit Cmd+Enter (or Ctrl+Enter on Windows)
Expand Down Expand Up @@ -148,6 +149,28 @@ function handleLinkShortcut(event) {
}
}

function handleTodoShortcut(event) {
// Check if the user hit Cmd+L (or Ctrl+L on Windows), but not Shift+Cmd+L
if ((event.metaKey || event.ctrlKey) && event.key === 'l' && !event.shiftKey) {
event.preventDefault(); // Prevent the default action
const start = thoughtInputEl.selectionStart;
const end = thoughtInputEl.selectionEnd;
const selectedText = thoughtInputEl.value.substring(start, end);

if (selectedText) {
// Wrap the selected text with - [ ]
thoughtInputEl.value = thoughtInputEl.value.substring(0, start) + '- [ ] ' + selectedText + thoughtInputEl.value.substring(end);
thoughtInputEl.selectionStart = start + 6;
thoughtInputEl.selectionEnd = start + 6 + selectedText.length;
} else {
// Insert - [ ] and place the cursor between the brackets
thoughtInputEl.value = thoughtInputEl.value.substring(0, start) + '- [ ] ' + thoughtInputEl.value.substring(end);
thoughtInputEl.selectionStart = start + 6;
thoughtInputEl.selectionEnd = start + 6;
}
}
}


window.addEventListener("DOMContentLoaded", () => {
// Load the path from local storage
Expand All @@ -161,7 +184,7 @@ window.addEventListener("DOMContentLoaded", () => {
if (draftThought) {
thoughtInputEl.value = draftThought;
}

updateStatus("edit");
});
function saveDraft() {
Expand Down

0 comments on commit 8fa3feb

Please sign in to comment.