mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-10-19 09:22:12 +00:00
refactoring
This commit is contained in:
1 parent
4795513344
commit
890bcd765a
169 files changed
+263
-256
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import { readable } from 'svelte/store';
|
||||
import { engine, FileStateCollectionObserver, type FileState, type Selection } from '$lib/engine';
|
||||
import { isSelectionHidden } from '$lib/engine/selection-helpers';
|
||||
|
||||
/**
|
||||
* Whether all the selected elements are hidden. It follows the selection and, through the
|
||||
* observer of the files, the state of every file (hiding, editing).
|
||||
*/
|
||||
export const allHidden = readable(false, (set) => {
|
||||
let selection: Selection = { type: 'empty' };
|
||||
const states = new Map<string, FileState>();
|
||||
const unsubscribes = new Map<string, () => void>();
|
||||
|
||||
const update = () => set(isSelectionHidden(selection, states));
|
||||
|
||||
const unsubscribeSelection = engine.selection.subscribe(($selection) => {
|
||||
selection = $selection;
|
||||
update();
|
||||
});
|
||||
const observer = new FileStateCollectionObserver(
|
||||
(files) => {
|
||||
files.forEach((store, fileId) => {
|
||||
unsubscribes.set(
|
||||
fileId,
|
||||
store.subscribe(($file) => {
|
||||
states.set(fileId, $file);
|
||||
update();
|
||||
})
|
||||
);
|
||||
});
|
||||
},
|
||||
(fileId) => {
|
||||
unsubscribes.get(fileId)?.();
|
||||
unsubscribes.delete(fileId);
|
||||
states.delete(fileId);
|
||||
update();
|
||||
},
|
||||
() => {
|
||||
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
||||
unsubscribes.clear();
|
||||
states.clear();
|
||||
}
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubscribeSelection();
|
||||
observer.destroy();
|
||||
};
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { beforeEach, describe, it } from 'node:test';
|
||||
import { get } from 'svelte/store';
|
||||
import { isHidden } from '$lib/file-visibility';
|
||||
import { isHidden } from '$lib/engine/file-visibility';
|
||||
import {
|
||||
ALL_EXPORT_OPTIONS,
|
||||
engine,
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { browser } from '$app/environment';
|
||||
import { get, writable, type Readable, type Writable } from 'svelte/store';
|
||||
import { FileColorAllocator } from '$lib/file-colors';
|
||||
import { FileColorAllocator } from '$lib/engine/file-colors';
|
||||
import {
|
||||
setHidden,
|
||||
stringifyVisibility,
|
||||
VISIBILITY_KEY,
|
||||
type Visibility,
|
||||
} from '$lib/file-visibility';
|
||||
import { categoryIntervals } from '$lib/trackpoint-categories';
|
||||
import { selectedElementIds, type FileTreeNode } from '$lib/selection-helpers';
|
||||
} from '$lib/engine/file-visibility';
|
||||
import { categoryIntervals } from '$lib/engine/trackpoint-categories';
|
||||
import { selectedElementIds, type FileTreeNode } from '$lib/engine/selection-helpers';
|
||||
import type { RouteAttributes } from 'gpx-rs';
|
||||
import { idsToBytes, selectMode, type Wasm } from './convert';
|
||||
import { readFileState } from './file-state';
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { describe, it } from 'node:test';
|
||||
import { FileColorAllocator, normalizeColor } from '$lib/engine/file-colors';
|
||||
|
||||
describe('normalizeColor', () => {
|
||||
it('adds the # of hex colors that have none', () => {
|
||||
assert.equal(normalizeColor('ff0000'), '#ff0000');
|
||||
assert.equal(normalizeColor('FF00aa'), '#FF00aa');
|
||||
assert.equal(normalizeColor('#ff0000'), '#ff0000');
|
||||
assert.equal(normalizeColor('red'), 'red');
|
||||
assert.equal(normalizeColor('fff'), 'fff');
|
||||
assert.equal(normalizeColor('hsl(0, 70%, 50%)'), 'hsl(0, 70%, 50%)');
|
||||
});
|
||||
});
|
||||
|
||||
describe('FileColorAllocator', () => {
|
||||
it('uses the color the tracks define', () => {
|
||||
const colors = new FileColorAllocator();
|
||||
assert.equal(colors.resolve('a', [undefined, 'ff9900', '0000ff']), '#ff9900');
|
||||
assert.equal(colors.resolve('b', ['#123456']), '#123456');
|
||||
});
|
||||
|
||||
it('gives the files different palette colors while some are unused', () => {
|
||||
const colors = new FileColorAllocator();
|
||||
const given = Array.from({ length: 11 }, (_, i) => colors.resolve(`file-${i}`, []));
|
||||
assert.equal(new Set(given).size, 11);
|
||||
// then the least used ones again
|
||||
const twelfth = colors.resolve('file-11', []);
|
||||
assert.equal(twelfth, given[0]);
|
||||
});
|
||||
|
||||
it('keeps the color of a file when its style comes and goes', () => {
|
||||
const colors = new FileColorAllocator();
|
||||
const palette = colors.resolve('a', []);
|
||||
colors.resolve('b', []);
|
||||
// a style is defined, then removed (edit, undo): the file gets its color back
|
||||
assert.equal(colors.resolve('a', ['ff0000']), '#ff0000');
|
||||
assert.equal(colors.resolve('a', []), palette);
|
||||
assert.equal(colors.resolve('a', [undefined]), palette);
|
||||
});
|
||||
|
||||
it('does not count the colors files define as used palette colors twice', () => {
|
||||
const colors = new FileColorAllocator();
|
||||
const first = colors.resolve('a', []);
|
||||
// a file with the color of the palette uses it
|
||||
colors.resolve('b', [first]);
|
||||
const next = colors.resolve('c', []);
|
||||
assert.notEqual(next, first);
|
||||
});
|
||||
|
||||
it('frees the colors of the files that go away', () => {
|
||||
const colors = new FileColorAllocator();
|
||||
const a = colors.resolve('a', []);
|
||||
const b = colors.resolve('b', []);
|
||||
assert.notEqual(a, b);
|
||||
colors.release('a');
|
||||
// a color that nobody uses is picked first again
|
||||
assert.equal(colors.resolve('c', []), a);
|
||||
// and a file that comes back gets a color again
|
||||
colors.release('b');
|
||||
colors.resolve('b', []);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
const PALETTE = [
|
||||
'#ff0000',
|
||||
'#0000ff',
|
||||
'#46e646',
|
||||
'#00ccff',
|
||||
'#ff9900',
|
||||
'#ff00ff',
|
||||
'#ffff32',
|
||||
'#288228',
|
||||
'#9933ff',
|
||||
'#50f0be',
|
||||
'#8c645a',
|
||||
];
|
||||
|
||||
/** GPX style colors are hex digits, with or without the leading `#`. */
|
||||
export function normalizeColor(color: string): string {
|
||||
return /^[0-9a-f]{6}$/i.test(color) ? `#${color}` : color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks the base color of each file: the color defined by the file itself if any, otherwise a
|
||||
* palette color, the least used one when the file first shows up. A file keeps its palette color
|
||||
* for its whole lifetime, so that it does not change when style information comes and goes
|
||||
* (edits, undo, redo).
|
||||
*/
|
||||
export class FileColorAllocator {
|
||||
/** Palette color reserved for each file. */
|
||||
private _reserved = new Map<string, string>();
|
||||
/** Color currently displayed for each file. */
|
||||
private _base = new Map<string, string>();
|
||||
|
||||
/** Updates (and returns) the base color of a file, given the colors defined by its tracks. */
|
||||
resolve(fileId: string, trackColors: (string | undefined)[]): string {
|
||||
const explicit = trackColors.find((color) => color !== undefined);
|
||||
if (explicit === undefined && !this._reserved.has(fileId)) {
|
||||
this._reserved.set(fileId, this.leastUsed());
|
||||
}
|
||||
const color =
|
||||
explicit !== undefined ? normalizeColor(explicit) : this._reserved.get(fileId)!;
|
||||
this._base.set(fileId, color);
|
||||
return color;
|
||||
}
|
||||
|
||||
release(fileId: string) {
|
||||
this._reserved.delete(fileId);
|
||||
this._base.delete(fileId);
|
||||
}
|
||||
|
||||
private leastUsed(): string {
|
||||
const usage = new Map(PALETTE.map((color) => [color, 0]));
|
||||
this._base.forEach((color) => {
|
||||
const count = usage.get(color);
|
||||
if (count !== undefined) {
|
||||
usage.set(color, count + 1);
|
||||
}
|
||||
});
|
||||
return PALETTE.reduce((a, b) => (usage.get(a)! <= usage.get(b)! ? a : b));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Feature, LineString } from 'geojson';
|
||||
import { normalizeColor, type FileColorAllocator } from '$lib/file-colors';
|
||||
import type { Visibility } from '$lib/file-visibility';
|
||||
import { normalizeColor, type FileColorAllocator } from '$lib/engine/file-colors';
|
||||
import type { Visibility } from '$lib/engine/file-visibility';
|
||||
import { toPositions, type Wasm } from './convert';
|
||||
import { EMPTY_STATISTICS, type FileState, type SegmentProperties } from './types';
|
||||
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
import { describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import type { FileStructure } from 'gpx-rs';
|
||||
import {
|
||||
isHidden,
|
||||
isSegmentHidden,
|
||||
isWaypointHidden,
|
||||
parseVisibility,
|
||||
setHidden,
|
||||
stringifyVisibility,
|
||||
waypointsKey,
|
||||
type Visibility,
|
||||
type VisibilityState,
|
||||
} from '$lib/engine/file-visibility';
|
||||
|
||||
const segment = (id: string) => ({ id, rev: `rev-${id}`, length: 2 });
|
||||
|
||||
// F
|
||||
// ├── waypoints: W1, W2
|
||||
// ├── T1: S1, S2
|
||||
// └── T2: S3
|
||||
const structure: FileStructure = {
|
||||
id: 'F',
|
||||
name: 'file',
|
||||
waypointsRev: 'wpt-rev',
|
||||
waypoints: [{ id: 'W1' }, { id: 'W2' }],
|
||||
tracks: [
|
||||
{ id: 'T1', segments: [segment('S1'), segment('S2')] },
|
||||
{ id: 'T2', segments: [segment('S3')] },
|
||||
],
|
||||
};
|
||||
const WPTS = waypointsKey('F');
|
||||
const elements = ['F', WPTS, 'W1', 'W2', 'T1', 'S1', 'S2', 'T2', 'S3'];
|
||||
|
||||
/** Applies the actions one after the other, starting with everything visible. */
|
||||
function apply(...actions: [ids: string[], hidden: boolean][]): VisibilityState {
|
||||
let visibility: Visibility = new Map();
|
||||
for (const [ids, hidden] of actions) {
|
||||
visibility = setHidden(structure, visibility, ids, hidden);
|
||||
}
|
||||
return { structure, visibility };
|
||||
}
|
||||
|
||||
function hiddenElements(state: VisibilityState): string[] {
|
||||
return elements.filter((id) => isHidden(state, id));
|
||||
}
|
||||
|
||||
describe('file visibility', () => {
|
||||
it('shows everything by default', () => {
|
||||
assert.deepEqual(hiddenElements(apply()), []);
|
||||
});
|
||||
|
||||
it('hides an element and what is below it', () => {
|
||||
assert.deepEqual(hiddenElements(apply([['T1'], true])), ['T1', 'S1', 'S2']);
|
||||
assert.deepEqual(hiddenElements(apply([['S1'], true])), ['S1']);
|
||||
assert.deepEqual(hiddenElements(apply([[WPTS], true])), [WPTS, 'W1', 'W2']);
|
||||
assert.deepEqual(hiddenElements(apply([['W2'], true])), ['W2']);
|
||||
});
|
||||
|
||||
it('hides a whole file', () => {
|
||||
assert.deepEqual(hiddenElements(apply([['F'], true])), elements);
|
||||
});
|
||||
|
||||
it('shows again what was hidden', () => {
|
||||
assert.deepEqual(hiddenElements(apply([['F'], true], [['F'], false])), []);
|
||||
assert.deepEqual(hiddenElements(apply([['S1'], true], [['S1'], false])), []);
|
||||
});
|
||||
|
||||
it('keeps the other segments hidden when one is shown below a hidden file', () => {
|
||||
const state = apply([['F'], true], [['S1'], false]);
|
||||
assert.deepEqual(hiddenElements(state), [WPTS, 'W1', 'W2', 'S2', 'T2', 'S3']);
|
||||
// the file and the track of the visible segment are not hidden, they have visible content
|
||||
assert.equal(isHidden(state, 'F'), false);
|
||||
assert.equal(isHidden(state, 'T1'), false);
|
||||
assert.equal(isHidden(state, 'S1'), false);
|
||||
});
|
||||
|
||||
it('hides the file again once what was shown is hidden', () => {
|
||||
const state = apply([['F'], true], [['S1'], false], [['S1'], true]);
|
||||
assert.deepEqual(hiddenElements(state), elements);
|
||||
});
|
||||
|
||||
it('resets what was shown below when hiding a parent', () => {
|
||||
const state = apply([['F'], true], [['S1'], false], [['F'], true]);
|
||||
assert.deepEqual(hiddenElements(state), elements);
|
||||
assert.equal(state.visibility.has('S1'), false);
|
||||
});
|
||||
|
||||
it('resets what was hidden below when showing a parent', () => {
|
||||
const state = apply([['S1', 'S3', 'W1'], true], [['F'], false]);
|
||||
assert.deepEqual(hiddenElements(state), []);
|
||||
assert.equal(state.visibility.has('S1'), false);
|
||||
});
|
||||
|
||||
it('shows a track hidden below a hidden file, with all its segments', () => {
|
||||
const state = apply([['F'], true], [['T1'], false]);
|
||||
assert.deepEqual(hiddenElements(state), [WPTS, 'W1', 'W2', 'T2', 'S3']);
|
||||
});
|
||||
|
||||
it('shows a waypoint below a hidden file', () => {
|
||||
const state = apply([['F'], true], [['W1'], false]);
|
||||
assert.deepEqual(hiddenElements(state), ['W2', 'T1', 'S1', 'S2', 'T2', 'S3']);
|
||||
assert.equal(isHidden(state, WPTS), false);
|
||||
assert.equal(isHidden(state, 'F'), false);
|
||||
});
|
||||
|
||||
it('hides a segment shown inside a hidden track of a visible file', () => {
|
||||
const state = apply([['T1'], true], [['S2'], false]);
|
||||
assert.deepEqual(hiddenElements(state), ['S1']);
|
||||
});
|
||||
|
||||
it('does not touch the other branches of the tree', () => {
|
||||
const state = apply([['T1'], true], [['T2'], true], [['T1'], false]);
|
||||
assert.deepEqual(hiddenElements(state), ['T2', 'S3']);
|
||||
});
|
||||
|
||||
it('has leaf helpers agreeing with isHidden', () => {
|
||||
const states = [
|
||||
apply(),
|
||||
apply([['F'], true]),
|
||||
apply([['F'], true], [['S1'], false], [['W2'], false]),
|
||||
apply([['T1'], true], [['S2'], false]),
|
||||
apply([[WPTS], true], [['W1'], false]),
|
||||
];
|
||||
for (const state of states) {
|
||||
for (const track of structure.tracks) {
|
||||
for (const { id } of track.segments) {
|
||||
assert.equal(isSegmentHidden(state, track.id, id), isHidden(state, id));
|
||||
}
|
||||
}
|
||||
for (const { id } of structure.waypoints) {
|
||||
assert.equal(isWaypointHidden(state, id), isHidden(state, id));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('does not consider unknown ids hidden', () => {
|
||||
assert.equal(isHidden(apply([['F'], true]), 'unknown'), false);
|
||||
});
|
||||
|
||||
it('does not mutate the previous visibility', () => {
|
||||
const before: Visibility = new Map([['S1', false]]);
|
||||
setHidden(structure, before, ['F'], true);
|
||||
assert.deepEqual([...before], [['S1', false]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('kept visibility', () => {
|
||||
const visibility = (entries: [string, boolean][]): Visibility => new Map(entries);
|
||||
|
||||
it('comes back as it was kept', () => {
|
||||
const files = new Map([
|
||||
[
|
||||
'F',
|
||||
visibility([
|
||||
['F', false],
|
||||
['S1', true],
|
||||
[WPTS, false],
|
||||
]),
|
||||
],
|
||||
['G', visibility([['G', false]])],
|
||||
]);
|
||||
const json = stringifyVisibility(files, () => true);
|
||||
assert.deepEqual(parseVisibility(json), files);
|
||||
});
|
||||
|
||||
it('leaves out the files that are gone and the ones with nothing set', () => {
|
||||
const files = new Map([
|
||||
['F', visibility([['F', false]])],
|
||||
['G', visibility([['G', false]])],
|
||||
['H', visibility([])],
|
||||
]);
|
||||
const json = stringifyVisibility(files, (fileId) => fileId !== 'G');
|
||||
assert.deepEqual([...parseVisibility(json).keys()], ['F']);
|
||||
});
|
||||
|
||||
it('is nothing when there is nothing to keep', () => {
|
||||
assert.equal(
|
||||
stringifyVisibility(new Map(), () => true),
|
||||
undefined
|
||||
);
|
||||
assert.equal(
|
||||
stringifyVisibility(new Map([['F', visibility([['F', false]])]]), () => false),
|
||||
undefined
|
||||
);
|
||||
assert.equal(parseVisibility(undefined).size, 0);
|
||||
});
|
||||
|
||||
it('ignores what is not visibility', () => {
|
||||
assert.equal(parseVisibility('not json').size, 0);
|
||||
assert.equal(parseVisibility('3').size, 0);
|
||||
assert.equal(parseVisibility('null').size, 0);
|
||||
const parsed = parseVisibility('{"F":{"F":false,"S1":"no"},"G":3,"H":{}}');
|
||||
assert.deepEqual([...parsed.keys()], ['F']);
|
||||
assert.deepEqual([...parsed.get('F')!], [['F', false]]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
import type { FileStructure } from 'gpx-rs';
|
||||
|
||||
/**
|
||||
* Visibility the user set explicitly, by element id (file, track, segment, waypoint, or the
|
||||
* waypoints of the file as a whole, see `waypointsKey`): `false` hides, `true` shows. An element
|
||||
* without entry follows its parent, and everything is shown by default.
|
||||
*/
|
||||
export type Visibility = ReadonlyMap<string, boolean>;
|
||||
|
||||
/** What is needed to tell whether the elements of a file are hidden (a `FileState` fits). */
|
||||
export type VisibilityState = { structure: FileStructure; visibility: Visibility };
|
||||
|
||||
/** Id to hide or show all the waypoints of a file at once. */
|
||||
export function waypointsKey(fileId: string): string {
|
||||
return `${fileId}:waypoints`;
|
||||
}
|
||||
|
||||
/** Ids from the file down to `id` in the file tree (file, tracks, segments / waypoints group). */
|
||||
function pathTo(structure: FileStructure, id: string): string[] {
|
||||
const fileId = structure.id;
|
||||
if (id === fileId) {
|
||||
return [fileId];
|
||||
}
|
||||
if (id === waypointsKey(fileId)) {
|
||||
return [fileId, id];
|
||||
}
|
||||
if (structure.waypoints.some((waypoint) => waypoint.id === id)) {
|
||||
return [fileId, waypointsKey(fileId), id];
|
||||
}
|
||||
for (const track of structure.tracks) {
|
||||
if (track.id === id) {
|
||||
return [fileId, id];
|
||||
}
|
||||
if (track.segments.some((segment) => segment.id === id)) {
|
||||
return [fileId, track.id, id];
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function children(structure: FileStructure, id: string): string[] {
|
||||
if (id === structure.id) {
|
||||
return [waypointsKey(id), ...structure.tracks.map((track) => track.id)];
|
||||
}
|
||||
if (id === waypointsKey(structure.id)) {
|
||||
return structure.waypoints.map((waypoint) => waypoint.id);
|
||||
}
|
||||
return structure.tracks.find((track) => track.id === id)?.segments.map((s) => s.id) ?? [];
|
||||
}
|
||||
|
||||
function descendants(structure: FileStructure, id: string): string[] {
|
||||
return children(structure, id).flatMap((child) => [child, ...descendants(structure, child)]);
|
||||
}
|
||||
|
||||
/** The closest explicit visibility on a path (from the file down): whether it is hidden. */
|
||||
function followsHidden(visibility: Visibility, path: string[]): boolean {
|
||||
for (let i = path.length - 1; i >= 0; i--) {
|
||||
const shown = visibility.get(path[i]);
|
||||
if (shown !== undefined) {
|
||||
return !shown;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an element is hidden: its closest explicit ancestor (or itself) is hidden, and none
|
||||
* of its descendants was shown. A file whose only visible segment was shown on its own is not
|
||||
* hidden, its other segments are. Unknown ids are not hidden.
|
||||
*/
|
||||
export function isHidden({ structure, visibility }: VisibilityState, id: string): boolean {
|
||||
return (
|
||||
followsHidden(visibility, pathTo(structure, id)) &&
|
||||
!descendants(structure, id).some((descendant) => visibility.get(descendant) === true)
|
||||
);
|
||||
}
|
||||
|
||||
// Leaf elements have no descendants: no need to look at the structure.
|
||||
|
||||
export function isSegmentHidden(
|
||||
{ structure, visibility }: VisibilityState,
|
||||
trackId: string,
|
||||
segmentId: string
|
||||
): boolean {
|
||||
return followsHidden(visibility, [structure.id, trackId, segmentId]);
|
||||
}
|
||||
|
||||
export function isWaypointHidden(
|
||||
{ structure, visibility }: VisibilityState,
|
||||
waypointId: string
|
||||
): boolean {
|
||||
return followsHidden(visibility, [structure.id, waypointsKey(structure.id), waypointId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides or shows elements of a file, and returns the new visibility. What was set explicitly
|
||||
* below these elements is reset, so that hiding a file then showing one of its segments leaves
|
||||
* only that segment visible, and hiding the file again hides all of it.
|
||||
*/
|
||||
export function setHidden(
|
||||
structure: FileStructure,
|
||||
visibility: Visibility,
|
||||
ids: string[],
|
||||
hidden: boolean
|
||||
): Visibility {
|
||||
const next = new Map(visibility);
|
||||
ids.forEach((id) => {
|
||||
descendants(structure, id).forEach((child) => next.delete(child));
|
||||
next.set(id, !hidden);
|
||||
});
|
||||
return next;
|
||||
}
|
||||
|
||||
/** Name under which the visibility of the files is kept in the storage. */
|
||||
export const VISIBILITY_KEY = 'visibility';
|
||||
|
||||
/**
|
||||
* The visibility of the files as JSON, to keep it: the explicit visibility of each file by id,
|
||||
* leaving out the files that `keep` refuses (the ones that are gone) and the ones that have none.
|
||||
* `undefined` when there is nothing to keep.
|
||||
*/
|
||||
export function stringifyVisibility(
|
||||
files: ReadonlyMap<string, Visibility>,
|
||||
keep: (fileId: string) => boolean
|
||||
): string | undefined {
|
||||
const data: Record<string, Record<string, boolean>> = {};
|
||||
files.forEach((visibility, fileId) => {
|
||||
if (visibility.size > 0 && keep(fileId)) {
|
||||
data[fileId] = Object.fromEntries(visibility);
|
||||
}
|
||||
});
|
||||
return Object.keys(data).length > 0 ? JSON.stringify(data) : undefined;
|
||||
}
|
||||
|
||||
/** What `stringifyVisibility` gave back. What does not make sense is left out. */
|
||||
export function parseVisibility(json: string | undefined): Map<string, Visibility> {
|
||||
const files = new Map<string, Visibility>();
|
||||
if (json === undefined) {
|
||||
return files;
|
||||
}
|
||||
try {
|
||||
const data: unknown = JSON.parse(json);
|
||||
if (typeof data !== 'object' || data === null) {
|
||||
return files;
|
||||
}
|
||||
for (const [fileId, entries] of Object.entries(data)) {
|
||||
if (typeof entries !== 'object' || entries === null) {
|
||||
continue;
|
||||
}
|
||||
const visibility = new Map<string, boolean>();
|
||||
for (const [id, shown] of Object.entries(entries)) {
|
||||
if (typeof shown === 'boolean') {
|
||||
visibility.set(id, shown);
|
||||
}
|
||||
}
|
||||
if (visibility.size > 0) {
|
||||
files.set(fileId, visibility);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// not something that was kept
|
||||
}
|
||||
return files;
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
import { describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import type { Clipboard, FileStructure, Selection } from 'gpx-rs';
|
||||
import type { VisibilityState } from '$lib/engine/file-visibility';
|
||||
import {
|
||||
elementId,
|
||||
hasSelectionWithin,
|
||||
isCovered,
|
||||
isInClipboard,
|
||||
isEmpty,
|
||||
isSelected,
|
||||
isSelectionHidden,
|
||||
selectedElementIds,
|
||||
selectedFileIds,
|
||||
selectionSize,
|
||||
type FileTreeNode,
|
||||
} from '$lib/engine/selection-helpers';
|
||||
|
||||
// Two files; F1 has the tracks T1 (segments S1, S2) and T2 (segment S3), and waypoints W1, W2.
|
||||
const nodes: Record<string, FileTreeNode> = {
|
||||
F1: { type: 'file', fileId: 'F1' },
|
||||
F2: { type: 'file', fileId: 'F2' },
|
||||
T1: { type: 'track', fileId: 'F1', trackId: 'T1' },
|
||||
T2: { type: 'track', fileId: 'F1', trackId: 'T2' },
|
||||
S1: { type: 'segment', fileId: 'F1', trackId: 'T1', segmentId: 'S1' },
|
||||
S2: { type: 'segment', fileId: 'F1', trackId: 'T1', segmentId: 'S2' },
|
||||
S3: { type: 'segment', fileId: 'F1', trackId: 'T2', segmentId: 'S3' },
|
||||
WPTS: { type: 'waypoints', fileId: 'F1' },
|
||||
W1: { type: 'waypoint', fileId: 'F1', waypointId: 'W1' },
|
||||
W2: { type: 'waypoint', fileId: 'F1', waypointId: 'W2' },
|
||||
// same ids in another file
|
||||
F2_WPTS: { type: 'waypoints', fileId: 'F2' },
|
||||
};
|
||||
|
||||
const empty: Selection = { type: 'empty' };
|
||||
const files = (...fileIds: string[]): Selection => ({ type: 'file', fileIds });
|
||||
const tracks = (...trackIds: string[]): Selection => ({ type: 'track', fileId: 'F1', trackIds });
|
||||
const segments = (trackId: string, ...segmentIds: string[]): Selection => ({
|
||||
type: 'segment',
|
||||
fileId: 'F1',
|
||||
trackId,
|
||||
segmentIds,
|
||||
});
|
||||
const waypoints: Selection = { type: 'waypoints', fileId: 'F1' };
|
||||
const waypoint = (...waypointIds: string[]): Selection => ({
|
||||
type: 'waypoint',
|
||||
fileId: 'F1',
|
||||
waypointIds,
|
||||
});
|
||||
|
||||
/** The names of the nodes for which `predicate` holds, to compare with what is expected. */
|
||||
function matching(selection: Selection, predicate: typeof isSelected): string[] {
|
||||
return Object.keys(nodes).filter((name) => predicate(selection, nodes[name]));
|
||||
}
|
||||
|
||||
describe('selection helpers', () => {
|
||||
it('tells whether the selection is empty', () => {
|
||||
assert.equal(isEmpty(empty), true);
|
||||
assert.equal(isEmpty(files('F1')), false);
|
||||
assert.equal(isEmpty(waypoints), false);
|
||||
});
|
||||
|
||||
it('lists the files holding something selected', () => {
|
||||
assert.deepEqual(selectedFileIds(empty), []);
|
||||
assert.deepEqual(selectedFileIds(files('F1', 'F2')), ['F1', 'F2']);
|
||||
assert.deepEqual(selectedFileIds(tracks('T1')), ['F1']);
|
||||
assert.deepEqual(selectedFileIds(segments('T1', 'S1')), ['F1']);
|
||||
assert.deepEqual(selectedFileIds(waypoints), ['F1']);
|
||||
assert.deepEqual(selectedFileIds(waypoint('W1')), ['F1']);
|
||||
});
|
||||
|
||||
it('gives the id of the element of a node', () => {
|
||||
assert.deepEqual(
|
||||
Object.keys(nodes).map((name) => elementId(nodes[name])),
|
||||
['F1', 'F2', 'T1', 'T2', 'S1', 'S2', 'S3', 'F1:waypoints', 'W1', 'W2', 'F2:waypoints']
|
||||
);
|
||||
});
|
||||
|
||||
it('tells which nodes are in the clipboard', () => {
|
||||
const clipboard = (type: Clipboard['type'], ids: string[]): Clipboard => ({
|
||||
type,
|
||||
ids,
|
||||
cut: true,
|
||||
});
|
||||
assert.deepEqual(
|
||||
matching(empty, (_, node) => isInClipboard(undefined, node)),
|
||||
[]
|
||||
);
|
||||
const inClipboard = (c: Clipboard) =>
|
||||
Object.keys(nodes).filter((name) => isInClipboard(c, nodes[name]));
|
||||
assert.deepEqual(inClipboard(clipboard('files', ['F2'])), ['F2']);
|
||||
assert.deepEqual(inClipboard(clipboard('tracks', ['T2', 'S1'])), ['T2']);
|
||||
assert.deepEqual(inClipboard(clipboard('segments', ['S1', 'S3'])), ['S1', 'S3']);
|
||||
assert.deepEqual(inClipboard(clipboard('waypoints', ['W2'])), ['W2']);
|
||||
// not mixed up with the other kinds of elements
|
||||
assert.deepEqual(inClipboard(clipboard('files', ['T1', 'S1', 'W1'])), []);
|
||||
});
|
||||
|
||||
it('counts the selected elements', () => {
|
||||
assert.equal(selectionSize(empty), 0);
|
||||
assert.equal(selectionSize(files('F1', 'F2')), 2);
|
||||
assert.equal(selectionSize(tracks('T1')), 1);
|
||||
assert.equal(selectionSize(segments('T1', 'S1', 'S2')), 2);
|
||||
assert.equal(selectionSize(waypoints), 1);
|
||||
assert.equal(selectionSize(waypoint('W1', 'W2')), 2);
|
||||
});
|
||||
|
||||
it('lists the ids of the selected elements by file', () => {
|
||||
assert.deepEqual(selectedElementIds(empty), []);
|
||||
assert.deepEqual(selectedElementIds(files('F1', 'F2')), [
|
||||
{ fileId: 'F1', ids: ['F1'] },
|
||||
{ fileId: 'F2', ids: ['F2'] },
|
||||
]);
|
||||
assert.deepEqual(selectedElementIds(tracks('T1', 'T2')), [
|
||||
{ fileId: 'F1', ids: ['T1', 'T2'] },
|
||||
]);
|
||||
assert.deepEqual(selectedElementIds(segments('T1', 'S1')), [{ fileId: 'F1', ids: ['S1'] }]);
|
||||
assert.deepEqual(selectedElementIds(waypoints), [{ fileId: 'F1', ids: ['F1:waypoints'] }]);
|
||||
assert.deepEqual(selectedElementIds(waypoint('W1')), [{ fileId: 'F1', ids: ['W1'] }]);
|
||||
});
|
||||
|
||||
describe('isSelected', () => {
|
||||
it('matches the selected nodes only', () => {
|
||||
assert.deepEqual(matching(empty, isSelected), []);
|
||||
assert.deepEqual(matching(files('F1', 'F2'), isSelected), ['F1', 'F2']);
|
||||
assert.deepEqual(matching(tracks('T1', 'T2'), isSelected), ['T1', 'T2']);
|
||||
assert.deepEqual(matching(segments('T1', 'S1'), isSelected), ['S1']);
|
||||
assert.deepEqual(matching(waypoints, isSelected), ['WPTS']);
|
||||
assert.deepEqual(matching(waypoint('W2'), isSelected), ['W2']);
|
||||
});
|
||||
|
||||
it('does not mix up the track of a segment, nor the file', () => {
|
||||
// S3 belongs to T2: selecting "S3 of T1" does not select it
|
||||
assert.equal(isSelected(segments('T1', 'S3'), nodes.S3), false);
|
||||
assert.equal(isSelected(files('F2'), nodes.F1), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isCovered', () => {
|
||||
it('covers everything below a selected file', () => {
|
||||
assert.deepEqual(
|
||||
matching(files('F1'), isCovered),
|
||||
Object.keys(nodes).filter((name) => name !== 'F2' && name !== 'F2_WPTS')
|
||||
);
|
||||
});
|
||||
|
||||
it('covers the segments of a selected track, not its siblings', () => {
|
||||
assert.deepEqual(matching(tracks('T1'), isCovered), ['T1', 'S1', 'S2']);
|
||||
});
|
||||
|
||||
it('covers the waypoints below the waypoints node', () => {
|
||||
assert.deepEqual(matching(waypoints, isCovered), ['WPTS', 'W1', 'W2']);
|
||||
});
|
||||
|
||||
it('does not cover parents, siblings or the other files', () => {
|
||||
assert.deepEqual(matching(segments('T1', 'S1'), isCovered), ['S1']);
|
||||
assert.deepEqual(matching(waypoint('W1'), isCovered), ['W1']);
|
||||
assert.deepEqual(matching(empty, isCovered), []);
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasSelectionWithin', () => {
|
||||
it('looks at the descendants of a file, whatever their kind', () => {
|
||||
for (const selection of [
|
||||
files('F1'),
|
||||
tracks('T1'),
|
||||
segments('T1', 'S1'),
|
||||
waypoints,
|
||||
waypoint('W1'),
|
||||
]) {
|
||||
assert.equal(hasSelectionWithin(selection, nodes.F1), true);
|
||||
assert.equal(hasSelectionWithin(selection, nodes.F2), false);
|
||||
}
|
||||
assert.equal(hasSelectionWithin(empty, nodes.F1), false);
|
||||
});
|
||||
|
||||
it('looks at the segments of a track', () => {
|
||||
assert.deepEqual(matching(tracks('T1'), hasSelectionWithin), ['F1', 'T1']);
|
||||
assert.deepEqual(matching(segments('T1', 'S2'), hasSelectionWithin), [
|
||||
'F1',
|
||||
'T1',
|
||||
'S2',
|
||||
]);
|
||||
});
|
||||
|
||||
it('looks at the waypoints below the waypoints node', () => {
|
||||
assert.deepEqual(matching(waypoints, hasSelectionWithin), ['F1', 'WPTS']);
|
||||
assert.deepEqual(matching(waypoint('W1'), hasSelectionWithin), ['F1', 'WPTS', 'W1']);
|
||||
});
|
||||
|
||||
it('does not look at ancestors', () => {
|
||||
// a selected file does not mean that its segments hold a selection
|
||||
assert.deepEqual(matching(files('F1'), hasSelectionWithin), ['F1']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isSelectionHidden', () => {
|
||||
const segment = (id: string) => ({ id, rev: id, length: 2 });
|
||||
const structure: FileStructure = {
|
||||
id: 'F1',
|
||||
name: 'file',
|
||||
waypointsRev: 'rev',
|
||||
waypoints: [{ id: 'W1' }, { id: 'W2' }],
|
||||
tracks: [
|
||||
{ id: 'T1', segments: [segment('S1'), segment('S2')] },
|
||||
{ id: 'T2', segments: [segment('S3')] },
|
||||
],
|
||||
};
|
||||
const states = (...hidden: string[]) =>
|
||||
new Map<string, VisibilityState>([
|
||||
[
|
||||
'F1',
|
||||
{ structure, visibility: new Map(hidden.map((id) => [id, false] as const)) },
|
||||
],
|
||||
]);
|
||||
|
||||
it('is false when nothing is selected', () => {
|
||||
assert.equal(isSelectionHidden(empty, states('F1')), false);
|
||||
});
|
||||
|
||||
it('is true when everything selected is hidden', () => {
|
||||
assert.equal(isSelectionHidden(files('F1'), states('F1')), true);
|
||||
assert.equal(isSelectionHidden(tracks('T1', 'T2'), states('T1', 'T2')), true);
|
||||
// through a hidden parent
|
||||
assert.equal(isSelectionHidden(segments('T1', 'S1', 'S2'), states('T1')), true);
|
||||
assert.equal(isSelectionHidden(waypoint('W1', 'W2'), states('F1:waypoints')), true);
|
||||
assert.equal(isSelectionHidden(waypoints, states('F1:waypoints')), true);
|
||||
});
|
||||
|
||||
it('is false as soon as one selected element is visible', () => {
|
||||
assert.equal(isSelectionHidden(files('F1'), states()), false);
|
||||
assert.equal(isSelectionHidden(tracks('T1', 'T2'), states('T1')), false);
|
||||
assert.equal(isSelectionHidden(waypoint('W1', 'W2'), states('W1')), false);
|
||||
});
|
||||
|
||||
it('is false for files that are not known', () => {
|
||||
assert.equal(isSelectionHidden(files('F1', 'F2'), states('F1')), false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,205 @@
|
||||
import type { Clipboard, Selection } from 'gpx-rs';
|
||||
import { isHidden, waypointsKey, type VisibilityState } from '$lib/engine/file-visibility';
|
||||
|
||||
/**
|
||||
* An element of the file tree, to ask questions about the selection:
|
||||
*
|
||||
* file ─┬─ track ─── segment
|
||||
* └─ waypoints ─── waypoint
|
||||
*
|
||||
* `waypoints` is the node standing for all the waypoints of a file.
|
||||
*/
|
||||
export type FileTreeNode =
|
||||
| { type: 'file'; fileId: string }
|
||||
| { type: 'track'; fileId: string; trackId: string }
|
||||
| { type: 'segment'; fileId: string; trackId: string; segmentId: string }
|
||||
| { type: 'waypoints'; fileId: string }
|
||||
| { type: 'waypoint'; fileId: string; waypointId: string };
|
||||
|
||||
/**
|
||||
* The id of the element of a node, as used by the visibility (`waypointsKey` for the waypoints
|
||||
* node).
|
||||
*/
|
||||
export function elementId(node: FileTreeNode): string {
|
||||
switch (node.type) {
|
||||
case 'file':
|
||||
return node.fileId;
|
||||
case 'track':
|
||||
return node.trackId;
|
||||
case 'segment':
|
||||
return node.segmentId;
|
||||
case 'waypoints':
|
||||
return waypointsKey(node.fileId);
|
||||
case 'waypoint':
|
||||
return node.waypointId;
|
||||
}
|
||||
}
|
||||
|
||||
export function isEmpty(selection: Selection): boolean {
|
||||
return selection.type === 'empty';
|
||||
}
|
||||
|
||||
/** Ids of the files holding something selected (or selected themselves). */
|
||||
export function selectedFileIds(selection: Selection): string[] {
|
||||
switch (selection.type) {
|
||||
case 'empty':
|
||||
return [];
|
||||
case 'file':
|
||||
return selection.fileIds;
|
||||
default:
|
||||
return [selection.fileId];
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether the node is one of the elements in the clipboard. */
|
||||
export function isInClipboard(clipboard: Clipboard | undefined, node: FileTreeNode): boolean {
|
||||
if (!clipboard) {
|
||||
return false;
|
||||
}
|
||||
switch (node.type) {
|
||||
case 'file':
|
||||
return clipboard.type === 'files' && clipboard.ids.includes(node.fileId);
|
||||
case 'track':
|
||||
return clipboard.type === 'tracks' && clipboard.ids.includes(node.trackId);
|
||||
case 'segment':
|
||||
return clipboard.type === 'segments' && clipboard.ids.includes(node.segmentId);
|
||||
case 'waypoint':
|
||||
return clipboard.type === 'waypoints' && clipboard.ids.includes(node.waypointId);
|
||||
case 'waypoints':
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Number of selected elements (the waypoints node counts for one). */
|
||||
export function selectionSize(selection: Selection): number {
|
||||
switch (selection.type) {
|
||||
case 'empty':
|
||||
return 0;
|
||||
case 'file':
|
||||
return selection.fileIds.length;
|
||||
case 'track':
|
||||
return selection.trackIds.length;
|
||||
case 'segment':
|
||||
return selection.segmentIds.length;
|
||||
case 'waypoints':
|
||||
return 1;
|
||||
case 'waypoint':
|
||||
return selection.waypointIds.length;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The ids of the selected elements, by file, as used by the visibility (a file, its tracks, its
|
||||
* segments, its waypoints, or `waypointsKey` for the waypoints node).
|
||||
*/
|
||||
export function selectedElementIds(selection: Selection): { fileId: string; ids: string[] }[] {
|
||||
switch (selection.type) {
|
||||
case 'empty':
|
||||
return [];
|
||||
case 'file':
|
||||
return selection.fileIds.map((fileId) => ({ fileId, ids: [fileId] }));
|
||||
case 'track':
|
||||
return [{ fileId: selection.fileId, ids: selection.trackIds }];
|
||||
case 'segment':
|
||||
return [{ fileId: selection.fileId, ids: selection.segmentIds }];
|
||||
case 'waypoints':
|
||||
return [{ fileId: selection.fileId, ids: [waypointsKey(selection.fileId)] }];
|
||||
case 'waypoint':
|
||||
return [{ fileId: selection.fileId, ids: selection.waypointIds }];
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether the node itself is selected. */
|
||||
export function isSelected(selection: Selection, node: FileTreeNode): boolean {
|
||||
switch (node.type) {
|
||||
case 'file':
|
||||
return selection.type === 'file' && selection.fileIds.includes(node.fileId);
|
||||
case 'track':
|
||||
return (
|
||||
selection.type === 'track' &&
|
||||
selection.fileId === node.fileId &&
|
||||
selection.trackIds.includes(node.trackId)
|
||||
);
|
||||
case 'segment':
|
||||
return (
|
||||
selection.type === 'segment' &&
|
||||
selection.fileId === node.fileId &&
|
||||
selection.trackId === node.trackId &&
|
||||
selection.segmentIds.includes(node.segmentId)
|
||||
);
|
||||
case 'waypoints':
|
||||
return selection.type === 'waypoints' && selection.fileId === node.fileId;
|
||||
case 'waypoint':
|
||||
return (
|
||||
selection.type === 'waypoint' &&
|
||||
selection.fileId === node.fileId &&
|
||||
selection.waypointIds.includes(node.waypointId)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** The parent of a node, `undefined` for a file. */
|
||||
function parent(node: FileTreeNode): FileTreeNode | undefined {
|
||||
switch (node.type) {
|
||||
case 'file':
|
||||
return undefined;
|
||||
case 'track':
|
||||
case 'waypoints':
|
||||
return { type: 'file', fileId: node.fileId };
|
||||
case 'segment':
|
||||
return { type: 'track', fileId: node.fileId, trackId: node.trackId };
|
||||
case 'waypoint':
|
||||
return { type: 'waypoints', fileId: node.fileId };
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether the node, or one of its ancestors, is selected: the node is covered by the selection. */
|
||||
export function isCovered(selection: Selection, node: FileTreeNode): boolean {
|
||||
for (let n: FileTreeNode | undefined = node; n; n = parent(n)) {
|
||||
if (isSelected(selection, n)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Whether the node, or one of its descendants, is selected. */
|
||||
export function hasSelectionWithin(selection: Selection, node: FileTreeNode): boolean {
|
||||
switch (node.type) {
|
||||
case 'file':
|
||||
return selectedFileIds(selection).includes(node.fileId);
|
||||
case 'track':
|
||||
return (
|
||||
isSelected(selection, node) ||
|
||||
(selection.type === 'segment' &&
|
||||
selection.fileId === node.fileId &&
|
||||
selection.trackId === node.trackId)
|
||||
);
|
||||
case 'waypoints':
|
||||
return (
|
||||
isSelected(selection, node) ||
|
||||
(selection.type === 'waypoint' && selection.fileId === node.fileId)
|
||||
);
|
||||
case 'segment':
|
||||
case 'waypoint':
|
||||
return isSelected(selection, node);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether all the selected elements are hidden (false when nothing is selected). `states` holds
|
||||
* the files by id: an element of an unknown file is not hidden.
|
||||
*/
|
||||
export function isSelectionHidden(
|
||||
selection: Selection,
|
||||
states: ReadonlyMap<string, VisibilityState>
|
||||
): boolean {
|
||||
const selected = selectedElementIds(selection);
|
||||
return (
|
||||
selected.length > 0 &&
|
||||
selected.every(({ fileId, ids }) => {
|
||||
const state = states.get(fileId);
|
||||
return state !== undefined && ids.every((id) => isHidden(state, id));
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { get, writable, type Readable } from 'svelte/store';
|
||||
import { categoryIntervals, type CategoryIntervals } from '$lib/trackpoint-categories';
|
||||
import { categoryIntervals, type CategoryIntervals } from '$lib/engine/trackpoint-categories';
|
||||
import type { Wasm } from './convert';
|
||||
import {
|
||||
EMPTY_SELECTION_STATISTICS,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { LEGACY_IMPORTED_KEY, readLegacyData } from '$lib/legacy-import';
|
||||
import { parseVisibility, VISIBILITY_KEY, type Visibility } from '$lib/file-visibility';
|
||||
import { LEGACY_IMPORTED_KEY, readLegacyData } from '$lib/storage/legacy-import';
|
||||
import { parseVisibility, VISIBILITY_KEY, type Visibility } from '$lib/engine/file-visibility';
|
||||
import type { Outcome, StorageOpened } from 'gpx-rs';
|
||||
import type { Wasm } from './convert';
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
categoryIntervals,
|
||||
categoryValues,
|
||||
type CategoryIntervals,
|
||||
} from '$lib/engine/trackpoint-categories';
|
||||
|
||||
describe('categoryValues', () => {
|
||||
// asphalt, asphalt, unknown, gravel, gravel, gravel, unknown
|
||||
const intervals: CategoryIntervals = {
|
||||
starts: new Uint32Array([0, 2, 3, 6]),
|
||||
values: new Uint8Array([1, 0, 2, 0]),
|
||||
names: ['asphalt', 'gravel'],
|
||||
};
|
||||
|
||||
it('is the name of the value of the interval of each trackpoint', () => {
|
||||
assert.deepEqual(categoryValues(intervals, 7), [
|
||||
'asphalt',
|
||||
'asphalt',
|
||||
undefined,
|
||||
'gravel',
|
||||
'gravel',
|
||||
'gravel',
|
||||
undefined,
|
||||
]);
|
||||
});
|
||||
|
||||
it('goes on with the last interval until the last trackpoint', () => {
|
||||
const last = { ...intervals, values: new Uint8Array([1, 0, 2, 2]) };
|
||||
assert.deepEqual(categoryValues(last, 9).slice(5), [
|
||||
'gravel',
|
||||
'gravel',
|
||||
'gravel',
|
||||
'gravel',
|
||||
]);
|
||||
});
|
||||
|
||||
it('does not go past the trackpoints', () => {
|
||||
assert.deepEqual(categoryValues(intervals, 3), ['asphalt', 'asphalt', undefined]);
|
||||
assert.deepEqual(categoryValues(intervals, 0), []);
|
||||
});
|
||||
|
||||
it('is undefined without intervals, or for a code without name', () => {
|
||||
assert.deepEqual(categoryValues(undefined, 2), [undefined, undefined]);
|
||||
const unnamed = { starts: new Uint32Array([0]), values: new Uint8Array([3]), names: [] };
|
||||
assert.deepEqual(categoryValues(unnamed, 2), [undefined, undefined]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('categoryIntervals', () => {
|
||||
it('groups the trackpoints that share a value', () => {
|
||||
const values = ['asphalt', 'asphalt', undefined, 'gravel', 'gravel', 'asphalt', undefined];
|
||||
const intervals = categoryIntervals(values);
|
||||
assert.deepEqual([...intervals.starts], [0, 2, 3, 5, 6]);
|
||||
assert.deepEqual([...intervals.values], [1, 0, 2, 1, 0]);
|
||||
assert.deepEqual(intervals.names, ['asphalt', 'gravel']);
|
||||
// and it goes back to the values
|
||||
assert.deepEqual(categoryValues(intervals, values.length), values);
|
||||
});
|
||||
|
||||
it('has no intervals when the value is unknown everywhere', () => {
|
||||
for (const values of [[], [undefined, undefined]]) {
|
||||
const intervals = categoryIntervals(values);
|
||||
assert.equal(intervals.starts.length, 0);
|
||||
assert.equal(intervals.values.length, 0);
|
||||
assert.deepEqual(intervals.names, []);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* A category of the engine (surface, highway...) for each trackpoint, as the intervals of
|
||||
* consecutive trackpoints that share the same value: `starts[i]` is the index of the first
|
||||
* trackpoint of the interval `i`, which lasts until the next start (or the last trackpoint). Its
|
||||
* `values[i]` is 0 when the value is unknown, else 1 + the index of the value in `names`.
|
||||
*/
|
||||
export type CategoryIntervals = {
|
||||
starts: Uint32Array;
|
||||
values: Uint8Array;
|
||||
/** Names of the values, by code: a name keeps its index. */
|
||||
names: string[];
|
||||
};
|
||||
|
||||
/**
|
||||
* The value of each of the `length` trackpoints for a category of the engine, `undefined` when it
|
||||
* is unknown, or when the category is missing (it is unknown for every trackpoint). The intervals
|
||||
* are scanned once, so it takes a time linear in `length`.
|
||||
*/
|
||||
export function categoryValues(
|
||||
intervals: CategoryIntervals | undefined,
|
||||
length: number
|
||||
): (string | undefined)[] {
|
||||
const result: (string | undefined)[] = new Array(length).fill(undefined);
|
||||
if (!intervals) {
|
||||
return result;
|
||||
}
|
||||
const { starts, values, names } = intervals;
|
||||
for (let i = 0; i < starts.length; i++) {
|
||||
if (!values[i]) {
|
||||
continue; // unknown, already `undefined`
|
||||
}
|
||||
const name = names[values[i] - 1];
|
||||
const end = i + 1 < starts.length ? Math.min(starts[i + 1], length) : length;
|
||||
result.fill(name, starts[i], end);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* The intervals of the trackpoints that share a value, for a value per trackpoint (`undefined`
|
||||
* when unknown): the inverse of `categoryValues`. The names are in the order of their first
|
||||
* appearance. A value that is unknown everywhere gives no intervals.
|
||||
*/
|
||||
export function categoryIntervals(values: readonly (string | undefined)[]): CategoryIntervals {
|
||||
const starts: number[] = [];
|
||||
const codes: number[] = [];
|
||||
const names: string[] = [];
|
||||
const known = new Map<string, number>();
|
||||
let last: number | undefined = undefined;
|
||||
values.forEach((value, index) => {
|
||||
let code = 0;
|
||||
if (value !== undefined) {
|
||||
if (!known.has(value)) {
|
||||
names.push(value);
|
||||
known.set(value, names.length);
|
||||
}
|
||||
code = known.get(value)!;
|
||||
}
|
||||
if (code !== last) {
|
||||
starts.push(index);
|
||||
codes.push(code);
|
||||
last = code;
|
||||
}
|
||||
});
|
||||
if (names.length === 0) {
|
||||
return { starts: new Uint32Array(), values: new Uint8Array(), names };
|
||||
}
|
||||
return { starts: Uint32Array.from(starts), values: Uint8Array.from(codes), names };
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Readable } from 'svelte/store';
|
||||
import type { Feature, FeatureCollection, LineString, Point } from 'geojson';
|
||||
import type { Visibility } from '$lib/file-visibility';
|
||||
import type { CategoryIntervals } from '$lib/trackpoint-categories';
|
||||
import type { Visibility } from '$lib/engine/file-visibility';
|
||||
import type { CategoryIntervals } from '$lib/engine/trackpoint-categories';
|
||||
import type { FileStructure, GlobalStatistics } from 'gpx-rs';
|
||||
|
||||
export type {
|
||||
|
||||
Reference in new issue
Block a user