Simplify file format read comparison

This commit is contained in:
Leonardo Murça 2023-01-01 13:14:10 -03:00
parent 8ae9a8c241
commit 1962f9faad
3 changed files with 21 additions and 22 deletions

View file

@ -1,31 +1,14 @@
import { jDataView } from "./jdataview"; import { jDataView } from "./jdataview";
import { pesRead } from "../format-readers/pes"; import { supportedFormats } from "../format-readers";
import { dstRead } from "../format-readers/dst";
import { Pattern } from "./pattern"; import { Pattern } from "./pattern";
import { pecRead } from "../format-readers/pec";
import { jefRead } from "../format-readers/jef";
import { expRead } from "../format-readers/exp";
String.prototype.endsWith = function (suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
function renderToCanvas(filename, evt, canvas) { function renderToCanvas(filename, evt, canvas) {
const fileExtension = filename.toLowerCase().split(".").pop();
const view = jDataView(evt.target.result, 0, evt.size); const view = jDataView(evt.target.result, 0, evt.size);
const pattern = new Pattern(); const pattern = new Pattern();
filename = filename.toLowerCase(); supportedFormats[fileExtension].read(view, pattern);
if (filename.endsWith("pes")) {
pesRead(view, pattern);
} else if (filename.endsWith("dst")) {
dstRead(view, pattern);
} else if (filename.endsWith("pec")) {
pecRead(view, pattern);
} else if (filename.endsWith("jef")) {
jefRead(view, pattern);
} else if (filename.endsWith("exp")) {
expRead(view, pattern);
}
pattern.moveToPositive(); pattern.moveToPositive();
pattern.drawShape(canvas); pattern.drawShape(canvas);
} }

View file

@ -0,0 +1,15 @@
import { dstRead } from "./dst";
import { expRead } from "./exp";
import { jefRead } from "./jef";
import { pecRead } from "./pec";
import { pesRead } from "./pes";
const supportedFormats = {
pes: { ext: ".pes", read: pesRead },
dst: { ext: ".dst", read: dstRead },
pec: { ext: ".pec", read: pecRead },
jef: { ext: ".jef", read: jefRead },
exp: { ext: ".exp", read: expRead },
};
export { supportedFormats };

View file

@ -4,12 +4,13 @@
import FileList from "./FileList.svelte"; import FileList from "./FileList.svelte";
import { filterFiles } from "../utils/filterFiles"; import { filterFiles } from "../utils/filterFiles";
import { supportedFormats } from "../format-readers";
let acceptedFiles; let acceptedFiles;
let rejectedFiles; let rejectedFiles;
let areAcceptedFilesRendered = false; let areAcceptedFilesRendered = false;
const fileRequirements = { const fileRequirements = {
supportedFormats: [".pes", ".dst", ".pec", ".jef", ".exp"], supportedFormats: Object.values(supportedFormats).map((f) => f.ext),
maxSize: 700000, maxSize: 700000,
}; };