wavesurfer.js 6.5.0 → 7.0.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.
Files changed (132) hide show
  1. package/.eslintrc.json +24 -0
  2. package/.prettierrc +8 -0
  3. package/README.md +26 -117
  4. package/dist/base-plugin.d.ts +10 -0
  5. package/dist/base-plugin.js +13 -0
  6. package/dist/decoder.d.ts +10 -0
  7. package/dist/decoder.js +43 -0
  8. package/dist/event-emitter.d.ts +11 -0
  9. package/dist/event-emitter.js +21 -0
  10. package/dist/fetcher.d.ts +4 -0
  11. package/dist/fetcher.js +17 -0
  12. package/dist/index.d.ts +92 -0
  13. package/dist/index.js +166 -0
  14. package/dist/player-webaudio.d.ts +8 -0
  15. package/dist/player-webaudio.js +31 -0
  16. package/dist/player.d.ts +16 -0
  17. package/dist/player.js +40 -0
  18. package/dist/plugins/regions.d.ts +48 -0
  19. package/dist/plugins/regions.js +183 -0
  20. package/dist/renderer.d.ts +34 -0
  21. package/dist/renderer.js +193 -0
  22. package/dist/timer.d.ts +10 -0
  23. package/dist/timer.js +17 -0
  24. package/examples/audio.ogg +0 -0
  25. package/examples/bars.js +19 -0
  26. package/examples/basic.js +8 -0
  27. package/examples/gradient.js +28 -0
  28. package/examples/regions.js +63 -0
  29. package/examples/video.js +19 -0
  30. package/examples/webaudio.js +14 -0
  31. package/package.json +19 -99
  32. package/src/base-plugin.ts +20 -0
  33. package/src/decoder.ts +41 -0
  34. package/src/event-emitter.ts +35 -0
  35. package/src/fetcher.ts +7 -0
  36. package/src/index.ts +252 -0
  37. package/src/player-webaudio.ts +34 -0
  38. package/src/player.ts +50 -0
  39. package/src/plugins/regions.ts +240 -0
  40. package/src/renderer.ts +250 -0
  41. package/src/timer.ts +27 -0
  42. package/tsconfig.json +105 -0
  43. package/tutorial/index.html +47 -0
  44. package/tutorial/src/editor.js +70 -0
  45. package/tutorial/src/init.js +66 -0
  46. package/tutorial/src/url.js +25 -0
  47. package/tutorial/style.css +211 -0
  48. package/yarn-error.log +1049 -0
  49. package/LICENSE +0 -29
  50. package/dist/plugin/wavesurfer.cursor.js +0 -418
  51. package/dist/plugin/wavesurfer.cursor.js.map +0 -1
  52. package/dist/plugin/wavesurfer.cursor.min.js +0 -7
  53. package/dist/plugin/wavesurfer.cursor.min.js.map +0 -1
  54. package/dist/plugin/wavesurfer.elan.js +0 -381
  55. package/dist/plugin/wavesurfer.elan.js.map +0 -1
  56. package/dist/plugin/wavesurfer.elan.min.js +0 -7
  57. package/dist/plugin/wavesurfer.elan.min.js.map +0 -1
  58. package/dist/plugin/wavesurfer.markers.js +0 -520
  59. package/dist/plugin/wavesurfer.markers.js.map +0 -1
  60. package/dist/plugin/wavesurfer.markers.min.js +0 -7
  61. package/dist/plugin/wavesurfer.markers.min.js.map +0 -1
  62. package/dist/plugin/wavesurfer.mediasession.js +0 -181
  63. package/dist/plugin/wavesurfer.mediasession.js.map +0 -1
  64. package/dist/plugin/wavesurfer.mediasession.min.js +0 -7
  65. package/dist/plugin/wavesurfer.mediasession.min.js.map +0 -1
  66. package/dist/plugin/wavesurfer.microphone.js +0 -466
  67. package/dist/plugin/wavesurfer.microphone.js.map +0 -1
  68. package/dist/plugin/wavesurfer.microphone.min.js +0 -7
  69. package/dist/plugin/wavesurfer.microphone.min.js.map +0 -1
  70. package/dist/plugin/wavesurfer.minimap.js +0 -440
  71. package/dist/plugin/wavesurfer.minimap.js.map +0 -1
  72. package/dist/plugin/wavesurfer.minimap.min.js +0 -7
  73. package/dist/plugin/wavesurfer.minimap.min.js.map +0 -1
  74. package/dist/plugin/wavesurfer.playhead.js +0 -300
  75. package/dist/plugin/wavesurfer.playhead.js.map +0 -1
  76. package/dist/plugin/wavesurfer.playhead.min.js +0 -7
  77. package/dist/plugin/wavesurfer.playhead.min.js.map +0 -1
  78. package/dist/plugin/wavesurfer.regions.js +0 -1342
  79. package/dist/plugin/wavesurfer.regions.js.map +0 -1
  80. package/dist/plugin/wavesurfer.regions.min.js +0 -7
  81. package/dist/plugin/wavesurfer.regions.min.js.map +0 -1
  82. package/dist/plugin/wavesurfer.spectrogram.js +0 -736
  83. package/dist/plugin/wavesurfer.spectrogram.js.map +0 -1
  84. package/dist/plugin/wavesurfer.spectrogram.min.js +0 -7
  85. package/dist/plugin/wavesurfer.spectrogram.min.js.map +0 -1
  86. package/dist/plugin/wavesurfer.timeline.js +0 -649
  87. package/dist/plugin/wavesurfer.timeline.js.map +0 -1
  88. package/dist/plugin/wavesurfer.timeline.min.js +0 -7
  89. package/dist/plugin/wavesurfer.timeline.min.js.map +0 -1
  90. package/dist/wavesurfer-html-init.js +0 -381
  91. package/dist/wavesurfer-html-init.js.map +0 -1
  92. package/dist/wavesurfer-html-init.min.js +0 -7
  93. package/dist/wavesurfer-html-init.min.js.map +0 -1
  94. package/dist/wavesurfer.js +0 -6145
  95. package/dist/wavesurfer.js.map +0 -1
  96. package/dist/wavesurfer.min.js +0 -7
  97. package/dist/wavesurfer.min.js.map +0 -1
  98. package/src/drawer.canvasentry.js +0 -427
  99. package/src/drawer.js +0 -413
  100. package/src/drawer.multicanvas.js +0 -617
  101. package/src/html-init.js +0 -241
  102. package/src/mediaelement-webaudio.js +0 -77
  103. package/src/mediaelement.js +0 -443
  104. package/src/peakcache.js +0 -127
  105. package/src/plugin/cursor/index.js +0 -350
  106. package/src/plugin/elan/index.js +0 -307
  107. package/src/plugin/markers/index.js +0 -436
  108. package/src/plugin/mediasession/index.js +0 -94
  109. package/src/plugin/microphone/index.js +0 -415
  110. package/src/plugin/minimap/index.js +0 -403
  111. package/src/plugin/playhead/index.js +0 -226
  112. package/src/plugin/regions/index.js +0 -478
  113. package/src/plugin/regions/region.js +0 -883
  114. package/src/plugin/spectrogram/fft.js +0 -222
  115. package/src/plugin/spectrogram/index.js +0 -522
  116. package/src/plugin/timeline/index.js +0 -600
  117. package/src/util/absMax.js +0 -16
  118. package/src/util/clamp.js +0 -11
  119. package/src/util/fetch.js +0 -222
  120. package/src/util/frame.js +0 -13
  121. package/src/util/get-id.js +0 -22
  122. package/src/util/index.js +0 -14
  123. package/src/util/max.js +0 -16
  124. package/src/util/min.js +0 -16
  125. package/src/util/observer.js +0 -144
  126. package/src/util/orientation.js +0 -98
  127. package/src/util/prevent-click.js +0 -19
  128. package/src/util/request-animation-frame.js +0 -15
  129. package/src/util/silence-mode.js +0 -42
  130. package/src/util/style.js +0 -16
  131. package/src/wavesurfer.js +0 -1797
  132. package/src/webaudio.js +0 -779
@@ -1,1342 +0,0 @@
1
- /*!
2
- * wavesurfer.js regions plugin 6.5.0 (2023-03-11)
3
- * https://wavesurfer-js.org
4
- * @license BSD-3-Clause
5
- */
6
- (function webpackUniversalModuleDefinition(root, factory) {
7
- if(typeof exports === 'object' && typeof module === 'object')
8
- module.exports = factory();
9
- else if(typeof define === 'function' && define.amd)
10
- define("WaveSurfer", [], factory);
11
- else if(typeof exports === 'object')
12
- exports["WaveSurfer"] = factory();
13
- else
14
- root["WaveSurfer"] = root["WaveSurfer"] || {}, root["WaveSurfer"]["regions"] = factory();
15
- })(self, () => {
16
- return /******/ (() => { // webpackBootstrap
17
- /******/ "use strict";
18
- /******/ var __webpack_modules__ = ({
19
-
20
- /***/ "./src/plugin/regions/index.js":
21
- /*!*************************************!*\
22
- !*** ./src/plugin/regions/index.js ***!
23
- \*************************************/
24
- /***/ ((module, exports, __webpack_require__) => {
25
-
26
-
27
-
28
- Object.defineProperty(exports, "__esModule", ({
29
- value: true
30
- }));
31
- exports["default"] = void 0;
32
- var _region = __webpack_require__(/*! ./region.js */ "./src/plugin/regions/region.js");
33
- function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
34
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
35
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
36
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
37
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
38
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
39
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
40
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
41
- function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
42
- /**
43
- * Regions are visual overlays on waveform that can be used to play and loop
44
- * portions of audio. Regions can be dragged and resized.
45
- *
46
- * Visual customization is possible via CSS (using the selectors
47
- * `.wavesurfer-region` and `.wavesurfer-handle`).
48
- *
49
- * @implements {PluginClass}
50
- * @extends {Observer}
51
- *
52
- * @example
53
- * // es6
54
- * import RegionsPlugin from 'wavesurfer.regions.js';
55
- *
56
- * // commonjs
57
- * var RegionsPlugin = require('wavesurfer.regions.js');
58
- *
59
- * // if you are using <script> tags
60
- * var RegionsPlugin = window.WaveSurfer.regions;
61
- *
62
- * // ... initialising wavesurfer with the plugin
63
- * var wavesurfer = WaveSurfer.create({
64
- * // wavesurfer options ...
65
- * plugins: [
66
- * RegionsPlugin.create({
67
- * // plugin options ...
68
- * })
69
- * ]
70
- * });
71
- */
72
- var RegionsPlugin = /*#__PURE__*/function () {
73
- function RegionsPlugin(params, ws) {
74
- var _this = this;
75
- _classCallCheck(this, RegionsPlugin);
76
- this.params = params;
77
- this.wavesurfer = ws;
78
- this.util = _objectSpread(_objectSpread({}, ws.util), {}, {
79
- getRegionSnapToGridValue: function getRegionSnapToGridValue(value) {
80
- return _this.getRegionSnapToGridValue(value, params);
81
- }
82
- });
83
- this.maxRegions = params.maxRegions;
84
- this.regionsMinLength = params.regionsMinLength || null;
85
-
86
- // turn the plugin instance into an observer
87
- var observerPrototypeKeys = Object.getOwnPropertyNames(this.util.Observer.prototype);
88
- observerPrototypeKeys.forEach(function (key) {
89
- _region.Region.prototype[key] = _this.util.Observer.prototype[key];
90
- });
91
- this.wavesurfer.Region = _region.Region;
92
-
93
- // By default, scroll the container if the user drags a region
94
- // within 5% (based on its initial size) of its edge
95
- var scrollWidthProportion = 0.05;
96
- this._onBackendCreated = function () {
97
- _this.wrapper = _this.wavesurfer.drawer.wrapper;
98
- _this.orientation = _this.wavesurfer.drawer.orientation;
99
- _this.defaultEdgeScrollWidth = _this.wrapper.clientWidth * scrollWidthProportion;
100
- if (_this.params.regions) {
101
- _this.params.regions.forEach(function (region) {
102
- _this.add(region);
103
- });
104
- }
105
- };
106
-
107
- // Id-based hash of regions
108
- this.list = {};
109
- this._onReady = function () {
110
- _this.wrapper = _this.wavesurfer.drawer.wrapper;
111
- _this.vertical = _this.wavesurfer.drawer.params.vertical;
112
- if (_this.params.dragSelection) {
113
- _this.enableDragSelection(_this.params);
114
- }
115
- Object.keys(_this.list).forEach(function (id) {
116
- _this.list[id].updateRender();
117
- });
118
- };
119
- }
120
- _createClass(RegionsPlugin, [{
121
- key: "init",
122
- value: function init() {
123
- // Check if ws is ready
124
- if (this.wavesurfer.isReady) {
125
- this._onBackendCreated();
126
- this._onReady();
127
- } else {
128
- this.wavesurfer.once('ready', this._onReady);
129
- this.wavesurfer.once('backend-created', this._onBackendCreated);
130
- }
131
- }
132
- }, {
133
- key: "destroy",
134
- value: function destroy() {
135
- this.wavesurfer.un('ready', this._onReady);
136
- this.wavesurfer.un('backend-created', this._onBackendCreated);
137
- // Disabling `region-removed' because destroying the plugin calls
138
- // the Region.remove() method that is also used to remove regions based
139
- // on user input. This can cause confusion since teardown is not a
140
- // user event, but would emit `region-removed` as if it was.
141
- this.wavesurfer.setDisabledEventEmissions(['region-removed']);
142
- this.disableDragSelection();
143
- this.clear();
144
- }
145
-
146
- /**
147
- * check to see if adding a new region would exceed maxRegions
148
- * @return {boolean} whether we should proceed and create a region
149
- * @private
150
- */
151
- }, {
152
- key: "wouldExceedMaxRegions",
153
- value: function wouldExceedMaxRegions() {
154
- return this.maxRegions && Object.keys(this.list).length >= this.maxRegions;
155
- }
156
-
157
- /**
158
- * Add a region
159
- *
160
- * @param {object} params Region parameters
161
- * @return {Region} The created region
162
- */
163
- }, {
164
- key: "add",
165
- value: function add(params) {
166
- var _this2 = this;
167
- if (this.wouldExceedMaxRegions()) {
168
- return null;
169
- }
170
- params = _objectSpread({
171
- edgeScrollWidth: this.params.edgeScrollWidth || this.defaultEdgeScrollWidth,
172
- contentEditable: this.params.contentEditable,
173
- removeButton: this.params.removeButton
174
- }, params);
175
-
176
- // Take formatTimeCallback from plugin params if not already set
177
- if (!params.formatTimeCallback && this.params.formatTimeCallback) {
178
- params = _objectSpread(_objectSpread({}, params), {}, {
179
- formatTimeCallback: this.params.formatTimeCallback
180
- });
181
- }
182
- if (!params.minLength && this.regionsMinLength) {
183
- params = _objectSpread(_objectSpread({}, params), {}, {
184
- minLength: this.regionsMinLength
185
- });
186
- }
187
- var region = new this.wavesurfer.Region(params, this.util, this.wavesurfer);
188
- this.list[region.id] = region;
189
- region.on('remove', function () {
190
- delete _this2.list[region.id];
191
- });
192
- return region;
193
- }
194
-
195
- /**
196
- * Remove all regions
197
- */
198
- }, {
199
- key: "clear",
200
- value: function clear() {
201
- var _this3 = this;
202
- Object.keys(this.list).forEach(function (id) {
203
- _this3.list[id].remove();
204
- });
205
- }
206
- }, {
207
- key: "enableDragSelection",
208
- value: function enableDragSelection(params) {
209
- var _this4 = this;
210
- this.disableDragSelection();
211
- var slop = params.slop || 2;
212
- var container = this.wavesurfer.drawer.container;
213
- var scroll = params.scroll !== false && this.wavesurfer.params.scrollParent;
214
- var scrollSpeed = params.scrollSpeed || 1;
215
- var scrollThreshold = params.scrollThreshold || 10;
216
- var drag;
217
- var duration = this.wavesurfer.getDuration();
218
- var maxScroll;
219
- var start;
220
- var region;
221
- var touchId;
222
- var pxMove = 0;
223
- var scrollDirection;
224
- var wrapperRect;
225
-
226
- // Scroll when the user is dragging within the threshold
227
- var edgeScroll = function edgeScroll(e) {
228
- if (!region || !scrollDirection) {
229
- return;
230
- }
231
-
232
- // Update scroll position
233
- var scrollLeft = _this4.wrapper.scrollLeft + scrollSpeed * scrollDirection;
234
- _this4.wrapper.scrollLeft = scrollLeft = Math.min(maxScroll, Math.max(0, scrollLeft));
235
-
236
- // Update range
237
- var end = _this4.wavesurfer.drawer.handleEvent(e);
238
- region.update({
239
- start: Math.min(end * duration, start * duration),
240
- end: Math.max(end * duration, start * duration)
241
- });
242
-
243
- // Check that there is more to scroll and repeat
244
- if (scrollLeft < maxScroll && scrollLeft > 0) {
245
- window.requestAnimationFrame(function () {
246
- edgeScroll(e);
247
- });
248
- }
249
- };
250
- var eventDown = function eventDown(e) {
251
- if (e.touches && e.touches.length > 1) {
252
- return;
253
- }
254
- duration = _this4.wavesurfer.getDuration();
255
- touchId = e.targetTouches ? e.targetTouches[0].identifier : null;
256
-
257
- // Store for scroll calculations
258
- maxScroll = _this4.wrapper.scrollWidth - _this4.wrapper.clientWidth;
259
- wrapperRect = _this4.util.withOrientation(_this4.wrapper.getBoundingClientRect(), _this4.vertical);
260
-
261
- // set the region channel index based on the clicked area
262
- if (_this4.wavesurfer.params.splitChannels && _this4.wavesurfer.params.splitChannelsOptions.splitDragSelection) {
263
- var y = (e.touches ? e.touches[0].clientY : e.clientY) - wrapperRect.top;
264
- var channelCount = _this4.wavesurfer.backend.buffer != null ? _this4.wavesurfer.backend.buffer.numberOfChannels : 1;
265
- var channelHeight = _this4.wrapper.clientHeight / channelCount;
266
- var channelIdx = Math.floor(y / channelHeight);
267
- params.channelIdx = channelIdx;
268
- var channelColors = _this4.wavesurfer.params.splitChannelsOptions.channelColors[channelIdx];
269
- if (channelColors && channelColors.dragColor) {
270
- params.color = channelColors.dragColor;
271
- }
272
- }
273
- drag = true;
274
- start = _this4.wavesurfer.drawer.handleEvent(e, true);
275
- region = null;
276
- scrollDirection = null;
277
- };
278
- this.wrapper.addEventListener('mousedown', eventDown);
279
- this.wrapper.addEventListener('touchstart', eventDown);
280
- this.on('disable-drag-selection', function () {
281
- _this4.wrapper.removeEventListener('touchstart', eventDown);
282
- _this4.wrapper.removeEventListener('mousedown', eventDown);
283
- });
284
- var eventUp = function eventUp(e) {
285
- if (e.touches && e.touches.length > 1) {
286
- return;
287
- }
288
- drag = false;
289
- pxMove = 0;
290
- scrollDirection = null;
291
- if (region) {
292
- _this4.util.preventClick();
293
- region.fireEvent('update-end', e);
294
- _this4.wavesurfer.fireEvent('region-update-end', region, e);
295
- }
296
- region = null;
297
- };
298
- this.wrapper.addEventListener('mouseleave', eventUp);
299
- this.wrapper.addEventListener('mouseup', eventUp);
300
- this.wrapper.addEventListener('touchend', eventUp);
301
- document.body.addEventListener('mouseup', eventUp);
302
- document.body.addEventListener('touchend', eventUp);
303
- this.on('disable-drag-selection', function () {
304
- document.body.removeEventListener('mouseup', eventUp);
305
- document.body.removeEventListener('touchend', eventUp);
306
- _this4.wrapper.removeEventListener('touchend', eventUp);
307
- _this4.wrapper.removeEventListener('mouseup', eventUp);
308
- _this4.wrapper.removeEventListener('mouseleave', eventUp);
309
- });
310
- var eventMove = function eventMove(event) {
311
- if (!drag) {
312
- return;
313
- }
314
- if (++pxMove <= slop) {
315
- return;
316
- }
317
- if (event.touches && event.touches.length > 1) {
318
- return;
319
- }
320
- if (event.targetTouches && event.targetTouches[0].identifier != touchId) {
321
- return;
322
- }
323
-
324
- // auto-create a region during mouse drag, unless region-count would exceed "maxRegions"
325
- if (!region) {
326
- region = _this4.add(params || {});
327
- if (!region) {
328
- return;
329
- }
330
- }
331
- var end = _this4.wavesurfer.drawer.handleEvent(event);
332
- var startUpdate = _this4.wavesurfer.regions.util.getRegionSnapToGridValue(start * duration);
333
- var endUpdate = _this4.wavesurfer.regions.util.getRegionSnapToGridValue(end * duration);
334
- region.update({
335
- start: Math.min(endUpdate, startUpdate),
336
- end: Math.max(endUpdate, startUpdate)
337
- });
338
- var orientedEvent = _this4.util.withOrientation(event, _this4.vertical);
339
-
340
- // If scrolling is enabled
341
- if (scroll && container.clientWidth < _this4.wrapper.scrollWidth) {
342
- // Check threshold based on mouse
343
- var x = orientedEvent.clientX - wrapperRect.left;
344
- if (x <= scrollThreshold) {
345
- scrollDirection = -1;
346
- } else if (x >= wrapperRect.right - scrollThreshold) {
347
- scrollDirection = 1;
348
- } else {
349
- scrollDirection = null;
350
- }
351
- scrollDirection && edgeScroll(event);
352
- }
353
- };
354
- this.wrapper.addEventListener('mousemove', eventMove);
355
- this.wrapper.addEventListener('touchmove', eventMove);
356
- this.on('disable-drag-selection', function () {
357
- _this4.wrapper.removeEventListener('touchmove', eventMove);
358
- _this4.wrapper.removeEventListener('mousemove', eventMove);
359
- });
360
- this.wavesurfer.on('region-created', function (region) {
361
- if (_this4.regionsMinLength) {
362
- region.minLength = _this4.regionsMinLength;
363
- }
364
- });
365
- }
366
- }, {
367
- key: "disableDragSelection",
368
- value: function disableDragSelection() {
369
- this.fireEvent('disable-drag-selection');
370
- }
371
-
372
- /**
373
- * Get current region
374
- *
375
- * The smallest region that contains the current time. If several such
376
- * regions exist, take the first. Return `null` if none exist.
377
- *
378
- * @returns {Region} The current region
379
- */
380
- }, {
381
- key: "getCurrentRegion",
382
- value: function getCurrentRegion() {
383
- var _this5 = this;
384
- var time = this.wavesurfer.getCurrentTime();
385
- var min = null;
386
- Object.keys(this.list).forEach(function (id) {
387
- var cur = _this5.list[id];
388
- if (cur.start <= time && cur.end >= time) {
389
- if (!min || cur.end - cur.start < min.end - min.start) {
390
- min = cur;
391
- }
392
- }
393
- });
394
- return min;
395
- }
396
-
397
- /**
398
- * Match the value to the grid, if required
399
- *
400
- * If the regions plugin params have a snapToGridInterval set, return the
401
- * value matching the nearest grid interval. If no snapToGridInterval is set,
402
- * the passed value will be returned without modification.
403
- *
404
- * @param {number} value the value to snap to the grid, if needed
405
- * @param {Object} params the regions plugin params
406
- * @returns {number} value
407
- */
408
- }, {
409
- key: "getRegionSnapToGridValue",
410
- value: function getRegionSnapToGridValue(value, params) {
411
- if (params.snapToGridInterval) {
412
- // the regions should snap to a grid
413
- var offset = params.snapToGridOffset || 0;
414
- return Math.round((value - offset) / params.snapToGridInterval) * params.snapToGridInterval + offset;
415
- }
416
-
417
- // no snap-to-grid
418
- return value;
419
- }
420
- }], [{
421
- key: "create",
422
- value:
423
- /**
424
- * Regions plugin definition factory
425
- *
426
- * This function must be used to create a plugin definition which can be
427
- * used by wavesurfer to correctly instantiate the plugin.
428
- *
429
- * @param {RegionsPluginParams} params parameters use to initialise the plugin
430
- * @return {PluginDefinition} an object representing the plugin
431
- */
432
- function create(params) {
433
- return {
434
- name: 'regions',
435
- deferInit: params && params.deferInit ? params.deferInit : false,
436
- params: params,
437
- staticProps: {
438
- addRegion: function addRegion(options) {
439
- if (!this.initialisedPluginList.regions) {
440
- this.initPlugin('regions');
441
- }
442
- return this.regions.add(options);
443
- },
444
- clearRegions: function clearRegions() {
445
- this.regions && this.regions.clear();
446
- },
447
- enableDragSelection: function enableDragSelection(options) {
448
- if (!this.initialisedPluginList.regions) {
449
- this.initPlugin('regions');
450
- }
451
- this.regions.enableDragSelection(options);
452
- },
453
- disableDragSelection: function disableDragSelection() {
454
- this.regions.disableDragSelection();
455
- }
456
- },
457
- instance: RegionsPlugin
458
- };
459
- }
460
- }]);
461
- return RegionsPlugin;
462
- }();
463
- exports["default"] = RegionsPlugin;
464
- module.exports = exports.default;
465
-
466
- /***/ }),
467
-
468
- /***/ "./src/plugin/regions/region.js":
469
- /*!**************************************!*\
470
- !*** ./src/plugin/regions/region.js ***!
471
- \**************************************/
472
- /***/ ((__unused_webpack_module, exports) => {
473
-
474
-
475
-
476
- Object.defineProperty(exports, "__esModule", ({
477
- value: true
478
- }));
479
- exports.Region = void 0;
480
- function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
481
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
482
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
483
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
484
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
485
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
486
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
487
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
488
- function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
489
- /**
490
- * @since 4.0.0
491
- *
492
- * (Single) Region plugin class
493
- *
494
- * Must be turned into an observer before instantiating. This is done in
495
- * `RegionsPlugin` (main plugin class).
496
- *
497
- * @extends {Observer}
498
- */
499
- var Region = /*#__PURE__*/function () {
500
- function Region(params, regionsUtils, ws) {
501
- var _params$showTooltip,
502
- _this = this;
503
- _classCallCheck(this, Region);
504
- this.wavesurfer = ws;
505
- this.wrapper = ws.drawer.wrapper;
506
- this.util = ws.util;
507
- this.style = this.util.style;
508
- this.regionsUtil = regionsUtils;
509
- this.vertical = ws.drawer.params.vertical;
510
- this.id = params.id == null ? ws.util.getId() : params.id;
511
- this.start = Number(params.start) || 0;
512
- this.end = params.end == null ?
513
- // small marker-like region
514
- this.start + 4 / this.wrapper.scrollWidth * this.wavesurfer.getDuration() : Number(params.end);
515
- this.resize = params.resize === undefined ? true : Boolean(params.resize);
516
- this.drag = params.drag === undefined ? true : Boolean(params.drag);
517
- this.contentEditable = Boolean(params.contentEditable);
518
- this.removeButton = Boolean(params.removeButton);
519
- // reflect resize and drag state of region for region-updated listener
520
- this.isResizing = false;
521
- this.isDragging = false;
522
- this.loop = Boolean(params.loop);
523
- this.color = params.color || 'rgba(0, 0, 0, 0.1)';
524
- // The left and right handleStyle properties can be set to 'none' for
525
- // no styling or can be assigned an object containing CSS properties.
526
- this.handleStyle = params.handleStyle || {
527
- left: {},
528
- right: {}
529
- };
530
- this.handleLeftEl = null;
531
- this.handleRightEl = null;
532
- this.data = params.data || {};
533
- this.attributes = params.attributes || {};
534
- this.showTooltip = (_params$showTooltip = params.showTooltip) !== null && _params$showTooltip !== void 0 ? _params$showTooltip : true;
535
- this.maxLength = params.maxLength;
536
- // It assumes the minLength parameter value, or the regionsMinLength parameter value, if the first one not provided
537
- this.minLength = params.minLength;
538
- this._onRedraw = function () {
539
- return _this.updateRender();
540
- };
541
- this.scroll = params.scroll !== false && ws.params.scrollParent;
542
- this.scrollSpeed = params.scrollSpeed || 1;
543
- this.scrollThreshold = params.scrollThreshold || 10;
544
- // Determines whether the context menu is prevented from being opened.
545
- this.preventContextMenu = params.preventContextMenu === undefined ? false : Boolean(params.preventContextMenu);
546
-
547
- // select channel ID to set region
548
- var channelIdx = params.channelIdx == null ? -1 : parseInt(params.channelIdx);
549
- this.channelIdx = channelIdx;
550
- this.regionHeight = '100%';
551
- this.marginTop = '0px';
552
- if (channelIdx !== -1) {
553
- var channelCount = this.wavesurfer.backend.buffer != null ? this.wavesurfer.backend.buffer.numberOfChannels : -1;
554
- if (channelCount >= 0 && channelIdx < channelCount) {
555
- this.regionHeight = Math.floor(1 / channelCount * 100) + '%';
556
- this.marginTop = this.wavesurfer.getHeight() * channelIdx + 'px';
557
- }
558
- }
559
- this.formatTimeCallback = params.formatTimeCallback;
560
- this.edgeScrollWidth = params.edgeScrollWidth;
561
- this.bindInOut();
562
- this.render();
563
- this.wavesurfer.on('zoom', this._onRedraw);
564
- this.wavesurfer.on('redraw', this._onRedraw);
565
- this.wavesurfer.fireEvent('region-created', this);
566
- }
567
-
568
- /* Update region params. */
569
- _createClass(Region, [{
570
- key: "update",
571
- value: function update(params, eventParams) {
572
- if (params.start != null) {
573
- this.start = Number(params.start);
574
- }
575
- if (params.end != null) {
576
- this.end = Number(params.end);
577
- }
578
- if (params.loop != null) {
579
- this.loop = Boolean(params.loop);
580
- }
581
- if (params.color != null) {
582
- this.color = params.color;
583
- }
584
- if (params.handleStyle != null) {
585
- this.handleStyle = params.handleStyle;
586
- }
587
- if (params.data != null) {
588
- this.data = params.data;
589
- }
590
- if (params.resize != null) {
591
- this.resize = Boolean(params.resize);
592
- this.updateHandlesResize(this.resize);
593
- }
594
- if (params.drag != null) {
595
- this.drag = Boolean(params.drag);
596
- }
597
- if (params.maxLength != null) {
598
- this.maxLength = Number(params.maxLength);
599
- }
600
- if (params.minLength != null) {
601
- this.minLength = Number(params.minLength);
602
- }
603
- if (params.attributes != null) {
604
- this.attributes = params.attributes;
605
- }
606
- this.updateRender();
607
- this.fireEvent('update');
608
- this.wavesurfer.fireEvent('region-updated', this, eventParams);
609
- }
610
-
611
- /* Remove a single region. */
612
- }, {
613
- key: "remove",
614
- value: function remove() {
615
- if (this.element) {
616
- this.wrapper.removeChild(this.element.domElement);
617
- this.element = null;
618
- this.fireEvent('remove');
619
- this.wavesurfer.un('zoom', this._onRedraw);
620
- this.wavesurfer.un('redraw', this._onRedraw);
621
- this.wavesurfer.fireEvent('region-removed', this);
622
- }
623
- }
624
-
625
- /**
626
- * Play the audio region.
627
- * @param {number} start Optional offset to start playing at
628
- */
629
- }, {
630
- key: "play",
631
- value: function play(start) {
632
- var s = start || this.start;
633
- this.wavesurfer.play(s, this.end);
634
- this.fireEvent('play');
635
- this.wavesurfer.fireEvent('region-play', this);
636
- }
637
-
638
- /**
639
- * Play the audio region in a loop.
640
- * @param {number} start Optional offset to start playing at
641
- * */
642
- }, {
643
- key: "playLoop",
644
- value: function playLoop(start) {
645
- this.loop = true;
646
- this.play(start);
647
- }
648
-
649
- /**
650
- * Set looping on/off.
651
- * @param {boolean} loop True if should play in loop
652
- */
653
- }, {
654
- key: "setLoop",
655
- value: function setLoop(loop) {
656
- this.loop = loop;
657
- }
658
-
659
- /* Render a region as a DOM element. */
660
- }, {
661
- key: "render",
662
- value: function render() {
663
- this.element = this.util.withOrientation(this.wrapper.appendChild(document.createElement('region')), this.vertical);
664
- this.element.className = 'wavesurfer-region';
665
- if (this.showTooltip) {
666
- this.element.title = this.formatTime(this.start, this.end);
667
- }
668
- this.element.setAttribute('data-id', this.id);
669
- for (var attrname in this.attributes) {
670
- this.element.setAttribute('data-region-' + attrname, this.attributes[attrname]);
671
- }
672
- this.style(this.element, {
673
- position: 'absolute',
674
- zIndex: 3,
675
- height: this.regionHeight,
676
- top: this.marginTop
677
- });
678
-
679
- /* Button Remove Region */
680
- if (this.removeButton) {
681
- var removeButtonEl = document.createElement('div');
682
- removeButtonEl.className = 'remove-region-button';
683
- removeButtonEl.innerText = 'x';
684
- this.removeButtonEl = this.element.appendChild(removeButtonEl);
685
- var css = {
686
- zIndex: 4,
687
- position: 'absolute',
688
- bottom: 0,
689
- right: 0,
690
- cursor: 'pointer',
691
- marginRight: '3px',
692
- fontSize: '80%',
693
- color: 'grey',
694
- height: '14px'
695
- };
696
- this.style(this.removeButtonEl, css);
697
- }
698
-
699
- /* Edit content */
700
- if (this.contentEditable) {
701
- var contentEl = document.createElement('div');
702
- contentEl.className = 'region-content';
703
- contentEl.contentEditable = true;
704
- contentEl.innerText = this.data.text || '';
705
- this.contentEl = this.element.appendChild(contentEl);
706
- var _css = {
707
- zIndex: 4,
708
- padding: '2px 5px',
709
- cursor: 'text'
710
- };
711
- this.style(this.contentEl, _css);
712
- }
713
-
714
- /* Resize handles */
715
- if (this.resize) {
716
- this.handleLeftEl = this.util.withOrientation(this.element.appendChild(document.createElement('handle')), this.vertical);
717
- this.handleRightEl = this.util.withOrientation(this.element.appendChild(document.createElement('handle')), this.vertical);
718
- this.handleLeftEl.className = 'wavesurfer-handle wavesurfer-handle-start';
719
- this.handleRightEl.className = 'wavesurfer-handle wavesurfer-handle-end';
720
-
721
- // Default CSS properties for both handles.
722
- var _css2 = {
723
- cursor: this.vertical ? 'row-resize' : 'col-resize',
724
- position: 'absolute',
725
- top: '0px',
726
- width: '2px',
727
- height: '100%',
728
- backgroundColor: 'rgba(0, 0, 0, 1)'
729
- };
730
-
731
- // Merge CSS properties per handle.
732
- var handleLeftCss = this.handleStyle.left !== 'none' ? Object.assign({
733
- left: '0px'
734
- }, _css2, this.handleStyle.left) : null;
735
- var handleRightCss = this.handleStyle.right !== 'none' ? Object.assign({
736
- right: '0px'
737
- }, _css2, this.handleStyle.right) : null;
738
- if (handleLeftCss) {
739
- this.style(this.handleLeftEl, handleLeftCss);
740
- }
741
- if (handleRightCss) {
742
- this.style(this.handleRightEl, handleRightCss);
743
- }
744
- }
745
- this.updateRender();
746
- this.bindEvents();
747
- }
748
- }, {
749
- key: "formatTime",
750
- value: function formatTime(start, end) {
751
- if (this.formatTimeCallback) {
752
- return this.formatTimeCallback(start, end);
753
- }
754
- return (start == end ? [start] : [start, end]).map(function (time) {
755
- return [Math.floor(time % 3600 / 60),
756
- // minutes
757
- ('00' + Math.floor(time % 60)).slice(-2) // seconds
758
- ].join(':');
759
- }).join('-');
760
- }
761
- }, {
762
- key: "getWidth",
763
- value: function getWidth() {
764
- return this.wavesurfer.drawer.width / this.wavesurfer.params.pixelRatio;
765
- }
766
-
767
- /* Update element's position, width, color. */
768
- }, {
769
- key: "updateRender",
770
- value: function updateRender() {
771
- // duration varies during loading process, so don't overwrite important data
772
- var dur = this.wavesurfer.getDuration();
773
- var width = this.getWidth();
774
- var startLimited = this.start;
775
- var endLimited = this.end;
776
- if (startLimited < 0) {
777
- startLimited = 0;
778
- endLimited = endLimited - startLimited;
779
- }
780
- if (endLimited > dur) {
781
- endLimited = dur;
782
- startLimited = dur - (endLimited - startLimited);
783
- }
784
- if (this.minLength != null) {
785
- endLimited = Math.max(startLimited + this.minLength, endLimited);
786
- }
787
- if (this.maxLength != null) {
788
- endLimited = Math.min(startLimited + this.maxLength, endLimited);
789
- }
790
- if (this.element != null) {
791
- // Calculate the left and width values of the region such that
792
- // no gaps appear between regions.
793
- var left = Math.round(startLimited / dur * width);
794
- var regionWidth = Math.round(endLimited / dur * width) - left;
795
- this.style(this.element, {
796
- left: left + 'px',
797
- width: regionWidth + 'px',
798
- backgroundColor: this.color,
799
- cursor: this.drag ? 'move' : 'default'
800
- });
801
- for (var attrname in this.attributes) {
802
- this.element.setAttribute('data-region-' + attrname, this.attributes[attrname]);
803
- }
804
- if (this.showTooltip) {
805
- this.element.title = this.formatTime(this.start, this.end);
806
- }
807
- }
808
- }
809
-
810
- /* Bind audio events. */
811
- }, {
812
- key: "bindInOut",
813
- value: function bindInOut() {
814
- var _this2 = this;
815
- this.firedIn = false;
816
- this.firedOut = false;
817
- var onProcess = function onProcess(time) {
818
- var start = Math.round(_this2.start * 10) / 10;
819
- var end = Math.round(_this2.end * 10) / 10;
820
- time = Math.round(time * 10) / 10;
821
- if (!_this2.firedOut && _this2.firedIn && (start > time || end <= time)) {
822
- _this2.firedOut = true;
823
- _this2.firedIn = false;
824
- _this2.fireEvent('out');
825
- _this2.wavesurfer.fireEvent('region-out', _this2);
826
- }
827
- if (!_this2.firedIn && start <= time && end > time) {
828
- _this2.firedIn = true;
829
- _this2.firedOut = false;
830
- _this2.fireEvent('in');
831
- _this2.wavesurfer.fireEvent('region-in', _this2);
832
- }
833
- };
834
- this.wavesurfer.backend.on('audioprocess', onProcess);
835
- this.on('remove', function () {
836
- _this2.wavesurfer.backend.un('audioprocess', onProcess);
837
- });
838
-
839
- /* Loop playback. */
840
- this.on('out', function () {
841
- if (_this2.loop) {
842
- var realTime = _this2.wavesurfer.getCurrentTime();
843
- if (realTime >= _this2.start && realTime <= _this2.end) {
844
- _this2.wavesurfer.play(_this2.start);
845
- }
846
- }
847
- });
848
- }
849
-
850
- /* Bind DOM events. */
851
- }, {
852
- key: "bindEvents",
853
- value: function bindEvents() {
854
- var _this3 = this;
855
- var preventContextMenu = this.preventContextMenu;
856
- this.element.addEventListener('mouseenter', function (e) {
857
- _this3.fireEvent('mouseenter', e);
858
- _this3.wavesurfer.fireEvent('region-mouseenter', _this3, e);
859
- });
860
- this.element.addEventListener('mouseleave', function (e) {
861
- _this3.fireEvent('mouseleave', e);
862
- _this3.wavesurfer.fireEvent('region-mouseleave', _this3, e);
863
- });
864
- this.element.addEventListener('click', function (e) {
865
- e.preventDefault();
866
- _this3.fireEvent('click', e);
867
- _this3.wavesurfer.fireEvent('region-click', _this3, e);
868
- });
869
- this.element.addEventListener('dblclick', function (e) {
870
- e.stopPropagation();
871
- e.preventDefault();
872
- _this3.fireEvent('dblclick', e);
873
- _this3.wavesurfer.fireEvent('region-dblclick', _this3, e);
874
- });
875
- this.element.addEventListener('contextmenu', function (e) {
876
- if (preventContextMenu) {
877
- e.preventDefault();
878
- }
879
- _this3.fireEvent('contextmenu', e);
880
- _this3.wavesurfer.fireEvent('region-contextmenu', _this3, e);
881
- });
882
-
883
- /* Drag or resize on mousemove. */
884
- if (this.drag || this.resize) {
885
- this.bindDragEvents();
886
- }
887
-
888
- /* Edit content */
889
- if (this.contentEditable) {
890
- this.contentEl.addEventListener('blur', this.onContentBlur.bind(this));
891
- this.contentEl.addEventListener('click', this.onContentClick.bind(this));
892
- }
893
- /* Remove button */
894
- if (this.removeButton) {
895
- this.removeButtonEl.addEventListener('click', this.onRemove.bind(this));
896
- }
897
- this.on('remove', function () {
898
- _this3.contentEl.removeEventListener('blur', _this3.onContentBlur.bind(_this3));
899
- _this3.contentEl.removeEventListener('click', _this3.onContentClick.bind(_this3));
900
- _this3.removeButtonEl.removeEventListener('click', _this3.onRemove.bind(_this3));
901
- });
902
- }
903
- }, {
904
- key: "bindDragEvents",
905
- value: function bindDragEvents() {
906
- var _this4 = this;
907
- var container = this.wavesurfer.drawer.container;
908
- var scrollSpeed = this.scrollSpeed;
909
- var scrollThreshold = this.scrollThreshold;
910
- var startTime;
911
- var touchId;
912
- var drag;
913
- var maxScroll;
914
- var resize;
915
- var updated = false;
916
- var scrollDirection;
917
- var wrapperRect;
918
- var regionLeftHalfTime;
919
- var regionRightHalfTime;
920
-
921
- // Scroll when the user is dragging within the threshold
922
- var edgeScroll = function edgeScroll(event) {
923
- var orientedEvent = _this4.util.withOrientation(event, _this4.vertical);
924
- var duration = _this4.wavesurfer.getDuration();
925
- if (!scrollDirection || !drag && !resize) {
926
- return;
927
- }
928
- var x = orientedEvent.clientX;
929
- var distanceBetweenCursorAndWrapperEdge = 0;
930
- var regionHalfTimeWidth = 0;
931
- var adjustment = 0;
932
-
933
- // Get the currently selected time according to the mouse position
934
- var time = _this4.regionsUtil.getRegionSnapToGridValue(_this4.wavesurfer.drawer.handleEvent(event) * duration);
935
- if (drag) {
936
- // Considering the point of contact with the region while edgescrolling
937
- if (scrollDirection === -1) {
938
- regionHalfTimeWidth = regionLeftHalfTime * _this4.wavesurfer.params.minPxPerSec;
939
- distanceBetweenCursorAndWrapperEdge = x - wrapperRect.left;
940
- } else {
941
- regionHalfTimeWidth = regionRightHalfTime * _this4.wavesurfer.params.minPxPerSec;
942
- distanceBetweenCursorAndWrapperEdge = wrapperRect.right - x;
943
- }
944
- } else {
945
- // Considering minLength while edgescroll
946
- var minLength = _this4.minLength;
947
- if (!minLength) {
948
- minLength = 0;
949
- }
950
- if (resize === 'start') {
951
- if (time > _this4.end - minLength) {
952
- time = _this4.end - minLength;
953
- adjustment = scrollSpeed * scrollDirection;
954
- }
955
- if (time < 0) {
956
- time = 0;
957
- }
958
- } else if (resize === 'end') {
959
- if (time < _this4.start + minLength) {
960
- time = _this4.start + minLength;
961
- adjustment = scrollSpeed * scrollDirection;
962
- }
963
- if (time > duration) {
964
- time = duration;
965
- }
966
- }
967
- }
968
-
969
- // Don't edgescroll if region has reached min or max limit
970
- var wrapperScrollLeft = _this4.wrapper.scrollLeft;
971
- if (scrollDirection === -1) {
972
- if (Math.round(wrapperScrollLeft) === 0) {
973
- return;
974
- }
975
- if (Math.round(wrapperScrollLeft - regionHalfTimeWidth + distanceBetweenCursorAndWrapperEdge) <= 0) {
976
- return;
977
- }
978
- } else {
979
- if (Math.round(wrapperScrollLeft) === maxScroll) {
980
- return;
981
- }
982
- if (Math.round(wrapperScrollLeft + regionHalfTimeWidth - distanceBetweenCursorAndWrapperEdge) >= maxScroll) {
983
- return;
984
- }
985
- }
986
-
987
- // Update scroll position
988
- var scrollLeft = wrapperScrollLeft - adjustment + scrollSpeed * scrollDirection;
989
- if (scrollDirection === -1) {
990
- var calculatedLeft = Math.max(0 + regionHalfTimeWidth - distanceBetweenCursorAndWrapperEdge, scrollLeft);
991
- _this4.wrapper.scrollLeft = scrollLeft = calculatedLeft;
992
- } else {
993
- var calculatedRight = Math.min(maxScroll - regionHalfTimeWidth + distanceBetweenCursorAndWrapperEdge, scrollLeft);
994
- _this4.wrapper.scrollLeft = scrollLeft = calculatedRight;
995
- }
996
- var delta = time - startTime;
997
- startTime = time;
998
-
999
- // Continue dragging or resizing
1000
- drag ? _this4.onDrag(delta) : _this4.onResize(delta, resize);
1001
-
1002
- // Repeat
1003
- window.requestAnimationFrame(function () {
1004
- edgeScroll(event);
1005
- });
1006
- };
1007
- var onDown = function onDown(event) {
1008
- var duration = _this4.wavesurfer.getDuration();
1009
- if (event.touches && event.touches.length > 1) {
1010
- return;
1011
- }
1012
- touchId = event.targetTouches ? event.targetTouches[0].identifier : null;
1013
-
1014
- // stop the event propagation, if this region is resizable or draggable
1015
- // and the event is therefore handled here.
1016
- if (_this4.drag || _this4.resize) {
1017
- event.stopPropagation();
1018
- }
1019
-
1020
- // Store the selected startTime we begun dragging or resizing
1021
- startTime = _this4.regionsUtil.getRegionSnapToGridValue(_this4.wavesurfer.drawer.handleEvent(event, true) * duration);
1022
-
1023
- // Store the selected point of contact when we begin dragging
1024
- regionLeftHalfTime = startTime - _this4.start;
1025
- regionRightHalfTime = _this4.end - startTime;
1026
-
1027
- // Store for scroll calculations
1028
- maxScroll = _this4.wrapper.scrollWidth - _this4.wrapper.clientWidth;
1029
- wrapperRect = _this4.util.withOrientation(_this4.wrapper.getBoundingClientRect(), _this4.vertical);
1030
- _this4.isResizing = false;
1031
- _this4.isDragging = false;
1032
- if (event.target.tagName.toLowerCase() === 'handle') {
1033
- _this4.isResizing = true;
1034
- resize = event.target.classList.contains('wavesurfer-handle-start') ? 'start' : 'end';
1035
- } else {
1036
- _this4.isDragging = true;
1037
- drag = true;
1038
- resize = false;
1039
- }
1040
- };
1041
- var onUp = function onUp(event) {
1042
- if (event.touches && event.touches.length > 1) {
1043
- return;
1044
- }
1045
- if (drag || resize) {
1046
- _this4.isDragging = false;
1047
- _this4.isResizing = false;
1048
- drag = false;
1049
- scrollDirection = null;
1050
- resize = false;
1051
- }
1052
- if (updated) {
1053
- updated = false;
1054
- _this4.util.preventClick();
1055
- _this4.fireEvent('update-end', event);
1056
- _this4.wavesurfer.fireEvent('region-update-end', _this4, event);
1057
- }
1058
- };
1059
- var onMove = function onMove(event) {
1060
- var duration = _this4.wavesurfer.getDuration();
1061
- var orientedEvent = _this4.util.withOrientation(event, _this4.vertical);
1062
- var delta = null;
1063
- if (event.touches && event.touches.length > 1) {
1064
- return;
1065
- }
1066
- if (event.targetTouches && event.targetTouches[0].identifier != touchId) {
1067
- return;
1068
- }
1069
- if (!drag && !resize) {
1070
- return;
1071
- }
1072
- var oldTime = startTime;
1073
- var time = _this4.regionsUtil.getRegionSnapToGridValue(_this4.wavesurfer.drawer.handleEvent(event) * duration);
1074
- if (drag) {
1075
- // To maintain relative cursor start point while dragging
1076
- var maxEnd = _this4.wavesurfer.getDuration();
1077
- if (time > maxEnd - regionRightHalfTime) {
1078
- time = maxEnd - regionRightHalfTime;
1079
- }
1080
- if (time - regionLeftHalfTime < 0) {
1081
- time = regionLeftHalfTime;
1082
- }
1083
- }
1084
- if (resize) {
1085
- // To maintain relative cursor start point while resizing
1086
- // we have to handle for minLength
1087
- var minLength = _this4.minLength;
1088
- if (!minLength) {
1089
- minLength = 0;
1090
- }
1091
- if (resize === 'start') {
1092
- if (time > _this4.end - minLength) {
1093
- time = _this4.end - minLength;
1094
- }
1095
- if (time < 0) {
1096
- time = 0;
1097
- }
1098
- } else if (resize === 'end') {
1099
- if (time < _this4.start + minLength) {
1100
- // Calculate the end time based on the min length of the region.
1101
- time = _this4.start + minLength;
1102
- delta = time - (_this4.end + (time - startTime));
1103
- }
1104
- if (time > duration) {
1105
- time = duration;
1106
- }
1107
- }
1108
- }
1109
- if (!delta) {
1110
- delta = time - startTime;
1111
- }
1112
- startTime = time;
1113
-
1114
- // Drag
1115
- if (_this4.drag && drag) {
1116
- updated = updated || !!delta;
1117
- _this4.onDrag(delta);
1118
- }
1119
-
1120
- // Resize
1121
- if (_this4.resize && resize) {
1122
- updated = updated || !!delta;
1123
- _this4.onResize(delta, resize);
1124
- }
1125
- if (_this4.scroll && container.clientWidth < _this4.wrapper.scrollWidth) {
1126
- // Triggering edgescroll from within edgeScrollWidth
1127
- var x = orientedEvent.clientX;
1128
-
1129
- // Check direction
1130
- if (x < wrapperRect.left + _this4.edgeScrollWidth) {
1131
- scrollDirection = -1;
1132
- } else if (x > wrapperRect.right - _this4.edgeScrollWidth) {
1133
- scrollDirection = 1;
1134
- } else {
1135
- scrollDirection = null;
1136
- }
1137
- if (scrollDirection) {
1138
- edgeScroll(event);
1139
- }
1140
- }
1141
- };
1142
- this.element.addEventListener('mousedown', onDown);
1143
- this.element.addEventListener('touchstart', onDown);
1144
- document.body.addEventListener('mousemove', onMove);
1145
- document.body.addEventListener('touchmove', onMove, {
1146
- passive: false
1147
- });
1148
- document.addEventListener('mouseup', onUp);
1149
- document.body.addEventListener('touchend', onUp);
1150
- this.on('remove', function () {
1151
- document.removeEventListener('mouseup', onUp);
1152
- document.body.removeEventListener('touchend', onUp);
1153
- document.body.removeEventListener('mousemove', onMove);
1154
- document.body.removeEventListener('touchmove', onMove);
1155
- });
1156
- this.wavesurfer.on('destroy', function () {
1157
- document.removeEventListener('mouseup', onUp);
1158
- document.body.removeEventListener('touchend', onUp);
1159
- });
1160
- }
1161
- }, {
1162
- key: "onDrag",
1163
- value: function onDrag(delta) {
1164
- var maxEnd = this.wavesurfer.getDuration();
1165
- if (this.end + delta > maxEnd) {
1166
- delta = maxEnd - this.end;
1167
- }
1168
- if (this.start + delta < 0) {
1169
- delta = this.start * -1;
1170
- }
1171
- var eventParams = {
1172
- direction: this._getDragDirection(delta),
1173
- action: 'drag'
1174
- };
1175
- this.update({
1176
- start: this.start + delta,
1177
- end: this.end + delta
1178
- }, eventParams);
1179
- }
1180
-
1181
- /**
1182
- * Returns the direction of dragging region based on delta
1183
- * Negative delta means region is moving to the left
1184
- * Positive - to the right
1185
- * For zero delta the direction is not defined
1186
- * @param {number} delta Drag offset
1187
- * @returns {string|null} Direction 'left', 'right' or null
1188
- */
1189
- }, {
1190
- key: "_getDragDirection",
1191
- value: function _getDragDirection(delta) {
1192
- if (delta < 0) {
1193
- return 'left';
1194
- }
1195
- if (delta > 0) {
1196
- return 'right';
1197
- }
1198
- return null;
1199
- }
1200
-
1201
- /**
1202
- * @example
1203
- * onResize(-5, 'start') // Moves the start point 5 seconds back
1204
- * onResize(0.5, 'end') // Moves the end point 0.5 seconds forward
1205
- *
1206
- * @param {number} delta How much to add or subtract, given in seconds
1207
- * @param {string} direction 'start 'or 'end'
1208
- */
1209
- }, {
1210
- key: "onResize",
1211
- value: function onResize(delta, direction) {
1212
- var duration = this.wavesurfer.getDuration();
1213
- var eventParams = {
1214
- action: 'resize',
1215
- direction: direction === 'start' ? 'left' : 'right'
1216
- };
1217
- if (direction === 'start') {
1218
- // Check if changing the start by the given delta would result in the region being smaller than minLength
1219
- if (delta > 0 && this.end - (this.start + delta) < this.minLength) {
1220
- delta = this.end - this.minLength - this.start;
1221
- }
1222
-
1223
- // Check if changing the start by the given delta would result in the region being larger than maxLength
1224
- if (delta < 0 && this.end - (this.start + delta) > this.maxLength) {
1225
- delta = this.end - this.start - this.maxLength;
1226
- }
1227
- if (delta < 0 && this.start + delta < 0) {
1228
- delta = this.start * -1;
1229
- }
1230
- this.update({
1231
- start: Math.min(this.start + delta, this.end),
1232
- end: Math.max(this.start + delta, this.end)
1233
- }, eventParams);
1234
- } else {
1235
- // Check if changing the end by the given delta would result in the region being smaller than minLength
1236
- if (delta < 0 && this.end + delta - this.start < this.minLength) {
1237
- delta = this.start + this.minLength - this.end;
1238
- }
1239
-
1240
- // Check if changing the end by the given delta would result in the region being larger than maxLength
1241
- if (delta > 0 && this.end + delta - this.start > this.maxLength) {
1242
- delta = this.maxLength - (this.end - this.start);
1243
- }
1244
- if (delta > 0 && this.end + delta > duration) {
1245
- delta = duration - this.end;
1246
- }
1247
- this.update({
1248
- start: Math.min(this.end + delta, this.start),
1249
- end: Math.max(this.end + delta, this.start)
1250
- }, eventParams);
1251
- }
1252
- }
1253
- }, {
1254
- key: "onContentBlur",
1255
- value: function onContentBlur(event) {
1256
- var _ref = this.data || {},
1257
- oldText = _ref.text;
1258
- var text = event.target.innerText;
1259
- var data = _objectSpread(_objectSpread({}, this.data), {}, {
1260
- text: text
1261
- });
1262
- var eventParams = {
1263
- action: 'contentEdited',
1264
- oldText: oldText,
1265
- text: text
1266
- };
1267
- this.update({
1268
- data: data
1269
- }, eventParams);
1270
- }
1271
- }, {
1272
- key: "onContentClick",
1273
- value: function onContentClick(event) {
1274
- event.stopPropagation();
1275
- }
1276
- }, {
1277
- key: "onRemove",
1278
- value: function onRemove(event) {
1279
- event.stopPropagation();
1280
- this.remove();
1281
- }
1282
- }, {
1283
- key: "updateHandlesResize",
1284
- value: function updateHandlesResize(resize) {
1285
- var cursorStyle;
1286
- if (resize) {
1287
- cursorStyle = this.vertical ? 'row-resize' : 'col-resize';
1288
- } else {
1289
- cursorStyle = 'auto';
1290
- }
1291
- this.handleLeftEl && this.style(this.handleLeftEl, {
1292
- cursor: cursorStyle
1293
- });
1294
- this.handleRightEl && this.style(this.handleRightEl, {
1295
- cursor: cursorStyle
1296
- });
1297
- }
1298
- }]);
1299
- return Region;
1300
- }();
1301
- exports.Region = Region;
1302
-
1303
- /***/ })
1304
-
1305
- /******/ });
1306
- /************************************************************************/
1307
- /******/ // The module cache
1308
- /******/ var __webpack_module_cache__ = {};
1309
- /******/
1310
- /******/ // The require function
1311
- /******/ function __webpack_require__(moduleId) {
1312
- /******/ // Check if module is in cache
1313
- /******/ var cachedModule = __webpack_module_cache__[moduleId];
1314
- /******/ if (cachedModule !== undefined) {
1315
- /******/ return cachedModule.exports;
1316
- /******/ }
1317
- /******/ // Create a new module (and put it into the cache)
1318
- /******/ var module = __webpack_module_cache__[moduleId] = {
1319
- /******/ // no module.id needed
1320
- /******/ // no module.loaded needed
1321
- /******/ exports: {}
1322
- /******/ };
1323
- /******/
1324
- /******/ // Execute the module function
1325
- /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
1326
- /******/
1327
- /******/ // Return the exports of the module
1328
- /******/ return module.exports;
1329
- /******/ }
1330
- /******/
1331
- /************************************************************************/
1332
- /******/
1333
- /******/ // startup
1334
- /******/ // Load entry module and return exports
1335
- /******/ // This entry module is referenced by other modules so it can't be inlined
1336
- /******/ var __webpack_exports__ = __webpack_require__("./src/plugin/regions/index.js");
1337
- /******/
1338
- /******/ return __webpack_exports__;
1339
- /******/ })()
1340
- ;
1341
- });
1342
- //# sourceMappingURL=wavesurfer.regions.js.map