Fix loading samples

This commit is contained in:
Jonathan Tran
2025-03-14 12:38:35 -04:00
parent 1e23f37287
commit fd45574652
6 changed files with 26 additions and 26 deletions

View File

@ -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) => {

View File

@ -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)

View File

@ -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,

View File

@ -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)

View File

@ -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) {

View File

@ -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',