Skip to content

Commit

Permalink
Correctly add brackets to scrap settings on export
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
daem-on committed Apr 28, 2024
1 parent 37a4182 commit 93485b1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/colors.ts
Original file line number Diff line number Diff line change
@@ -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');
20 changes: 14 additions & 6 deletions src/export/processLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand All @@ -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(" ");
Expand Down
4 changes: 1 addition & 3 deletions src/guides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/layer.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
7 changes: 5 additions & 2 deletions src/objectSettings/model/ScrapSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import type { AssertFunction } from "../../validation/assertTypes.ts";

export default class ScrapSettings {

static readonly stringSettings: ReadonlyArray<string> =
["scale", "projection", "author", "copyright"];
static readonly rawStringeSettings: ReadonlyArray<string> =
["author", "copyright"];
static readonly bracketSettings: ReadonlyArray<string> =
["scale", "projection"];
static readonly stringSettings = [...ScrapSettings.rawStringeSettings, ...ScrapSettings.bracketSettings];

readonly className = "ScrapSettings";
projection: string;
Expand Down
41 changes: 39 additions & 2 deletions tests/vitest/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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: [""],
}]
],
Expand All @@ -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"
]);
});

0 comments on commit 93485b1

Please sign in to comment.