@checksub_team/peaks_timeline 2.4.0 → 2.5.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/peaks.js CHANGED
@@ -15410,6 +15410,12 @@ module.exports = function (SourceGroup, Utils, Konva) {
15410
15410
  }
15411
15411
  return sources;
15412
15412
  };
15413
+ LineGroup.prototype.getSegmentsAfter = function (time) {
15414
+ if (!this.isSegmentsLine()) {
15415
+ return [];
15416
+ }
15417
+ return this._segmentsGroup.getSegmentsAfter(time);
15418
+ };
15413
15419
  LineGroup.prototype.getSourcesAround = function (time) {
15414
15420
  var left = null;
15415
15421
  var right = null;
@@ -15571,8 +15577,12 @@ module.exports = function (SegmentsGroup, LineGroup, LineIndicator, Utils) {
15571
15577
  LineGroups.prototype._onSegmentsUpdate = function (segment) {
15572
15578
  this._segmentsGroups[segment.line].onSegmentsUpdate(segment);
15573
15579
  };
15574
- LineGroups.prototype._onSegmentUpdated = function (segment) {
15575
- this._segmentsGroups[segment.line].onSegmentUpdated();
15580
+ LineGroups.prototype._onSegmentUpdated = function () {
15581
+ for (var lineId in this._segmentsGroups) {
15582
+ if (Utils.objectHasProperty(this._segmentsGroups, lineId) && this._segmentsGroups[lineId].hasUpdatedSegments()) {
15583
+ this._segmentsGroups[lineId].onSegmentUpdated();
15584
+ }
15585
+ }
15576
15586
  };
15577
15587
  LineGroups.prototype._onSegmentsRemove = function (segments) {
15578
15588
  var self = this;
@@ -15654,6 +15664,13 @@ module.exports = function (SegmentsGroup, LineGroup, LineIndicator, Utils) {
15654
15664
  LineGroups.prototype.getSourcesOnLineAfter = function (lineId, time) {
15655
15665
  return this._lineGroupsById[lineId].getSourcesAfter(time);
15656
15666
  };
15667
+ LineGroups.prototype.getSegmentsOnLineAfter = function (lineId, time) {
15668
+ const lineGroup = this._lineGroupsById[lineId] || this._segmentsGroupToLine[lineId];
15669
+ if (!lineGroup) {
15670
+ return [];
15671
+ }
15672
+ return lineGroup.getSegmentsAfter(time);
15673
+ };
15657
15674
  LineGroups.prototype.getSegmentsGroups = function () {
15658
15675
  return this._segmentsGroups;
15659
15676
  };
@@ -16587,11 +16604,8 @@ module.exports = function (SourceGroup, Invoker, Source, Utils, Konva) {
16587
16604
  ModeLayer.prototype._onMouseClickInDefaultMode = function () {
16588
16605
  const hoveredKonvaElement = this._view.getHoveredElement();
16589
16606
  if (hoveredKonvaElement) {
16590
- if (hoveredKonvaElement instanceof SourceGroup) {
16591
- this.selectElements([hoveredKonvaElement.getSource()], true);
16592
- } else {
16593
- this.selectElements([hoveredKonvaElement.getSegment()], true);
16594
- }
16607
+ const element = hoveredKonvaElement instanceof SourceGroup ? hoveredKonvaElement.getSource() : hoveredKonvaElement.getSegment();
16608
+ this.selectElements([element], true);
16595
16609
  } else {
16596
16610
  this.deselectDifference([], true);
16597
16611
  this._view.batchDrawSourcesLayer();
@@ -17175,28 +17189,8 @@ module.exports = function (Konva) {
17175
17189
  });
17176
17190
  }
17177
17191
  };
17178
- SegmentMarker.prototype._dragBoundFunc = function (pos) {
17179
- if (this._startMarker) {
17180
- var newStartX;
17181
- if (this._segment.duration) {
17182
- newStartX = Math.max(pos.x + this._view.getFrameOffset(), this._view.timeToPixels(this._segment.endTime - this._segment.duration));
17183
- } else {
17184
- newStartX = pos.x + this._view.getFrameOffset();
17185
- }
17186
- this._segmentShape.getSegmentsGroup().updateSegment(this._segment, newStartX, null);
17187
- } else {
17188
- var newEndX;
17189
- if (this._segment.duration) {
17190
- newEndX = Math.min(pos.x + this._view.getFrameOffset() + this._marker.getHandleWidth(), this._view.timeToPixels(this._segment.startTime + this._segment.duration));
17191
- } else {
17192
- newEndX = pos.x + this._view.getFrameOffset() + this._marker.getHandleWidth();
17193
- }
17194
- this._segmentShape.getSegmentsGroup().updateSegment(this._segment, null, newEndX);
17195
- }
17196
- return {
17197
- x: this._group.getAbsolutePosition().x,
17198
- y: this._group.getAbsolutePosition().y
17199
- };
17192
+ SegmentMarker.prototype._dragBoundFunc = function () {
17193
+ return this._view._sourcesLayer.onSegmentHandleDrag(this);
17200
17194
  };
17201
17195
  SegmentMarker.prototype.moveTo = function (group) {
17202
17196
  this._group.moveTo(group);
@@ -17207,6 +17201,9 @@ module.exports = function (Konva) {
17207
17201
  SegmentMarker.prototype.getX = function () {
17208
17202
  return this._group.getX();
17209
17203
  };
17204
+ SegmentMarker.prototype.getAbsolutePosition = function () {
17205
+ return this._group.getAbsolutePosition();
17206
+ };
17210
17207
  SegmentMarker.prototype.getWidth = function () {
17211
17208
  return this._group.getWidth();
17212
17209
  };
@@ -17222,6 +17219,9 @@ module.exports = function (Konva) {
17222
17219
  SegmentMarker.prototype.setX = function (x) {
17223
17220
  this._group.setX(x);
17224
17221
  };
17222
+ SegmentMarker.prototype.stopDrag = function () {
17223
+ this._group.stopDrag();
17224
+ };
17225
17225
  SegmentMarker.prototype.timeUpdated = function (time) {
17226
17226
  if (this._marker.timeUpdated) {
17227
17227
  this._marker.timeUpdated(time);
@@ -17313,21 +17313,17 @@ module.exports = function (Konva, SegmentMarker, Utils) {
17313
17313
  this.createIndicators();
17314
17314
  }
17315
17315
  SegmentShape.prototype._onShapeGroupDrag = function (draggedElement) {
17316
- const diff = this._view.getPointerPosition().x - this._mouseDownX;
17317
- this._group.updateSegment(this._segment, this._initialStartPixel + diff, this._initialEndPixel + diff);
17318
- this._startMarker.timeUpdated(this._segment.startTime);
17319
- this._endMarker.timeUpdated(this._segment.endTime);
17320
- return {
17321
- x: draggedElement.absolutePosition().x,
17322
- y: draggedElement.absolutePosition().y
17323
- };
17316
+ return this._view._sourcesLayer.onSegmentDrag(draggedElement);
17324
17317
  };
17325
17318
  SegmentShape.prototype._cornerRadius = function () {
17326
17319
  return !Utils.isNullOrUndefined(this._segment.borderRadius) ? this._segment.borderRadius : SEGMENT_CORNER_RADIUS;
17327
17320
  };
17328
17321
  SegmentShape.prototype.update = function () {
17329
- var startPixel = this._view.timeToPixels(this._segment.startTime);
17330
- var endPixel = this._view.timeToPixels(this._segment.endTime);
17322
+ this._applyDisplayTimes(this._segment.startTime, this._segment.endTime);
17323
+ };
17324
+ SegmentShape.prototype._applyDisplayTimes = function (startTime, endTime) {
17325
+ var startPixel = this._view.timeToPixels(startTime);
17326
+ var endPixel = this._view.timeToPixels(endTime);
17331
17327
  var frameOffset = this._view.timeToPixels(this._view.getTimeOffset());
17332
17328
  this._shapeGroup.x(startPixel - frameOffset);
17333
17329
  this._segmentWidth = endPixel - startPixel;
@@ -17351,11 +17347,11 @@ module.exports = function (Konva, SegmentMarker, Utils) {
17351
17347
  }
17352
17348
  if (this._startMarker) {
17353
17349
  this._startMarker.setX(startPixel - frameOffset);
17354
- this._startMarker.timeUpdated(this._segment.startTime);
17350
+ this._startMarker.timeUpdated(startTime);
17355
17351
  }
17356
17352
  if (this._endMarker) {
17357
17353
  this._endMarker.setX(endPixel - this._endMarker.getHandleWidth() - frameOffset);
17358
- this._endMarker.timeUpdated(this._segment.endTime);
17354
+ this._endMarker.timeUpdated(endTime);
17359
17355
  }
17360
17356
  if (this._label) {
17361
17357
  this._label.setX(startPixel - frameOffset);
@@ -17380,6 +17376,9 @@ module.exports = function (Konva, SegmentMarker, Utils) {
17380
17376
  SegmentShape.prototype.getEndMarker = function () {
17381
17377
  return this._endMarker;
17382
17378
  };
17379
+ SegmentShape.prototype.stopDrag = function () {
17380
+ this._shapeGroup.stopDrag();
17381
+ };
17383
17382
  SegmentShape.prototype.moveTo = function (group, segmentsGroup) {
17384
17383
  if (segmentsGroup) {
17385
17384
  this._segmentsGroup = segmentsGroup;
@@ -17529,27 +17528,20 @@ module.exports = function (Konva, SegmentMarker, Utils) {
17529
17528
  };
17530
17529
  SegmentShape.prototype._onSegmentDragStart = function () {
17531
17530
  this._view.setCursor('grab');
17532
- this._mouseDownX = this._view.getPointerPosition().x;
17533
- this._initialStartTime = this._segment.startTime;
17534
- this._initialStartPixel = this._view.timeToPixels(this._initialStartTime);
17535
- this._initialEndTime = this._segment.endTime;
17536
- this._initialEndPixel = this._view.timeToPixels(this._initialEndTime);
17537
- this._peaks.emit('segments.dragstart', this._segment);
17531
+ this._view._sourcesLayer.onSegmentDragStart(this);
17538
17532
  };
17539
17533
  SegmentShape.prototype._onSegmentDragEnd = function () {
17540
17534
  this._view.setCursor('pointer');
17541
- this._peaks.emit('segments.dragend', this._segment);
17535
+ this._view._sourcesLayer.onSegmentDragEnd(this);
17542
17536
  };
17543
17537
  SegmentShape.prototype._onSegmentHandleDrag = function () {
17544
17538
  this._peaks.emit('segments.dragged');
17545
17539
  };
17546
17540
  SegmentShape.prototype._onSegmentHandleDragStart = function (segmentMarker) {
17547
- var startMarker = segmentMarker.isStartMarker();
17548
- this._peaks.emit('segments.dragstart', this._segment, startMarker);
17541
+ this._view._sourcesLayer.onSegmentHandleDragStart(segmentMarker);
17549
17542
  };
17550
17543
  SegmentShape.prototype._onSegmentHandleDragEnd = function (segmentMarker) {
17551
- var startMarker = segmentMarker.isStartMarker();
17552
- this._peaks.emit('segments.dragend', this._segment, startMarker);
17544
+ this._view._sourcesLayer.onSegmentHandleDragEnd(segmentMarker);
17553
17545
  };
17554
17546
  SegmentShape.prototype.fitToView = function () {
17555
17547
  if (this._startMarker) {
@@ -17835,6 +17827,23 @@ module.exports = function (SegmentShape, Utils, Konva) {
17835
17827
  }
17836
17828
  return visibleSegments;
17837
17829
  };
17830
+ SegmentsGroup.prototype.getSegmentsAfter = function (time) {
17831
+ const segments = [];
17832
+ var currentId = this._firstSegmentId;
17833
+ while (currentId) {
17834
+ var segmentData = this._segments[currentId];
17835
+ if (segmentData.segment.startTime >= time) {
17836
+ while (currentId) {
17837
+ segmentData = this._segments[currentId];
17838
+ segments.push(segmentData.segment);
17839
+ currentId = segmentData.nextSegmentId;
17840
+ }
17841
+ break;
17842
+ }
17843
+ currentId = segmentData.nextSegmentId;
17844
+ }
17845
+ return segments;
17846
+ };
17838
17847
  SegmentsGroup.prototype._draw = function () {
17839
17848
  this._view.batchDrawSourcesLayer();
17840
17849
  };
@@ -17904,6 +17913,9 @@ module.exports = function (SegmentShape, Utils, Konva) {
17904
17913
  SegmentsGroup.prototype.isMagnetized = function () {
17905
17914
  return this._isMagnetized;
17906
17915
  };
17916
+ SegmentsGroup.prototype.markSegmentsUpdated = function (segments) {
17917
+ segments.forEach(this.addToUpdatedSegments.bind(this));
17918
+ };
17907
17919
  SegmentsGroup.prototype.setIndicators = function (segment) {
17908
17920
  var segmentShape = this._segmentShapes[segment.id];
17909
17921
  if (segmentShape) {
@@ -17916,18 +17928,86 @@ module.exports = function (SegmentShape, Utils, Konva) {
17916
17928
  this._updatedSegments.push(segment);
17917
17929
  }
17918
17930
  };
17919
- SegmentsGroup.prototype.updateSegment = function (segment, newStartX, newEndX) {
17931
+ SegmentsGroup.prototype.hasUpdatedSegments = function () {
17932
+ return this._updatedSegments.length > 0;
17933
+ };
17934
+ SegmentsGroup.prototype.getSegmentsDragTimeLimits = function (segments, initialPositions) {
17935
+ var selectedIds = {};
17936
+ var minTimeOffset = -Infinity;
17937
+ var maxTimeOffset = Infinity;
17938
+ segments.forEach(function (segment) {
17939
+ selectedIds[segment.id] = true;
17940
+ });
17941
+ segments.forEach(function (segment) {
17942
+ var segmentData = this._segments[segment.id];
17943
+ var initialPosition = initialPositions[segment.id];
17944
+ var previousSegmentId = segmentData.prevSegmentId;
17945
+ var nextSegmentId = segmentData.nextSegmentId;
17946
+ while (previousSegmentId && selectedIds[previousSegmentId]) {
17947
+ previousSegmentId = this._segments[previousSegmentId].prevSegmentId;
17948
+ }
17949
+ while (nextSegmentId && selectedIds[nextSegmentId]) {
17950
+ nextSegmentId = this._segments[nextSegmentId].nextSegmentId;
17951
+ }
17952
+ if (previousSegmentId) {
17953
+ minTimeOffset = Math.max(minTimeOffset, this._segments[previousSegmentId].segment.endTime - initialPosition.startTime);
17954
+ } else {
17955
+ minTimeOffset = Math.max(minTimeOffset, -initialPosition.startTime);
17956
+ }
17957
+ if (nextSegmentId) {
17958
+ maxTimeOffset = Math.min(maxTimeOffset, this._segments[nextSegmentId].segment.startTime - initialPosition.endTime);
17959
+ }
17960
+ }.bind(this));
17961
+ return {
17962
+ minTimeOffset: minTimeOffset,
17963
+ maxTimeOffset: maxTimeOffset
17964
+ };
17965
+ };
17966
+ SegmentsGroup.prototype.dragSegmentsByTimeOffset = function (segments, initialPositions, timeOffset) {
17967
+ var hasChanges = false;
17968
+ segments.forEach(function (segment) {
17969
+ var initialPosition = initialPositions[segment.id];
17970
+ var newStartTime = Utils.roundTime(initialPosition.startTime + timeOffset);
17971
+ var newEndTime = Utils.roundTime(initialPosition.endTime + timeOffset);
17972
+ if (segment.startTime !== newStartTime || segment.endTime !== newEndTime) {
17973
+ segment.updateTimes(newStartTime, newEndTime);
17974
+ hasChanges = true;
17975
+ }
17976
+ });
17977
+ if (hasChanges) {
17978
+ this._updateVisibleSegmentsInView();
17979
+ }
17980
+ };
17981
+ SegmentsGroup.prototype._getVisibleTimeRange = function () {
17982
+ var frameOffset = this._view.getFrameOffset();
17983
+ var width = this._view.getWidth();
17984
+ return {
17985
+ startTime: this._view.pixelsToTime(frameOffset),
17986
+ endTime: this._view.pixelsToTime(frameOffset + width)
17987
+ };
17988
+ };
17989
+ SegmentsGroup.prototype._updateVisibleSegmentsInView = function () {
17990
+ var visibleTimeRange = this._getVisibleTimeRange();
17991
+ this.find(visibleTimeRange.startTime, visibleTimeRange.endTime).forEach(this._updateSegment.bind(this));
17992
+ this._removeInvisibleSegments(visibleTimeRange.startTime, visibleTimeRange.endTime);
17993
+ };
17994
+ SegmentsGroup.prototype.updateSegment = function (segment, newStartX, newEndX, shouldDraw) {
17920
17995
  var newXs = this.manageCollision(segment, newStartX, newEndX);
17996
+ if (Utils.isNullOrUndefined(shouldDraw)) {
17997
+ shouldDraw = true;
17998
+ }
17921
17999
  if (!Utils.isNullOrUndefined(newXs.startX)) {
17922
- segment.startTime = this._view.pixelsToTime(newXs.startX);
18000
+ segment.updateTimes(this._view.pixelsToTime(newXs.startX), null);
17923
18001
  }
17924
18002
  if (!Utils.isNullOrUndefined(newXs.endX)) {
17925
- segment.endTime = this._view.pixelsToTime(newXs.endX);
18003
+ segment.updateTimes(null, this._view.pixelsToTime(newXs.endX));
17926
18004
  }
17927
18005
  if (newXs) {
17928
18006
  this._updateSegment(segment);
17929
18007
  this.addToUpdatedSegments(segment);
17930
- this._draw();
18008
+ if (shouldDraw) {
18009
+ this._draw();
18010
+ }
17931
18011
  }
17932
18012
  };
17933
18013
  SegmentsGroup.prototype.manageCollision = function (segment, newStartX, newEndX) {
@@ -18303,20 +18383,7 @@ module.exports = function (WaveformBuilder, WaveformShape, Loader, Invoker, Util
18303
18383
  this._rightHandle.x(this._width - handleWidth);
18304
18384
  };
18305
18385
  SourceGroup.prototype._onSourceGroupHandleDrag = function (draggedElement, leftHandle) {
18306
- const {start, end} = this._initialTimes;
18307
- this._view.updateWithAutoScroll(function () {
18308
- var pointer = this._view.getPointerPosition();
18309
- var pointerX = pointer ? pointer.x : this._mouseDownX;
18310
- const diff = this._view.pixelsToTime(pointerX - this._mouseDownX);
18311
- const timeOffsetDiff = this._view.getTimeOffset() - this._initialTimeOffset;
18312
- if (this._layer.manageSourceMovements([this._source], leftHandle ? start + diff + timeOffsetDiff : null, leftHandle ? null : end + diff + timeOffsetDiff)) {
18313
- this._layer.batchDraw();
18314
- }
18315
- }.bind(this), null, false);
18316
- return {
18317
- x: draggedElement.absolutePosition().x,
18318
- y: draggedElement.absolutePosition().y
18319
- };
18386
+ return this._layer.onSourceHandleDrag(draggedElement, leftHandle);
18320
18387
  };
18321
18388
  SourceGroup.prototype.update = function () {
18322
18389
  const startPixel = this._view.timeToPixels(this._source.startTime);
@@ -18376,20 +18443,15 @@ module.exports = function (WaveformBuilder, WaveformShape, Loader, Invoker, Util
18376
18443
  this._rightHandle.height(this._unwrappedHeight);
18377
18444
  }
18378
18445
  };
18379
- SourceGroup.prototype._onHandleDragStart = function () {
18380
- this._initialTimeOffset = this._view.getTimeOffset();
18381
- this._mouseDownX = this._view.getPointerPosition().x;
18382
- this._initialTimes = {
18383
- start: this._source.startTime,
18384
- end: this._source.endTime
18385
- };
18446
+ SourceGroup.prototype._onHandleDragStart = function (leftHandle) {
18386
18447
  this._isHandleDragged = true;
18387
18448
  this._hideButtons();
18449
+ this._layer.onSourceHandleDragStart(this, leftHandle);
18388
18450
  };
18389
18451
  SourceGroup.prototype._onHandleDragEnd = function () {
18390
18452
  this._isHandleDragged = false;
18391
18453
  this._showButtons();
18392
- this._layer.processSourceUpdates([this._source]);
18454
+ this._layer.onSourceHandleDragEnd(this);
18393
18455
  };
18394
18456
  SourceGroup.prototype._addHandles = function (forceCreate) {
18395
18457
  var self = this;
@@ -18407,7 +18469,7 @@ module.exports = function (WaveformBuilder, WaveformShape, Loader, Invoker, Util
18407
18469
  });
18408
18470
  this._leftHandle.on('dragstart', function (event) {
18409
18471
  event.cancelBubble = true;
18410
- self._onHandleDragStart(event);
18472
+ self._onHandleDragStart(true);
18411
18473
  });
18412
18474
  this._leftHandle.on('dragend', function (event) {
18413
18475
  event.cancelBubble = true;
@@ -18435,7 +18497,7 @@ module.exports = function (WaveformBuilder, WaveformShape, Loader, Invoker, Util
18435
18497
  });
18436
18498
  this._rightHandle.on('dragstart', function (event) {
18437
18499
  event.cancelBubble = true;
18438
- self._onHandleDragStart(event);
18500
+ self._onHandleDragStart(false);
18439
18501
  });
18440
18502
  this._rightHandle.on('dragend', function (event) {
18441
18503
  event.cancelBubble = true;
@@ -19877,6 +19939,8 @@ module.exports = function (SourceGroup, LineGroups, DataRetriever, Invoker, Util
19877
19939
  this._throttledBatchDraw = this._invoker.throttleTrailing(this._layer.batchDraw.bind(this._layer));
19878
19940
  this._drawPending = false;
19879
19941
  this._suppressDragLifecycleForSourceId = null;
19942
+ this._sourceHandleDragSession = null;
19943
+ this._segmentDragSession = null;
19880
19944
  this._peaks.on('handler.sources.add', this._onSourcesAdd.bind(this));
19881
19945
  this._peaks.on('handler.sources.destroy', this._onSourcesDestroy.bind(this));
19882
19946
  this._peaks.on('handler.sources.show', this._onSourcesShow.bind(this));
@@ -19899,6 +19963,9 @@ module.exports = function (SourceGroup, LineGroups, DataRetriever, Invoker, Util
19899
19963
  if (draggedSourceGroup) {
19900
19964
  draggedSourceGroup.stopDrag();
19901
19965
  }
19966
+ if (this._segmentDragSession && this._segmentDragSession.draggedNode) {
19967
+ this._segmentDragSession.draggedNode.stopDrag();
19968
+ }
19902
19969
  };
19903
19970
  SourcesLayer.prototype.fitToView = function () {
19904
19971
  this._lineGroups.fitToView();
@@ -19912,6 +19979,9 @@ module.exports = function (SourceGroup, LineGroups, DataRetriever, Invoker, Util
19912
19979
  SourcesLayer.prototype.getSegmentsGroups = function () {
19913
19980
  return this._lineGroups.getSegmentsGroups();
19914
19981
  };
19982
+ SourcesLayer.prototype.getSegmentsOnLineAfter = function (lineId, time) {
19983
+ return this._lineGroups.getSegmentsOnLineAfter(lineId, time);
19984
+ };
19915
19985
  SourcesLayer.prototype.add = function (element) {
19916
19986
  this._layer.add(element);
19917
19987
  if (this._dragGroup) {
@@ -20112,6 +20182,244 @@ module.exports = function (SourceGroup, LineGroups, DataRetriever, Invoker, Util
20112
20182
  this._draggedElementId = null;
20113
20183
  this._draggedElementsData = null;
20114
20184
  };
20185
+ SourcesLayer.prototype.cleanupAfterSourceHandleDrag = function () {
20186
+ this._sourceHandleDragSession = null;
20187
+ };
20188
+ SourcesLayer.prototype.onSourceHandleDragStart = function (sourceGroup, leftHandle) {
20189
+ var source = sourceGroup.getSource();
20190
+ this._sourceHandleDragSession = {
20191
+ source: source,
20192
+ leftHandle: leftHandle,
20193
+ mouseDownX: this._view.getPointerPosition().x,
20194
+ initialTimeOffset: this._view.getTimeOffset(),
20195
+ initialStartTime: source.startTime,
20196
+ initialEndTime: source.endTime
20197
+ };
20198
+ };
20199
+ SourcesLayer.prototype.onSourceHandleDrag = function (draggedElement) {
20200
+ this._view.updateWithAutoScroll(this._dragSourceHandle.bind(this), null, false);
20201
+ return {
20202
+ x: draggedElement.absolutePosition().x,
20203
+ y: draggedElement.absolutePosition().y
20204
+ };
20205
+ };
20206
+ SourcesLayer.prototype._dragSourceHandle = function () {
20207
+ var session = this._sourceHandleDragSession;
20208
+ var pointer = this._view.getPointerPosition();
20209
+ var pointerX;
20210
+ var diff;
20211
+ var timeOffsetDiff;
20212
+ var shouldRedraw;
20213
+ if (!session) {
20214
+ return;
20215
+ }
20216
+ pointerX = pointer ? pointer.x : session.mouseDownX;
20217
+ diff = this._view.pixelsToTime(pointerX - session.mouseDownX);
20218
+ timeOffsetDiff = this._view.getTimeOffset() - session.initialTimeOffset;
20219
+ shouldRedraw = this.manageSourceMovements([session.source], session.leftHandle ? session.initialStartTime + diff + timeOffsetDiff : null, session.leftHandle ? null : session.initialEndTime + diff + timeOffsetDiff);
20220
+ if (shouldRedraw) {
20221
+ this.batchDraw();
20222
+ }
20223
+ };
20224
+ SourcesLayer.prototype.onSourceHandleDragEnd = function (sourceGroup) {
20225
+ this.cleanupAfterSourceHandleDrag();
20226
+ this.processSourceUpdates([sourceGroup.getSource()]);
20227
+ };
20228
+ SourcesLayer.prototype.isSegmentDragInProgress = function () {
20229
+ return Boolean(this._segmentDragSession);
20230
+ };
20231
+ SourcesLayer.prototype.cleanupAfterSegmentDrag = function () {
20232
+ this._segmentDragSession = null;
20233
+ };
20234
+ SourcesLayer.prototype._getSelectedSegmentsForDrag = function (draggedSegment) {
20235
+ var selectedElements = this._view.getSelectedElements();
20236
+ var selectedSegments;
20237
+ var draggedSegmentIsSelected;
20238
+ if (!Array.isArray(selectedElements)) {
20239
+ selectedElements = Object.values(selectedElements);
20240
+ }
20241
+ selectedSegments = selectedElements.filter(function (element) {
20242
+ return !Utils.isNullOrUndefined(element.line) && Utils.isNullOrUndefined(element.lineId);
20243
+ });
20244
+ draggedSegmentIsSelected = selectedSegments.some(function (segment) {
20245
+ return segment.id === draggedSegment.id;
20246
+ });
20247
+ if (!draggedSegmentIsSelected) {
20248
+ selectedSegments = [draggedSegment];
20249
+ this._view.deselectAll();
20250
+ }
20251
+ return selectedSegments;
20252
+ };
20253
+ SourcesLayer.prototype._buildSegmentDragGroups = function (segments) {
20254
+ var segmentsGroups = this._lineGroups.getSegmentsGroups();
20255
+ var groupsByLine = {};
20256
+ segments.forEach(function (segment) {
20257
+ var groupContext = groupsByLine[segment.line];
20258
+ if (!groupContext) {
20259
+ groupContext = {
20260
+ group: segmentsGroups[segment.line],
20261
+ segments: [],
20262
+ initialPositions: {}
20263
+ };
20264
+ groupsByLine[segment.line] = groupContext;
20265
+ }
20266
+ groupContext.segments.push(segment);
20267
+ groupContext.initialPositions[segment.id] = {
20268
+ startTime: segment.startTime,
20269
+ endTime: segment.endTime
20270
+ };
20271
+ });
20272
+ return Object.keys(groupsByLine).map(function (lineId) {
20273
+ groupsByLine[lineId].segments.sort(function (a, b) {
20274
+ return a.startTime - b.startTime;
20275
+ });
20276
+ return groupsByLine[lineId];
20277
+ });
20278
+ };
20279
+ SourcesLayer.prototype.onSegmentDragStart = function (segmentShape) {
20280
+ var segment = segmentShape.getSegment();
20281
+ var selectedSegments = this._getSelectedSegmentsForDrag(segment);
20282
+ var groups = this._buildSegmentDragGroups(selectedSegments);
20283
+ var minTimeOffset = -Infinity;
20284
+ var maxTimeOffset = Infinity;
20285
+ var draggable = true;
20286
+ var shouldClampToAdjacentGroups = !(groups.length === 1 && groups[0].segments.length === 1);
20287
+ groups.forEach(function (groupContext) {
20288
+ if (shouldClampToAdjacentGroups) {
20289
+ var limits = groupContext.group.getSegmentsDragTimeLimits(groupContext.segments, groupContext.initialPositions);
20290
+ minTimeOffset = Math.max(minTimeOffset, limits.minTimeOffset);
20291
+ maxTimeOffset = Math.min(maxTimeOffset, limits.maxTimeOffset);
20292
+ }
20293
+ draggable = draggable && groupContext.segments.every(function (draggedSegment) {
20294
+ return draggedSegment.editable;
20295
+ });
20296
+ });
20297
+ this._segmentDragSession = {
20298
+ kind: 'move',
20299
+ draggedNode: segmentShape,
20300
+ draggedSegment: segment,
20301
+ mouseDownX: this._view.getPointerPosition().x,
20302
+ initialTimeOffset: this._view.getTimeOffset(),
20303
+ groups: groups,
20304
+ minTimeOffset: minTimeOffset,
20305
+ maxTimeOffset: maxTimeOffset,
20306
+ appliedTimeOffset: 0,
20307
+ draggable: draggable
20308
+ };
20309
+ this._peaks.emit('segments.dragstart', segment);
20310
+ };
20311
+ SourcesLayer.prototype.onSegmentDrag = function (draggedElement) {
20312
+ this._view.updateWithAutoScroll(this._dragSegments.bind(this), null, false);
20313
+ return {
20314
+ x: draggedElement.absolutePosition().x,
20315
+ y: draggedElement.absolutePosition().y
20316
+ };
20317
+ };
20318
+ SourcesLayer.prototype._dragSegments = function () {
20319
+ var session = this._segmentDragSession;
20320
+ var pointerPos;
20321
+ var mousePos;
20322
+ var timeDiff;
20323
+ var timeOffsetDiff;
20324
+ var requestedTimeOffset;
20325
+ var appliedTimeOffset;
20326
+ var groupContext;
20327
+ var segment;
20328
+ var initialPosition;
20329
+ if (!session || session.kind !== 'move' || !session.draggable) {
20330
+ return;
20331
+ }
20332
+ pointerPos = this._view.getPointerPosition();
20333
+ mousePos = Math.min(this._view.getWidth() - this._peaks.options.autoScrollThreshold * this._view.getWidth(), Math.max(0, pointerPos ? pointerPos.x : session.mouseDownX));
20334
+ timeDiff = this._view.pixelsToTime(mousePos - session.mouseDownX);
20335
+ timeOffsetDiff = this._view.getTimeOffset() - session.initialTimeOffset;
20336
+ requestedTimeOffset = Utils.roundTime(timeDiff + timeOffsetDiff);
20337
+ appliedTimeOffset = Utils.clamp(requestedTimeOffset, session.minTimeOffset, session.maxTimeOffset);
20338
+ if (appliedTimeOffset === session.appliedTimeOffset) {
20339
+ return;
20340
+ }
20341
+ if (session.groups.length === 1 && session.groups[0].segments.length === 1) {
20342
+ groupContext = session.groups[0];
20343
+ segment = groupContext.segments[0];
20344
+ initialPosition = groupContext.initialPositions[segment.id];
20345
+ groupContext.group.updateSegment(segment, this._view.timeToPixels(initialPosition.startTime + appliedTimeOffset), this._view.timeToPixels(initialPosition.endTime + appliedTimeOffset), false);
20346
+ } else {
20347
+ session.groups.forEach(function (groupContext) {
20348
+ groupContext.group.dragSegmentsByTimeOffset(groupContext.segments, groupContext.initialPositions, appliedTimeOffset);
20349
+ });
20350
+ }
20351
+ session.appliedTimeOffset = appliedTimeOffset;
20352
+ this._view.updateTimelineLength();
20353
+ this.batchDraw();
20354
+ };
20355
+ SourcesLayer.prototype.onSegmentDragEnd = function (segmentShape) {
20356
+ var session = this._segmentDragSession;
20357
+ if (session && session.kind === 'move' && session.appliedTimeOffset !== 0) {
20358
+ session.groups.forEach(function (groupContext) {
20359
+ groupContext.group.markSegmentsUpdated(groupContext.segments);
20360
+ });
20361
+ this._view.updateTimelineLength();
20362
+ }
20363
+ this.cleanupAfterSegmentDrag();
20364
+ this._peaks.emit('segments.dragend', segmentShape.getSegment());
20365
+ };
20366
+ SourcesLayer.prototype.onSegmentHandleDragStart = function (segmentMarker) {
20367
+ this._segmentDragSession = {
20368
+ kind: segmentMarker.isStartMarker() ? 'resize-start' : 'resize-end',
20369
+ draggedNode: segmentMarker,
20370
+ segment: segmentMarker.getSegment(),
20371
+ group: this._lineGroups.getSegmentsGroups()[segmentMarker.getSegment().line],
20372
+ mouseDownX: this._view.getPointerPosition().x,
20373
+ initialTimeOffset: this._view.getTimeOffset(),
20374
+ initialStartTime: segmentMarker.getSegment().startTime,
20375
+ initialEndTime: segmentMarker.getSegment().endTime,
20376
+ startMarker: segmentMarker.isStartMarker()
20377
+ };
20378
+ this._peaks.emit('segments.dragstart', segmentMarker.getSegment(), segmentMarker.isStartMarker());
20379
+ };
20380
+ SourcesLayer.prototype.onSegmentHandleDrag = function (segmentMarker) {
20381
+ this._view.updateWithAutoScroll(this._dragSegmentHandle.bind(this), null, false);
20382
+ return {
20383
+ x: segmentMarker.getAbsolutePosition().x,
20384
+ y: segmentMarker.getAbsolutePosition().y
20385
+ };
20386
+ };
20387
+ SourcesLayer.prototype._dragSegmentHandle = function () {
20388
+ var session = this._segmentDragSession;
20389
+ var pointerPos;
20390
+ var pointerX;
20391
+ var timeDiff;
20392
+ var timeOffsetDiff;
20393
+ var newStartTime;
20394
+ var newEndTime;
20395
+ if (!session || session.kind !== 'resize-start' && session.kind !== 'resize-end') {
20396
+ return;
20397
+ }
20398
+ pointerPos = this._view.getPointerPosition();
20399
+ pointerX = pointerPos ? pointerPos.x : session.mouseDownX;
20400
+ timeDiff = this._view.pixelsToTime(pointerX - session.mouseDownX);
20401
+ timeOffsetDiff = this._view.getTimeOffset() - session.initialTimeOffset;
20402
+ if (session.startMarker) {
20403
+ newStartTime = Utils.roundTime(session.initialStartTime + timeDiff + timeOffsetDiff);
20404
+ if (session.segment.duration) {
20405
+ newStartTime = Math.max(newStartTime, Utils.roundTime(session.segment.endTime - session.segment.duration));
20406
+ }
20407
+ session.group.updateSegment(session.segment, this._view.timeToPixels(newStartTime), null, false);
20408
+ } else {
20409
+ newEndTime = Utils.roundTime(session.initialEndTime + timeDiff + timeOffsetDiff);
20410
+ if (session.segment.duration) {
20411
+ newEndTime = Math.min(newEndTime, Utils.roundTime(session.segment.startTime + session.segment.duration));
20412
+ }
20413
+ session.group.updateSegment(session.segment, null, this._view.timeToPixels(newEndTime), false);
20414
+ }
20415
+ this._view.updateTimelineLength();
20416
+ this.batchDraw();
20417
+ };
20418
+ SourcesLayer.prototype.onSegmentHandleDragEnd = function (segmentMarker) {
20419
+ this._view.updateTimelineLength();
20420
+ this.cleanupAfterSegmentDrag();
20421
+ this._peaks.emit('segments.dragend', segmentMarker.getSegment(), segmentMarker.isStartMarker());
20422
+ };
20115
20423
  SourcesLayer.prototype.onSourcesGroupDragStart = function (element) {
20116
20424
  if (this._suppressDragLifecycleForSourceId && element && element.currentTarget && element.currentTarget.attrs && element.currentTarget.attrs.sourceId === this._suppressDragLifecycleForSourceId) {
20117
20425
  return;
@@ -21330,6 +21638,12 @@ module.exports = function (Colors, EventEmitter, SegmentHandler, SourceHandler,
21330
21638
  Peaks.prototype.selectSourcesOnLineAfter = function (lineId, time) {
21331
21639
  return this.view.selectSourcesOnLineAfter(lineId, time);
21332
21640
  };
21641
+ Peaks.prototype.selectSegmentById = function (segmentId) {
21642
+ return this.view.selectSegmentById(segmentId);
21643
+ };
21644
+ Peaks.prototype.selectSegmentsOnLineAfter = function (lineId, time) {
21645
+ return this.view.selectSegmentsOnLineAfter(lineId, time);
21646
+ };
21333
21647
  Peaks.prototype.deselectAll = function (notify) {
21334
21648
  return this.view.deselectAll(notify);
21335
21649
  };
@@ -21774,6 +22088,14 @@ module.exports = function (Utils) {
21774
22088
  this._indicators = opts.indicators;
21775
22089
  this._peaks.emit('model.segment.updated', this);
21776
22090
  };
22091
+ Segment.prototype.updateTimes = function (newStartTime, newEndTime) {
22092
+ if (!Utils.isNullOrUndefined(newStartTime)) {
22093
+ this._startTime = Utils.roundTime(newStartTime);
22094
+ }
22095
+ if (!Utils.isNullOrUndefined(newEndTime)) {
22096
+ this._endTime = Utils.roundTime(newEndTime);
22097
+ }
22098
+ };
21777
22099
  Segment.prototype.setSelected = function (selected) {
21778
22100
  this._selected = selected;
21779
22101
  this._peaks.emit('model.segment.selected', this);
@@ -23877,13 +24199,25 @@ module.exports = function (MouseDragHandler, PlayheadLayer, SourcesLayer, ModeLa
23877
24199
  View.prototype.selectSourceById = function (sourceId) {
23878
24200
  const sourceGroup = this._sourcesLayer.getSourceGroupById(sourceId);
23879
24201
  if (sourceGroup) {
23880
- this._modeLayer.selectElements([sourceGroup.getSource()], false);
24202
+ this._modeLayer.selectElements([sourceGroup.getSource()], true);
23881
24203
  }
23882
24204
  };
23883
24205
  View.prototype.selectSourcesOnLineAfter = function (lineId, time) {
23884
24206
  const sources = this._sourcesLayer.getSourcesOnLineAfter(lineId, time);
23885
- if (sources) {
23886
- this._modeLayer.selectElements(sources, false);
24207
+ if (sources.length) {
24208
+ this._modeLayer.selectElements(sources, true);
24209
+ }
24210
+ };
24211
+ View.prototype.selectSegmentById = function (segmentId) {
24212
+ const segment = this._peaks.segmentHandler.getSegment(segmentId);
24213
+ if (segment) {
24214
+ this._modeLayer.selectElements([segment], true);
24215
+ }
24216
+ };
24217
+ View.prototype.selectSegmentsOnLineAfter = function (lineId, time) {
24218
+ const segments = this._sourcesLayer.getSegmentsOnLineAfter(lineId, time);
24219
+ if (segments.length) {
24220
+ this._modeLayer.selectElements(segments, true);
23887
24221
  }
23888
24222
  };
23889
24223
  View.prototype.deselectAll = function (notify) {