-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: [Regions] contenteditable
content
#3453
Changes from 4 commits
5320a23
7168d42
fa51ae2
cf7acd3
cab9ccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,4 @@ | ||||||||||||||||||||||
0 | ||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Regions are visual overlays on the waveform that can be used to mark segments of audio. | ||||||||||||||||||||||
* Regions can be clicked on, dragged and resized. | ||||||||||||||||||||||
|
@@ -60,6 +61,8 @@ export type RegionParams = { | |||||||||||||||||||||
maxLength?: number | ||||||||||||||||||||||
/** The index of the channel */ | ||||||||||||||||||||||
channelIdx?: number | ||||||||||||||||||||||
/** Allow/Disallow contenteditable property for content */ | ||||||||||||||||||||||
contentEditable?: boolean | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
class SingleRegion extends EventEmitter<RegionEvents> { | ||||||||||||||||||||||
|
@@ -74,6 +77,7 @@ class SingleRegion extends EventEmitter<RegionEvents> { | |||||||||||||||||||||
public minLength = 0 | ||||||||||||||||||||||
public maxLength = Infinity | ||||||||||||||||||||||
public channelIdx: number | ||||||||||||||||||||||
public contentEditable = false | ||||||||||||||||||||||
|
||||||||||||||||||||||
constructor(params: RegionParams, private totalDuration: number, private numberOfChannels = 0) { | ||||||||||||||||||||||
super() | ||||||||||||||||||||||
|
@@ -87,6 +91,7 @@ class SingleRegion extends EventEmitter<RegionEvents> { | |||||||||||||||||||||
this.minLength = params.minLength ?? this.minLength | ||||||||||||||||||||||
this.maxLength = params.maxLength ?? this.maxLength | ||||||||||||||||||||||
this.channelIdx = params.channelIdx ?? -1 | ||||||||||||||||||||||
this.contentEditable = params.contentEditable ?? this.contentEditable | ||||||||||||||||||||||
this.element = this.initElement() | ||||||||||||||||||||||
this.setContent(params.content) | ||||||||||||||||||||||
this.setPart() | ||||||||||||||||||||||
|
@@ -234,6 +239,13 @@ class SingleRegion extends EventEmitter<RegionEvents> { | |||||||||||||||||||||
this.drag && this.emit('update-end') | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (this.contentEditable && this.content) { | ||||||||||||||||||||||
this.content.addEventListener('click', (e) => this.onContentClick(e)) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
if (this.contentEditable && this.content) { | ||||||||||||||||||||||
this.content.addEventListener('blur', (e) => this.onContentBlur(e)) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public _onUpdate(dx: number, side?: 'start' | 'end') { | ||||||||||||||||||||||
|
@@ -274,6 +286,16 @@ class SingleRegion extends EventEmitter<RegionEvents> { | |||||||||||||||||||||
this.emit('update-end') | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
private onContentClick(event: MouseEvent) { | ||||||||||||||||||||||
event.stopPropagation() | ||||||||||||||||||||||
const contentContainer = event.target as HTMLDivElement | ||||||||||||||||||||||
contentContainer.focus() | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this necessary btw? Isn't focusing the default action on click on editable elements? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, but with the region container having an event listener for clicks, this (focus on text content) does not happen. The event is immediately propagated to its parent. This function is in hopes that we can step in and handle the content property before propagating the event further. In the latest commit, I have made changes to emit the private onContentClick(event: MouseEvent) {
event.stopPropagation()
const contentContainer = event.target as HTMLDivElement
contentContainer.focus()
this.emit('click', event)
} |
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public onContentBlur(event: FocusEvent) { | ||||||||||||||||||||||
this.emit('update-end') | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public _setTotalDuration(totalDuration: number) { | ||||||||||||||||||||||
this.totalDuration = totalDuration | ||||||||||||||||||||||
this.renderPosition() | ||||||||||||||||||||||
|
@@ -297,6 +319,9 @@ class SingleRegion extends EventEmitter<RegionEvents> { | |||||||||||||||||||||
this.content.style.padding = `0.2em ${isMarker ? 0.2 : 0.4}em` | ||||||||||||||||||||||
this.content.style.display = 'inline-block' | ||||||||||||||||||||||
this.content.textContent = content | ||||||||||||||||||||||
if (this.contentEditable) { | ||||||||||||||||||||||
this.content.contentEditable = 'true' | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
this.content = content | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nani?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no! That's embarrasing :D