Skip to content

Commit

Permalink
fix: 🐛 check if the navigator global is available before usage (antvi…
Browse files Browse the repository at this point in the history
  • Loading branch information
NewByVector authored Oct 13, 2022
1 parent c6ca043 commit 8a8d14a
Showing 1 changed file with 89 additions and 43 deletions.
132 changes: 89 additions & 43 deletions packages/x6/src/util/platform/index.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
const ua = navigator.userAgent
/* eslint-disable no-underscore-dangle */
let _IS_MAC = false
let _IS_IOS = false
let _IS_WINDOWS = false
let _IS_IE = false
let _IS_IE11 = false
let _IS_EDGE = false
let _IS_NETSCAPE = false
let _IS_CHROME_APP = false
let _IS_CHROME = false
let _IS_OPERA = false
let _IS_FIREFOX = false
let _IS_SAFARI = false
let _SUPPORT_TOUCH = false
let _SUPPORT_POINTER = false
let _SUPPORT_PASSIVE = false
let _NO_FOREIGNOBJECT = false

export namespace Platform {
export const IS_MAC = navigator.appVersion.indexOf('Mac') > 0
export const IS_IOS = !!ua.match(/(iPad|iPhone|iPod)/g)
export const IS_WINDOWS = navigator.appVersion.indexOf('Win') > 0
if (typeof navigator === 'object') {
const ua = navigator.userAgent
_IS_MAC = ua.indexOf('Macintosh') >= 0
_IS_IOS = !!ua.match(/(iPad|iPhone|iPod)/g)
_IS_WINDOWS = ua.indexOf('Windows') >= 0

export const IS_IE = ua.indexOf('MSIE') >= 0
export const IS_IE11 = !!ua.match(/Trident\/7\./)
export const IS_EDGE = !!ua.match(/Edge\//)
_IS_IE = ua.indexOf('MSIE') >= 0
_IS_IE11 = !!ua.match(/Trident\/7\./)
_IS_EDGE = !!ua.match(/Edge\//)

/**
* A flag indicating whether the browser is Netscape (including Firefox).
*/
export const IS_NETSCAPE =
_IS_NETSCAPE =
ua.indexOf('Mozilla/') >= 0 &&
ua.indexOf('MSIE') < 0 &&
ua.indexOf('Edge/') < 0

/**
* A flag indicating whether the the this is running inside a Chrome App.
*/
export const IS_CHROME_APP =
(window as any).chrome != null &&
(window as any).chrome.app != null &&
(window as any).chrome.app.runtime != null

export const IS_CHROME = ua.indexOf('Chrome/') >= 0 && ua.indexOf('Edge/') < 0
export const IS_OPERA = ua.indexOf('Opera/') >= 0 || ua.indexOf('OPR/') >= 0
export const IS_FIREFOX = ua.indexOf('Firefox/') >= 0
export const IS_SAFARI =
_IS_CHROME = ua.indexOf('Chrome/') >= 0 && ua.indexOf('Edge/') < 0
_IS_OPERA = ua.indexOf('Opera/') >= 0 || ua.indexOf('OPR/') >= 0
_IS_FIREFOX = ua.indexOf('Firefox/') >= 0
_IS_SAFARI =
ua.indexOf('AppleWebKit/') >= 0 &&
ua.indexOf('Chrome/') < 0 &&
ua.indexOf('Edge/') < 0

/**
* A flag indicating whether this device supports touchstart/-move/-end
* events (Apple iOS, Android, Chromebook and Chrome Browser on touch-enabled
* devices).
*/
export const SUPPORT_TOUCH = 'ontouchstart' in document.documentElement
if (typeof document === 'object') {
_NO_FOREIGNOBJECT =
!document.createElementNS ||
`${document.createElementNS(
'http://www.w3.org/2000/svg',
'foreignObject',
)}` !== '[object SVGForeignObjectElement]' ||
ua.indexOf('Opera/') >= 0
}
}

/**
* A flag indicating whether this device supports Microsoft pointer events.
*/
export const SUPPORT_POINTER = (window as any).PointerEvent != null && !IS_MAC
if (typeof window === 'object') {
_IS_CHROME_APP =
(window as any).chrome != null &&
(window as any).chrome.app != null &&
(window as any).chrome.app.runtime != null
_SUPPORT_POINTER = (window as any).PointerEvent != null && !_IS_MAC
}

export let SUPPORT_PASSIVE = false // eslint-disable-line import/no-mutable-exports
if (typeof document === 'object') {
_SUPPORT_TOUCH = 'ontouchstart' in document.documentElement

try {
const options = Object.defineProperty({}, 'passive', {
get() {
SUPPORT_PASSIVE = true
_SUPPORT_PASSIVE = true
},
})
const div = document.createElement('div')
Expand All @@ -60,18 +74,50 @@ export namespace Platform {
} catch (err) {
// pass
}
}
export namespace Platform {
export const IS_MAC = _IS_MAC
export const IS_IOS = _IS_IOS
export const IS_WINDOWS = _IS_WINDOWS

export const IS_IE = _IS_IE
export const IS_IE11 = _IS_IE11
export const IS_EDGE = _IS_EDGE

/**
* A flag indicating whether the browser is Netscape (including Firefox).
*/
export const IS_NETSCAPE = _IS_NETSCAPE

/**
* A flag indicating whether the the this is running inside a Chrome App.
*/
export const IS_CHROME_APP = _IS_CHROME_APP

export const IS_CHROME = _IS_CHROME
export const IS_OPERA = _IS_OPERA
export const IS_FIREFOX = _IS_FIREFOX
export const IS_SAFARI = _IS_SAFARI

/**
* A flag indicating whether this device supports touchstart/-move/-end
* events (Apple iOS, Android, Chromebook and Chrome Browser on touch-enabled
* devices).
*/
export const SUPPORT_TOUCH = _SUPPORT_TOUCH

/**
* A flag indicating whether this device supports Microsoft pointer events.
*/
export const SUPPORT_POINTER = _SUPPORT_POINTER

export const SUPPORT_PASSIVE = _SUPPORT_PASSIVE

/**
* A flag indicating whether foreignObject support is not available. This
* is the case for Opera, older SVG-based browsers and all versions of IE.
*/
export const NO_FOREIGNOBJECT =
!document.createElementNS ||
`${document.createElementNS(
'http://www.w3.org/2000/svg',
'foreignObject',
)}` !== '[object SVGForeignObjectElement]' ||
ua.indexOf('Opera/') >= 0
export const NO_FOREIGNOBJECT = _NO_FOREIGNOBJECT

export const SUPPORT_FOREIGNOBJECT = !NO_FOREIGNOBJECT
}
Expand Down

0 comments on commit 8a8d14a

Please sign in to comment.