diff --git a/src/components/FileMachineProvider.tsx b/src/components/FileMachineProvider.tsx index b72027df7..4389f5c6d 100644 --- a/src/components/FileMachineProvider.tsx +++ b/src/components/FileMachineProvider.tsx @@ -34,7 +34,8 @@ import { settingsActor, useSettings } from 'machines/appMachine' import { createRouteCommands } from 'lib/commandBarConfigs/routeCommandConfig' import { useToken } from 'machines/appMachine' import { createNamedViewsCommand } from 'lib/commandBarConfigs/namedViewsConfig' -import { reportRejection } from 'lib/trap' +import { err, reportRejection } from 'lib/trap' +import { newKclFile } from 'lang/project' type MachineContext = { state: StateFrom @@ -248,7 +249,12 @@ export const FileMachineProvider = ({ createdPath ) } else { - await window.electron.writeFile(createdPath, input.content ?? '') + const codeToWrite = newKclFile( + input.content ?? '', + settings.modeling.defaultUnit.current + ) + if (err(codeToWrite)) return Promise.reject(codeToWrite) + await window.electron.writeFile(createdPath, codeToWrite) } } @@ -277,7 +283,12 @@ export const FileMachineProvider = ({ }) createdName = name createdPath = path - await window.electron.writeFile(createdPath, input.content ?? '') + const codeToWrite = newKclFile( + input.content ?? '', + settings.modeling.defaultUnit.current + ) + if (err(codeToWrite)) return Promise.reject(codeToWrite) + await window.electron.writeFile(createdPath, codeToWrite) } return { diff --git a/src/components/ProjectsContextProvider.tsx b/src/components/ProjectsContextProvider.tsx index f1d1448ef..1f59ec545 100644 --- a/src/components/ProjectsContextProvider.tsx +++ b/src/components/ProjectsContextProvider.tsx @@ -32,6 +32,8 @@ import { } from 'lib/constants' import { codeManager, kclManager } from 'lib/singletons' import { Project } from 'lib/project' +import { newKclFile } from 'lang/project' +import { err } from 'lib/trap' type MachineContext = { state?: StateFrom @@ -120,7 +122,13 @@ const ProjectsContextWeb = ({ children }: { children: React.ReactNode }) => { createFile: fromPromise(async ({ input }) => { // Browser version doesn't navigate, just overwrites the current file clearImportSearchParams() - codeManager.updateCodeStateEditor(input.code || '') + + const codeToWrite = newKclFile( + input.code ?? '', + settings.modeling.defaultUnit.current + ) + if (err(codeToWrite)) return Promise.reject(codeToWrite) + codeManager.updateCodeStateEditor(codeToWrite) await codeManager.writeToFile() await kclManager.executeCode(true) @@ -412,7 +420,12 @@ const ProjectsContextDesktop = ({ }) fileName = name - await window.electron.writeFile(path, input.code || '') + const codeToWrite = newKclFile( + input.code ?? '', + settings.modeling.defaultUnit.current + ) + if (err(codeToWrite)) return Promise.reject(codeToWrite) + await window.electron.writeFile(path, codeToWrite) return { message, diff --git a/src/lang/project.ts b/src/lang/project.ts new file mode 100644 index 000000000..08be8b7c1 --- /dev/null +++ b/src/lang/project.ts @@ -0,0 +1,28 @@ +import { UnitLength } from '@rust/kcl-lib/bindings/ModelingCmd' +import { + changeKclSettings, + DEFAULT_DEFAULT_ANGLE_UNIT, + unitAngleToUnitAng, + unitLengthToUnitLen, +} from './wasm' +import { DEFAULT_DEFAULT_LENGTH_UNIT } from 'lib/settings/settingsTypes' + +/** + * Create a new KCL file with the given initial content and default length unit. + */ +export function newKclFile( + initialContent: string, + defaultLengthUnit: UnitLength +): string | Error { + // 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 changeKclSettings(initialContent, { + defaultLengthUnits: unitLengthToUnitLen(defaultLengthUnit), + defaultAngleUnits: unitAngleToUnitAng(DEFAULT_DEFAULT_ANGLE_UNIT), + stdPath: null, + }) +}