Skip to content

Commit

Permalink
Virtual Timeline elements
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed May 18, 2024
1 parent dd8ca5b commit 5a9e351
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/plugins/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
}

if (this.options.insertPosition) {
;(container.firstElementChild || container).insertAdjacentElement(
; (container.firstElementChild || container).insertAdjacentElement(

Check failure on line 85 in src/plugins/timeline.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/timeline.ts#L85

[prettier/prettier] Delete `·`
this.options.insertPosition,
this.timelineWrapper,
)
Expand Down Expand Up @@ -143,6 +143,28 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
return 2
}

private virtualAppend(start: number, container: HTMLElement, element: HTMLElement) {
const renderIfVisible = () => {
if (!this.wavesurfer) return
const clientWidth = this.wavesurfer.getWidth()
const scrollLeft = this.wavesurfer.getScroll()
const width = element.clientWidth
const isVisible = start + width > scrollLeft && start < scrollLeft + clientWidth

if (isVisible) {
container.appendChild(element)
} else {
element.remove()
}
}

setTimeout(() => {
if (!this.wavesurfer) return
renderIfVisible()
this.subscriptions.push(this.wavesurfer.on('scroll', renderIfVisible))
}, 0)
}

private initTimeline() {
const duration = this.wavesurfer?.getDuration() ?? this.options.duration ?? 0
const pxPerSec = this.timelineWrapper.scrollWidth / duration
Expand All @@ -161,15 +183,15 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
whiteSpace: 'nowrap',
...(isTop
? {
position: 'absolute',
top: '0',
left: '0',
right: '0',
zIndex: '2',
}
position: 'absolute',

Check failure on line 186 in src/plugins/timeline.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/timeline.ts#L186

[prettier/prettier] Insert `··`
top: '0',

Check failure on line 187 in src/plugins/timeline.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/timeline.ts#L187

[prettier/prettier] Insert `··`
left: '0',

Check failure on line 188 in src/plugins/timeline.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/timeline.ts#L188

[prettier/prettier] Insert `··`
right: '0',

Check failure on line 189 in src/plugins/timeline.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/timeline.ts#L189

[prettier/prettier] Insert `··`
zIndex: '2',

Check failure on line 190 in src/plugins/timeline.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/timeline.ts#L190

[prettier/prettier] Insert `··`
}

Check failure on line 191 in src/plugins/timeline.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/timeline.ts#L191

[prettier/prettier] Insert `··`
: {
position: 'relative',
}),
position: 'relative',

Check failure on line 193 in src/plugins/timeline.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/timeline.ts#L193

[prettier/prettier] Insert `··`
}),

Check failure on line 194 in src/plugins/timeline.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/timeline.ts#L194

[prettier/prettier] Insert `··`
},
})

Expand Down Expand Up @@ -217,8 +239,9 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
const mode = isPrimary ? 'primary' : isSecondary ? 'secondary' : 'tick'
notch.setAttribute('part', `timeline-notch timeline-notch-${mode}`)

notch.style.left = `${i * pxPerSec}px`
timeline.appendChild(notch)
const offset = i * pxPerSec
notch.style.left = `${offset}px`
this.virtualAppend(offset, timeline, notch)
}

this.timelineWrapper.innerHTML = ''
Expand Down

0 comments on commit 5a9e351

Please sign in to comment.