推进 OPFS G-code 程序路径 helper

This commit is contained in:
2026-06-14 15:57:35 +08:00
parent a158f66071
commit d40a6422e8
5 changed files with 115 additions and 13 deletions

View File

@@ -3,9 +3,12 @@ import {
gcodeProgramPath,
machineIniPath,
parameterFilePath,
splitOpfsPath,
toolTablePath,
} from "./path-model.js";
const GCODE_PROGRAM_ROOT = ["linuxcnc", "gcode"];
const MACHINE_FILE_PATHS = {
ini: machineIniPath,
toolTable: toolTablePath,
@@ -43,6 +46,18 @@ export async function loadMachineTextFiles(machineId, options = {}) {
};
}
export function gcodeFilenameFromProgramPath(path) {
const parts = splitOpfsPath(path);
if (
parts.length !== 3 ||
parts[0] !== GCODE_PROGRAM_ROOT[0] ||
parts[1] !== GCODE_PROGRAM_ROOT[1]
) {
throw new Error(`Invalid G-code program OPFS path: ${path}`);
}
return parts[2];
}
export async function saveGcodeProgram(filename, text, options = {}) {
const path = gcodeProgramPath(filename);
await saveTextFile(path, text, options.storage);

View File

@@ -8,6 +8,7 @@ import {
saveTextFile,
} from "../../opfs/file-service.js";
import {
gcodeFilenameFromProgramPath,
loadGcodeProgram,
loadMachineTextFiles,
machineFilePaths,
@@ -311,23 +312,24 @@ function syncEditorToWasmFs() {
requireIniSdk().writeTextFile(WASM_FILE, editor.value);
}
function gcodeFilenameFromSnapshotPath(path) {
const prefix = "linuxcnc/gcode/";
if (typeof path !== "string" || !path.startsWith(prefix)) {
return GCODE_FILENAME;
}
const filename = path.slice(prefix.length);
return filename || GCODE_FILENAME;
}
async function loadSelectedGcodeProgram() {
const opfsPath = restoredSessionSnapshot?.payload?.files?.gcode;
const filename = gcodeFilenameFromSnapshotPath(opfsPath);
const hasSnapshotGcode = typeof opfsPath === "string" && opfsPath.startsWith("linuxcnc/gcode/");
let filename = GCODE_FILENAME;
let selectedOpfsPath = `linuxcnc/gcode/${GCODE_FILENAME}`;
let source = "default";
if (typeof opfsPath === "string") {
try {
filename = gcodeFilenameFromProgramPath(opfsPath);
selectedOpfsPath = opfsPath;
source = "snapshot";
} catch {
filename = GCODE_FILENAME;
}
}
return {
filename,
opfsPath: opfsPath ?? `linuxcnc/gcode/${filename}`,
source: hasSnapshotGcode ? "snapshot" : "default",
opfsPath: selectedOpfsPath,
source,
wasmPath: GCODE_WASM_FILE,
text: await loadGcodeProgram(filename),
};