[Feature]: Load external model becomes Add file to project, global application add file to project with home page update. (#6506)
* chore: saving off skeleton * fix: saving skeleton * chore: skeleton for loading projects from project directory path * chore: cleaning up useless state transition to be an on event direct to action state * fix: new structure for web vs desktop vs react machine provider code * chore: saving off skeleton * fix: skeleton logic for react? going to move it from a string to obj.string * fix: trying to prevent error element unmount on global react components. This is bricking JS state * fix: we are so back * chore: implemented navigating to specfic KCL file * chore: implementing renaming project * chore: deleting project * fix: auto fixes * fix: old debug/testing file oops * chore: generic create new file * chore: skeleton for web create file provide * chore: basic machine vitest... need to figure out how to get window.electron implemented in vitest? * chore: save off progress before deleting other project implementation, a few missing features still * chore: trying a different init skeleton? most likely will migrate * chore: first attempt of purging projects context provider * chore: enabling toast for some machine state * chore: enabling more toast success and error * chore: writing read write state to the system io based on the project path * fix: tsc fixes * fix: use file system watcher, navigate to project after creation via the requestProjectName * chore: open project command, hooks vs snapshot context helpers * chore: implemented open and create project for e2e testing. They are hard coded in poor spot for now. * fix: codespell fixes * chore: implementing more project commands * chore: PR improvements for root.tsx * chore: leaving comment about new Router.tsx layout * fix: removing debugging code * fix: rewriting component for readability * fix: improving web initialization * chore: implementing import file from url which is not actually that? * fix: clearing search params on import file from url * fix: fixed two e2e tests, forgot needsReview when making new command * fix: fixing some import from url business logic to pass e2e tests * chore: script for diffing circular deps +/- * fix: formatting * fix: massive fix for circular depsga! * fix: trying to fix some errors and auto fmt * fix: updating deps * fix: removing debugging code * fix: big clean up * fix: more deletion * fix: tsc cleanup * fix: TSC TSC TSC TSC! * fix: typo fix * fix: clear query params on web only, desktop not required * fix: removing unused code * fmt * Bring back `trap` removed in merge * Use explicit types instead of `any`s on arg configs * Add project commands directly to command palette * fix: deleting debugging code, from PR review * fix: this got added back(?) * fix: using referred type * fix: more PR clean up * fix: big block comment for xstate architecture decision * fix: more pr comment fixes * fix: saving off logic, need a big cleanup because I hacked it together to get a POC * fix: extra business? * fix: merge conflict just added them back why dude * fix: more PR comments * fix: big ciruclar deps fix, commandBarActor in appActor * chore: writing e2e test, still need to fix 3 bugs * chore: adding more scenarios * fix: formatting * fix: fixing tsc errors * chore: deleting the old text to cad and using the new application level one, almost there * fix: prompt to edit works * fix: large push to get 1 text to cad command... the usage is a little buggy with delete and navigate within /file * fix: settings for highlight edges now works * chore: adding another e2e test * fix: cleaning up e2e tests and writing more of them * fix: tsc type * chore: more e2e improvements, unique project name in text to cad * chore: e2e tests should be good to go * fix: gotcha comment * fix: enabled web t2c, codespell fixes * fix: fixing merge conflcits?? * feat: implemented load external for kcl samples * feat: load external model from disk * fix: trying to delete old stuff * fix: all command trigger locations now have defaults for current project * fix: gotcha comment for the future * chore: hiding import file from url command, two separate commands for 3d and kcl file adding * chore: commands are now add file to project, 3rd iteration * fix: t2c in file menu fixed * chore: updating file menu for new global actions * fix: auto fixes * fix: the command bar arg flow for web add kcl file seems backwards? * chore: updated home layout, added create from kcl sample button * chore: remapping some menu actions * fix: fixing open dialog copy * fix: an e2e test * fix: fixed e2e tests * fix: fixed e2e tests * fix: auto fixes * fix: pr clean up * fix: removing console log * fix: PR updates * fix: the reviewed stage boolean required the expected state to change. Also I progressed the command bar too soon * fix: no idea how this passed locally yesterday? I removed the {dir} unused but I need the function's logic but not the return value... * fix: should be good to go? --------- Co-authored-by: Frank Noirot <frankjohnson1993@gmail.com>
This commit is contained in:
@ -3,6 +3,12 @@ import type { ActorRefFrom } from 'xstate'
|
||||
import type { Command, CommandArgumentOption } from '@src/lib/commandTypes'
|
||||
import { SystemIOMachineEvents } from '@src/machines/systemIO/utils'
|
||||
import { isDesktop } from '@src/lib/isDesktop'
|
||||
import { kclSamplesManifestWithNoMultipleFiles } from '@src/lib/kclSamples'
|
||||
import { getUniqueProjectName } from '@src/lib/desktopFS'
|
||||
import { FILE_EXT } from '@src/lib/constants'
|
||||
import toast from 'react-hot-toast'
|
||||
import { reportRejection } from '@src/lib/trap'
|
||||
import { relevantFileExtensions } from '@src/lang/wasmUtils'
|
||||
|
||||
export function createApplicationCommands({
|
||||
systemIOActor,
|
||||
@ -79,5 +85,196 @@ export function createApplicationCommands({
|
||||
},
|
||||
}
|
||||
|
||||
return isDesktop() ? [textToCADCommand] : [textToCADCommand]
|
||||
const addKCLFileToProject: Command = {
|
||||
name: 'add-kcl-file-to-project',
|
||||
displayName: 'Add file to project',
|
||||
description:
|
||||
'Add KCL file, Zoo sample, or 3D model to new or existing project.',
|
||||
needsReview: false,
|
||||
icon: 'importFile',
|
||||
groupId: 'application',
|
||||
onSubmit(data) {
|
||||
if (data) {
|
||||
/** TODO: Make a new machine for models. This is only a temporary location
|
||||
* to move it to the global application level. To reduce its footprint
|
||||
* and complexity the implementation lives here with systemIOMachine. Not
|
||||
* inside the systemIOMachine. We can have a fancy model machine that loads
|
||||
* KCL samples
|
||||
*/
|
||||
const folders = systemIOActor.getSnapshot().context.folders
|
||||
const isProjectNew = !!data.newProjectName
|
||||
const requestedProjectName = data.newProjectName || data.projectName
|
||||
const uniqueNameIfNeeded = isProjectNew
|
||||
? getUniqueProjectName(requestedProjectName, folders)
|
||||
: requestedProjectName
|
||||
|
||||
if (data.source === 'kcl-samples' && data.sample) {
|
||||
const pathParts = data.sample.split('/')
|
||||
const projectPathPart = pathParts[0]
|
||||
const primaryKclFile = pathParts[1]
|
||||
const folderNameBecomesKCLFileName = projectPathPart + FILE_EXT
|
||||
|
||||
const sampleCodeUrl =
|
||||
(isDesktop() ? '.' : '') +
|
||||
`/kcl-samples/${encodeURIComponent(
|
||||
projectPathPart
|
||||
)}/${encodeURIComponent(primaryKclFile)}`
|
||||
|
||||
fetch(sampleCodeUrl)
|
||||
.then(async (codeResponse) => {
|
||||
if (!codeResponse.ok) {
|
||||
console.error(
|
||||
'Failed to fetch sample code:',
|
||||
codeResponse.statusText
|
||||
)
|
||||
return Promise.reject(new Error('Failed to fetch sample code'))
|
||||
}
|
||||
const code = await codeResponse.text()
|
||||
systemIOActor.send({
|
||||
type: SystemIOMachineEvents.importFileFromURL,
|
||||
data: {
|
||||
requestedProjectName: uniqueNameIfNeeded,
|
||||
requestedFileName: folderNameBecomesKCLFileName,
|
||||
requestedCode: code,
|
||||
},
|
||||
})
|
||||
})
|
||||
.catch(reportError)
|
||||
} else if (data.source === 'local' && data.path) {
|
||||
const clonePath = data.path
|
||||
const fileWithExtension = clonePath.split('/').pop()
|
||||
const readFileContentsAndCreateNewFile = async () => {
|
||||
const text = await window.electron.readFile(clonePath, 'utf8')
|
||||
systemIOActor.send({
|
||||
type: SystemIOMachineEvents.importFileFromURL,
|
||||
data: {
|
||||
requestedProjectName: uniqueNameIfNeeded,
|
||||
requestedFileName: fileWithExtension,
|
||||
requestedCode: text,
|
||||
},
|
||||
})
|
||||
}
|
||||
readFileContentsAndCreateNewFile().catch(reportRejection)
|
||||
} else {
|
||||
toast.error("The command couldn't be submitted, check the arguments.")
|
||||
}
|
||||
}
|
||||
},
|
||||
args: {
|
||||
source: {
|
||||
inputType: 'options',
|
||||
required: true,
|
||||
skip: false,
|
||||
defaultValue: isDesktop() ? 'local' : 'kcl-samples',
|
||||
options() {
|
||||
return [
|
||||
{
|
||||
value: 'kcl-samples',
|
||||
name: 'KCL Samples',
|
||||
isCurrent: true,
|
||||
},
|
||||
...(isDesktop()
|
||||
? [
|
||||
{
|
||||
value: 'local',
|
||||
name: 'Local Drive',
|
||||
isCurrent: false,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
]
|
||||
},
|
||||
},
|
||||
method: {
|
||||
inputType: 'options',
|
||||
required: true,
|
||||
skip: true,
|
||||
options: isDesktop()
|
||||
? [
|
||||
{ name: 'New project', value: 'newProject', isCurrent: true },
|
||||
{ name: 'Existing project', value: 'existingProject' },
|
||||
]
|
||||
: [{ name: 'Overwrite', value: 'existingProject' }],
|
||||
valueSummary(value) {
|
||||
return isDesktop()
|
||||
? value === 'newProject'
|
||||
? 'New project'
|
||||
: 'Existing project'
|
||||
: 'Overwrite'
|
||||
},
|
||||
},
|
||||
projectName: {
|
||||
inputType: 'options',
|
||||
required: (commandsContext) =>
|
||||
isDesktop() &&
|
||||
commandsContext.argumentsToSubmit.method === 'existingProject',
|
||||
skip: true,
|
||||
options: (_, context) => {
|
||||
const { folders } = systemIOActor.getSnapshot().context
|
||||
const options: CommandArgumentOption<string>[] = []
|
||||
folders.forEach((folder) => {
|
||||
options.push({
|
||||
name: folder.name,
|
||||
value: folder.name,
|
||||
isCurrent: false,
|
||||
})
|
||||
})
|
||||
return options
|
||||
},
|
||||
},
|
||||
newProjectName: {
|
||||
inputType: 'text',
|
||||
required: (commandsContext) =>
|
||||
isDesktop() &&
|
||||
commandsContext.argumentsToSubmit.method === 'newProject',
|
||||
skip: true,
|
||||
},
|
||||
sample: {
|
||||
inputType: 'options',
|
||||
required: (commandContext) =>
|
||||
!['local'].includes(
|
||||
commandContext.argumentsToSubmit.source as string
|
||||
),
|
||||
hidden: (commandContext) =>
|
||||
['local'].includes(commandContext.argumentsToSubmit.source as string),
|
||||
valueSummary(value) {
|
||||
const MAX_LENGTH = 12
|
||||
if (typeof value === 'string') {
|
||||
return value.length > MAX_LENGTH
|
||||
? value.substring(0, MAX_LENGTH) + '...'
|
||||
: value
|
||||
}
|
||||
return value
|
||||
},
|
||||
options: kclSamplesManifestWithNoMultipleFiles.map((sample) => {
|
||||
return {
|
||||
value: sample.pathFromProjectDirectoryToFirstFile,
|
||||
name: sample.title,
|
||||
}
|
||||
}),
|
||||
},
|
||||
path: {
|
||||
inputType: 'path',
|
||||
skip: true,
|
||||
hidden: !isDesktop(),
|
||||
defaultValue: '',
|
||||
valueSummary: (value) => {
|
||||
return isDesktop() ? window.electron.path.basename(value) : ''
|
||||
},
|
||||
required: (commandContext) =>
|
||||
isDesktop() &&
|
||||
['local'].includes(commandContext.argumentsToSubmit.source as string),
|
||||
filters: [
|
||||
{
|
||||
name: `Import ${relevantFileExtensions().map((f) => ` .${f}`)}`,
|
||||
extensions: relevantFileExtensions(),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return isDesktop()
|
||||
? [textToCADCommand, addKCLFileToProject]
|
||||
: [textToCADCommand, addKCLFileToProject]
|
||||
}
|
||||
|
Reference in New Issue
Block a user