diff --git a/src/components/FileMachineProvider.tsx b/src/components/FileMachineProvider.tsx index 4389f5c6d..f69247d8e 100644 --- a/src/components/FileMachineProvider.tsx +++ b/src/components/FileMachineProvider.tsx @@ -16,6 +16,7 @@ import { import { fileMachine } from 'machines/fileMachine' import { isDesktop } from 'lib/isDesktop' import { + DEFAULT_DEFAULT_LENGTH_UNIT, DEFAULT_FILE_NAME, DEFAULT_PROJECT_KCL_FILE, FILE_EXT, @@ -250,7 +251,7 @@ export const FileMachineProvider = ({ ) } else { const codeToWrite = newKclFile( - input.content ?? '', + input.content, settings.modeling.defaultUnit.current ) if (err(codeToWrite)) return Promise.reject(codeToWrite) @@ -284,7 +285,7 @@ export const FileMachineProvider = ({ createdName = name createdPath = path const codeToWrite = newKclFile( - input.content ?? '', + input.content, settings.modeling.defaultUnit.current ) if (err(codeToWrite)) return Promise.reject(codeToWrite) @@ -423,7 +424,9 @@ export const FileMachineProvider = ({ authToken: token ?? '', projectData, settings: { - defaultUnit: settings.modeling.defaultUnit.current ?? 'mm', + defaultUnit: + settings.modeling.defaultUnit.current ?? + DEFAULT_DEFAULT_LENGTH_UNIT, }, specialPropsForSampleCommand: { onSubmit: async (data) => { diff --git a/src/components/ProjectsContextProvider.tsx b/src/components/ProjectsContextProvider.tsx index b242b1f85..28a1b0bb5 100644 --- a/src/components/ProjectsContextProvider.tsx +++ b/src/components/ProjectsContextProvider.tsx @@ -124,7 +124,7 @@ const ProjectsContextWeb = ({ children }: { children: React.ReactNode }) => { clearImportSearchParams() const codeToWrite = newKclFile( - input.code ?? '', + input.code, settings.modeling.defaultUnit.current ) if (err(codeToWrite)) return Promise.reject(codeToWrite) @@ -424,7 +424,7 @@ const ProjectsContextDesktop = ({ fileName = name if (!fileLoaded) { const codeToWrite = newKclFile( - input.code ?? '', + input.code, settings.modeling.defaultUnit.current ) if (err(codeToWrite)) return Promise.reject(codeToWrite) diff --git a/src/lang/project.ts b/src/lang/project.ts index 353ac2ec7..29483123d 100644 --- a/src/lang/project.ts +++ b/src/lang/project.ts @@ -9,18 +9,24 @@ import { DEFAULT_DEFAULT_LENGTH_UNIT } from 'lib/constants' /** * Create a new KCL file with the given initial content and default length unit. + * @returns KCL string */ export function newKclFile( - initialContent: string, + initialContent: string | undefined, defaultLengthUnit: UnitLength ): string | Error { + // If we're given initial content, we're loading a file that should already + // have units in it. Don't modify it. + if (initialContent !== undefined) { + return initialContent + } // If the default length unit is the same as the default default length unit, // there's no need to add the attribute. if (defaultLengthUnit === DEFAULT_DEFAULT_LENGTH_UNIT) { - return initialContent + return '' } - return changeKclSettings(initialContent, { + return changeKclSettings('', { defaultLengthUnits: unitLengthToUnitLen(defaultLengthUnit), defaultAngleUnits: unitAngleToUnitAng(DEFAULT_DEFAULT_ANGLE_UNIT), stdPath: null, diff --git a/src/lib/desktop.ts b/src/lib/desktop.ts index 455e0c4a8..b40ad1dcd 100644 --- a/src/lib/desktop.ts +++ b/src/lib/desktop.ts @@ -118,13 +118,10 @@ export async function createNewProjectDirectory( // When initialCode is present, we're loading existing code. If it's not // present, we're creating a new project, and we want to incorporate the // user's settings. - const codeToWrite = - initialCode ?? - newKclFile( - '', - configuration?.settings?.modeling?.base_unit ?? - DEFAULT_DEFAULT_LENGTH_UNIT - ) + const codeToWrite = newKclFile( + initialCode, + configuration?.settings?.modeling?.base_unit ?? DEFAULT_DEFAULT_LENGTH_UNIT + ) if (err(codeToWrite)) return Promise.reject(codeToWrite) await window.electron.writeFile(projectFile, codeToWrite) const metadata = await window.electron.stat(projectFile) diff --git a/src/lib/kclCommands.ts b/src/lib/kclCommands.ts index f8c478627..ead612b1c 100644 --- a/src/lib/kclCommands.ts +++ b/src/lib/kclCommands.ts @@ -48,13 +48,7 @@ export function kclCommands(commandProps: KclCommandConfig): Command[] { description: 'Imports an example KCL program into the editor.', needsReview: true, icon: 'code', - reviewMessage: ({ argumentsToSubmit }) => - argumentsToSubmit.method === 'newFile' - ? CommandBarOverwriteWarning({ - heading: 'Create a new file, overwrite project units?', - message: `This will add the sample as a new file to your project, and replace your current project units with the sample's units.`, - }) - : CommandBarOverwriteWarning({}), + reviewMessage: ({ argumentsToSubmit }) => CommandBarOverwriteWarning({}), groupId: 'code', onSubmit(data) { if (!data?.sample) { diff --git a/src/machines/fileMachine.ts b/src/machines/fileMachine.ts index e067d85dc..f628ed643 100644 --- a/src/machines/fileMachine.ts +++ b/src/machines/fileMachine.ts @@ -126,7 +126,7 @@ export const fileMachine = setup({ makeDir: boolean selectedDirectory: FileEntry targetPathToClone?: string - content: string + content?: string shouldSetToRename: boolean } }) => Promise.resolve({ message: '', path: '' }) @@ -152,7 +152,7 @@ export const fileMachine = setup({ name: string makeDir: boolean selectedDirectory: FileEntry - content: string + content?: string } }) => Promise.resolve({ path: '' }) ), @@ -238,7 +238,7 @@ export const fileMachine = setup({ makeDir: event.data.makeDir, selectedDirectory: context.selectedDirectory, targetPathToClone: event.data.targetPathToClone, - content: event.data.content ?? '', + content: event.data.content, shouldSetToRename: event.data.shouldSetToRename ?? false, } }, @@ -417,7 +417,7 @@ export const fileMachine = setup({ name: event.data.name, makeDir: event.data.makeDir, selectedDirectory: context.selectedDirectory, - content: event.data.content ?? '', + content: event.data.content, } }, onDone: 'Reading files',