import { faker } from '@faker-js/faker'
import React, { useCallback, useEffect, useState } from 'react'
import { createEditor as slateCreateEditor, Editor } from 'slate'
import { Editable, Slate, withReact, useSelected } from 'slate-react'
const SUPPORTS_EVENT_TIMING =
typeof window !== 'undefined' && 'PerformanceEventTiming' in window
const SUPPORTS_LOAF_TIMING =
typeof window !== 'undefined' &&
'PerformanceLongAnimationFrameTiming' in window
const blocksOptions = [
2, 1000, 2500, 5000, 7500, 10000, 15000, 20000, 25000, 30000, 40000, 50000,
100000, 200000,
]
const chunkSizeOptions = [3, 10, 100, 1000]
const searchParams =
typeof document === 'undefined'
? null
: new URLSearchParams(document.location.search)
const parseNumber = (key, defaultValue) =>
parseInt(searchParams?.get(key) ?? '', 10) || defaultValue
const parseBoolean = (key, defaultValue) => {
const value = searchParams?.get(key)
if (value) return value === 'true'
return defaultValue
}
const parseEnum = (key, options, defaultValue) => {
const value = searchParams?.get(key)
if (value && options.includes(value)) return value
return defaultValue
}
const initialConfig = {
blocks: parseNumber('blocks', 10000),
chunking: parseBoolean('chunking', true),
chunkSize: parseNumber('chunk_size', 1000),
chunkDivs: parseBoolean('chunk_divs', true),
chunkOutlines: parseBoolean('chunk_outlines', false),
contentVisibilityMode: parseEnum(
'content_visibility',
['none', 'element', 'chunk'],
'chunk'
),
showSelectedHeadings: parseBoolean('selected_headings', false),
}
const setSearchParams = config => {
if (searchParams) {
searchParams.set('blocks', config.blocks.toString())
searchParams.set('chunking', config.chunking ? 'true' : 'false')
searchParams.set('chunk_size', config.chunkSize.toString())
searchParams.set('chunk_divs', config.chunkDivs ? 'true' : 'false')
searchParams.set('chunk_outlines', config.chunkOutlines ? 'true' : 'false')
searchParams.set('content_visibility', config.contentVisibilityMode)
searchParams.set(
'selected_headings',
config.showSelectedHeadings ? 'true' : 'false'
)
history.replaceState({}, '', `?${searchParams.toString()}`)
}
}
const cachedInitialValue = []
const getInitialValue = blocks => {
if (cachedInitialValue.length >= blocks) {
return cachedInitialValue.slice(0, blocks)
}
faker.seed(1)
for (let i = cachedInitialValue.length; i < blocks; i++) {
if (i % 100 === 0) {
const heading = {
type: 'heading-one',
children: [{ text: faker.lorem.sentence() }],
}
cachedInitialValue.push(heading)
} else {
const paragraph = {
type: 'paragraph',
children: [{ text: faker.lorem.paragraph() }],
}
cachedInitialValue.push(paragraph)
}
}
return cachedInitialValue.slice()
}
const initialInitialValue =
typeof window === 'undefined' ? [] : getInitialValue(initialConfig.blocks)
const createEditor = config => {
const editor = withReact(slateCreateEditor())
editor.getChunkSize = node =>
config.chunking && Editor.isEditor(node) ? config.chunkSize : null
return editor
}
const HugeDocumentExample = () => {
const [rendering, setRendering] = useState(false)
const [config, baseSetConfig] = useState(initialConfig)
const [initialValue, setInitialValue] = useState(initialInitialValue)
const [editor, setEditor] = useState(() => createEditor(config))
const [editorVersion, setEditorVersion] = useState(0)
const setConfig = useCallback(
partialConfig => {
const newConfig = { ...config, ...partialConfig }
setRendering(true)
baseSetConfig(newConfig)
setSearchParams(newConfig)
setTimeout(() => {
setRendering(false)
setInitialValue(getInitialValue(newConfig.blocks))
setEditor(createEditor(newConfig))
setEditorVersion(n => n + 1)
})
},
[config]
)
const renderElement = useCallback(
props => (
{config.chunking && ( <>
{config.chunkDivs && (
)}
> )}
Last keypress (ms):{' '} {SUPPORTS_EVENT_TIMING ? lastKeyPressDuration ?? '-' : 'Not supported'}
Average of last 10 keypresses (ms):{' '} {SUPPORTS_EVENT_TIMING ? averageKeyPressDuration ?? '-' : 'Not supported'}
Last long animation frame (ms):{' '} {SUPPORTS_LOAF_TIMING ? lastLongAnimationFrameDuration ?? '-' : 'Not supported'}
{SUPPORTS_EVENT_TIMING && lastKeyPressDuration === null && (Events shorter than 16ms may not be detected.
)}