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 { fileMachine } from 'machines/fileMachine'
import { isDesktop } from 'lib/isDesktop' import { isDesktop } from 'lib/isDesktop'
import { import {
DEFAULT_DEFAULT_LENGTH_UNIT,
DEFAULT_FILE_NAME, DEFAULT_FILE_NAME,
DEFAULT_PROJECT_KCL_FILE, DEFAULT_PROJECT_KCL_FILE,
FILE_EXT, FILE_EXT,
@ -250,7 +251,7 @@ export const FileMachineProvider = ({
) )
} else { } else {
const codeToWrite = newKclFile( const codeToWrite = newKclFile(
input.content ?? '', input.content,
settings.modeling.defaultUnit.current settings.modeling.defaultUnit.current
) )
if (err(codeToWrite)) return Promise.reject(codeToWrite) if (err(codeToWrite)) return Promise.reject(codeToWrite)
@ -284,7 +285,7 @@ export const FileMachineProvider = ({
createdName = name createdName = name
createdPath = path createdPath = path
const codeToWrite = newKclFile( const codeToWrite = newKclFile(
input.content ?? '', input.content,
settings.modeling.defaultUnit.current settings.modeling.defaultUnit.current
) )
if (err(codeToWrite)) return Promise.reject(codeToWrite) if (err(codeToWrite)) return Promise.reject(codeToWrite)
@ -423,7 +424,9 @@ export const FileMachineProvider = ({
authToken: token ?? '', authToken: token ?? '',
projectData, projectData,
settings: { settings: {
defaultUnit: settings.modeling.defaultUnit.current ?? 'mm', defaultUnit:
settings.modeling.defaultUnit.current ??
DEFAULT_DEFAULT_LENGTH_UNIT,
}, },
specialPropsForSampleCommand: { specialPropsForSampleCommand: {
onSubmit: async (data) => { onSubmit: async (data) => {

View File

@ -124,7 +124,7 @@ const ProjectsContextWeb = ({ children }: { children: React.ReactNode }) => {
clearImportSearchParams() clearImportSearchParams()
const codeToWrite = newKclFile( const codeToWrite = newKclFile(
input.code ?? '', input.code,
settings.modeling.defaultUnit.current settings.modeling.defaultUnit.current
) )
if (err(codeToWrite)) return Promise.reject(codeToWrite) if (err(codeToWrite)) return Promise.reject(codeToWrite)
@ -424,7 +424,7 @@ const ProjectsContextDesktop = ({
fileName = name fileName = name
if (!fileLoaded) { if (!fileLoaded) {
const codeToWrite = newKclFile( const codeToWrite = newKclFile(
input.code ?? '', input.code,
settings.modeling.defaultUnit.current settings.modeling.defaultUnit.current
) )
if (err(codeToWrite)) return Promise.reject(codeToWrite) 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. * Create a new KCL file with the given initial content and default length unit.
* @returns KCL string
*/ */
export function newKclFile( export function newKclFile(
initialContent: string, initialContent: string | undefined,
defaultLengthUnit: UnitLength defaultLengthUnit: UnitLength
): string | Error { ): 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, // If the default length unit is the same as the default default length unit,
// there's no need to add the attribute. // there's no need to add the attribute.
if (defaultLengthUnit === DEFAULT_DEFAULT_LENGTH_UNIT) { if (defaultLengthUnit === DEFAULT_DEFAULT_LENGTH_UNIT) {
return initialContent return ''
} }
return changeKclSettings(initialContent, { return changeKclSettings('', {
defaultLengthUnits: unitLengthToUnitLen(defaultLengthUnit), defaultLengthUnits: unitLengthToUnitLen(defaultLengthUnit),
defaultAngleUnits: unitAngleToUnitAng(DEFAULT_DEFAULT_ANGLE_UNIT), defaultAngleUnits: unitAngleToUnitAng(DEFAULT_DEFAULT_ANGLE_UNIT),
stdPath: null, 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 // 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 // present, we're creating a new project, and we want to incorporate the
// user's settings. // user's settings.
const codeToWrite = const codeToWrite = newKclFile(
initialCode ?? initialCode,
newKclFile( configuration?.settings?.modeling?.base_unit ?? DEFAULT_DEFAULT_LENGTH_UNIT
'', )
configuration?.settings?.modeling?.base_unit ??
DEFAULT_DEFAULT_LENGTH_UNIT
)
if (err(codeToWrite)) return Promise.reject(codeToWrite) if (err(codeToWrite)) return Promise.reject(codeToWrite)
await window.electron.writeFile(projectFile, codeToWrite) await window.electron.writeFile(projectFile, codeToWrite)
const metadata = await window.electron.stat(projectFile) 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.', description: 'Imports an example KCL program into the editor.',
needsReview: true, needsReview: true,
icon: 'code', icon: 'code',
reviewMessage: ({ argumentsToSubmit }) => reviewMessage: ({ argumentsToSubmit }) => CommandBarOverwriteWarning({}),
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({}),
groupId: 'code', groupId: 'code',
onSubmit(data) { onSubmit(data) {
if (!data?.sample) { if (!data?.sample) {

View File

@ -126,7 +126,7 @@ export const fileMachine = setup({
makeDir: boolean makeDir: boolean
selectedDirectory: FileEntry selectedDirectory: FileEntry
targetPathToClone?: string targetPathToClone?: string
content: string content?: string
shouldSetToRename: boolean shouldSetToRename: boolean
} }
}) => Promise.resolve({ message: '', path: '' }) }) => Promise.resolve({ message: '', path: '' })
@ -152,7 +152,7 @@ export const fileMachine = setup({
name: string name: string
makeDir: boolean makeDir: boolean
selectedDirectory: FileEntry selectedDirectory: FileEntry
content: string content?: string
} }
}) => Promise.resolve({ path: '' }) }) => Promise.resolve({ path: '' })
), ),
@ -238,7 +238,7 @@ export const fileMachine = setup({
makeDir: event.data.makeDir, makeDir: event.data.makeDir,
selectedDirectory: context.selectedDirectory, selectedDirectory: context.selectedDirectory,
targetPathToClone: event.data.targetPathToClone, targetPathToClone: event.data.targetPathToClone,
content: event.data.content ?? '', content: event.data.content,
shouldSetToRename: event.data.shouldSetToRename ?? false, shouldSetToRename: event.data.shouldSetToRename ?? false,
} }
}, },
@ -417,7 +417,7 @@ export const fileMachine = setup({
name: event.data.name, name: event.data.name,
makeDir: event.data.makeDir, makeDir: event.data.makeDir,
selectedDirectory: context.selectedDirectory, selectedDirectory: context.selectedDirectory,
content: event.data.content ?? '', content: event.data.content,
} }
}, },
onDone: 'Reading files', onDone: 'Reading files',