From 5465cfb444612ec87b33e40bdfdc7638e70b9c73 Mon Sep 17 00:00:00 2001 From: Terry Truong Date: Wed, 12 Oct 2022 14:48:49 +1100 Subject: Convert to represent dates instead of numbers - Add HistDate class - Adapt TimeLine and BaseLine to work with HistDates - For own-size-watching components, add onMounted() hook to run before ResizeObserver is set up --- .eslintrc.js | 1 + src/App.vue | 6 +- src/components/BaseLine.vue | 25 ++- src/components/TimeLine.vue | 382 ++++++++++++++++++++++++++++---------------- src/lib.ts | 213 +++++++++++++++++++++++- 5 files changed, 476 insertions(+), 151 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index abbda84..167024a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -22,6 +22,7 @@ module.exports = { ], "rules": { "@typescript-eslint/no-non-null-assertion": "off", + "@typescript-eslint/no-this-alias": "off", "no-constant-condition": "off", } } diff --git a/src/App.vue b/src/App.vue index f32b367..278ef63 100644 --- a/src/App.vue +++ b/src/App.vue @@ -38,7 +38,7 @@ import PlusIcon from './components/icon/PlusIcon.vue'; import SettingsIcon from './components/icon/SettingsIcon.vue'; import HelpIcon from './components/icon/HelpIcon.vue'; // Other -import {TimelineRange} from './lib'; +import {HistDate, TimelineRange} from './lib'; import {useStore} from './store'; // Refs @@ -62,11 +62,11 @@ onMounted(updateAreaDims) const timelineRanges: Ref = ref([]); let nextTimelineId = 1; function addNewTimelineRange(){ - timelineRanges.value.push({id: nextTimelineId, start: -500, end: 500}); + timelineRanges.value.push({id: nextTimelineId, start: new HistDate(1900, 1, 1), end: new HistDate(2000, 1, 1)}); nextTimelineId++; } addNewTimelineRange(); -function onRangeChg(newBounds: [number, number], idx: number){ +function onRangeChg(newBounds: [HistDate, HistDate], idx: number){ let range = timelineRanges.value[idx]; range.start = newBounds[0]; range.end = newBounds[1]; diff --git a/src/components/BaseLine.vue b/src/components/BaseLine.vue index 081225c..ccadb0b 100644 --- a/src/components/BaseLine.vue +++ b/src/components/BaseLine.vue @@ -4,7 +4,7 @@
{{p.label}}
- +
{{range.id}}
@@ -32,9 +32,11 @@ const props = defineProps({ // Static time periods type Period = {label: string, len: number}; const periods: Ref = ref([ - {label: 'One', len: 1}, - {label: 'Two', len: 2}, - {label: 'Three', len: 1}, + {label: 'Pre Hadean', len: 8}, + {label: 'Hadean', len: 1}, + {label: 'Archaean', len: 1.5}, + {label: 'Proterozoic', len: 2}, + {label: 'Phanerozoic', len: 0.5}, ]); // For skipping transitions on startup @@ -44,6 +46,13 @@ onMounted(() => setTimeout(() => {skipTransition.value = false}, 100)); // For size tracking (used to prevent time spans shrinking below 1 pixel) const width = ref(0); const height = ref(0); +const mounted = ref(false); +onMounted(() => { + let rootEl = rootRef.value!; + width.value = rootEl.offsetWidth; + height.value = rootEl.offsetHeight; + mounted.value = true; +}) const resizeObserver = new ResizeObserver((entries) => { for (const entry of entries){ if (entry.contentBoxSize){ @@ -71,11 +80,13 @@ const labelStyles = computed((): Record => ({ function spanStyles(range: TimelineRange){ let styles: Record; let availLen = props.vert ? height.value : width.value; - let startFrac = (range.start - MIN_DATE) / (MAX_DATE - MIN_DATE); - let lenFrac = (range.end - range.start) / (MAX_DATE - MIN_DATE); + // Determine positions in full timeline (only considers year values) + let startFrac = (range.start.year - MIN_DATE.year) / (MAX_DATE.year - MIN_DATE.year); + let lenFrac = (range.end.year - range.start.year) / (MAX_DATE.year - MIN_DATE.year); let startPx = Math.max(0, availLen * startFrac); // Prevent negatives due to end-padding let lenPx = Math.min(availLen - startPx, availLen * lenFrac); - lenPx = Math.max(1, lenPx); + lenPx = Math.max(1, lenPx); // Prevent zero length + // if (props.vert){ styles = { top: startPx + 'px', diff --git a/src/components/TimeLine.vue b/src/components/TimeLine.vue index bf886b1..7ed83d2 100644 --- a/src/components/TimeLine.vue +++ b/src/components/TimeLine.vue @@ -8,23 +8,26 @@ - - - {{Math.floor(n * 10) / 10}} - +