Skip to content

Commit 67b6d6b

Browse files
committed
fix(merge): ref view not available on load. #681
1 parent e79b1f3 commit 67b6d6b

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

merge/src/Internal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useImperativeHandle, useRef } from 'react';
2-
import { EditorStateConfig } from '@codemirror/state';
2+
import { EditorStateConfig, StateEffect } from '@codemirror/state';
33
import { getDefaultExtensions } from '@uiw/react-codemirror';
44
import { MergeView, MergeConfig, DirectMergeConfig } from '@codemirror/merge';
55
import { useStore } from './store';
@@ -86,6 +86,7 @@ export const Internal = React.forwardRef<InternalRef, CodeMirrorMergeProps>((pro
8686
parent: editor.current,
8787
...opts,
8888
});
89+
dispatch!({ view: view.current });
8990
}
9091
}, [view, editor, originalExtension, modifiedExtension]);
9192

www/src/pages/examples/Example681.tsx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { Fragment, useRef, useState } from 'react';
2+
import { ViewPlugin, ViewUpdate } from '@codemirror/view';
3+
import { StateEffect } from '@codemirror/state';
4+
import CodeMirrorMerge, { type CodeMirrorMergeRef } from 'react-codemirror-merge';
5+
6+
const Original = CodeMirrorMerge.Original;
7+
const Modified = CodeMirrorMerge.Modified;
8+
let doc = `one
9+
two
10+
three
11+
four
12+
five`;
13+
14+
const log1UpdatePlugin = ViewPlugin.fromClass(
15+
class {
16+
update(update: ViewUpdate) {
17+
if (update.docChanged) {
18+
console.log('Document changed! test 1');
19+
}
20+
}
21+
},
22+
);
23+
24+
const log2UpdatePlugin = ViewPlugin.fromClass(
25+
class {
26+
update(update: ViewUpdate) {
27+
if (update.docChanged) {
28+
console.log('Document changed! test 2');
29+
}
30+
}
31+
},
32+
);
33+
34+
/**
35+
* https://github.com/uiwjs/react-codemirror/issues/681#issuecomment-2341521112
36+
*/
37+
export function Component() {
38+
return (
39+
<Fragment>
40+
<Example />
41+
</Fragment>
42+
);
43+
}
44+
45+
function Example() {
46+
const [value, setValue] = useState(doc);
47+
const [valueModified, setValueModified] = useState(doc);
48+
const $ref = useRef<CodeMirrorMergeRef>(null);
49+
const handle1 = () => {
50+
$ref.current?.view?.a.dispatch({
51+
effects: StateEffect.appendConfig.of([log2UpdatePlugin]),
52+
});
53+
};
54+
return (
55+
<div style={{ width: '100%' }}>
56+
<div>
57+
<button onClick={handle1}>Add extension 2</button>
58+
</div>
59+
<CodeMirrorMerge ref={$ref} destroyRerender={false}>
60+
<Original
61+
value={value}
62+
extensions={[log1UpdatePlugin]}
63+
onChange={(val) => {
64+
setValue(val);
65+
}}
66+
/>
67+
<Modified
68+
value={valueModified}
69+
onChange={(val) => {
70+
setValueModified(val);
71+
}}
72+
/>
73+
</CodeMirrorMerge>
74+
<div style={{ display: 'flex', marginTop: 10 }}>
75+
<pre style={{ flex: 1 }}>{value} </pre>
76+
<pre style={{ backgroundColor: '#fff', flex: 1 }}>{valueModified} </pre>
77+
</div>
78+
</div>
79+
);
80+
}

www/src/router.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,11 @@ export const routes: MenuRouteObject[] = [
673673
label: 'CodeMirrorMerge use theme',
674674
lazy: () => import('./pages/examples/Example455'),
675675
},
676+
{
677+
path: '681',
678+
label: 'CodeMirrorMerge Update Extensions',
679+
lazy: () => import('./pages/examples/Example681'),
680+
},
676681
{
677682
path: 'refs',
678683
label: 'Refs Example',

0 commit comments

Comments
 (0)