From fc64ff4de296466be1c392353d60091c8bc6d616 Mon Sep 17 00:00:00 2001 From: Jordan Dalby Date: Wed, 30 Oct 2024 20:15:07 +0000 Subject: [PATCH] Adds line numbers to the code snippets, with a setting to turn them off if desired --- client/src/components/settings/SettingsModal.tsx | 14 +++++++++++++- client/src/components/snippets/FullCodeBlock.tsx | 15 ++++++++++++++- .../src/components/snippets/PreviewCodeBlock.tsx | 15 ++++++++++++++- client/src/components/snippets/SnippetCard.tsx | 4 +++- client/src/components/snippets/SnippetList.tsx | 4 +++- client/src/components/snippets/SnippetModal.tsx | 5 +++-- client/src/components/snippets/SnippetStorage.tsx | 6 ++++-- client/src/hooks/useSettings.ts | 10 +++++++++- client/src/types/types.ts | 6 ++++++ 9 files changed, 69 insertions(+), 10 deletions(-) diff --git a/client/src/components/settings/SettingsModal.tsx b/client/src/components/settings/SettingsModal.tsx index 065dd51..bbf24e4 100644 --- a/client/src/components/settings/SettingsModal.tsx +++ b/client/src/components/settings/SettingsModal.tsx @@ -9,6 +9,7 @@ const SettingsModal: React.FC = ({ isOpen, onClose, settings const [includeCodeInSearch, setIncludeCodeInSearch] = useState(settings.includeCodeInSearch); const [showCategories, setShowCategories] = useState(settings.showCategories); const [expandCategories, setExpandCategories] = useState(settings.expandCategories); + const [showLineNumbers, setShowLineNumbers] = useState(settings.showLineNumbers); const handleSave = () => { onSettingsChange({ @@ -17,7 +18,8 @@ const SettingsModal: React.FC = ({ isOpen, onClose, settings previewLines, includeCodeInSearch, showCategories, - expandCategories + expandCategories, + showLineNumbers }); onClose(); }; @@ -93,6 +95,16 @@ const SettingsModal: React.FC = ({ isOpen, onClose, settings /> )} +
+ + setShowLineNumbers(e.target.checked)} + className="form-checkbox h-5 w-5 text-blue-600 rounded focus:ring-blue-500" + /> +
{snippet.code.split('\n').length > previewLines && ( diff --git a/client/src/components/snippets/SnippetList.tsx b/client/src/components/snippets/SnippetList.tsx index 4e97ff5..5bea7a0 100644 --- a/client/src/components/snippets/SnippetList.tsx +++ b/client/src/components/snippets/SnippetList.tsx @@ -13,7 +13,8 @@ const SnippetList: React.FC = ({ showCodePreview, previewLines, showCategories, - expandCategories + expandCategories, + showLineNumbers }) => { if (snippets.length === 0) { return ( @@ -42,6 +43,7 @@ const SnippetList: React.FC = ({ previewLines={previewLines} showCategories={showCategories} expandCategories={expandCategories} + showLineNumbers={showLineNumbers} /> ))} diff --git a/client/src/components/snippets/SnippetModal.tsx b/client/src/components/snippets/SnippetModal.tsx index 737f1da..78448a5 100644 --- a/client/src/components/snippets/SnippetModal.tsx +++ b/client/src/components/snippets/SnippetModal.tsx @@ -8,7 +8,8 @@ const SnippetModal: React.FC = ({ snippet, isOpen, onClose, - onCategoryClick + onCategoryClick, + showLineNumbers }) => { if (!snippet) return null; @@ -30,7 +31,7 @@ const SnippetModal: React.FC = ({ showAll={true} />
- +
); diff --git a/client/src/components/snippets/SnippetStorage.tsx b/client/src/components/snippets/SnippetStorage.tsx index 7b51d06..f1e8396 100644 --- a/client/src/components/snippets/SnippetStorage.tsx +++ b/client/src/components/snippets/SnippetStorage.tsx @@ -14,7 +14,7 @@ const SnippetStorage: React.FC = () => { const { viewMode, setViewMode, compactView, showCodePreview, previewLines, includeCodeInSearch, updateSettings, - showCategories, expandCategories + showCategories, expandCategories, showLineNumbers } = useSettings(); const [searchTerm, setSearchTerm] = useState(''); @@ -157,6 +157,7 @@ const SnippetStorage: React.FC = () => { previewLines={previewLines} showCategories={showCategories} expandCategories={expandCategories} + showLineNumbers={showLineNumbers} /> {selectedSnippet && ( @@ -165,6 +166,7 @@ const SnippetStorage: React.FC = () => { isOpen={!!selectedSnippet} onClose={closeSnippet} onCategoryClick={handleCategoryClick} + showLineNumbers={showLineNumbers} /> )} @@ -178,7 +180,7 @@ const SnippetStorage: React.FC = () => { setIsSettingsModalOpen(false)} - settings={{ compactView, showCodePreview, previewLines, includeCodeInSearch, showCategories, expandCategories }} + settings={{ compactView, showCodePreview, previewLines, includeCodeInSearch, showCategories, expandCategories, showLineNumbers }} onSettingsChange={updateSettings} /> diff --git a/client/src/hooks/useSettings.ts b/client/src/hooks/useSettings.ts index d335628..f782dbc 100644 --- a/client/src/hooks/useSettings.ts +++ b/client/src/hooks/useSettings.ts @@ -10,6 +10,7 @@ export const useSettings = () => { const [includeCodeInSearch, setIncludeCodeInSearch] = useState(() => localStorage.getItem('includeCodeInSearch') === 'true'); const [showCategories, setShowCategories] = useState(() => localStorage.getItem('showCategories') !== 'false'); const [expandCategories, setExpandCategories] = useState(() => localStorage.getItem('expandCategories') === 'true'); + const [showLineNumbers, setShowLineNumbers] = useState(() => localStorage.getItem('showLineNumbers') === 'true') useEffect(() => { localStorage.setItem('viewMode', viewMode); @@ -39,6 +40,10 @@ export const useSettings = () => { localStorage.setItem('expandCategories', expandCategories.toString()); }, [expandCategories]); + useEffect(() => { + localStorage.setItem('showLineNumbers', showLineNumbers.toString()); + }, [showLineNumbers]); + const updateSettings = (newSettings: { compactView: boolean; showCodePreview: boolean; @@ -46,6 +51,7 @@ export const useSettings = () => { includeCodeInSearch: boolean; showCategories: boolean; expandCategories: boolean; + showLineNumbers: boolean; }) => { setCompactView(newSettings.compactView); setShowCodePreview(newSettings.showCodePreview); @@ -53,6 +59,7 @@ export const useSettings = () => { setIncludeCodeInSearch(newSettings.includeCodeInSearch); setShowCategories(newSettings.showCategories); setExpandCategories(newSettings.expandCategories); + setShowLineNumbers(newSettings.showLineNumbers); }; return { @@ -64,6 +71,7 @@ export const useSettings = () => { includeCodeInSearch, showCategories, expandCategories, - updateSettings + updateSettings, + showLineNumbers }; }; \ No newline at end of file diff --git a/client/src/types/types.ts b/client/src/types/types.ts index 1f5acc3..cde8d7c 100644 --- a/client/src/types/types.ts +++ b/client/src/types/types.ts @@ -20,6 +20,7 @@ export interface Snippet { previewLines: number; showCategories: boolean; expandCategories: boolean; + showLineNumbers: boolean; } export interface SnippetModalProps { @@ -27,6 +28,7 @@ export interface Snippet { isOpen: boolean; onClose: () => void; onCategoryClick: (category: string) => void; + showLineNumbers: boolean; } export interface EditSnippetModalProps { @@ -89,6 +91,7 @@ export interface Snippet { includeCodeInSearch: boolean; showCategories: boolean; expandCategories: boolean; + showLineNumbers: boolean; }; onSettingsChange: (newSettings: SettingsModalProps['settings']) => void; } @@ -122,6 +125,7 @@ export interface Snippet { previewLines: number; showCategories: boolean; expandCategories: boolean; + showLineNumbers: boolean; } export interface ModalProps { @@ -137,12 +141,14 @@ export interface Snippet { export interface FullCodeBlockProps { code: string; language?: string; + showLineNumbers?: boolean; } export interface PreviewCodeBlockProps { code: string; language?: string; previewLines?: number; + showLineNumbers?: boolean; } export interface DeleteConfirmationModalProps {