-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathenv.ts
84 lines (78 loc) · 2.27 KB
/
env.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* eslint-disable no-restricted-globals */
import { config } from './config';
import type { Canvas } from 'canvas';
type TFabricEnv = {
document: Document;
window: Window;
isTouchSupported: boolean;
isLikelyNode: boolean;
nodeCanvas: Canvas;
jsdomImplForWrapper: any;
};
let fabricDocument: Document;
let fabricWindow: Window;
let isTouchSupported: boolean;
let isLikelyNode: boolean;
let nodeCanvas: Canvas;
let jsdomImplForWrapper: any;
function setupEnv() {
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
if (
document instanceof
(typeof HTMLDocument !== 'undefined' ? HTMLDocument : Document)
) {
fabricDocument = document;
} else {
fabricDocument = document.implementation.createHTMLDocument('');
}
fabricWindow = window;
isLikelyNode = false;
} else {
// assume we're running under node.js when document/window are not present
const jsdom = require('jsdom');
const virtualWindow = new jsdom.JSDOM(
decodeURIComponent(
'%3C!DOCTYPE%20html%3E%3Chtml%3E%3Chead%3E%3C%2Fhead%3E%3Cbody%3E%3C%2Fbody%3E%3C%2Fhtml%3E'
),
{
features: {
FetchExternalResources: ['img'],
},
resources: 'usable',
}
).window;
fabricDocument = virtualWindow.document;
jsdomImplForWrapper =
require('jsdom/lib/jsdom/living/generated/utils').implForWrapper;
nodeCanvas = require('jsdom/lib/jsdom/utils').Canvas;
isLikelyNode = true;
fabricWindow = virtualWindow;
global.DOMParser = (fabricWindow as any).DOMParser;
}
isTouchSupported =
'ontouchstart' in fabricWindow ||
'ontouchstart' in fabricDocument ||
(fabricWindow &&
fabricWindow.navigator &&
fabricWindow.navigator.maxTouchPoints > 0);
config.configure({
devicePixelRatio: fabricWindow.devicePixelRatio || 1,
});
}
setupEnv();
export const getEnv = (): TFabricEnv => {
return {
document: fabricDocument,
window: fabricWindow,
isTouchSupported,
isLikelyNode,
nodeCanvas,
jsdomImplForWrapper,
};
};
export const getDocument = (): Document => fabricDocument;
export const getWindow = (): Window => fabricWindow;
export const setEnvForTests = (window: Window) => {
fabricDocument = window.document;
fabricWindow = window;
};