|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Fragment, useRef, useState } from 'react'; |
| 4 | +import CodeMirrorMerge, { CodeMirrorMergeProps } from 'react-codemirror-merge'; |
| 5 | +import { langs } from '@uiw/codemirror-extensions-langs'; |
| 6 | +import { originalCode, modifiedCode } from './code'; |
| 7 | + |
| 8 | +const Original = CodeMirrorMerge.Original; |
| 9 | +const Modified = CodeMirrorMerge.Modified; |
| 10 | + |
| 11 | +export const MergeExample = () => { |
| 12 | + const [orientation, setOrientation] = useState<CodeMirrorMergeProps['orientation']>('a-b'); |
| 13 | + const [revertControls, setRevertControls] = useState<CodeMirrorMergeProps['revertControls']>(); |
| 14 | + const [highlightChanges, setHighlightChanges] = useState<CodeMirrorMergeProps['highlightChanges']>(true); |
| 15 | + const [gutter, setGutter] = useState<CodeMirrorMergeProps['gutter']>(true); |
| 16 | + const [collapseUnchanged, setCollapseUnchanged] = useState<CodeMirrorMergeProps['collapseUnchanged']>(); |
| 17 | + const handleOrientation = (evn: React.ChangeEvent<HTMLSelectElement>) => { |
| 18 | + setOrientation(evn.target.value as CodeMirrorMergeProps['orientation']); |
| 19 | + }; |
| 20 | + const [originalValue, setOriginalValue] = useState(originalCode); |
| 21 | + const [modifiedValue, setModifiedValue] = useState(modifiedCode); |
| 22 | + const random = useRef<number>(); |
| 23 | + const click = () => { |
| 24 | + random.current = Math.floor(Math.random() * 101); |
| 25 | + const code = '// hello world' + random.current + '\n' + originalCode; |
| 26 | + setOriginalValue(code); |
| 27 | + }; |
| 28 | + return ( |
| 29 | + <Fragment> |
| 30 | + <button onClick={click}>Change Original Value {random.current}</button> |
| 31 | + <CodeMirrorMerge |
| 32 | + orientation={orientation} |
| 33 | + revertControls={revertControls} |
| 34 | + collapseUnchanged={collapseUnchanged} |
| 35 | + highlightChanges={highlightChanges} |
| 36 | + // destroyRerender={false} |
| 37 | + gutter={gutter} |
| 38 | + style={{ height: 300, overflow: 'auto' }} |
| 39 | + theme="dark" |
| 40 | + > |
| 41 | + <Original |
| 42 | + value={originalValue} |
| 43 | + extensions={[langs.javascript()]} |
| 44 | + onChange={(val) => { |
| 45 | + setOriginalValue(val); |
| 46 | + // console.log('::::::::::', val) |
| 47 | + }} |
| 48 | + /> |
| 49 | + <Modified |
| 50 | + value={modifiedValue} |
| 51 | + extensions={[ |
| 52 | + langs.javascript(), |
| 53 | + // EditorView.editable.of(false), |
| 54 | + // EditorState.readOnly.of(true) |
| 55 | + ]} |
| 56 | + onChange={(val) => { |
| 57 | + setModifiedValue(val); |
| 58 | + }} |
| 59 | + /> |
| 60 | + </CodeMirrorMerge> |
| 61 | + <label> |
| 62 | + Orientation |
| 63 | + <select onChange={handleOrientation} defaultValue={orientation}> |
| 64 | + <option value="">please orientation choose</option> |
| 65 | + <option value="a-b">a-b</option> |
| 66 | + <option value="b-a">b-a</option> |
| 67 | + </select> |
| 68 | + </label> |
| 69 | + <br /> |
| 70 | + <label> |
| 71 | + Revert buttons |
| 72 | + <select |
| 73 | + defaultValue={revertControls} |
| 74 | + onChange={(evn) => setRevertControls(evn.target.value as CodeMirrorMergeProps['revertControls'])} |
| 75 | + > |
| 76 | + <option value="">please revertControls choose</option> |
| 77 | + <option value="a-to-b">a-to-b</option> |
| 78 | + <option value="b-to-a">b-to-a</option> |
| 79 | + </select> |
| 80 | + </label> |
| 81 | + <br /> |
| 82 | + <label> |
| 83 | + Highlight changes |
| 84 | + <input |
| 85 | + type="checkbox" |
| 86 | + checked={!!highlightChanges} |
| 87 | + onChange={(evn) => setHighlightChanges(evn.target.checked)} |
| 88 | + /> |
| 89 | + </label> |
| 90 | + <br /> |
| 91 | + <label> |
| 92 | + Gutter markers |
| 93 | + <input type="checkbox" checked={!!gutter} onChange={(evn) => setGutter(evn.target.checked)} /> |
| 94 | + </label> |
| 95 | + <label> |
| 96 | + Collapse unchanged code |
| 97 | + <input |
| 98 | + type="checkbox" |
| 99 | + checked={!!collapseUnchanged} |
| 100 | + onChange={(evn) => setCollapseUnchanged(evn.target.checked ? {} : undefined)} |
| 101 | + /> |
| 102 | + </label> |
| 103 | + </Fragment> |
| 104 | + ); |
| 105 | +}; |
0 commit comments