Skip to content

Commit

Permalink
Add defaultName for saving .js or .zip
Browse files Browse the repository at this point in the history
  • Loading branch information
North101 committed Jan 11, 2025
1 parent a768da9 commit 221d066
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
31 changes: 27 additions & 4 deletions packages/studio/src/builder.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,33 @@ const extractDefaultParamsFromCode = async (code) => {

const editedText = `
${code}
let dp = null
try {
dp = defaultParams;
} catch (e) { }
return dp
return defaultParams;
} catch (e) {
return null;
}
`;

try {
return runInContext(editedText, {});
} catch (e) {
return {};
}
};

const extractDefaultNameFromCode = async (code) => {
if (code.match(/^\s*export\s+/m)) {
const module = await buildModuleEvaluator(code);
return module.defaultName || null;
}

const editedText = `
${code}
try {
return defaultName;
} catch (e) {
return null;
}
`;

try {
Expand Down Expand Up @@ -216,6 +238,7 @@ const service = {
ready: () => OC.then(() => true),
buildShapesFromCode,
extractDefaultParamsFromCode,
extractDefaultNameFromCode,
exportShape,
edgeInfo,
faceInfo,
Expand Down
9 changes: 6 additions & 3 deletions packages/studio/src/utils/downloadCode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { fileSave } from 'browser-fs-access'
import { fileSave } from 'browser-fs-access';
import builderAPI from './builderAPI';

export default (code, fileName) => {
fileName = fileName ?? 'replicad-script'
export default async (code, fileName) => {
fileName = fileName
?? await builderAPI.extractDefaultNameFromCode(code)
?? 'replicad-script'
return fileSave(
new Blob([code], {
type: "application/javascript",
Expand Down
5 changes: 3 additions & 2 deletions packages/studio/src/utils/saveShape.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const mapExt = (ext) => {
return ext;
};

export default async function saveShapes(shapeId, fileType = "stl") {
export default async function saveShapes(shapeId, fileType = "stl", code) {
const shapes = await builderAPI.exportShape(fileType, shapeId);
if (shapes.length === 1) {
const { blob, name } = shapes[0];
Expand All @@ -25,6 +25,7 @@ export default async function saveShapes(shapeId, fileType = "stl") {
return;
}

const defaultName = await builderAPI.extractDefaultNameFromCode(code);
const zip = new JSZip();
shapes.forEach((shape, i) => {
zip.file(`${shape.name || `shape-${i}`}.${mapExt(fileType)}`, shape.blob);
Expand All @@ -33,7 +34,7 @@ export default async function saveShapes(shapeId, fileType = "stl") {
await fileSave(zipBlob, {
id: "exports",
description: "Save zip",
fileName: `${shapeId}.zip`,
fileName: `${defaultName ?? shapeId}.zip`,
extensions: [".zip"],
});
}
2 changes: 1 addition & 1 deletion packages/studio/src/visualiser/editor/DownloadDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default function DownloadDialog({ onClose }) {
);
} else {
try {
await saveShape("defaultShape", saveMode);
await saveShape("defaultShape", saveMode, store.code.current);
} catch (e) {
console.error(e);
} finally {
Expand Down

0 comments on commit 221d066

Please sign in to comment.