Skip to content

Commit

Permalink
prepare for SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 committed Jan 31, 2023
1 parent bb66394 commit c98d6e0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
2 changes: 0 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import './src/env/browser';

export * from './fabric';
31 changes: 14 additions & 17 deletions src/env/browser.ts
Original file line number Diff line number Diff line change
@@ -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());
7 changes: 4 additions & 3 deletions src/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { TFabricEnv } from './types';
import { getEnv as getBrowserEnv } from './browser';

let env: TFabricEnv;

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;
Expand Down

0 comments on commit c98d6e0

Please sign in to comment.