diff --git a/src/code_manager/model/CodeMirrorEditor.js b/src/code_manager/model/CodeMirrorEditor.js index 808f205ff..6ecd89b3c 100644 --- a/src/code_manager/model/CodeMirrorEditor.js +++ b/src/code_manager/model/CodeMirrorEditor.js @@ -1,9 +1,15 @@ import { bindAll } from 'underscore'; import Backbone from 'backbone'; -import CodeMirror from 'codemirror/lib/codemirror'; -import 'codemirror/mode/htmlmixed/htmlmixed'; -import 'codemirror/mode/css/css'; -import 'codemirror-formatting'; +import { hasWin } from 'utils/mixins'; + +let CodeMirror; + +if (hasWin()) { + CodeMirror = require('codemirror/lib/codemirror'); + require('codemirror/mode/htmlmixed/htmlmixed'); + require('codemirror/mode/css/css'); + require('codemirror-formatting'); +} export default Backbone.Model.extend({ CodeMirror, diff --git a/src/css_composer/model/CssRule.js b/src/css_composer/model/CssRule.js index 58a76ff60..1607dad77 100644 --- a/src/css_composer/model/CssRule.js +++ b/src/css_composer/model/CssRule.js @@ -3,9 +3,9 @@ import { Model } from 'backbone'; import Styleable from 'domain_abstract/model/Styleable'; import { isEmpty, forEach, isString } from 'underscore'; import Selectors from 'selector_manager/model/Selectors'; -import { isEmptyObj } from 'utils/mixins'; +import { isEmptyObj, hasWin } from 'utils/mixins'; -const { CSS } = window; +const { CSS } = hasWin() ? window : {}; export default class CssRule extends Model.extend(Styleable) { defaults() { diff --git a/src/keymaps/index.js b/src/keymaps/index.js index 5c30b77d2..7528b17d9 100644 --- a/src/keymaps/index.js +++ b/src/keymaps/index.js @@ -32,7 +32,10 @@ */ import { isString } from 'underscore'; -import keymaster from 'keymaster'; +import { hasWin } from 'utils/mixins'; +import keymaster from 'utils/keymaster'; + +hasWin() && keymaster.init(window); export default () => { let em; diff --git a/src/utils/ColorPicker.js b/src/utils/ColorPicker.js index 62d8ef097..bdbc9c484 100644 --- a/src/utils/ColorPicker.js +++ b/src/utils/ColorPicker.js @@ -4,9 +4,11 @@ // https://github.com/bgrins/spectrum // Author: Brian Grinstead // License: MIT +import { hasWin } from 'utils/mixins'; export default function($, undefined) { 'use strict'; + if (!hasWin()) return; var defaultOpts = { // Callbacks diff --git a/src/utils/extender.js b/src/utils/extender.js index 55991b2e9..7e4fcb777 100644 --- a/src/utils/extender.js +++ b/src/utils/extender.js @@ -1,7 +1,7 @@ import { isObject, isString, each, isUndefined } from 'underscore'; export default ({ $ }) => { - if ($ && $.prototype.constructor.name !== 'jQuery') { + if ($ && $.prototype && $.prototype.constructor.name !== 'jQuery') { const fn = $.fn; // Additional helpers diff --git a/src/utils/fetch.js b/src/utils/fetch.js index 8db987abb..951b369c1 100644 --- a/src/utils/fetch.js +++ b/src/utils/fetch.js @@ -1,8 +1,11 @@ import Promise from 'promise-polyfill'; +import { hasWin } from 'utils/mixins'; -window.Promise = window.Promise || Promise; +if (hasWin()) { + window.Promise = window.Promise || Promise; +} -export default (typeof fetch == 'function' +export default typeof fetch == 'function' ? fetch.bind() : (url, options) => { return new Promise((res, rej) => { @@ -30,4 +33,4 @@ export default (typeof fetch == 'function' // Include body only if present options.body ? req.send(options.body) : req.send(); }); - }); + }; diff --git a/src/utils/mixins.js b/src/utils/mixins.js index 6e1a9ff27..1730d1b8f 100644 --- a/src/utils/mixins.js +++ b/src/utils/mixins.js @@ -1,6 +1,8 @@ import { keys, isUndefined, isElement, isArray } from 'underscore'; -const elProt = window.Element.prototype; +export const hasWin = () => typeof window !== 'undefined'; + +const elProt = hasWin() ? window.Element.prototype : {}; const matches = elProt.matches || elProt.webkitMatchesSelector || diff --git a/src/utils/polyfills.js b/src/utils/polyfills.js index 9b6a6e179..928fe6ce8 100644 --- a/src/utils/polyfills.js +++ b/src/utils/polyfills.js @@ -2,6 +2,7 @@ * File made for IE/Edge support * https://github.com/artf/grapesjs/issues/214 */ +import { hasWin } from './mixins'; export default () => { /** @@ -27,7 +28,7 @@ export default () => { return !!match; }; - if (isIE()) { + if (hasWin() && isIE()) { const originalCreateHTMLDocument = DOMImplementation.prototype.createHTMLDocument; DOMImplementation.prototype.createHTMLDocument = title => {