Skip to content

Commit 576e335

Browse files
committed
Do not throw error if imported in nodeJS context
1 parent 5fccf71 commit 576e335

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

src/BrowserInfo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function userAgent(pattern) {
2-
return !!/*@__PURE__*/navigator.userAgent.match(pattern);
2+
if (typeof window !== 'undefined' && window.navigator) {
3+
return !!/*@__PURE__*/navigator.userAgent.match(pattern);
4+
}
35
}
46

57
export const IE11OrLess = userAgent(/(?:Trident.*rv[ :]?11\.|msie|iemobile|Windows Phone)/i);

src/Sortable.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* @license MIT
66
*/
77

8-
import './windowCheck.js'; // Always import first
9-
108
import { version } from '../package.json';
119

1210
import { IE11OrLess, Edge, FireFox, Safari, IOS, ChromeForAndroid } from './BrowserInfo.js';
@@ -140,14 +138,17 @@ let dragEl,
140138
savedInputChecked = [];
141139

142140
/** @const */
143-
const PositionGhostAbsolutely = IOS,
141+
const documentExists = typeof document !== 'undefined',
142+
143+
PositionGhostAbsolutely = IOS,
144144

145145
CSSFloatProperty = Edge || IE11OrLess ? 'cssFloat' : 'float',
146146

147147
// This will not pass for IE9, because IE9 DnD only works on anchors
148-
supportDraggable = !ChromeForAndroid && !IOS && ('draggable' in document.createElement('div')),
148+
supportDraggable = documentExists && !ChromeForAndroid && !IOS && ('draggable' in document.createElement('div')),
149149

150150
supportCssPointerEvents = (function() {
151+
if (!documentExists) return;
151152
// false when <= IE11
152153
if (IE11OrLess) {
153154
return false;
@@ -297,15 +298,17 @@ let dragEl,
297298

298299

299300
// #1184 fix - Prevent click event on fallback if dragged but item not changed position
300-
document.addEventListener('click', function(evt) {
301-
if (ignoreNextClick) {
302-
evt.preventDefault();
303-
evt.stopPropagation && evt.stopPropagation();
304-
evt.stopImmediatePropagation && evt.stopImmediatePropagation();
305-
ignoreNextClick = false;
306-
return false;
307-
}
308-
}, true);
301+
if (documentExists) {
302+
document.addEventListener('click', function(evt) {
303+
if (ignoreNextClick) {
304+
evt.preventDefault();
305+
evt.stopPropagation && evt.stopPropagation();
306+
evt.stopImmediatePropagation && evt.stopImmediatePropagation();
307+
ignoreNextClick = false;
308+
return false;
309+
}
310+
}, true);
311+
}
309312

310313
let nearestEmptyInsertDetectEvent = function(evt) {
311314
if (dragEl) {
@@ -1613,7 +1616,7 @@ Sortable.prototype = /** @lends Sortable.prototype */ {
16131616
return options[name];
16141617
} else {
16151618
let modifiedValue = PluginManager.modifyOption(this, name, value);
1616-
if (typeof(modifiedValue) !== 'undefined') {
1619+
if (typeof modifiedValue !== 'undefined') {
16171620
options[name] = modifiedValue;
16181621
} else {
16191622
options[name] = value;
@@ -1884,11 +1887,13 @@ function _cancelNextTick(id) {
18841887
}
18851888

18861889
// Fixed #973:
1887-
on(document, 'touchmove', function(evt) {
1888-
if ((Sortable.active || awaitingDragStarted) && evt.cancelable) {
1889-
evt.preventDefault();
1890-
}
1891-
});
1890+
if (documentExists) {
1891+
on(document, 'touchmove', function(evt) {
1892+
if ((Sortable.active || awaitingDragStarted) && evt.cancelable) {
1893+
evt.preventDefault();
1894+
}
1895+
});
1896+
}
18921897

18931898

18941899
// Export utils

src/windowCheck.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)