Skip to content

Commit

Permalink
regions plugin: add regionsMinLength option (katspaugh#2009)
Browse files Browse the repository at this point in the history
* rebased fork

* added comment to minLength param
  • Loading branch information
marizuccara committed Aug 13, 2020
1 parent 615f46c commit 97ec876
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Next (unreleased)
- Improved and unified loop playback logic (#1868)
- Check `minLength` before resizing region (#2001)
- Dragging and resizing will continue outside canvas (#2006)
- `regionsMinLength` parameter to assign a min length to those regions for which the `minLength` is not specified (#2009)
- Revert PR #1926 click propagation on regions. Use event parameter passed in `region-click` if you need stopPropagation. (#2024)
- Edgescroll works for both edges (#2011)
- Microphone plugin: move to separate directory (#1997)
Expand Down
1 change: 1 addition & 0 deletions example/regions/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ document.addEventListener('DOMContentLoaded', function() {
backend: 'MediaElement',
plugins: [
WaveSurfer.regions.create({
regionsMinLength: 2,
regions: [
{
start: 1,
Expand Down
1 change: 1 addition & 0 deletions example/regions/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ <h1 itemprop="name">wavesurfer.js Regions</h1>
backend: 'MediaElement',
plugins: [
WaveSurfer.regions.create({
regionsMinLength: 2,
regions: [
{
start: 1,
Expand Down
11 changes: 11 additions & 0 deletions src/plugin/regions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export default class RegionsPlugin {
}
};
this.maxRegions = params.maxRegions;
this.regionsMinLength = params.regionsMinLength || null;

// turn the plugin instance into an observer
const observerPrototypeKeys = Object.getOwnPropertyNames(
Expand Down Expand Up @@ -187,6 +188,10 @@ export default class RegionsPlugin {
add(params) {
if (this.wouldExceedMaxRegions()) return null;

if (!params.minLength && this.regionsMinLength) {
params = {...params, minLength: this.regionsMinLength};
}

const region = new this.wavesurfer.Region(params, this.util, this.wavesurfer);

this.list[region.id] = region;
Expand Down Expand Up @@ -360,6 +365,12 @@ export default class RegionsPlugin {
this.wrapper.removeEventListener('touchmove', eventMove);
this.wrapper.removeEventListener('mousemove', eventMove);
});

this.wavesurfer.on('region-created', region => {
if (this.regionsMinLength) {
region.minLength = this.regionsMinLength;
}
});
}

disableDragSelection() {
Expand Down
1 change: 1 addition & 0 deletions src/plugin/regions/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class Region {
this.attributes = params.attributes || {};

this.maxLength = params.maxLength;
// It assumes the minLength parameter value, or the regionsMinLength parameter value, if the first one not provided
this.minLength = params.minLength;
this._onRedraw = () => this.updateRender();

Expand Down

0 comments on commit 97ec876

Please sign in to comment.