Skip to content

Commit f73d7f5

Browse files
committed
Squashed commit of the following:
commit 26eca32 Author: Carl Smith <[email protected]> Date: Tue Apr 15 00:13:33 2025 +1200 Lazy load themes commit bc54006 Author: Carl Smith <[email protected]> Date: Mon Apr 14 23:59:44 2025 +1200 Lazy load source indicator commit c5951c9 Author: Carl Smith <[email protected]> Date: Mon Apr 14 23:53:28 2025 +1200 Remove rimraf commit cea35df Author: Carl Smith <[email protected]> Date: Mon Apr 14 23:42:25 2025 +1200 Update scripts and vite config commit a49d5a4 Author: Carl Smith <[email protected]> Date: Mon Apr 14 18:02:33 2025 +1200 Linter fixes commit 0828ac8 Author: Carl Smith <[email protected]> Date: Mon Apr 14 17:34:56 2025 +1200 Bunch of linter fixes commit fa55ab9 Author: Carl Smith <[email protected]> Date: Mon Apr 14 16:17:06 2025 +1200 Install and configure ESlint
1 parent 071d47c commit f73d7f5

34 files changed

+1324
-1402
lines changed

.eslintignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

.eslintrc.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

demo/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
"type": "module",
66
"homepage": "https://carlosnz.github.io/json-edit-react",
77
"scripts": {
8-
"prepareLaunch": "rimraf ./src/json-edit-react && mkdir ./src/json-edit-react && mkdir ./src/json-edit-react/src && node src/imports/swap_import.cjs",
9-
"launch": "concurrently --kill-others-on-fail \"vite\" \"nodemon watch.cjs\"",
8+
"prepareLaunch": "node scripts/prepareLaunch.mjs && node src/imports/swapImport.mjs",
9+
"launch": "concurrently --kill-others-on-fail \"vite\" \"nodemon watch.mjs\"",
1010
"start": "yarn prepareLaunch && yarn launch",
1111
"start:package": "cross-env VITE_JRE_SOURCE=package yarn prepareLaunch && VITE_JRE_SOURCE=package yarn launch",
1212
"dev": "cross-env VITE_JRE_SOURCE=local yarn prepareLaunch && VITE_JRE_SOURCE=local yarn launch",
13-
"build": "node src/imports/swap_import.cjs && tsc -b && vite build",
13+
"build": "node src/imports/swapImport.mjs && tsc -b && vite build",
1414
"build-local": "yarn build --base=/",
15-
"prebuild": "node ./scripts/getVersion.cjs",
1615
"predeploy": "yarn build",
1716
"deploy": "gh-pages -d build",
1817
"lint": "eslint .",
1918
"serve": "vite preview",
2019
"serve-local": "vite preview --base=/",
21-
"check": "tsc -b --noEmit"
20+
"compile": "tsc -b --noEmit"
2221
},
2322
"dependencies": {
2423
"@chakra-ui/icons": "^2.1.1",
@@ -46,6 +45,7 @@
4645
},
4746
"devDependencies": {
4847
"@eslint/js": "^9.21.0",
48+
"@types/fs-extra": "^11.0.4",
4949
"@types/node": "^20.11.6",
5050
"@types/react": "^19.0.0",
5151
"@types/react-dom": "^19.0.0",
@@ -55,11 +55,11 @@
5555
"eslint": "^9.21.0",
5656
"eslint-plugin-react-hooks": "^5.1.0",
5757
"eslint-plugin-react-refresh": "^0.4.19",
58+
"fs-extra": "^11.3.0",
5859
"gh-pages": "^6.1.1",
5960
"globals": "^15.15.0",
6061
"node-fetch": "^3.3.2",
6162
"nodemon": "^3.0.3",
62-
"rimraf": "^6.0.1",
6363
"typescript": "~5.7.2",
6464
"typescript-eslint": "^8.24.1",
6565
"vite": "^6.2.0"

demo/scripts/getVersion.cjs

Lines changed: 0 additions & 13 deletions
This file was deleted.

demo/scripts/prepareLaunch.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import path from 'path'
2+
import fs from 'fs-extra'
3+
4+
fs.emptyDirSync('./src/json-edit-react')
5+
fs.copySync(path.join('..', 'src'), path.join('src', 'json-edit-react', 'src'))
6+
fs.copySync(path.join('..', 'package.json'), path.join('src', 'json-edit-react', 'package.json'))

demo/src/App.tsx

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,9 @@ import {
99
JsonData,
1010
OnErrorFunction,
1111
defaultTheme,
12-
// Additional Themes
13-
githubDarkTheme,
14-
githubLightTheme,
15-
monoLightTheme,
16-
monoDarkTheme,
17-
candyWrapperTheme,
18-
psychedelicTheme,
1912
// ExternalTriggers,
2013
// type CollapseState
2114
} from './imports'
22-
import SourceIndicator from './SourceIndicator'
2315
import { FaNpm, FaExternalLinkAlt, FaGithub } from 'react-icons/fa'
2416
import { BiReset } from 'react-icons/bi'
2517
import { AiOutlineCloudUpload } from 'react-icons/ai'
@@ -55,10 +47,10 @@ import { ArrowBackIcon, ArrowForwardIcon, InfoIcon } from '@chakra-ui/icons'
5547
import { demoDataDefinitions } from './demoData'
5648
import { useDatabase } from './useDatabase'
5749
import './style.css'
58-
import { timestamp, version } from './version'
5950
import { getLineHeight, truncate } from './helpers'
6051

6152
const CodeEditor = lazy(() => import('./CodeEditor'))
53+
const SourceIndicator = lazy(() => import('./SourceIndicator'))
6254

6355
interface AppState {
6456
rootName: string
@@ -79,18 +71,23 @@ interface AppState {
7971
customTextEditor: boolean
8072
}
8173

82-
const themes = [
83-
defaultTheme,
84-
githubDarkTheme,
85-
githubLightTheme,
86-
monoLightTheme,
87-
monoDarkTheme,
88-
candyWrapperTheme,
89-
psychedelicTheme,
74+
// Additional themes are loaded dynamically when needed
75+
76+
const themeNames = [
77+
'Default',
78+
'Github Dark',
79+
'Github Light',
80+
'White & Black',
81+
'Black & White',
82+
'Candy Wrapper',
83+
'Psychedelic',
9084
]
9185

92-
console.log(`json-edit-react v${version}`)
93-
console.log('Site built:', timestamp)
86+
// Only default theme is loaded initially
87+
const themes = [defaultTheme]
88+
89+
console.log(`json-edit-react v${__VERSION__}`)
90+
console.log(`Site built: ${__BUILD_TIME__}`)
9491

9592
function App() {
9693
const navigate = useLocation()[1]
@@ -231,9 +228,39 @@ function App() {
231228
else navigate(`./?data=${selected}`)
232229
}
233230

234-
const handleThemeChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
235-
const theme = themes.find((th) => th.displayName === e.target.value)
231+
const handleThemeChange = async (e: React.ChangeEvent<HTMLSelectElement>) => {
232+
const themeName = e.target.value
233+
234+
// If theme is already loaded, use it
235+
let theme = themes.find((th) => th.displayName === themeName)
236+
237+
// If theme is not loaded yet, load it using LazyThemes
238+
if (!theme && themeName !== 'Default') {
239+
try {
240+
// Create a function name for the getter based on theme name
241+
const functionName = `get${themeName.replace(/\s+&\s+|\s+/g, '')}Theme`
242+
243+
// Dynamically import the themes module
244+
const lazyThemesModule = await import('./theme/LazyThemes')
245+
246+
// Get the theme using the themeGetters map
247+
if (lazyThemesModule.themeGetters[functionName]) {
248+
const newTheme = lazyThemesModule.themeGetters[functionName]()
249+
250+
// Add to available themes to avoid loading again
251+
if (newTheme) {
252+
themes.push(newTheme)
253+
theme = newTheme
254+
}
255+
}
256+
} catch (error) {
257+
console.error('Failed to load theme:', error)
258+
return
259+
}
260+
}
261+
236262
if (!theme) return
263+
237264
updateState({ theme })
238265
if (selectedDataSet === 'editTheme') {
239266
setData(theme)
@@ -291,7 +318,10 @@ function App() {
291318
minH="100%"
292319
>
293320
<HStack w="100%" justify="space-between" align="flex-start">
294-
<SourceIndicator />
321+
<Suspense fallback={null}>
322+
<SourceIndicator />
323+
</Suspense>
324+
295325
<VStack align="flex-start" gap={3}>
296326
<HStack align="flex-end" mt={2} gap={4} flexWrap="wrap">
297327
<Flex gap={4} align="center">
@@ -626,9 +656,9 @@ function App() {
626656
</FormLabel>
627657
<div className="inputWidth" style={{ flexGrow: 1 }}>
628658
<Select id="themeSelect" onChange={handleThemeChange} value={theme.displayName}>
629-
{themes.map((theme) => (
630-
<option value={theme.displayName} key={theme.displayName}>
631-
{theme.displayName}
659+
{themeNames.map((themeName) => (
660+
<option value={themeName} key={themeName}>
661+
{themeName}
632662
</option>
633663
))}
634664
</Select>
@@ -826,7 +856,7 @@ function App() {
826856
</Flex>
827857
<Box h={50} />
828858
<footer>
829-
<Text fontSize="sm">{`json-edit-react v${version}`}</Text>
859+
<Text fontSize="sm">{`json-edit-react v${__VERSION__}`}</Text>
830860
</footer>
831861
</div>
832862
)

demo/src/demoData/dataDefinitions.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @ts-nocheck
2+
13
import React from 'react'
24
import { data } from './data'
35
import { Flex, Box, Link, Text, UnorderedList, ListItem } from '@chakra-ui/react'

demo/src/imports/swap_import.cjs renamed to demo/src/imports/swapImport.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
* to npm (import.published.ts)
1010
*/
1111

12-
const fs = require('fs')
13-
const path = require('path')
12+
import fs from 'fs'
13+
import path from 'path'
1414

1515
// Read the provider from the environment variable
1616
const provider = process.env.VITE_JRE_SOURCE ?? 'published'
1717

18-
const src = path.resolve(__dirname, `import.${provider}.ts`)
19-
const dest = path.resolve(__dirname, 'import.ts')
18+
const src = path.resolve('src/imports', `import.${provider}.ts`)
19+
const dest = path.resolve('src/imports', 'import.ts')
2020

2121
if (!fs.existsSync(src)) {
2222
console.error(`❌ No file found for provider "${provider}". Expected at: ${src}`)

demo/src/theme/LazyThemes.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {
2+
githubDarkTheme,
3+
githubLightTheme,
4+
monoLightTheme,
5+
monoDarkTheme,
6+
candyWrapperTheme,
7+
psychedelicTheme,
8+
Theme
9+
} from '../imports'
10+
11+
// This file contains functions that return theme objects
12+
// Each function is dynamically imported when needed to reduce initial bundle size
13+
14+
export const getGithubDarkTheme = (): Theme => githubDarkTheme
15+
export const getGithubLightTheme = (): Theme => githubLightTheme
16+
export const getWhiteBlackTheme = (): Theme => monoLightTheme
17+
export const getBlackWhiteTheme = (): Theme => monoDarkTheme
18+
export const getCandyWrapperTheme = (): Theme => candyWrapperTheme
19+
export const getPsychedelicTheme = (): Theme => psychedelicTheme
20+
21+
// Allow dynamic accessing of theme getter functions
22+
interface ThemeGetters {
23+
[key: string]: () => Theme
24+
}
25+
26+
// Export a map for safer dynamic access
27+
export const themeGetters: ThemeGetters = {
28+
getGithubDarkTheme,
29+
getGithubLightTheme,
30+
getWhiteBlackTheme,
31+
getBlackWhiteTheme,
32+
getCandyWrapperTheme,
33+
getPsychedelicTheme
34+
}

demo/src/version.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)