mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-11-30 09:34:29 +00:00
Migration to new Rust-based engine (#401)
* wasm first steps * parsing start * parsing continued * progress * progress * progress * progress * progress * progress * progress * progress * fixes * reorganize * change type * fix * progress * refactoring * progress * progress * progress * keep style info * progress * progress * remove (copy) in name * progress * merge methods into 1 * splice * progress * progress * progress * cleaning * progress * progress * progress * visibility * progress * progress * progress * progress * progress * fixes * progress * fix markers * fix * progress * progress * progress * progress * progress * progress * small fix * use engine info * fix popups * small fix * progress * progress * progress * clean tool * handle self closing elements * reduce data exchanges across boundary * copy on wasm side to avoid any data corruption * speed fix * routing * refactoring * reverse * reverse * crop * split * merge * progress * time tool * fix bounds for waypoints * reduce * fix reactivity * handle corrupt time data * export * progress * remove gpx js package * remove todo * update readme * cleaning * progress * fix link fields * remove unused fields * refactoring and tests * import and export of routes, ref #192 * per-chunk categories storage * cross-tab sync * outdated state * refactoring * reorder methods * refactoring * fix * improve flow * renaming * progress * progress * fix * progress * per file history * keep original flag * use history variant * fix * progress * file explorer * progress * fix * sticky folders * improved sticky folders * fix sortable * fix sortable * fix * fix tab size * no file delete on desktop * fix routing on other file * fix sorting * remember language * open links in browser * commit missing changes * avoid selecting on resize * clarify filename * explorer actions * fix folder styling * drop anywhere in folder * cleaning * fixes * remove focus-ring * full screen * small fix * consistent highlight color * improved preview * performance improvements * sparser data transfers * sparser data transfers * show popup once data is loaded * fix routing control size * fix * sparse desktop build * remove custom ign styles * use new endpoint for openhikingmap * strip native menu * reorder context menu items * route full support * route - track conversion * create route file action * route export, filter out anchors at high zoom level * clarify name handling * move hide & center to view * routes continued * update docs * fix power format, closes #378 * lint fixes * fix display of additional data with gaps, closes #361 * fix linting issues * fix import modules * fix multidrag * change base to resolve * fix type errors * fix layer typings * fix last type error * each keys * improve typings * deploy to cloudflare --------- Co-authored-by: vcoppe <vcoppe@hexaly.com>
This commit is contained in:
1 parent
6a0d434371
commit
8fe7d8d64d
484 files changed
+53949
-41152
No files matched your search
@@ -0,0 +1,352 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{File, FileId, Files, Route, Selection, State, Track, TrackSegment, Waypoint};
|
||||
|
||||
/// What an [`Editor`] hook did to the element it was given.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Edit {
|
||||
Unchanged,
|
||||
Changed,
|
||||
}
|
||||
|
||||
impl Edit {
|
||||
fn merge(self, other: Edit) -> Edit {
|
||||
if self == Edit::Changed || other == Edit::Changed {
|
||||
Edit::Changed
|
||||
} else {
|
||||
Edit::Unchanged
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Independent edit of each selected element, without needing the rest of the state.
|
||||
///
|
||||
/// A selected file, route, track or segment is passed to the hook of its own level. By default
|
||||
/// a file is forwarded to its routes and tracks, and a route or a track to its segments, so an
|
||||
/// editor only overrides the level(s) it cares about (metadata: file, route and track, style:
|
||||
/// route and track, reverse: segment). Waypoints are only edited when selected, never through
|
||||
/// their file.
|
||||
pub trait Editor {
|
||||
fn file(&mut self, file: &mut File) -> Edit {
|
||||
let routes = edit_each(&mut file.rte, |rte| self.route(rte));
|
||||
routes.merge(edit_each(&mut file.trk, |trk| self.track(trk)))
|
||||
}
|
||||
|
||||
fn route(&mut self, route: &mut Route) -> Edit {
|
||||
edit_segment(self, &mut route.seg)
|
||||
}
|
||||
|
||||
fn track(&mut self, track: &mut Track) -> Edit {
|
||||
edit_each(&mut track.trkseg, |seg| edit_segment(self, seg))
|
||||
}
|
||||
|
||||
fn segment(&mut self, _segment: &mut TrackSegment) -> Edit {
|
||||
Edit::Unchanged
|
||||
}
|
||||
|
||||
fn waypoint(&mut self, _waypoint: &mut Waypoint) -> Edit {
|
||||
Edit::Unchanged
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls the segment hook and gives the segment a new revision id if it changed, so that
|
||||
/// derived data (statistics) is recomputed.
|
||||
fn edit_segment<E: Editor + ?Sized>(editor: &mut E, segment: &mut TrackSegment) -> Edit {
|
||||
let edit = editor.segment(segment);
|
||||
if edit == Edit::Changed {
|
||||
segment.rev_id = Default::default();
|
||||
}
|
||||
edit
|
||||
}
|
||||
|
||||
fn edit_each<T>(items: &mut [T], f: impl FnMut(&mut T) -> Edit) -> Edit {
|
||||
edit_where(items, |_| true, f)
|
||||
}
|
||||
|
||||
fn edit_where<T>(
|
||||
items: &mut [T],
|
||||
filter: impl Fn(&T) -> bool,
|
||||
mut f: impl FnMut(&mut T) -> Edit,
|
||||
) -> Edit {
|
||||
items
|
||||
.iter_mut()
|
||||
.filter(|item| filter(item))
|
||||
.fold(Edit::Unchanged, |acc, item| acc.merge(f(item)))
|
||||
}
|
||||
|
||||
/// Edits a copy of the file (cheap, track points are shared chunks) and stores it back as a
|
||||
/// new snapshot only if something changed.
|
||||
fn update_file(files: &mut Files, id: FileId, f: impl FnOnce(&mut File) -> Edit) {
|
||||
let Some(current) = files.get(&id) else {
|
||||
return;
|
||||
};
|
||||
let mut file = (**current).clone();
|
||||
if f(&mut file) == Edit::Changed {
|
||||
files.insert(id, Rc::new(file));
|
||||
}
|
||||
}
|
||||
|
||||
fn edit_waypoints<E: Editor + ?Sized>(
|
||||
file: &mut File,
|
||||
filter: impl Fn(&Waypoint) -> bool,
|
||||
editor: &mut E,
|
||||
) -> Edit {
|
||||
let changed = file.wpt.edit(&filter, |wpts| {
|
||||
edit_where(wpts, &filter, |wpt| editor.waypoint(wpt)) == Edit::Changed
|
||||
});
|
||||
if changed {
|
||||
Edit::Changed
|
||||
} else {
|
||||
Edit::Unchanged
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies the editor to every selected file, route, track or segment.
|
||||
pub fn update_selected<E: Editor>(state: &mut State, editor: &mut E) {
|
||||
match &*state.selection {
|
||||
Selection::File { file_ids } => {
|
||||
for id in file_ids {
|
||||
update_file(state.files, *id, |file| editor.file(file));
|
||||
}
|
||||
}
|
||||
Selection::Route { file_id, rte_ids } => {
|
||||
update_file(state.files, *file_id, |file| {
|
||||
edit_where(
|
||||
&mut file.rte,
|
||||
|rte| rte_ids.contains(&rte.id),
|
||||
|rte| editor.route(rte),
|
||||
)
|
||||
});
|
||||
}
|
||||
Selection::Track { file_id, trk_ids } => {
|
||||
update_file(state.files, *file_id, |file| {
|
||||
edit_where(
|
||||
&mut file.trk,
|
||||
|trk| trk_ids.contains(&trk.id),
|
||||
|trk| editor.track(trk),
|
||||
)
|
||||
});
|
||||
}
|
||||
Selection::TrackSegment {
|
||||
file_id,
|
||||
trk_id,
|
||||
trkseg_ids,
|
||||
} => {
|
||||
update_file(state.files, *file_id, |file| {
|
||||
edit_where(
|
||||
&mut file.trk,
|
||||
|trk| trk.id == *trk_id,
|
||||
|trk| {
|
||||
edit_where(
|
||||
&mut trk.trkseg,
|
||||
|seg| trkseg_ids.contains(&seg.id),
|
||||
|seg| edit_segment(editor, seg),
|
||||
)
|
||||
},
|
||||
)
|
||||
});
|
||||
}
|
||||
Selection::Waypoints { file_id } => {
|
||||
update_file(state.files, *file_id, |file| {
|
||||
edit_waypoints(file, |_| true, editor)
|
||||
});
|
||||
}
|
||||
Selection::Waypoint { file_id, wpt_ids } => {
|
||||
update_file(state.files, *file_id, |file| {
|
||||
edit_waypoints(file, |wpt| wpt_ids.contains(&wpt.id), editor)
|
||||
});
|
||||
}
|
||||
Selection::Empty => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::engine::command::fixture::Fixture;
|
||||
|
||||
use super::*;
|
||||
|
||||
struct Rename;
|
||||
|
||||
impl Editor for Rename {
|
||||
fn file(&mut self, file: &mut File) -> Edit {
|
||||
file.info.name.push('!');
|
||||
Edit::Changed
|
||||
}
|
||||
|
||||
fn track(&mut self, track: &mut Track) -> Edit {
|
||||
track.info.name = Some("track".to_string());
|
||||
Edit::Changed
|
||||
}
|
||||
|
||||
fn segment(&mut self, _: &mut TrackSegment) -> Edit {
|
||||
Edit::Changed
|
||||
}
|
||||
}
|
||||
|
||||
struct Nothing;
|
||||
impl Editor for Nothing {}
|
||||
|
||||
fn fixture_with_tracks() -> (Fixture, FileId) {
|
||||
let mut fx = Fixture::default();
|
||||
let data = std::fs::read("data/with_tracks_and_segments.gpx").unwrap();
|
||||
let file = Rc::new(crate::parse(&data, &mut Default::default()).unwrap());
|
||||
let id = file.id;
|
||||
fx.files.insert(id, file);
|
||||
(fx, id)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_only_selected_files_are_updated() {
|
||||
let mut fx = Fixture::default();
|
||||
let (a, b, c) = (File::default(), File::default(), File::default());
|
||||
let ids = [a.id, b.id, c.id];
|
||||
for file in [a, b, c] {
|
||||
fx.files.insert(file.id, Rc::new(file));
|
||||
}
|
||||
let untouched = fx.files[&ids[2]].clone();
|
||||
fx.selection = Selection::File {
|
||||
file_ids: HashSet::from([ids[0], ids[1]]),
|
||||
};
|
||||
|
||||
update_selected(&mut fx.state(), &mut Rename);
|
||||
|
||||
assert_eq!(fx.files[&ids[0]].info.name, "!");
|
||||
assert_eq!(fx.files[&ids[1]].info.name, "!");
|
||||
assert!(Rc::ptr_eq(&fx.files[&ids[2]], &untouched));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unchanged_file_keeps_its_snapshot() {
|
||||
let (mut fx, id) = fixture_with_tracks();
|
||||
fx.selection = Selection::File {
|
||||
file_ids: HashSet::from([id]),
|
||||
};
|
||||
let before = fx.files[&id].clone();
|
||||
update_selected(&mut fx.state(), &mut Nothing);
|
||||
assert!(Rc::ptr_eq(&fx.files[&id], &before));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_track_selection_uses_track_hook() {
|
||||
let (mut fx, id) = fixture_with_tracks();
|
||||
let trk_id = fx.files[&id].trk[0].id;
|
||||
fx.selection = Selection::Track {
|
||||
file_id: id,
|
||||
trk_ids: HashSet::from([trk_id]),
|
||||
};
|
||||
update_selected(&mut fx.state(), &mut Rename);
|
||||
|
||||
let file = &fx.files[&id];
|
||||
assert_eq!(file.info.name, "with_tracks_and_segments");
|
||||
assert_eq!(file.trk[0].info.name.as_deref(), Some("track"));
|
||||
assert_ne!(file.trk[1].info.name.as_deref(), Some("track"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_segment_selection_bumps_revision_id() {
|
||||
let (mut fx, id) = fixture_with_tracks();
|
||||
let before = fx.files[&id].clone();
|
||||
let trk = &before.trk[0];
|
||||
fx.selection = Selection::TrackSegment {
|
||||
file_id: id,
|
||||
trk_id: trk.id,
|
||||
trkseg_ids: HashSet::from([trk.trkseg[0].id]),
|
||||
};
|
||||
update_selected(&mut fx.state(), &mut Rename);
|
||||
|
||||
let after = &fx.files[&id];
|
||||
assert_ne!(after.trk[0].trkseg[0].rev_id, trk.trkseg[0].rev_id);
|
||||
assert_eq!(after.trk[0].trkseg[1].rev_id, trk.trkseg[1].rev_id);
|
||||
assert_eq!(
|
||||
after.trk[1].trkseg[0].rev_id,
|
||||
before.trk[1].trkseg[0].rev_id
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_file_selection_cascades_to_segments() {
|
||||
struct SegmentsOnly;
|
||||
impl Editor for SegmentsOnly {
|
||||
fn segment(&mut self, _: &mut TrackSegment) -> Edit {
|
||||
Edit::Changed
|
||||
}
|
||||
}
|
||||
let (mut fx, id) = fixture_with_tracks();
|
||||
let before = fx.files[&id].clone();
|
||||
fx.selection = Selection::File {
|
||||
file_ids: HashSet::from([id]),
|
||||
};
|
||||
update_selected(&mut fx.state(), &mut SegmentsOnly);
|
||||
for (b, a) in before.trk.iter().zip(fx.files[&id].trk.iter()) {
|
||||
for (b, a) in b.trkseg.iter().zip(a.trkseg.iter()) {
|
||||
assert_ne!(b.rev_id, a.rev_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_waypoint_selection_edits_only_selected_waypoints() {
|
||||
struct Name;
|
||||
impl Editor for Name {
|
||||
fn waypoint(&mut self, wpt: &mut Waypoint) -> Edit {
|
||||
wpt.name = Some("x".into());
|
||||
Edit::Changed
|
||||
}
|
||||
}
|
||||
let (mut fx, id) = fixture_with_tracks();
|
||||
let wpts: Vec<_> = (0..3).map(|_| Waypoint::default()).collect();
|
||||
let ids: Vec<_> = wpts.iter().map(|w| w.id).collect();
|
||||
let mut file = (*fx.files[&id]).clone();
|
||||
file.wpt = crate::Waypoints::new([crate::WaypointChunk {
|
||||
wpt: wpts,
|
||||
..Default::default()
|
||||
}]);
|
||||
fx.files.insert(id, Rc::new(file));
|
||||
let chunk_id = fx.files[&id].wpt.chunks()[0].id;
|
||||
|
||||
// A file selection never touches waypoints.
|
||||
fx.selection = Selection::File {
|
||||
file_ids: HashSet::from([id]),
|
||||
};
|
||||
update_selected(&mut fx.state(), &mut Name);
|
||||
assert!(fx.files[&id].wpt.iter().all(|w| w.name.is_none()));
|
||||
|
||||
fx.selection = Selection::Waypoint {
|
||||
file_id: id,
|
||||
wpt_ids: HashSet::from([ids[1]]),
|
||||
};
|
||||
let rev = fx.files[&id].wpt.rev_id;
|
||||
update_selected(&mut fx.state(), &mut Name);
|
||||
assert_ne!(fx.files[&id].wpt.rev_id, rev);
|
||||
assert_ne!(fx.files[&id].wpt.chunks()[0].id, chunk_id);
|
||||
let named: Vec<_> = fx.files[&id].wpt.iter().map(|w| w.name.is_some()).collect();
|
||||
assert_eq!(named, [false, true, false]);
|
||||
|
||||
fx.selection = Selection::Waypoints { file_id: id };
|
||||
update_selected(&mut fx.state(), &mut Name);
|
||||
assert!(fx.files[&id].wpt.iter().all(|w| w.name.is_some()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_missing_files_and_other_selections_are_ignored() {
|
||||
let mut fx = Fixture::default();
|
||||
let file = File::default();
|
||||
let id = file.id;
|
||||
fx.files.insert(id, Rc::new(file));
|
||||
let before = fx.files[&id].clone();
|
||||
|
||||
for selection in [
|
||||
Selection::File {
|
||||
file_ids: HashSet::from([FileId::default()]),
|
||||
},
|
||||
Selection::Empty,
|
||||
] {
|
||||
fx.selection = selection;
|
||||
update_selected(&mut fx.state(), &mut Rename);
|
||||
assert!(Rc::ptr_eq(&fx.files[&id], &before));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user