-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(merge): fix
onChange
issue. (#566)
- Loading branch information
1 parent
92c4abe
commit b84b763
Showing
5 changed files
with
252 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use client'; | ||
|
||
import { Fragment, useRef, useState } from 'react'; | ||
import CodeMirrorMerge, { CodeMirrorMergeProps } from 'react-codemirror-merge'; | ||
import { langs } from '@uiw/codemirror-extensions-langs'; | ||
import { originalCode, modifiedCode } from './code'; | ||
|
||
const Original = CodeMirrorMerge.Original; | ||
const Modified = CodeMirrorMerge.Modified; | ||
|
||
export const MergeExample = () => { | ||
const [orientation, setOrientation] = useState<CodeMirrorMergeProps['orientation']>('a-b'); | ||
const [revertControls, setRevertControls] = useState<CodeMirrorMergeProps['revertControls']>(); | ||
const [highlightChanges, setHighlightChanges] = useState<CodeMirrorMergeProps['highlightChanges']>(true); | ||
const [gutter, setGutter] = useState<CodeMirrorMergeProps['gutter']>(true); | ||
const [collapseUnchanged, setCollapseUnchanged] = useState<CodeMirrorMergeProps['collapseUnchanged']>(); | ||
const handleOrientation = (evn: React.ChangeEvent<HTMLSelectElement>) => { | ||
setOrientation(evn.target.value as CodeMirrorMergeProps['orientation']); | ||
}; | ||
const [originalValue, setOriginalValue] = useState(originalCode); | ||
const [modifiedValue, setModifiedValue] = useState(modifiedCode); | ||
const random = useRef<number>(); | ||
const click = () => { | ||
random.current = Math.floor(Math.random() * 101); | ||
const code = '// hello world' + random.current + '\n' + originalCode; | ||
setOriginalValue(code); | ||
}; | ||
return ( | ||
<Fragment> | ||
<button onClick={click}>Change Original Value {random.current}</button> | ||
<CodeMirrorMerge | ||
orientation={orientation} | ||
revertControls={revertControls} | ||
collapseUnchanged={collapseUnchanged} | ||
highlightChanges={highlightChanges} | ||
// destroyRerender={false} | ||
gutter={gutter} | ||
style={{ height: 300, overflow: 'auto' }} | ||
theme="dark" | ||
> | ||
<Original | ||
value={originalValue} | ||
extensions={[langs.javascript()]} | ||
onChange={(val) => { | ||
setOriginalValue(val); | ||
// console.log('::::::::::', val) | ||
}} | ||
/> | ||
<Modified | ||
value={modifiedValue} | ||
extensions={[ | ||
langs.javascript(), | ||
// EditorView.editable.of(false), | ||
// EditorState.readOnly.of(true) | ||
]} | ||
onChange={(val) => { | ||
setModifiedValue(val); | ||
}} | ||
/> | ||
</CodeMirrorMerge> | ||
<label> | ||
Orientation | ||
<select onChange={handleOrientation} defaultValue={orientation}> | ||
<option value="">please orientation choose</option> | ||
<option value="a-b">a-b</option> | ||
<option value="b-a">b-a</option> | ||
</select> | ||
</label> | ||
<br /> | ||
<label> | ||
Revert buttons | ||
<select | ||
defaultValue={revertControls} | ||
onChange={(evn) => setRevertControls(evn.target.value as CodeMirrorMergeProps['revertControls'])} | ||
> | ||
<option value="">please revertControls choose</option> | ||
<option value="a-to-b">a-to-b</option> | ||
<option value="b-to-a">b-to-a</option> | ||
</select> | ||
</label> | ||
<br /> | ||
<label> | ||
Highlight changes | ||
<input | ||
type="checkbox" | ||
checked={!!highlightChanges} | ||
onChange={(evn) => setHighlightChanges(evn.target.checked)} | ||
/> | ||
</label> | ||
<br /> | ||
<label> | ||
Gutter markers | ||
<input type="checkbox" checked={!!gutter} onChange={(evn) => setGutter(evn.target.checked)} /> | ||
</label> | ||
<label> | ||
Collapse unchanged code | ||
<input | ||
type="checkbox" | ||
checked={!!collapseUnchanged} | ||
onChange={(evn) => setCollapseUnchanged(evn.target.checked ? {} : undefined)} | ||
/> | ||
</label> | ||
</Fragment> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
export const originalCode = `// The player has a position, a size, and a current speed. | ||
class Player { | ||
size = new Vec(0.8, 1.5); | ||
constructor(pos, speed) { | ||
this.pos = pos; | ||
this.speed = speed; | ||
} | ||
static create(pos) { | ||
return new Player(pos.plus(new Vec(0, -0.5)), new Vec(0, 0)); | ||
} | ||
} | ||
// Lava block. When you touch it, you die. | ||
class Lava { | ||
size = new Vec(1, 1) | ||
constructor(pos, speed, reset) { | ||
this.pos = pos; | ||
this.speed = speed; | ||
this.reset = reset; | ||
} | ||
static horizontal(pos) { | ||
return new Lava(pos, new Vec(2, 0)); | ||
} | ||
static vertical(pos) { | ||
return new Lava(pos, new Vec(0, 2)); | ||
} | ||
static drip(pos) { | ||
return new Lava(pos, new Vec(0, 3), pos); | ||
} | ||
} | ||
`; | ||
|
||
export const modifiedCode = `class Player { | ||
get type() { return "player" } | ||
constructor(pos, speed) { | ||
this.pos = pos; | ||
this.speed = speed; | ||
} | ||
static create(pos) { | ||
return new Player(pos.plus(new Vec(0, -0.5)), new Vec(0, 0)); | ||
} | ||
} | ||
class Lava { | ||
constructor(pos, speed, reset) { | ||
this.pos = pos; | ||
this.speed = speed; | ||
this.reset = reset; | ||
} | ||
get type() { return "lava"; } | ||
static create(pos, ch) { | ||
if (ch == "=") { | ||
return new Lava(pos, new Vec(2, 0)); | ||
} else if (ch == "|") { | ||
return new Lava(pos, new Vec(0, 2)); | ||
} else if (ch == "v") { | ||
return new Lava(pos, new Vec(0, 3), pos); | ||
} | ||
} | ||
} | ||
Player.prototype.size = new Vec(0.8, 1.5); | ||
Lava.prototype.size = new Vec(1, 1); | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use client'; | ||
|
||
import React, { useState } from 'react'; | ||
import CodeMirrorMerge from 'react-codemirror-merge'; | ||
import { abyss } from '@uiw/codemirror-theme-abyss'; | ||
|
||
const Original = CodeMirrorMerge.Original; | ||
const Modified = CodeMirrorMerge.Modified; | ||
let doc = `one | ||
two | ||
three | ||
four | ||
five`; | ||
|
||
export default function Editor() { | ||
const [value, setValue] = useState(doc); | ||
const [valueModified, setValueModified] = useState(doc.replace(/t/g, 'T') + 'Six'); | ||
return ( | ||
<div> | ||
<CodeMirrorMerge theme={abyss}> | ||
<Original | ||
value={value} | ||
onChange={(value) => { | ||
setValue(value); | ||
}} | ||
/> | ||
<Modified | ||
value={valueModified} | ||
onChange={(value) => { | ||
setValueModified(value); | ||
}} | ||
/> | ||
</CodeMirrorMerge> | ||
<div style={{ display: 'flex', marginTop: 10 }}> | ||
<pre style={{ flex: 1 }}>{value} </pre> | ||
<pre style={{ flex: 1 }}>{valueModified} </pre> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// import Editor from './_editor'; | ||
import { MergeExample } from './_editor/Com'; | ||
|
||
export default function Merg() { | ||
return ( | ||
<div> | ||
<MergeExample /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters