|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { createUniver, defaultTheme, LocaleType, merge } from '@univerjs/presets' |
| 4 | +import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core' |
| 5 | +import sheetsCoreEnUS from '@univerjs/presets/preset-sheets-core/locales/en-US' |
| 6 | +import sheetsCoreZhCN from '@univerjs/presets/preset-sheets-core/locales/zh-CN' |
| 7 | +import { useTheme } from 'nextra-theme-docs' |
| 8 | +import { useEffect, useRef } from 'react' |
| 9 | +import { Preview } from '@/components/preview' |
| 10 | +import { useCodeHighlight } from '@/hooks/use-code' |
| 11 | +import { UniverSheetsCustomShortcutPlugin } from './plugin' |
| 12 | + |
| 13 | +const code = ` |
| 14 | +import { createUniver, defaultTheme, LocaleType, merge } from '@univerjs/presets' |
| 15 | +
|
| 16 | +import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core' |
| 17 | +import sheetsCoreEnUS from '@univerjs/presets/preset-sheets-core/locales/en-US' |
| 18 | +import '@univerjs/presets/lib/styles/preset-sheets-core.css' |
| 19 | +
|
| 20 | +import { UniverSheetsCustomShortcutPlugin } from './plugin' |
| 21 | +
|
| 22 | +const { univerAPI } = createUniver({ |
| 23 | + locale: LocaleType.EN_US, |
| 24 | + locales: { |
| 25 | + [LocaleType.EN_US]: merge( |
| 26 | + {}, |
| 27 | + sheetsCoreEnUS, |
| 28 | + ), |
| 29 | + }, |
| 30 | + theme: defaultTheme, |
| 31 | + presets: [ |
| 32 | + UniverSheetsCorePreset(), |
| 33 | + ], |
| 34 | + plugins: [ |
| 35 | + UniverSheetsCustomShortcutPlugin, |
| 36 | + ], |
| 37 | +}) |
| 38 | +
|
| 39 | +univerAPI.createWorkbook({}) |
| 40 | +` |
| 41 | + |
| 42 | +interface IDemoProps { |
| 43 | + lang: 'zh-CN' | 'en-US' |
| 44 | +} |
| 45 | + |
| 46 | +const localesMap = { |
| 47 | + 'zh-CN': { |
| 48 | + name: 'ZH_CN', |
| 49 | + locale: LocaleType.ZH_CN, |
| 50 | + locales: merge({}, sheetsCoreZhCN), |
| 51 | + }, |
| 52 | + 'en-US': { |
| 53 | + name: 'EN_US', |
| 54 | + locale: LocaleType.EN_US, |
| 55 | + locales: merge({}, sheetsCoreEnUS), |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | +export default function Demo(props: IDemoProps) { |
| 60 | + const { lang } = props |
| 61 | + |
| 62 | + const containerRef = useRef<HTMLDivElement>(null!) |
| 63 | + |
| 64 | + const { theme } = useTheme() |
| 65 | + |
| 66 | + const { locale, locales } = localesMap[lang] |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + const { univerAPI } = createUniver({ |
| 70 | + darkMode: theme === 'dark', |
| 71 | + locale, |
| 72 | + locales: { |
| 73 | + [locale]: locales, |
| 74 | + }, |
| 75 | + theme: defaultTheme, |
| 76 | + presets: [ |
| 77 | + UniverSheetsCorePreset({ |
| 78 | + container: containerRef.current, |
| 79 | + }), |
| 80 | + ], |
| 81 | + plugins: [ |
| 82 | + UniverSheetsCustomShortcutPlugin, |
| 83 | + ], |
| 84 | + }) |
| 85 | + |
| 86 | + univerAPI.createWorkbook({}) |
| 87 | + |
| 88 | + return () => { |
| 89 | + univerAPI.dispose() |
| 90 | + } |
| 91 | + }, []) |
| 92 | + |
| 93 | + const codeWithHighlight = useCodeHighlight({ |
| 94 | + code, |
| 95 | + lang, |
| 96 | + }) |
| 97 | + |
| 98 | + return ( |
| 99 | + <Preview ref={containerRef} code={codeWithHighlight} /> |
| 100 | + ) |
| 101 | +} |
0 commit comments