diff --git a/src/file-renderer/index.js b/src/file-renderer/index.js index 5ae2d6c..594a918 100644 --- a/src/file-renderer/index.js +++ b/src/file-renderer/index.js @@ -1,31 +1,14 @@ import { jDataView } from "./jdataview"; -import { pesRead } from "../format-readers/pes"; -import { dstRead } from "../format-readers/dst"; +import { supportedFormats } from "../format-readers"; 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) { + const fileExtension = filename.toLowerCase().split(".").pop(); const view = jDataView(evt.target.result, 0, evt.size); const pattern = new Pattern(); - filename = filename.toLowerCase(); - 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); - } + supportedFormats[fileExtension].read(view, pattern); + pattern.moveToPositive(); pattern.drawShape(canvas); } diff --git a/src/format-readers/index.js b/src/format-readers/index.js new file mode 100644 index 0000000..eeb157b --- /dev/null +++ b/src/format-readers/index.js @@ -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 }; diff --git a/src/lib/FileViewer.svelte b/src/lib/FileViewer.svelte index 514606b..41e7cb0 100644 --- a/src/lib/FileViewer.svelte +++ b/src/lib/FileViewer.svelte @@ -4,12 +4,13 @@ import FileList from "./FileList.svelte"; import { filterFiles } from "../utils/filterFiles"; + import { supportedFormats } from "../format-readers"; let acceptedFiles; let rejectedFiles; let areAcceptedFilesRendered = false; const fileRequirements = { - supportedFormats: [".pes", ".dst", ".pec", ".jef", ".exp"], + supportedFormats: Object.values(supportedFormats).map((f) => f.ext), maxSize: 700000, };