-
-
Notifications
You must be signed in to change notification settings - Fork 213
feat: add font size and word wrap settings to editor preferences #721
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
d78f35e
87c91cf
bfcc2e7
ef8926e
67cf583
c0a43e6
be9a668
4b8effb
c4e2423
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export const FONT_SIZE_OPTIONS = [12, 13, 14, 15, 16, 18, 20] as const; | ||
| export const DEFAULT_FONT_SIZE = 14; | ||
| export const MIN_FONT_SIZE = FONT_SIZE_OPTIONS[0]; | ||
| export const MAX_FONT_SIZE = FONT_SIZE_OPTIONS[FONT_SIZE_OPTIONS.length - 1]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { ModelManager } from "@accordproject/concerto-core"; | |
| import { TemplateMarkInterpreter } from "@accordproject/template-engine"; | ||
| import { TemplateMarkTransformer } from "@accordproject/markdown-template"; | ||
| import { transform } from "@accordproject/markdown-transform"; | ||
| import { FONT_SIZE_OPTIONS, DEFAULT_FONT_SIZE, MIN_FONT_SIZE, MAX_FONT_SIZE } from "../constants/editorSettings"; | ||
| import { SAMPLES, Sample } from "../samples"; | ||
| import * as playground from "../samples/playground"; | ||
| import { compress, decompress } from "../utils/compression/compression"; | ||
|
|
@@ -63,6 +64,10 @@ interface AppState { | |
| toggleDataCollapse: () => void; | ||
| showLineNumbers: boolean; | ||
| setShowLineNumbers: (value: boolean) => void; | ||
| editorFontSize: number; | ||
| setEditorFontSize: (value: number) => void; | ||
| editorWordWrap: boolean; | ||
| setEditorWordWrap: (value: boolean) => void; | ||
| isSettingsOpen: boolean; | ||
| setSettingsOpen: (value: boolean) => void; | ||
| keyProtectionLevel: KeyProtectionLevel | null; | ||
|
|
@@ -162,6 +167,27 @@ const getInitialLineNumbers = () => { | |
| return true; // Default to showing line numbers | ||
| }; | ||
|
|
||
| const getInitialFontSize = () => { | ||
| if (typeof window !== 'undefined') { | ||
| const saved = localStorage.getItem('editorFontSize'); | ||
| if (saved !== null) { | ||
| const parsed = parseInt(saved, 10); | ||
| if (!isNaN(parsed) && parsed >= MIN_FONT_SIZE && parsed <= MAX_FONT_SIZE) return parsed; | ||
| } | ||
| } | ||
| return DEFAULT_FONT_SIZE; | ||
| }; | ||
|
Comment on lines
+170
to
+179
|
||
|
|
||
| const getInitialWordWrap = () => { | ||
| if (typeof window !== 'undefined') { | ||
| const saved = localStorage.getItem('editorWordWrap'); | ||
| if (saved !== null) { | ||
| return saved === 'true'; | ||
| } | ||
| } | ||
| return true; // Default to word wrap on | ||
| }; | ||
|
|
||
| const useAppStore = create<AppState>()( | ||
| immer( | ||
| devtools((set, get) => { | ||
|
|
@@ -197,6 +223,8 @@ const useAppStore = create<AppState>()( | |
| isTemplateCollapsed: false, | ||
| isDataCollapsed: false, | ||
| showLineNumbers: getInitialLineNumbers(), | ||
| editorFontSize: getInitialFontSize(), | ||
| editorWordWrap: getInitialWordWrap(), | ||
| isSettingsOpen: false, | ||
| keyProtectionLevel: null, | ||
| toggleModelCollapse: () => set((state) => ({ isModelCollapsed: !state.isModelCollapsed })), | ||
|
|
@@ -208,6 +236,19 @@ const useAppStore = create<AppState>()( | |
| } | ||
| set({ showLineNumbers: value }); | ||
| }, | ||
| setEditorFontSize: (value: number) => { | ||
| if (!FONT_SIZE_OPTIONS.includes(value as typeof FONT_SIZE_OPTIONS[number])) return; | ||
| if (typeof window !== 'undefined') { | ||
| localStorage.setItem('editorFontSize', String(value)); | ||
| } | ||
| set({ editorFontSize: value }); | ||
| }, | ||
| setEditorWordWrap: (value: boolean) => { | ||
| if (typeof window !== 'undefined') { | ||
| localStorage.setItem('editorWordWrap', String(value)); | ||
| } | ||
| set({ editorWordWrap: value }); | ||
| }, | ||
| setSettingsOpen: (value: boolean) => set({ isSettingsOpen: value }), | ||
| setEditorsVisible: (value) => { | ||
| const state = get(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,10 @@ vi.mock('../../store/store', () => { | |||||||||||||||||||
| setSettingsOpen: vi.fn(), | ||||||||||||||||||||
| showLineNumbers: true, | ||||||||||||||||||||
| setShowLineNumbers: vi.fn(), | ||||||||||||||||||||
| editorFontSize: 14, | ||||||||||||||||||||
| setEditorFontSize: vi.fn(), | ||||||||||||||||||||
| editorWordWrap: true, | ||||||||||||||||||||
| setEditorWordWrap: vi.fn(), | ||||||||||||||||||||
| textColor: '#121212', | ||||||||||||||||||||
| backgroundColor: '#ffffff', | ||||||||||||||||||||
| toggleDarkMode: vi.fn(), | ||||||||||||||||||||
|
|
@@ -76,10 +80,39 @@ describe('SettingsModal', () => { | |||||||||||||||||||
| expect(toggle).toBeChecked(); | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| it('renders the Font Size setting', () => { | ||||||||||||||||||||
| render(<SettingsModal />); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| expect(screen.getByText('Font Size')).toBeInTheDocument(); | ||||||||||||||||||||
| expect(screen.getByText('Adjust font size in code editors')).toBeInTheDocument(); | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
||||||||||||||||||||
| it('renders the Font Size select with the correct initial value', () => { | |
| render(<SettingsModal />); | |
| const fontSizeSelect = screen.getByRole('combobox', { name: /font size/i }); | |
| expect(fontSizeSelect).toBeInTheDocument(); | |
| expect(fontSizeSelect).toHaveValue('14'); | |
| }); |
Shubh-Raj marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,55 @@ | ||||||||||||||||||||||||||||||||
| import { describe, it, expect, beforeEach } from 'vitest'; | ||||||||||||||||||||||||||||||||
| import useAppStore from '../../store/store'; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| describe('useAppStore - editorFontSize', () => { | ||||||||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||||||||
| localStorage.clear(); | ||||||||||||||||||||||||||||||||
| useAppStore.setState({ | ||||||||||||||||||||||||||||||||
| editorFontSize: 14, | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should have editorFontSize set to 14 after reset', () => { | ||||||||||||||||||||||||||||||||
| const state = useAppStore.getState(); | ||||||||||||||||||||||||||||||||
| expect(state.editorFontSize).toBe(14); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+5
to
+15
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should update editorFontSize when setEditorFontSize is called', () => { | ||||||||||||||||||||||||||||||||
| const store = useAppStore.getState(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| store.setEditorFontSize(18); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| expect(useAppStore.getState().editorFontSize).toBe(18); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should persist editorFontSize to localStorage', () => { | ||||||||||||||||||||||||||||||||
| const store = useAppStore.getState(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| store.setEditorFontSize(16); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| expect(localStorage.getItem('editorFontSize')).toBe('16'); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should set various valid font sizes correctly', () => { | ||||||||||||||||||||||||||||||||
| const store = useAppStore.getState(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| store.setEditorFontSize(12); | ||||||||||||||||||||||||||||||||
| expect(useAppStore.getState().editorFontSize).toBe(12); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| store.setEditorFontSize(20); | ||||||||||||||||||||||||||||||||
| expect(useAppStore.getState().editorFontSize).toBe(20); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| store.setEditorFontSize(14); | ||||||||||||||||||||||||||||||||
| expect(useAppStore.getState().editorFontSize).toBe(14); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it('should reject font sizes outside the allowed options', () => { | ||||||||||||||||||||||||||||||||
| const store = useAppStore.getState(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| store.setEditorFontSize(5); | ||||||||||||||||||||||||||||||||
| expect(useAppStore.getState().editorFontSize).toBe(14); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| store.setEditorFontSize(100); | ||||||||||||||||||||||||||||||||
| expect(useAppStore.getState().editorFontSize).toBe(14); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| store.setEditorFontSize(5); | |
| expect(useAppStore.getState().editorFontSize).toBe(14); | |
| store.setEditorFontSize(100); | |
| expect(useAppStore.getState().editorFontSize).toBe(14); | |
| // Start from a non-default valid size | |
| store.setEditorFontSize(18); | |
| expect(useAppStore.getState().editorFontSize).toBe(18); | |
| // Invalid sizes should not change the current font size | |
| store.setEditorFontSize(5); | |
| expect(useAppStore.getState().editorFontSize).toBe(18); | |
| store.setEditorFontSize(100); | |
| expect(useAppStore.getState().editorFontSize).toBe(18); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getInitialFontSizecurrently accepts any numeric value betweenMIN_FONT_SIZEandMAX_FONT_SIZEfrom localStorage. This can load a font size that's not inFONT_SIZE_OPTIONS(e.g., 17), leaving the Settings<Select>with a value that has no matching option and making the UI/state inconsistent. Consider validating the parsed value againstFONT_SIZE_OPTIONS(or clamping/mapping to the nearest allowed option) and falling back toDEFAULT_FONT_SIZEotherwise.