Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: add go to next/previous tab shortcut #7109

Merged
merged 1 commit into from
Jan 2, 2025
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
54 changes: 54 additions & 0 deletions apps/web/__e2e__/editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,57 @@ test("when autosave is disabled, closing the note should save it", async ({
await expect(notes.editor.savedIcon).toBeVisible();
expect(await notes.editor.getContent("text")).toBe(content.trim());
});

test("control + alt + right arrow should go to next note", async ({ page }) => {
const app = new AppModel(page);
await app.goto();
const notes = await app.goToNotes();
const note1 = await notes.createNote({
title: "Note 1",
content: "Note 1 content"
});
const note2 = await notes.createNote({
title: "Note 2",
content: "Note 2 content"
});

await note1?.openNote();
await note2?.openNote();
await page.keyboard.press("Control+Alt+ArrowRight");

expect(await notes.editor.getTitle()).toBe("Note 1");
expect(await notes.editor.getContent("text")).toBe("Note 1 content");

await page.keyboard.press("Control+Alt+ArrowRight");

expect(await notes.editor.getTitle()).toBe("Note 2");
expect(await notes.editor.getContent("text")).toBe("Note 2 content");
});

test("control + alt + left arrow should go to previous note", async ({
page
}) => {
const app = new AppModel(page);
await app.goto();
const notes = await app.goToNotes();
const note1 = await notes.createNote({
title: "Note 1",
content: "Note 1 content"
});
const note2 = await notes.createNote({
title: "Note 2",
content: "Note 2 content"
});

await note1?.openNote();
await note2?.openNote();
await page.keyboard.press("Control+Alt+ArrowLeft");

expect(await notes.editor.getTitle()).toBe("Note 1");
expect(await notes.editor.getContent("text")).toBe("Note 1 content");

await page.keyboard.press("Control+Alt+ArrowLeft");

expect(await notes.editor.getTitle()).toBe("Note 2");
expect(await notes.editor.getContent("text")).toBe("Note 2 content");
});
19 changes: 17 additions & 2 deletions apps/web/src/components/editor/action-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Button, Flex, Text } from "@theme-ui/components";
import { useState } from "react";
import { useEffect, useRef, useState } from "react";
import {
ArrowLeft,
Cross,
Expand Down Expand Up @@ -450,10 +450,25 @@ function Tab(props: TabProps) {
: Note;
const { attributes, listeners, setNodeRef, transform, transition, active } =
useSortable({ id });
const activeTabRef = useRef<HTMLElement | null>(null);

useEffect(() => {
if (activeTabRef.current && isActive) {
const tab = activeTabRef.current;
tab.scrollIntoView({
behavior: "smooth",
block: "nearest",
inline: "nearest"
});
}
}, [isActive]);

return (
<Flex
ref={setNodeRef}
ref={(el) => {
setNodeRef(el);
activeTabRef.current = el;
}}
className="tab"
data-test-id={`tab-${id}`}
sx={{
Expand Down
25 changes: 25 additions & 0 deletions apps/web/src/components/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ export default function TabsView() {
const isTOCVisible = useEditorStore((store) => store.isTOCVisible);
const [dropRef, overlayRef] = useDragOverlay();

useEffect(() => {
const onKeyDown = (event: KeyboardEvent) => {
if (
(event.ctrlKey || event.metaKey) &&
event.altKey &&
event.key === "ArrowRight"
) {
event.preventDefault();
useEditorStore.getState().openNextSession();
}
if (
(event.ctrlKey || event.metaKey) &&
event.altKey &&
event.key === "ArrowLeft"
) {
event.preventDefault();
useEditorStore.getState().openPreviousSession();
}
};
document.body.addEventListener("keydown", onKeyDown);
return () => {
document.body.removeEventListener("keydown", onKeyDown);
};
}, []);

return (
<>
{!hasNativeTitlebar ? (
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/editor/tiptap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function TipTap(props: TipTapProps) {
const tiptapOptions = useMemo<Partial<TiptapOptions>>(() => {
return {
editorProps: {
handleKeyDown(view, event) {
handleKeyDown(_, event) {
if ((event.ctrlKey || event.metaKey) && event.key === "s") {
event.preventDefault();
onChange?.(
Expand Down
26 changes: 26 additions & 0 deletions apps/web/src/stores/editor-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,32 @@ class EditorStore extends BaseStore<EditorStore> {
}
};

openNextSession = () => {
const { sessions, activeSessionId } = this.get();
if (sessions.length === 0 || sessions.length === 1) return;

const index = sessions.findIndex((s) => s.id === activeSessionId);
if (index === -1) return;

if (index === sessions.length - 1) {
return this.openSession(sessions[0].id);
}
return this.openSession(sessions[index + 1].id);
};

openPreviousSession = () => {
const { sessions, activeSessionId } = this.get();
if (sessions.length === 0 || sessions.length === 1) return;

const index = sessions.findIndex((s) => s.id === activeSessionId);
if (index === -1) return;

if (index === 0) {
return this.openSession(sessions[sessions.length - 1].id);
}
return this.openSession(sessions[index - 1].id);
};

addSession = (session: EditorSession, activate = true) => {
let oldSessionId: string | null = null;

Expand Down
Loading