From 93485b1debc4b8bf5197a1742615dcdaab887787 Mon Sep 17 00:00:00 2001 From: Csongor Zih Date: Sun, 28 Apr 2024 19:50:40 +0200 Subject: [PATCH] Correctly add brackets to scrap settings on export Fixes #24 --- src/colors.ts | 4 +++ src/export/processLayer.ts | 20 +++++++---- src/guides.ts | 4 +-- src/layer.ts | 2 +- src/objectSettings/model/ScrapSettings.ts | 7 ++-- tests/vitest/export.test.ts | 41 +++++++++++++++++++++-- 6 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 src/colors.ts diff --git a/src/colors.ts b/src/colors.ts new file mode 100644 index 0000000..02a6db7 --- /dev/null +++ b/src/colors.ts @@ -0,0 +1,4 @@ +import paper from "paper"; + +export const COLOR_GUIDE_PRIMARY = new paper.Color('#59c99c'); +export const COLOR_GUIDE_SECONDARY = new paper.Color('#aaaaaa'); \ No newline at end of file diff --git a/src/export/processLayer.ts b/src/export/processLayer.ts index a3ec3b1..b7a9cf6 100644 --- a/src/export/processLayer.ts +++ b/src/export/processLayer.ts @@ -5,6 +5,12 @@ import { processLine, processCompoundPath } from "./processLine"; import { LayerExportData } from "./models"; import { getSettingsInExport, ExportFormatter } from "./util"; +function wrapIfNeeded(value: string, brackets: boolean): string { + if (!value.includes(" ")) return value; + if (brackets) return `[${value}]`; + return `"${value}"`; +} + export function processLayer(layer: LayerExportData, format: ExportFormatter): string[] { if (!layer.children || layer.children.length === 0) return []; @@ -18,16 +24,18 @@ export function processLayer(layer: LayerExportData, format: ExportFormatter): s const s = settings; const o = []; - for (const setting of ScrapSettings.stringSettings.slice(1)) { + for (const setting of ScrapSettings.bracketSettings) { if (s[setting]) - o.push(`-${setting} ${s[setting]}`); + o.push(`-${setting} ${wrapIfNeeded(s[setting], true)}`); } - - if (s.scale !== "") { - const val = s.scale.includes(" ") ? `[${s.scale}]` : s.scale; - o.push(`-scale ${val}`); + for (const setting of ScrapSettings.rawStringeSettings) { + if (s[setting]) + o.push(`-${setting} ${s[setting]}`); } + if (s.stationNames !== "") + o.push(`-station-names ${s.stationNames}`); + if (s.otherSettings !== "") o.push(s.otherSettings.replace(/\n/g, " ")); optionsString = o.join(" "); diff --git a/src/guides.ts b/src/guides.ts index fba79a3..1151fbe 100644 --- a/src/guides.ts +++ b/src/guides.ts @@ -3,9 +3,7 @@ import { removePaperItemsByDataTags, removePaperItemsByTags } from "./helper"; import paper from "paper"; import { getGuideLayer } from "./layer"; - -export const COLOR_GUIDE_PRIMARY = new paper.Color('#59c99c'); -export const COLOR_GUIDE_SECONDARY = new paper.Color('#aaaaaa'); +import { COLOR_GUIDE_PRIMARY, COLOR_GUIDE_SECONDARY } from "./colors"; export function hoverItem(hitResult) { const segments = hitResult.item.segments; diff --git a/src/layer.ts b/src/layer.ts index 4fbefb6..d58f52e 100644 --- a/src/layer.ts +++ b/src/layer.ts @@ -1,7 +1,7 @@ import paper from "paper"; import ScrapSettings from "./objectSettings/model/ScrapSettings"; import { triggers } from "./triggers"; -import { COLOR_GUIDE_PRIMARY } from "./guides"; +import { COLOR_GUIDE_PRIMARY } from "./colors"; export function setup() { const defaultLayer = addNewLayer('Scrap 1'); diff --git a/src/objectSettings/model/ScrapSettings.ts b/src/objectSettings/model/ScrapSettings.ts index e14b18c..5a5ce35 100644 --- a/src/objectSettings/model/ScrapSettings.ts +++ b/src/objectSettings/model/ScrapSettings.ts @@ -2,8 +2,11 @@ import type { AssertFunction } from "../../validation/assertTypes.ts"; export default class ScrapSettings { - static readonly stringSettings: ReadonlyArray = - ["scale", "projection", "author", "copyright"]; + static readonly rawStringeSettings: ReadonlyArray = + ["author", "copyright"]; + static readonly bracketSettings: ReadonlyArray = + ["scale", "projection"]; + static readonly stringSettings = [...ScrapSettings.rawStringeSettings, ...ScrapSettings.bracketSettings]; readonly className = "ScrapSettings"; projection: string; diff --git a/tests/vitest/export.test.ts b/tests/vitest/export.test.ts index 5df6154..19975c1 100644 --- a/tests/vitest/export.test.ts +++ b/tests/vitest/export.test.ts @@ -3,11 +3,14 @@ import { processProject } from "../../src/export/processProject"; import PointSettings from "../../src/objectSettings/model/PointSettings"; import ScrapSettings from "../../src/objectSettings/model/ScrapSettings"; -test("export scrap with 1 station", () => { +function createStationSettings(): PointSettings { const pointSettings = PointSettings.defaultSettings(); pointSettings.type = "station"; pointSettings.name = "0"; + return pointSettings; +} +test("export scrap with 1 station", () => { const scrapSettings = ScrapSettings.defaultSettings(); const result = processProject([ @@ -17,7 +20,7 @@ test("export scrap with 1 station", () => { children: [ ["SymbolItem", { matrix: [1, 0, 0, 1, 10, -20], - data: { therionData: pointSettings }, + data: { therionData: createStationSettings() }, symbol: [""], }] ], @@ -34,3 +37,37 @@ test("export scrap with 1 station", () => { "endscrap" ]); }); + +test("export with all scrap settings", () => { + const scrapSettings = ScrapSettings.defaultSettings(); + scrapSettings.projection = "elevation 100"; + scrapSettings.scale = "0 0 39.3701 0 0 0 1 0 m"; + scrapSettings.author = `2021.08.01 "Author Name"`; + scrapSettings.copyright = `2021.08.01 "Author Name"`; + scrapSettings.stationNames = "prefix1 suffix1"; + scrapSettings.otherSettings = `-walls on`; + + const result = processProject([ + ["dictionary", []], + [ + ["Layer", { + children: [ + ["SymbolItem", { + matrix: [1, 0, 0, 1, 10, -20], + data: { therionData: createStationSettings() }, + symbol: [""], + }] + ], + data: { therionData: scrapSettings }, + name: "scrap1" + }] + ] + ]); + + expect(result).toEqual([ + "encoding utf-8", + `scrap scrap1 -scale [0 0 39.3701 0 0 0 1 0 m] -projection [elevation 100] -author 2021.08.01 "Author Name" -copyright 2021.08.01 "Author Name" -station-names prefix1 suffix1 -walls on`, + " point 10 20 station -name 0", + "endscrap" + ]); +});