diff --git a/index.ts b/index.ts index 1a97b5e4495..d4cff6ca7b8 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1 @@ -import './src/env/browser'; - export * from './fabric'; diff --git a/src/env/browser.ts b/src/env/browser.ts index 8fd9bd59876..3376f42ca12 100644 --- a/src/env/browser.ts +++ b/src/env/browser.ts @@ -1,31 +1,28 @@ /* eslint-disable no-restricted-globals */ -import { setEnv } from '.'; import { config } from '../config'; import { TCopyPasteData, TFabricEnv } from './types'; const copyPasteData: TCopyPasteData = {}; -const fabricDocument = - document instanceof - (typeof HTMLDocument !== 'undefined' ? HTMLDocument : Document) - ? document - : document.implementation.createHTMLDocument(''); - -config.configure({ - devicePixelRatio: window.devicePixelRatio || 1, -}); +let initialized = false; +let isTouchSupported: boolean; export const getEnv = (): TFabricEnv => { + if (!initialized) { + config.configure({ + devicePixelRatio: window.devicePixelRatio || 1, + }); + isTouchSupported = + 'ontouchstart' in window || + 'ontouchstart' in document || + (window && window.navigator && window.navigator.maxTouchPoints > 0); + initialized = true; + } return { - document: fabricDocument, + document, window, - isTouchSupported: - 'ontouchstart' in window || - 'ontouchstart' in fabricDocument || - (window && window.navigator && window.navigator.maxTouchPoints > 0), + isTouchSupported, isLikelyNode: false, copyPasteData, }; }; - -setEnv(getEnv()); diff --git a/src/env/index.ts b/src/env/index.ts index 91baf1c076b..04325462569 100644 --- a/src/env/index.ts +++ b/src/env/index.ts @@ -1,4 +1,5 @@ import { TFabricEnv } from './types'; +import { getEnv as getBrowserEnv } from './browser'; let env: TFabricEnv; @@ -6,11 +7,11 @@ export const setEnv = (value: TFabricEnv) => { env = value; }; -export const getEnv = () => env; +export const getEnv = () => env || getBrowserEnv(); -export const getDocument = (): Document => env.document; +export const getDocument = (): Document => getEnv().document; -export const getWindow = (): Window => env.window; +export const getWindow = (): Window => getEnv().window; export const setEnvForTests = (window: Window) => { env.document = window.document;