|
| 1 | +import {memo, useState} from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + MarkdownEditorView, |
| 5 | + NumberInput, |
| 6 | + setHighlightedLine, |
| 7 | + useMarkdownEditor, |
| 8 | +} from '@gravity-ui/markdown-editor'; |
| 9 | +import type {MarkupLineNumbersConfig} from '@gravity-ui/markdown-editor'; |
| 10 | +import {Button, Flex} from '@gravity-ui/uikit'; |
| 11 | + |
| 12 | +import {PlaygroundLayout} from '../../../components/PlaygroundLayout'; |
| 13 | + |
| 14 | +const longMarkup = [ |
| 15 | + '# Markup Line Numbers Demo', |
| 16 | + '', |
| 17 | + 'This document demonstrates the new markup-mode features:', |
| 18 | + 'line numbers, line highlighting, and scroll-to-line.', |
| 19 | + '', |
| 20 | + '## Getting Started', |
| 21 | + '', |
| 22 | + 'The editor below is running in **markup mode** (CodeMirror 6).', |
| 23 | + 'You can see line numbers in the gutter on the left side.', |
| 24 | + '', |
| 25 | + '### Line Numbers', |
| 26 | + '', |
| 27 | + 'When `markupConfig.lineNumbers` is enabled, the editor', |
| 28 | + 'displays line numbers in the left gutter. This is useful', |
| 29 | + 'for referencing specific lines in documentation or code reviews.', |
| 30 | + '', |
| 31 | + '### Line Highlighting', |
| 32 | + '', |
| 33 | + 'When `lineHighlight()` extension is passed via `markupConfig.extensions`,', |
| 34 | + 'clicking on a line number in the gutter will highlight that entire line.', |
| 35 | + 'This extension automatically includes line numbers as well.', |
| 36 | + '', |
| 37 | + 'You can also programmatically highlight a line using the', |
| 38 | + '`setHighlightedLine` StateEffect exported from the package.', |
| 39 | + '', |
| 40 | + '### Scroll to Line', |
| 41 | + '', |
| 42 | + 'The `markupConfig.lineNumbers.scrollToLine` option allows you to scroll the', |
| 43 | + 'editor to a specific line (0-based) on mount. This is useful', |
| 44 | + 'when you want to draw attention to a particular section of', |
| 45 | + 'a long document.', |
| 46 | + '', |
| 47 | + '## Example Content', |
| 48 | + '', |
| 49 | + 'Here is some additional content to make the document long', |
| 50 | + 'enough to demonstrate scrolling behavior.', |
| 51 | + '', |
| 52 | + '### Lists', |
| 53 | + '', |
| 54 | + '- Item one', |
| 55 | + '- Item two', |
| 56 | + '- Item three', |
| 57 | + '- Item four', |
| 58 | + '- Item five', |
| 59 | + '', |
| 60 | + '### Code Block', |
| 61 | + '', |
| 62 | + '```typescript', |
| 63 | + "import {lineHighlight, useMarkdownEditor} from '@gravity-ui/markdown-editor';", |
| 64 | + '', |
| 65 | + 'const editor = useMarkdownEditor({', |
| 66 | + " initial: {mode: 'markup'},", |
| 67 | + ' markupConfig: {', |
| 68 | + ' extensions: [lineHighlight()],', |
| 69 | + ' },', |
| 70 | + '});', |
| 71 | + '```', |
| 72 | + '', |
| 73 | + '### Table', |
| 74 | + '', |
| 75 | + '| Feature | Option | Default |', |
| 76 | + '|---------|--------|---------|', |
| 77 | + '| Line numbers | `markupConfig.lineNumbers` | `false` |', |
| 78 | + '| Highlight line | `markupConfig.extensions: [lineHighlight()]` | — |', |
| 79 | + '| Scroll to line | `markupConfig.lineNumbers.scrollToLine` | `undefined` |', |
| 80 | + '', |
| 81 | + '### More Text', |
| 82 | + '', |
| 83 | + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', |
| 84 | + 'Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', |
| 85 | + 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.', |
| 86 | + '', |
| 87 | + 'Duis aute irure dolor in reprehenderit in voluptate velit esse', |
| 88 | + 'cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat', |
| 89 | + 'cupidatat non proident, sunt in culpa qui officia deserunt mollit.', |
| 90 | + '', |
| 91 | + '### Final Section', |
| 92 | + '', |
| 93 | + 'This is the end of the demo document. If `scrollToLine` is set', |
| 94 | + 'to a value like 20, the editor should scroll to approximately', |
| 95 | + 'this area of the document on initialization.', |
| 96 | + '', |
| 97 | + '> **Tip:** Try clicking on line numbers in the gutter to highlight lines!', |
| 98 | +].join('\n'); |
| 99 | + |
| 100 | +export type MarkupLineNumbersEditorProps = { |
| 101 | + lineNumbers?: MarkupLineNumbersConfig; |
| 102 | +}; |
| 103 | + |
| 104 | +export const MarkupLineNumbersEditor = memo<MarkupLineNumbersEditorProps>( |
| 105 | + function MarkupLineNumbersEditor({lineNumbers}) { |
| 106 | + const [fromLine, setFromLine] = useState<number | undefined>( |
| 107 | + lineNumbers?.initialSelectedLines?.from ?? 0, |
| 108 | + ); |
| 109 | + const [toLine, setToLine] = useState<number | undefined>( |
| 110 | + lineNumbers?.initialSelectedLines?.to ?? 0, |
| 111 | + ); |
| 112 | + const [lastClickedLine, setLastClickedLine] = useState<number | null>(null); |
| 113 | + |
| 114 | + const markupLineNumbers: MarkupLineNumbersConfig | undefined = lineNumbers |
| 115 | + ? { |
| 116 | + ...lineNumbers, |
| 117 | + onLineClick: (line) => setLastClickedLine(line), |
| 118 | + } |
| 119 | + : undefined; |
| 120 | + |
| 121 | + const editor = useMarkdownEditor( |
| 122 | + { |
| 123 | + initial: { |
| 124 | + mode: 'markup', |
| 125 | + markup: longMarkup, |
| 126 | + }, |
| 127 | + markupConfig: { |
| 128 | + lineNumbers: markupLineNumbers, |
| 129 | + }, |
| 130 | + }, |
| 131 | + [], |
| 132 | + ); |
| 133 | + |
| 134 | + const handleHighlightLine = () => { |
| 135 | + if (typeof fromLine !== 'number' || Number.isNaN(fromLine)) return; |
| 136 | + if (typeof toLine !== 'number' || Number.isNaN(toLine)) return; |
| 137 | + |
| 138 | + const cm = (editor as any).cm; |
| 139 | + if (cm) { |
| 140 | + cm.dispatch({effects: setHighlightedLine.of({from: fromLine, to: toLine})}); |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + return ( |
| 145 | + <PlaygroundLayout |
| 146 | + title="Markup Line Numbers" |
| 147 | + editor={editor} |
| 148 | + actions={({className}) => |
| 149 | + lineNumbers?.highlightLines ? ( |
| 150 | + <Flex gap="2" className={className} alignItems="center"> |
| 151 | + <span>From line:</span> |
| 152 | + <NumberInput |
| 153 | + size="s" |
| 154 | + value={fromLine} |
| 155 | + onUpdate={setFromLine} |
| 156 | + min={0} |
| 157 | + style={{width: '72px'}} |
| 158 | + /> |
| 159 | + <span>To line:</span> |
| 160 | + <NumberInput |
| 161 | + size="s" |
| 162 | + value={toLine} |
| 163 | + onUpdate={setToLine} |
| 164 | + min={0} |
| 165 | + style={{width: '72px'}} |
| 166 | + /> |
| 167 | + <Button size="s" onClick={handleHighlightLine}> |
| 168 | + Highlight |
| 169 | + </Button> |
| 170 | + <Button |
| 171 | + size="s" |
| 172 | + view="outlined" |
| 173 | + onClick={() => { |
| 174 | + const cm = (editor as any).cm; |
| 175 | + if (cm) { |
| 176 | + cm.dispatch({effects: setHighlightedLine.of(null)}); |
| 177 | + } |
| 178 | + }} |
| 179 | + > |
| 180 | + Clear |
| 181 | + </Button> |
| 182 | + {lastClickedLine !== null && ( |
| 183 | + <span>Last clicked: line {lastClickedLine + 1}</span> |
| 184 | + )} |
| 185 | + </Flex> |
| 186 | + ) : null |
| 187 | + } |
| 188 | + view={({className}) => ( |
| 189 | + <MarkdownEditorView |
| 190 | + autofocus |
| 191 | + stickyToolbar |
| 192 | + settingsVisible |
| 193 | + editor={editor} |
| 194 | + className={className} |
| 195 | + /> |
| 196 | + )} |
| 197 | + /> |
| 198 | + ); |
| 199 | + }, |
| 200 | +); |
0 commit comments