Change so that new KCL files respect the length unit setting
This commit is contained in:
		@ -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<T extends AnyStateMachine> = {
 | 
			
		||||
  state: StateFrom<T>
 | 
			
		||||
@ -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 {
 | 
			
		||||
 | 
			
		||||
@ -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<T extends AnyStateMachine> = {
 | 
			
		||||
  state?: StateFrom<T>
 | 
			
		||||
@ -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,
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										28
									
								
								src/lang/project.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/lang/project.ts
									
									
									
									
									
										Normal file
									
								
							@ -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,
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user