Skip to content

Commit

Permalink
feat(app,api): add line number start option (#624)
Browse files Browse the repository at this point in the history
* feat(app,api): add line number start option
* chore: cleanup
* Create slimy-dolphins-add.md
* test(api): add integration test
* lint: fix lint
* lint: fix lint
  • Loading branch information
riccardoperra authored Apr 20, 2024
1 parent 8b49087 commit 9e537bc
Show file tree
Hide file tree
Showing 23 changed files with 1,706 additions and 155 deletions.
6 changes: 6 additions & 0 deletions .changeset/slimy-dolphins-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@codeimage/api": minor
"@codeimage/app": minor
---

feat(app,api): add line number start option
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "SnippetEditorTab" ADD COLUMN "lineNumberStart" INTEGER NOT NULL DEFAULT 1;
2 changes: 2 additions & 0 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ model SnippetEditorTab {
languageId String
tabName String @default("")
lineNumberStart Int @default(1)
@@unique([id, projectId])
}

Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/modules/project/domain/projectUpdateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ interface EditorUpdateRequest {
code: NonNullable<SnippetEditorTab['code']>;
languageId: NonNullable<SnippetEditorTab['languageId']>;
tabName: NonNullable<SnippetEditorTab['tabName']>;
lineNumberStart: NonNullable<SnippetEditorTab['lineNumberStart']>;
}

interface EditorTabResponse {
id: NonNullable<SnippetEditorTab['id']>;
code: NonNullable<SnippetEditorTab['code']>;
languageId: NonNullable<SnippetEditorTab['languageId']>;
tabName: NonNullable<SnippetEditorTab['tabName']>;
lineNumberStart: NonNullable<SnippetEditorTab['lineNumberStart']>;
}

export interface ProjectUpdateRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,22 @@ export function makePrismaProjectRepository(
},
},
upsert: data.editors.map(editor => {
const {languageId, code, tabName} = editor;
const {languageId, code, tabName, lineNumberStart, id} = editor;
return {
where: {
id: editor.id,
id,
},
create: {
code,
tabName,
languageId,
lineNumberStart,
},
update: {
code,
tabName,
languageId,
lineNumberStart,
},
};
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export function createProjectRequestMapper(
languageId: editor.languageId,
code: editor.code,
tabName: editor.tabName,
lineNumberStart: editor.lineNumberStart,
})),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function createCompleteProjectGetByIdResponseMapper(
languageId: editor.languageId,
code: editor.code,
tabName: editor.tabName,
lineNumberStart: editor.lineNumberStart,
})),
isOwner: false,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const SnippetEditorTabsCreateRequestSchema = Type.Array(
code: Type.String(),
languageId: Type.String(),
tabName: Type.String(),
lineNumberStart: Type.Integer({minimum: 1, maximum: 999_999}),
},
{title: 'SnippetEditorTabCreateRequest'},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const SnippetEditorTabsUpdateRequestSchema = Type.Array(
code: Type.String(),
languageId: Type.String(),
tabName: Type.String(),
lineNumberStart: Type.Integer({minimum: 1, maximum: 999_999}),
},
{title: 'SnippetEditorTabUpdateRequest'},
),
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/modules/project/schema/project.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const BaseSnippetEditorTabsSchema = Type.Array(
code: Type.String(),
languageId: Type.String(),
tabName: Type.String(),
lineNumberStart: Type.Integer({minimum: 1, maximum: 999_999}),
}),
);

Expand Down
7 changes: 6 additions & 1 deletion apps/api/test/routes/v1/project/create.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ test<TestContext>('POST /v1/project/ [Create Project] -> 200', async context =>
padding: 0,
},
editors: [
{code: 'function(){}', languageId: 'javascript', tabName: 'index.jsx'},
{
code: 'function(){}',
languageId: 'javascript',
tabName: 'index.jsx',
lineNumberStart: 300,
},
],
editorOptions: {
fontWeight: 400,
Expand Down
4 changes: 4 additions & 0 deletions apps/api/test/routes/v1/project/update.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ test<TestContext>('POST /v1/project/:id [Update Project] -> 200', async context
code: '## title',
languageId: 'markdown',
tabName: 'README.md',
lineNumberStart: 1,
},
{
id: 'temp',
code: '2',
languageId: 'typescript',
tabName: 'index.tsx',
lineNumberStart: 300,
},
],
editorOptions: {
Expand Down Expand Up @@ -134,12 +136,14 @@ test<TestContext>('POST /v1/project/:id [Update Project] -> 200', async context
code: '## title',
languageId: 'markdown',
tabName: 'README.md',
lineNumberStart: 1,
},
{
id: body.editorTabs[1].id,
code: '2',
languageId: 'typescript',
tabName: 'index.tsx',
lineNumberStart: 300,
},
] as ProjectUpdateResponse['editorTabs'],
'return updated editor tabs',
Expand Down
11 changes: 4 additions & 7 deletions apps/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
"compilerOptions": {
"outDir": "dist",
"resolveJsonModule": false,
"declarationMap": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"moduleResolution": "NodeNext",
"module": "NodeNext",
"target": "ES2022",
"typeRoots": [
"./src/common/domainFunctions/functions.d.ts"
]
"typeRoots": ["./src/common/domainFunctions/functions.d.ts"]
},
"include": [
"./src/**/*.ts"
]
}
"include": ["./src/**/*.ts"]
}
2 changes: 1 addition & 1 deletion apps/codeimage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@codemirror/search": "^6.4.0",
"@codemirror/state": "^6.2.0",
"@codemirror/view": "^6.11.0",
"@codeui/kit": "^0.0.36",
"@codeui/kit": "^0.0.37",
"@floating-ui/core": "^1.2.2",
"@floating-ui/dom": "^1.2.3",
"@formatjs/intl-relativetimeformat": "11.1.4",
Expand Down
12 changes: 9 additions & 3 deletions apps/codeimage/src/components/CustomEditor/CustomEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,15 @@ export default function CustomEditor(props: VoidProps<CustomEditorProps>) {
createExtension(() => customFontExtension());
createExtension(currentLanguage);
createExtension(currentExtraLanguage);
createExtension(() =>
editorState.options.showLineNumbers ? lineNumbers() : [],
);

const lineNumberStart = createMemo(() => editor()?.lineNumberStart);
createExtension(() => {
const lnStart = lineNumberStart() ?? 1;
const newLn = (ln: number) => ln + (lnStart - 1);
return editorState.options.showLineNumbers
? lineNumbers({formatNumber: lineNo => String(newLn(lineNo))})
: [];
});
createExtension(() => themeConfiguration()?.editorTheme || []);
createExtension(baseTheme);

Expand Down
35 changes: 32 additions & 3 deletions apps/codeimage/src/components/PropertyEditor/EditorStyleForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {getRootEditorStore} from '@codeimage/store/editor';
import {getActiveEditorStore} from '@codeimage/store/editor/activeEditor';
import {dispatchUpdateTheme} from '@codeimage/store/effects/onThemeChange';
import {getThemeStore} from '@codeimage/store/theme/theme.store';
import {createSelectOptions, Select} from '@codeui/kit';
import {createSelectOptions, Select, NumberField} from '@codeui/kit';
import {getUmami} from '@core/constants/umami';
import {DynamicSizedContainer} from '@ui/DynamicSizedContainer/DynamicSizedContainer';
import {SegmentedField} from '@ui/SegmentedField/SegmentedField';
Expand All @@ -18,6 +18,7 @@ import {PanelDivider} from './PanelDivider';
import {PanelHeader} from './PanelHeader';
import {PanelRow, TwoColumnPanelRow} from './PanelRow';
import {SuspenseEditorItem} from './SuspenseEditorItem';
import {appEnvironment} from '@core/configuration';

const languages: readonly LanguageDefinition[] = [...SUPPORTED_LANGUAGES].sort(
(a, b) => {
Expand All @@ -35,9 +36,15 @@ const languages: readonly LanguageDefinition[] = [...SUPPORTED_LANGUAGES].sort(

export const EditorStyleForm: ParentComponent = () => {
const {themeArray} = getThemeStore();
const {lineNumbers: lineNumbersConfig} = appEnvironment;
const [t] = useI18n<AppLocaleEntries>();
const {editor, setLanguageId, formatter, setFormatterName} =
getActiveEditorStore();
const {
editor,
setLanguageId,
formatter,
setFormatterName,
setLineNumberStart,
} = getActiveEditorStore();
const {
state,
actions: {setShowLineNumbers, setFontWeight, setFontId, setEnableLigatures},
Expand Down Expand Up @@ -207,6 +214,28 @@ export const EditorStyleForm: ParentComponent = () => {
</SuspenseEditorItem>
</TwoColumnPanelRow>
</PanelRow>

<Show when={state.options.showLineNumbers}>
<PanelRow
for={'frameLineNumberStartField'}
label={t('frame.lineNumberStart')}
>
<TwoColumnPanelRow>
<SuspenseEditorItem
fallback={<SkeletonLine width={'100%'} height={'26px'} />}
>
<NumberField
size={'xs'}
min={lineNumbersConfig.min}
max={lineNumbersConfig.max}
id={'frameLineNumberStartField'}
value={editor().lineNumberStart}
onChange={setLineNumberStart}
/>
</SuspenseEditorItem>
</TwoColumnPanelRow>
</PanelRow>
</Show>
</DynamicSizedContainer>

<PanelDivider />
Expand Down
14 changes: 9 additions & 5 deletions apps/codeimage/src/core/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ export const [appEnvironment] = createConfiguration({
locales: SUPPORTED_LOCALES,
themes: [],
languages: [],
lineNumbers: {
min: 1,
max: 999_999,
},
editorPadding: [
{label: '0', value: "0"},
{label: '16', value: "16"},
{label: '32', value: "32"},
{label: '64', value: "64"},
{label: '128', value: "128"},
{label: '0', value: '0'},
{label: '16', value: '16'},
{label: '32', value: '32'},
{label: '64', value: '64'},
{label: '128', value: '128'},
],
editorRadius: [
{label: '0', value: 0},
Expand Down
4 changes: 4 additions & 0 deletions apps/codeimage/src/i18n/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default {
editor: 'Editor',
language: 'Language',
lineNumbers: 'Line number',
lineNumberStart: 'Line start',
font: 'Font',
theme: 'Theme',
fontWeight: 'Font weight',
Expand All @@ -43,6 +44,7 @@ export default {
editor: 'Editor',
language: 'Linguaggio',
lineNumbers: 'Numeri di riga',
lineNumberStart: 'Line start',
font: 'Carattere',
fontWeight: 'Peso carattere',
reflection: 'Riflesso',
Expand Down Expand Up @@ -70,6 +72,7 @@ export default {
editor: 'Editor',
language: 'Sprache',
lineNumbers: 'Zeilennummer',
lineNumberStart: 'Line start',
font: 'Schriftart',
fontWeight: 'Schriftstärke',
reflection: 'Reflektion',
Expand Down Expand Up @@ -97,6 +100,7 @@ export default {
editor: 'Editor',
language: 'Idioma',
lineNumbers: 'Número de línea',
lineNumberStart: 'Line start',
font: 'Fuente',
fontWeight: 'Peso de fuente',
reflection: 'Reflexión',
Expand Down
1 change: 1 addition & 0 deletions apps/codeimage/src/pages/Dashboard/dashboard.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function makeDashboardState(authState = getAuth0State()) {
code: appEnvironment.defaultState.editor.code,
languageId: appEnvironment.defaultState.editor.languageId,
tabName: 'index.tsx',
lineNumberStart: 1,
},
],
},
Expand Down
18 changes: 17 additions & 1 deletion apps/codeimage/src/state/editor/activeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {useI18n} from '@codeimage/locale';
import {getRootEditorStore} from '@codeimage/store/editor';
import {getUiStore} from '@codeimage/store/ui';
import {toast} from '@codeimage/ui';
import {isNonNullable} from '@solid-primitives/utils';
import {appEnvironment} from '@core/configuration';
import {clamp, isNonNullable} from '@solid-primitives/utils';
import {createEffect, createMemo, createRoot, on} from 'solid-js';
import {createPrettierFormatter} from '../../hooks/createPrettierFormatter';
import {AppLocaleEntries} from '../../i18n';
Expand Down Expand Up @@ -46,6 +47,20 @@ const $activeEditorState = () => {
const setCode = (code: string) =>
setEditors(currentEditorIndex(), 'code', code);

const setLineNumberStart = (lineNumberStart: number | null | undefined) => {
if (lineNumberStart === null) return;
return setEditors(
currentEditorIndex(),
'lineNumberStart',
// TODO Already done by @codeui/kit but I don't feel safe about this component I made
clamp(
lineNumberStart ?? 1,
appEnvironment.lineNumbers.min,
appEnvironment.lineNumbers.max,
),
);
};

const setFormatterName = (formatter: string | null) =>
setEditors(currentEditorIndex(), 'formatter', formatter);

Expand Down Expand Up @@ -83,6 +98,7 @@ const $activeEditorState = () => {
editor: currentEditor,
setLanguageId,
setCode,
setLineNumberStart,
formatter,
setFormatterName,
canFormat: formatter.canFormat,
Expand Down
5 changes: 5 additions & 0 deletions apps/codeimage/src/state/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function getInitialEditorState(): EditorState {
code: appEnvironment.defaultState.editor.code,
languageId: appEnvironment.defaultState.editor.languageId,
formatter: null,
lineNumberStart: 1,
tab: {
tabName: 'index.tsx',
tabIcon: undefined,
Expand Down Expand Up @@ -104,6 +105,7 @@ export function createEditorsStore() {
languageId: editor.languageId,
id: editor.id,
code: editor.code,
lineNumberStart: editor.lineNumberStart,
}));
return {
options: {...state.options, ...persistedState.options},
Expand All @@ -114,6 +116,7 @@ export function createEditorsStore() {
languageId: editor.languageId,
tab: {tabName: editor.tabName},
id: editor.id,
lineNumberStart: editor.lineNumberStart,
};
}),
};
Expand All @@ -136,6 +139,7 @@ export function createEditorsStore() {
code: editor.code,
tabName: editor.tab.tabName ?? '',
id: editor.id,
lineNumberStart: editor.lineNumberStart ?? 1,
};
}),
options: {
Expand Down Expand Up @@ -245,6 +249,7 @@ export function createEditorsStore() {
languageId: editor.languageId,
id: editor.id,
code: editor.code,
lineNumberStart: editor.lineNumberStart ?? 1,
} as EditorState),
),
);
Expand Down
Loading

0 comments on commit 9e537bc

Please sign in to comment.