Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/components/QuickSwitcherDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ const QuickSwitcherDialog = ({
icon="document-open"
key={bookmark.id}
labelElement={
<Tag minimal>
{formatShortcutForDisplay({
shortcut: bookmark.shortcut,
isMac,
})}
</Tag>
bookmark.shortcut ? (
<Tag minimal>
{formatShortcutForDisplay({
shortcut: bookmark.shortcut,
isMac,
})}
</Tag>
) : undefined
}
multiline
onClick={(): void => onOpenBookmark(bookmark)}
Expand Down
37 changes: 22 additions & 15 deletions src/components/QuickSwitcherSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,21 @@ export const createQuickSwitcherSettingsComponent = ({
return;
}

const normalizedShortcut = normalizeShortcut({ shortcut });
if (!normalizedShortcut) {
const normalizedShortcut = shortcut
? normalizeShortcut({ shortcut })
: null;
if (shortcut && !normalizedShortcut) {
showToast({
content: "Capture a shortcut before adding",
content: "Capture a valid shortcut or leave it blank",
intent: "warning",
});
return;
}

if (!shortcutHasModifier({ shortcut: normalizedShortcut })) {
if (
normalizedShortcut &&
!shortcutHasModifier({ shortcut: normalizedShortcut })
) {
showToast({
content: "Shortcut must include at least one modifier key",
intent: "warning",
Expand All @@ -140,9 +145,9 @@ export const createQuickSwitcherSettingsComponent = ({
return;
}

const existingShortcut = bookmarks.find(
(bookmark) => bookmark.shortcut === normalizedShortcut,
);
const existingShortcut = normalizedShortcut
? bookmarks.find((bookmark) => bookmark.shortcut === normalizedShortcut)
: null;
if (existingShortcut) {
showToast({
content: `Shortcut already used by "${existingShortcut.title}"`,
Expand Down Expand Up @@ -196,12 +201,12 @@ export const createQuickSwitcherSettingsComponent = ({
</FormGroup>

<FormGroup
helperText="Click then press your keys (e.g. Ctrl + Shift + 1)."
helperText="Optional. Click then press your keys (e.g. Ctrl + Shift + 1)."
label="Shortcut"
>
<InputGroup
onKeyDown={onShortcutKeyDown}
placeholder="Capture shortcut"
placeholder="Capture optional shortcut"
readOnly
value={shortcutLabel}
/>
Expand All @@ -227,12 +232,14 @@ export const createQuickSwitcherSettingsComponent = ({
</div>
</div>
<div className="flex items-center gap-1">
<Tag minimal>
{formatShortcutForDisplay({
shortcut: bookmark.shortcut,
isMac,
})}
</Tag>
{bookmark.shortcut ? (
<Tag minimal>
{formatShortcutForDisplay({
shortcut: bookmark.shortcut,
isMac,
})}
</Tag>
) : null}
<Button
disabled={index === 0}
icon="arrow-up"
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default runExtension(async ({ extensionAPI }) => {
id: "bookmarked-pages",
name: "Bookmarked Pages",
description:
"Add Roam pages and assign shortcuts. These bookmarks are the only pages searchable in the dialog.",
"Add Roam pages with optional shortcuts. These bookmarks are the only pages searchable in the dialog.",
action: {
type: "reactComponent",
component: settingsComponent,
Expand Down
19 changes: 13 additions & 6 deletions src/quickSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,25 @@ const sanitizeBookmarks = ({
const seenUrls = new Set<string>();

return bookmarks.reduce<QuickSwitcherBookmark[]>((result, bookmark) => {
const normalizedShortcut = normalizeShortcut({
shortcut: bookmark.shortcut,
});
const normalizedShortcut = bookmark.shortcut
? normalizeShortcut({
shortcut: bookmark.shortcut,
})
: null;
const normalizedUrl = toAbsoluteUrl({ url: bookmark.url });
if (!normalizedShortcut || !normalizedUrl) {
if (!normalizedUrl || (bookmark.shortcut && !normalizedShortcut)) {
return result;
}
if (seenShortcuts.has(normalizedShortcut) || seenUrls.has(normalizedUrl)) {
if (
(normalizedShortcut && seenShortcuts.has(normalizedShortcut)) ||
seenUrls.has(normalizedUrl)
) {
return result;
}

seenShortcuts.add(normalizedShortcut);
if (normalizedShortcut) {
seenShortcuts.add(normalizedShortcut);
}
seenUrls.add(normalizedUrl);

result.push({
Expand Down
2 changes: 1 addition & 1 deletion src/types/quickSwitcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export type QuickSwitcherBookmark = {
title: string;
url: string;
pageUid: string | null;
shortcut: string;
shortcut: string | null;
};

export type ShortcutKeyboardEvent = {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/quickSwitcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,12 @@ const parseStoredBookmark = ({
const title = typeof value.title === "string" ? value.title.trim() : "";
const shortcut =
typeof value.shortcut === "string" ? value.shortcut.trim() : "";
if (!url || !title || !shortcut) {
if (!url || !title) {
return null;
}

const normalizedShortcut = normalizeShortcut({ shortcut });
if (!normalizedShortcut) {
const normalizedShortcut = shortcut ? normalizeShortcut({ shortcut }) : null;
if (shortcut && !normalizedShortcut) {
return null;
}

Expand Down
14 changes: 13 additions & 1 deletion tests/quickSwitcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,27 @@ test("parses and sanitizes stored bookmarks", () => {
},
{
id: "id-2",
title: "No Shortcut",
url: "https://roamresearch.com/#/app/graph/page/no-shortcut",
},
{
id: "id-3",
title: "Invalid Shortcut",
url: "https://roamresearch.com/#/app/graph/page/invalid-shortcut",
shortcut: "Ctrl +",
},
{
id: "id-4",
title: "Invalid",
url: "",
shortcut: "Ctrl + 2",
},
],
});

expect(parsed).toHaveLength(1);
expect(parsed).toHaveLength(2);
expect(parsed[0].shortcut).toBe("ctrl+1");
expect(parsed[1].shortcut).toBeNull();
});

test("resolves relative urls to absolute urls", () => {
Expand Down
Loading