Skip to content

Commit

Permalink
fix(merge): ref view not available on load. #681
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 10, 2024
1 parent e79b1f3 commit 67b6d6b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
3 changes: 2 additions & 1 deletion merge/src/Internal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useImperativeHandle, useRef } from 'react';
import { EditorStateConfig } from '@codemirror/state';
import { EditorStateConfig, StateEffect } from '@codemirror/state';
import { getDefaultExtensions } from '@uiw/react-codemirror';
import { MergeView, MergeConfig, DirectMergeConfig } from '@codemirror/merge';
import { useStore } from './store';
Expand Down Expand Up @@ -86,6 +86,7 @@ export const Internal = React.forwardRef<InternalRef, CodeMirrorMergeProps>((pro
parent: editor.current,
...opts,
});
dispatch!({ view: view.current });
}
}, [view, editor, originalExtension, modifiedExtension]);

Expand Down
80 changes: 80 additions & 0 deletions www/src/pages/examples/Example681.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Fragment, useRef, useState } from 'react';
import { ViewPlugin, ViewUpdate } from '@codemirror/view';
import { StateEffect } from '@codemirror/state';
import CodeMirrorMerge, { type CodeMirrorMergeRef } from 'react-codemirror-merge';

const Original = CodeMirrorMerge.Original;
const Modified = CodeMirrorMerge.Modified;
let doc = `one
two
three
four
five`;

const log1UpdatePlugin = ViewPlugin.fromClass(
class {
update(update: ViewUpdate) {
if (update.docChanged) {
console.log('Document changed! test 1');
}
}
},
);

const log2UpdatePlugin = ViewPlugin.fromClass(
class {
update(update: ViewUpdate) {
if (update.docChanged) {
console.log('Document changed! test 2');
}
}
},
);

/**
* https://github.com/uiwjs/react-codemirror/issues/681#issuecomment-2341521112
*/
export function Component() {
return (
<Fragment>
<Example />
</Fragment>
);
}

function Example() {
const [value, setValue] = useState(doc);
const [valueModified, setValueModified] = useState(doc);
const $ref = useRef<CodeMirrorMergeRef>(null);
const handle1 = () => {
$ref.current?.view?.a.dispatch({
effects: StateEffect.appendConfig.of([log2UpdatePlugin]),
});
};
return (
<div style={{ width: '100%' }}>
<div>
<button onClick={handle1}>Add extension 2</button>
</div>
<CodeMirrorMerge ref={$ref} destroyRerender={false}>
<Original
value={value}
extensions={[log1UpdatePlugin]}
onChange={(val) => {
setValue(val);
}}
/>
<Modified
value={valueModified}
onChange={(val) => {
setValueModified(val);
}}
/>
</CodeMirrorMerge>
<div style={{ display: 'flex', marginTop: 10 }}>
<pre style={{ flex: 1 }}>{value} </pre>
<pre style={{ backgroundColor: '#fff', flex: 1 }}>{valueModified} </pre>
</div>
</div>
);
}
5 changes: 5 additions & 0 deletions www/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,11 @@ export const routes: MenuRouteObject[] = [
label: 'CodeMirrorMerge use theme',
lazy: () => import('./pages/examples/Example455'),
},
{
path: '681',
label: 'CodeMirrorMerge Update Extensions',
lazy: () => import('./pages/examples/Example681'),
},
{
path: 'refs',
label: 'Refs Example',
Expand Down

0 comments on commit 67b6d6b

Please sign in to comment.