From 38fe11075424a31a60c9db1c2758980f957c19c2 Mon Sep 17 00:00:00 2001 From: Whitewater Date: Tue, 7 Jan 2025 12:26:24 +0800 Subject: [PATCH] feat: customize toolbar (#2468) * feat: add customizable toolbar modal * refactor: extract SortableActionButton component * refactor: enhance CustomizeToolbar with drag-and-drop reordering and state management * refactor: update CustomizeToolbar for improved state management * refactor: enhance drag-and-drop functionality * feat: add CustomizeToolbarCommand * chore: update i18n * feat: extract customizable toolbar actions state * feat: integrate customizable toolbar order for entry header and more actions * chore: add @dnd-kit/sortable dependency * feat: add reset action order functionality * feat: sync action order * chore: filter out read action * chore: add changeset --- apps/renderer/package.json | 1 + apps/renderer/src/atoms/settings/ui.ts | 5 + .../src/hooks/biz/useEntryActions.tsx | 4 + .../src/modules/command/command-manager.ts | 4 +- .../src/modules/command/commands/id.ts | 9 +- .../commands/{theme.tsx => settings.tsx} | 31 +++- .../src/modules/command/commands/types.ts | 11 +- .../src/modules/customize-toolbar/constant.ts | 26 ++++ .../src/modules/customize-toolbar/dnd.tsx | 83 ++++++++++ .../src/modules/customize-toolbar/hooks.ts | 32 ++++ .../src/modules/customize-toolbar/modal.tsx | 144 ++++++++++++++++++ .../entry-content/actions/header-actions.tsx | 24 +-- .../entry-content/actions/more-actions.tsx | 23 +-- changelog/next.md | 3 + locales/settings/en.json | 1 + packages/shared/src/interface/settings.ts | 6 + pnpm-lock.yaml | 16 ++ 17 files changed, 391 insertions(+), 32 deletions(-) rename apps/renderer/src/modules/command/commands/{theme.tsx => settings.tsx} (56%) create mode 100644 apps/renderer/src/modules/customize-toolbar/constant.ts create mode 100644 apps/renderer/src/modules/customize-toolbar/dnd.tsx create mode 100644 apps/renderer/src/modules/customize-toolbar/hooks.ts create mode 100644 apps/renderer/src/modules/customize-toolbar/modal.tsx diff --git a/apps/renderer/package.json b/apps/renderer/package.json index e2616ad8a..054822739 100644 --- a/apps/renderer/package.json +++ b/apps/renderer/package.json @@ -13,6 +13,7 @@ }, "dependencies": { "@dnd-kit/core": "^6.3.1", + "@dnd-kit/sortable": "^10.0.0", "@egoist/tipc": "0.3.2", "@electron-toolkit/preload": "^3.0.1", "@follow/electron-main": "workspace:*", diff --git a/apps/renderer/src/atoms/settings/ui.ts b/apps/renderer/src/atoms/settings/ui.ts index fd8407123..5133b35f1 100644 --- a/apps/renderer/src/atoms/settings/ui.ts +++ b/apps/renderer/src/atoms/settings/ui.ts @@ -4,6 +4,8 @@ import { jotaiStore } from "@follow/utils/jotai" import { atom, useAtomValue, useSetAtom } from "jotai" import { useEventCallback } from "usehooks-ts" +import { DEFAULT_ACTION_ORDER } from "~/modules/customize-toolbar/constant" + export const createDefaultSettings = (): UISettings => ({ // Sidebar entryColWidth: 356, @@ -40,6 +42,9 @@ export const createDefaultSettings = (): UISettings => ({ pictureViewMasonry: true, pictureViewFilterNoImage: false, wideMode: false, + + // Action Order + toolbarOrder: DEFAULT_ACTION_ORDER, }) const zenModeAtom = atom(false) diff --git a/apps/renderer/src/hooks/biz/useEntryActions.tsx b/apps/renderer/src/hooks/biz/useEntryActions.tsx index d0665b921..2c0233ad2 100644 --- a/apps/renderer/src/hooks/biz/useEntryActions.tsx +++ b/apps/renderer/src/hooks/biz/useEntryActions.tsx @@ -200,6 +200,10 @@ export const useEntryActions = ({ entryId, view }: { entryId: string; view?: Fee hide: !hasEntry || !entry.read || !!entry.collections || !!inList, shortcut: shortcuts.entry.toggleRead.key, }, + { + id: COMMAND_ID.settings.customizeToolbar, + onClick: runCmdFn(COMMAND_ID.settings.customizeToolbar, []), + }, ].filter((config) => !config.hide) }, [ entry?.collections, diff --git a/apps/renderer/src/modules/command/command-manager.ts b/apps/renderer/src/modules/command/command-manager.ts index a06780cfb..63eb4b674 100644 --- a/apps/renderer/src/modules/command/command-manager.ts +++ b/apps/renderer/src/modules/command/command-manager.ts @@ -1,10 +1,10 @@ import { useRegisterEntryCommands } from "./commands/entry" import { useRegisterIntegrationCommands } from "./commands/integration" import { useRegisterListCommands } from "./commands/list" -import { useRegisterThemeCommands } from "./commands/theme" +import { useRegisterSettingsCommands } from "./commands/settings" export function useRegisterFollowCommands() { - useRegisterThemeCommands() + useRegisterSettingsCommands() useRegisterListCommands() useRegisterEntryCommands() useRegisterIntegrationCommands() diff --git a/apps/renderer/src/modules/command/commands/id.ts b/apps/renderer/src/modules/command/commands/id.ts index fa461fed3..bf044ea92 100644 --- a/apps/renderer/src/modules/command/commands/id.ts +++ b/apps/renderer/src/modules/command/commands/id.ts @@ -31,9 +31,10 @@ export const COMMAND_ID = { copyUrl: "list:copy-url", copyId: "list:copy-id", }, - theme: { - toAuto: "follow:change-color-mode-to-auto", - toDark: "follow:change-color-mode-to-dark", - toLight: "follow:change-color-mode-to-light", + settings: { + changeThemeToAuto: "follow:change-color-mode-to-auto", + changeThemeToDark: "follow:change-color-mode-to-dark", + changeThemeToLight: "follow:change-color-mode-to-light", + customizeToolbar: "follow:customize-toolbar", }, } as const diff --git a/apps/renderer/src/modules/command/commands/theme.tsx b/apps/renderer/src/modules/command/commands/settings.tsx similarity index 56% rename from apps/renderer/src/modules/command/commands/theme.tsx rename to apps/renderer/src/modules/command/commands/settings.tsx index 8757b503c..2dc753a12 100644 --- a/apps/renderer/src/modules/command/commands/theme.tsx +++ b/apps/renderer/src/modules/command/commands/settings.tsx @@ -2,17 +2,40 @@ import { useThemeAtomValue } from "@follow/hooks" import { useTranslation } from "react-i18next" import { useSetTheme } from "~/hooks/common" +import { useShowCustomizeToolbarModal } from "~/modules/customize-toolbar/modal" import { useRegisterCommandEffect } from "../hooks/use-register-command" +import { COMMAND_ID } from "./id" -export const useRegisterThemeCommands = () => { +export const useRegisterSettingsCommands = () => { + useCustomizeToolbarCommand() + useRegisterThemeCommands() +} + +const useCustomizeToolbarCommand = () => { + const [t] = useTranslation("settings") + const showModal = useShowCustomizeToolbarModal() + useRegisterCommandEffect([ + { + id: COMMAND_ID.settings.customizeToolbar, + label: t("customizeToolbar.title"), + category: "follow:settings", + icon: , + run() { + showModal() + }, + }, + ]) +} + +const useRegisterThemeCommands = () => { const [t] = useTranslation("settings") const theme = useThemeAtomValue() const setTheme = useSetTheme() useRegisterCommandEffect([ { - id: "follow:change-color-mode-to-auto", + id: COMMAND_ID.settings.changeThemeToAuto, label: `To ${t("appearance.theme.system")}`, category: "follow:settings", icon: , @@ -22,7 +45,7 @@ export const useRegisterThemeCommands = () => { }, }, { - id: "follow:change-color-mode-to-dark", + id: COMMAND_ID.settings.changeThemeToDark, label: `To ${t("appearance.theme.dark")}`, category: "follow:settings", icon: , @@ -32,7 +55,7 @@ export const useRegisterThemeCommands = () => { }, }, { - id: "follow:change-color-mode-to-light", + id: COMMAND_ID.settings.changeThemeToLight, label: `To ${t("appearance.theme.light")}`, category: "follow:settings", icon: , diff --git a/apps/renderer/src/modules/command/commands/types.ts b/apps/renderer/src/modules/command/commands/types.ts index cbdc31838..f6a4f97bf 100644 --- a/apps/renderer/src/modules/command/commands/types.ts +++ b/apps/renderer/src/modules/command/commands/types.ts @@ -89,6 +89,15 @@ export type EntryCommand = | ToggleAISummaryCommand | ToggleAITranslationCommand +// Settings commands + +export type CustomizeToolbarCommand = Command<{ + id: typeof COMMAND_ID.settings.customizeToolbar + fn: () => void +}> + +export type SettingsCommand = CustomizeToolbarCommand + // Integration commands export type SaveToEagleCommand = Command<{ @@ -129,4 +138,4 @@ export type IntegrationCommand = | SaveToOutlineCommand | SaveToReadeckCommand -export type BasicCommand = EntryCommand | IntegrationCommand +export type BasicCommand = EntryCommand | SettingsCommand | IntegrationCommand diff --git a/apps/renderer/src/modules/customize-toolbar/constant.ts b/apps/renderer/src/modules/customize-toolbar/constant.ts new file mode 100644 index 000000000..402a4094f --- /dev/null +++ b/apps/renderer/src/modules/customize-toolbar/constant.ts @@ -0,0 +1,26 @@ +import type { UniqueIdentifier } from "@dnd-kit/core" + +import { COMMAND_ID } from "../command/commands/id" + +export interface ToolbarActionOrder { + main: UniqueIdentifier[] + more: UniqueIdentifier[] +} + +export const DEFAULT_ACTION_ORDER: ToolbarActionOrder = { + main: Object.values(COMMAND_ID.entry) + .filter((id) => !([COMMAND_ID.entry.read, COMMAND_ID.entry.unread] as string[]).includes(id)) + .filter( + (id) => + !([COMMAND_ID.entry.copyLink, COMMAND_ID.entry.openInBrowser] as string[]).includes(id), + ), + more: [ + ...Object.values(COMMAND_ID.integration), + ...Object.values(COMMAND_ID.entry) + .filter((id) => !([COMMAND_ID.entry.read, COMMAND_ID.entry.unread] as string[]).includes(id)) + .filter((id) => + ([COMMAND_ID.entry.copyLink, COMMAND_ID.entry.openInBrowser] as string[]).includes(id), + ), + COMMAND_ID.settings.customizeToolbar, + ], +} diff --git a/apps/renderer/src/modules/customize-toolbar/dnd.tsx b/apps/renderer/src/modules/customize-toolbar/dnd.tsx new file mode 100644 index 000000000..d8c0e846d --- /dev/null +++ b/apps/renderer/src/modules/customize-toolbar/dnd.tsx @@ -0,0 +1,83 @@ +import type { UniqueIdentifier } from "@dnd-kit/core" +import { useSortable } from "@dnd-kit/sortable" +import { CSS } from "@dnd-kit/utilities" +import type { ReactNode } from "react" +import { useMemo } from "react" + +import { getCommand } from "../command/hooks/use-command" +import type { FollowCommandId } from "../command/types" + +const SortableItem = ({ id, children }: { id: UniqueIdentifier; children: ReactNode }) => { + const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ + id, + animateLayoutChanges: () => true, // Enable layout animations + transition: { + duration: 400, + easing: "cubic-bezier(0.25, 1, 0.5, 1)", + }, + }) + + const style = useMemo(() => { + return { + transform: CSS.Transform.toString(transform), + transition, + width: "100px", // Fixed width + height: "80px", // Fixed height + zIndex: isDragging ? 999 : undefined, + } + }, [transform, transition, isDragging]) + + return ( +
+ {children} +
+ ) +} + +export const SortableActionButton = ({ id }: { id: UniqueIdentifier }) => { + const cmd = getCommand(id as FollowCommandId) + if (!cmd) return null + return ( + +
+
{cmd.icon}
+
+ {cmd.label.title} +
+
+
+ ) +} + +export function DroppableContainer({ + id, + children, +}: { + children: ReactNode + id: UniqueIdentifier +}) { + const { attributes, isDragging, listeners, setNodeRef, transition, transform } = useSortable({ + id, + }) + return ( +
+ {children} +
+ ) +} diff --git a/apps/renderer/src/modules/customize-toolbar/hooks.ts b/apps/renderer/src/modules/customize-toolbar/hooks.ts new file mode 100644 index 000000000..97d514642 --- /dev/null +++ b/apps/renderer/src/modules/customize-toolbar/hooks.ts @@ -0,0 +1,32 @@ +import type { UniqueIdentifier } from "@dnd-kit/core" +import { useMemo } from "react" + +import { useUISettingSelector } from "~/atoms/settings/ui" + +export const useToolbarOrderMap = () => { + const actionOrder = useUISettingSelector((s) => s.toolbarOrder) + + const actionOrderMap = useMemo(() => { + const actionOrderMap = new Map< + UniqueIdentifier, + { + type: "main" | "more" + order: number + } + >() + actionOrder.main.forEach((id, index) => + actionOrderMap.set(id, { + type: "main", + order: index, + }), + ) + actionOrder.more.forEach((id, index) => + actionOrderMap.set(id, { + type: "more", + order: index, + }), + ) + return actionOrderMap + }, [actionOrder]) + return actionOrderMap +} diff --git a/apps/renderer/src/modules/customize-toolbar/modal.tsx b/apps/renderer/src/modules/customize-toolbar/modal.tsx new file mode 100644 index 000000000..91ce7e513 --- /dev/null +++ b/apps/renderer/src/modules/customize-toolbar/modal.tsx @@ -0,0 +1,144 @@ +import type { DragOverEvent } from "@dnd-kit/core" +import { + closestCenter, + DndContext, + KeyboardSensor, + PointerSensor, + useSensor, + useSensors, +} from "@dnd-kit/core" +import { + arrayMove, + SortableContext, + sortableKeyboardCoordinates, + verticalListSortingStrategy, +} from "@dnd-kit/sortable" +import { Button } from "@follow/components/ui/button/index.js" +import { useCallback } from "react" +import { useTranslation } from "react-i18next" + +import { setUISetting, useUISettingSelector } from "~/atoms/settings/ui" +import { useModalStack } from "~/components/ui/modal/stacked/hooks" + +import { DEFAULT_ACTION_ORDER } from "./constant" +import { DroppableContainer, SortableActionButton } from "./dnd" + +const CustomizeToolbar = () => { + const actionOrder = useUISettingSelector((s) => s.toolbarOrder) + + const sensors = useSensors( + useSensor(PointerSensor), + useSensor(KeyboardSensor, { + coordinateGetter: sortableKeyboardCoordinates, + }), + ) + + const handleDragOver = useCallback( + ({ active, over }: DragOverEvent) => { + if (!over) return + const activeId = active.id + const overId = over.id + const isActiveInMain = actionOrder.main.includes(activeId) + const isOverInMain = overId === "container-main" || actionOrder.main.includes(overId) + const isCrossContainer = isActiveInMain !== isOverInMain + + if (isCrossContainer) { + // Moving between containers + const sourceList = isActiveInMain ? "main" : "more" + const targetList = isActiveInMain ? "more" : "main" + const item = actionOrder[sourceList].find((item) => item === activeId) + if (!item) return + const newIndexOfOver = actionOrder[targetList].indexOf(overId) + setUISetting("toolbarOrder", { + ...actionOrder, + [sourceList]: actionOrder[sourceList].filter((item) => item !== activeId), + [targetList]: [ + ...actionOrder[targetList].slice(0, newIndexOfOver), + item, + ...actionOrder[targetList].slice(newIndexOfOver), + ], + }) + return + } + // Reordering within container + const list = isActiveInMain ? "main" : "more" + const items = actionOrder[list] + const oldIndex = items.indexOf(activeId) + const newIndex = items.indexOf(overId) + + setUISetting("toolbarOrder", { + ...actionOrder, + [list]: arrayMove(items, oldIndex, newIndex), + }) + }, + [actionOrder], + ) + + const resetActionOrder = useCallback(() => { + setUISetting("toolbarOrder", DEFAULT_ACTION_ORDER) + }, []) + + return ( +
+
+

Quick Actions

+

Customize and reorder your frequently used actions

+
+ {/* Refer to https://github.com/clauderic/dnd-kit/blob/master/stories/2%20-%20Presets/Sortable/MultipleContainers.tsx */} + +
+ {/* Main toolbar */} + + + item)} + strategy={verticalListSortingStrategy} + > + {actionOrder.main.map((id) => ( + + ))} + + + + {/* More panel */} +
+

More Actions

+

Will be shown in the dropdown menu

+
+ + + item)} + strategy={verticalListSortingStrategy} + > + {actionOrder.more.map((id) => ( + + ))} + + +
+
+ +
+ +
+
+ ) +} + +export const useShowCustomizeToolbarModal = () => { + const [t] = useTranslation("settings") + const { present } = useModalStack() + + return useCallback(() => { + present({ + id: "customize-toolbar", + title: t("customizeToolbar.title"), + content: () => , + overlay: true, + clickOutsideToDismiss: true, + }) + }, [present, t]) +} diff --git a/apps/renderer/src/modules/entry-content/actions/header-actions.tsx b/apps/renderer/src/modules/entry-content/actions/header-actions.tsx index 0f8318ac3..344a64379 100644 --- a/apps/renderer/src/modules/entry-content/actions/header-actions.tsx +++ b/apps/renderer/src/modules/entry-content/actions/header-actions.tsx @@ -6,10 +6,12 @@ import { shortcuts } from "~/constants/shortcuts" import { useEntryActions } from "~/hooks/biz/useEntryActions" import { COMMAND_ID } from "~/modules/command/commands/id" import { useCommandHotkey } from "~/modules/command/hooks/use-register-hotkey" +import { useToolbarOrderMap } from "~/modules/customize-toolbar/hooks" import { useEntry } from "~/store/entry/hooks" export const EntryHeaderActions = ({ entryId, view }: { entryId: string; view?: FeedViewType }) => { const actionConfigs = useEntryActions({ entryId, view }) + const orderMap = useToolbarOrderMap() const entry = useEntry(entryId) const hasModal = useHasModal() @@ -22,18 +24,16 @@ export const EntryHeaderActions = ({ entryId, view }: { entryId: string; view?: }) return actionConfigs - .filter( - (item) => - !item.id.startsWith("integration") && - !( - [ - COMMAND_ID.entry.read, - COMMAND_ID.entry.unread, - COMMAND_ID.entry.copyLink, - COMMAND_ID.entry.openInBrowser, - ] as string[] - ).includes(item.id), - ) + .filter((item) => { + const order = orderMap.get(item.id) + if (!order) return false + return order.type === "main" + }) + .sort((a, b) => { + const orderA = orderMap.get(a.id)?.order || 0 + const orderB = orderMap.get(b.id)?.order || 0 + return orderA - orderB + }) .map((config) => { return ( { const actionConfigs = useEntryActions({ entryId, view }) + const orderMap = useToolbarOrderMap() const availableActions = useMemo( () => - actionConfigs.filter( - (item) => - item.id.startsWith("integration") || - ([COMMAND_ID.entry.copyLink, COMMAND_ID.entry.openInBrowser] as string[]).includes( - item.id, - ), - ), - [actionConfigs], + actionConfigs + .filter((item) => { + const order = orderMap.get(item.id) + if (!order) return false + return order.type !== "main" + }) + .sort((a, b) => { + const orderA = orderMap.get(a.id)?.order || 0 + const orderB = orderMap.get(b.id)?.order || 0 + return orderA - orderB + }), + [actionConfigs, orderMap], ) if (availableActions.length === 0) { diff --git a/changelog/next.md b/changelog/next.md index 17888d80b..d27f3fdff 100644 --- a/changelog/next.md +++ b/changelog/next.md @@ -2,6 +2,9 @@ ## New Features +- **Customize toolbar**: Customize the toolbar to display the items you most frequently use. + ![Customize toolbar](https://github.com/RSSNext/assets/blob/main/customize-toolbar.mp4?raw=true) + ## Improvements ## Bug Fixes diff --git a/locales/settings/en.json b/locales/settings/en.json index c377b5d8a..c53a76421 100644 --- a/locales/settings/en.json +++ b/locales/settings/en.json @@ -97,6 +97,7 @@ "appearance.zen_mode.description": "Zen mode is an undisturbed reading mode that allows you to focus on the content without any interference. Enabling Zen mode will hide the sidebar.", "appearance.zen_mode.label": "Zen mode", "common.give_star": "Love our product? Give us a star on GitHub!", + "customizeToolbar.title": "Customize Toolbar", "data_control.app_cache_limit.description": "The maximum size of the app cache. Once the cache reaches this size, the oldest items will be deleted to free up space.", "data_control.app_cache_limit.label": "App Cache Limit", "data_control.clean_cache.button": "Clean Cache", diff --git a/packages/shared/src/interface/settings.ts b/packages/shared/src/interface/settings.ts index b66b6b72b..d9375de77 100644 --- a/packages/shared/src/interface/settings.ts +++ b/packages/shared/src/interface/settings.ts @@ -52,6 +52,12 @@ export interface UISettings { pictureViewMasonry: boolean pictureViewFilterNoImage: boolean wideMode: boolean + + // Action Order + toolbarOrder: { + main: (string | number)[] + more: (string | number)[] + } } export interface IntegrationSettings { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 96f838ee6..30d64bcd7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -632,6 +632,9 @@ importers: '@dnd-kit/core': specifier: ^6.3.1 version: 6.3.1(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@dnd-kit/sortable': + specifier: ^10.0.0 + version: 10.0.0(@dnd-kit/core@6.3.1(react-dom@18.3.1(react@18.3.1))(react@18.3.1))(react@18.3.1) '@egoist/tipc': specifier: 0.3.2 version: 0.3.2(electron@33.3.0)(react@18.3.1) @@ -2253,6 +2256,12 @@ packages: react: '>=16.8.0' react-dom: '>=16.8.0' + '@dnd-kit/sortable@10.0.0': + resolution: {integrity: sha512-+xqhmIIzvAYMGfBYYnbKuNicfSsk4RksY2XdmJhT+HAC01nix6fHCztU68jooFiMUB01Ky3F0FyOvhG/BZrWkg==} + peerDependencies: + '@dnd-kit/core': ^6.3.0 + react: '>=16.8.0' + '@dnd-kit/utilities@3.2.2': resolution: {integrity: sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg==} peerDependencies: @@ -15739,6 +15748,13 @@ snapshots: react-dom: 18.3.1(react@18.3.1) tslib: 2.8.1 + '@dnd-kit/sortable@10.0.0(@dnd-kit/core@6.3.1(react-dom@18.3.1(react@18.3.1))(react@18.3.1))(react@18.3.1)': + dependencies: + '@dnd-kit/core': 6.3.1(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@dnd-kit/utilities': 3.2.2(react@18.3.1) + react: 18.3.1 + tslib: 2.8.1 + '@dnd-kit/utilities@3.2.2(react@18.3.1)': dependencies: react: 18.3.1