From 942f21e7db9ee0d74fb5e2e729fcdae03405968b Mon Sep 17 00:00:00 2001 From: Michael Fix Date: Wed, 31 Jul 2019 15:50:32 -0700 Subject: [PATCH] Refactor mode loading into Carbon (#825) * refactor mode loading into Carbon * made alreadyLoaded Set a static property --- components/Carbon.js | 18 +++++++++++++++++- lib/constants.js | 15 --------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/components/Carbon.js b/components/Carbon.js index 06e5413..d94279c 100644 --- a/components/Carbon.js +++ b/components/Carbon.js @@ -13,7 +13,8 @@ import { LANGUAGE_NAME_HASH, LANGUAGE_MIME_HASH, DEFAULT_SETTINGS, - THEMES_HASH + THEMES_HASH, + LANGUAGES } from '../lib/constants' const Watermark = dynamic(() => import('./svg/Watermark'), { @@ -29,10 +30,25 @@ function searchLanguage(l) { } class Carbon extends React.PureComponent { + static modesLoaded = new Set() static defaultProps = { onChange: () => {} } + componentDidMount() { + LANGUAGES.filter(language => language.mode !== 'auto' && language.mode !== 'text').forEach( + language => { + if (language.mode && !Carbon.modesLoaded.has(language.mode)) { + // TODO useLayoutEffect + language.custom + ? require(`../lib/custom/modes/${language.mode}`) + : require(`codemirror/mode/${language.mode}/${language.mode}`) + Carbon.modesLoaded.add(language.mode) + } + } + ) + } + handleLanguageChange = debounce( (newCode, language) => { if (language === 'auto') { diff --git a/lib/constants.js b/lib/constants.js index ddba771..4dd4e7f 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -979,21 +979,6 @@ const unfold = (f, seed) => { return go(f, seed, []) }` -if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') { - const alreadyLoaded = new Set() - - LANGUAGES.filter(language => language.mode !== 'auto' && language.mode !== 'text').forEach( - language => { - if (language.mode && !alreadyLoaded.has(language.mode)) { - alreadyLoaded.add(language.mode) - language.custom - ? require(`./custom/modes/${language.mode}`) - : require(`codemirror/mode/${language.mode}/${language.mode}`) - } - } - ) -} - export const DEFAULT_SETTINGS = { paddingVertical: '56px', paddingHorizontal: '56px',