Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for onChange Event or Value Change Subscription in react-codemirror-merge #502

Open
kwonth211 opened this issue May 18, 2023 · 10 comments

Comments

@kwonth211
Copy link

I'm currently using react-codemirror-merge and would like to detect onChange events or subscribe to value changes.
Is there a way to achieve this functionality? Any insight or suggestions on how to implement this would be greatly appreciated. Thanks in advance!

@jaywcjlove
Copy link
Member

@kwonth211 Upgrade v4.20.0

import CodeMirrorMerge from 'react-codemirror-merge';
const Original = CodeMirrorMerge.Original;
const Modified = CodeMirrorMerge.Modified;

<CodeMirrorMerge
  revertControls="a-to-b"
  style={{ height: 300, overflow: 'auto' }}
>
  <Original
    value={originalCode}
    onChange={(value) => {
      console.log('value3:', value)
    }}
  />
  <Modified
    value={modifiedCode}
    onChange={(value) => {
      console.log('value3:', value)
    }}
  />
</CodeMirrorMerge>

@kwonth211
Copy link
Author

Thanks!

@kwonth211
Copy link
Author

@jaywcjlove

I have followed your instructions and updated to version 4.20.0, and have also implemented the provided interface. However, the onChange events are still not working as expected. Could you please provide further guidance or suggestions on how to resolve this issue? Thank you.

@jaywcjlove
Copy link
Member

@kwonth211

<CodeMirrorMerge
+  revertControls="a-to-b"
  style={{ height: 300, overflow: 'auto' }}
>

@kwonth211
Copy link
Author

Here is my code. I have followed the provided example and used the code exactly as written,
but it does not seem to be working. 🥲

import React, { useEffect } from 'react'
import styled from 'styled-components'
import CodeMirrorMerge from 'react-codemirror-merge'
import { EditorView, ViewUpdate } from '@codemirror/view'
import { EditorState } from '@codemirror/state'
import CodeMirror, { ReactCodeMirrorProps } from '@uiw/react-codemirror'
const Original = CodeMirrorMerge.Original
const Modified = CodeMirrorMerge.Modified

export interface JsonDiffProps {
  prevJson: string
  nextJson: string
  onChangePrevJson?: (value: string) => void
  onChangeNextJson?: (value: string) => void
  prevJsonProps?: {
    readonly?: boolean
    height?: string
    width?: string
    title?: React.ReactNode
  }
  nextJsonProps?: {
    readonly?: boolean
    height?: string
    width?: string
    title?: React.ReactNode
  }
}

export const JsonDiff = function JSONDiff({
  prevJson,
  nextJson,
  onChangePrevJson,
  onChangeNextJson,
  prevJsonProps,
  nextJsonProps,
}: JsonDiffProps) {
  // console.log(A.EditorState)

  return (
    <Container>
      <CodeMirrorMerge
        revertControls="a-to-b"
        style={{ height: 300, overflow: 'auto' }}
      >
        <Original
          value={prevJson}
          onChange={(value) => {
            console.log('value3:', value)
          }}
        />
        <Modified
          value={nextJson}
          onChange={(value) => {
            console.log('value3:', value)
          }}
        />
      </CodeMirrorMerge>
    </Container>
  )
}

jaywcjlove added a commit that referenced this issue May 19, 2023
github-actions bot pushed a commit that referenced this issue May 19, 2023
jaywcjlove added a commit that referenced this issue May 19, 2023
@kwonth211
Copy link
Author

@jaywcjlove

I am still experiencing the same issue with the onChange events not working, even after updating to version 4.20.2. I have tried using the exact example code provided in the documentation, but it didn't resolve the problem. Could it be an issue with my current package versions? Here are my package versions for reference:

"@uiw/codemirror-theme-dracula": "4.20.2",
"@uiw/codemirror-theme-github": "4.20.2",
"@uiw/react-codemirror": "4.20.2",
"react-codemirror-merge": "4.20.2",
"react": "^18.2.0",
"@storybook/addon-actions": "^7.0.10",
"@storybook/addon-controls": "^7.0.10",
"@storybook/addon-essentials": "^7.0.10",
"@storybook/addon-links": "^7.0.10",
"@storybook/react": "^7.0.10",
"@storybook/react-webpack5": "^7.0.10",

Any assistance or guidance would be greatly appreciated. Thank you!

@jaywcjlove
Copy link
Member

jaywcjlove commented May 22, 2023

@kwonth211 Example: https://codesandbox.io/embed/react-codemirror-example-codemirror-6-https-github-com-uiwjs-react-codemirror-issues-502-min95l?fontsize=14&hidenavigation=1&theme=dark

import React, { useState } from "react";
import CodeMirrorMerge from "react-codemirror-merge";

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

export default function App() {
  const [value, setValue] = useState();
  const onChange = React.useCallback((val) => {
    console.log("value:", val);
    setValue(val);
  }, []);
  return (
    <div>
      <CodeMirrorMerge>
        <Original value={doc} onChange={onChange} />
        <Modified
          value={doc.replace(/t/g, "T") + "Six"}
          onChange={(val) => {
            console.log("value:222:", val);
          }}
        />
      </CodeMirrorMerge>
      <pre>{value}</pre>
    </div>
  );
}

@vyazadji
Copy link

vyazadji commented Dec 7, 2023

It's broken in react-codemirror-merge v4.21.21
It works in the example codesandbox.io/embed/react-codemirror-example-codemirror-6-https-github-com-uiwjs-react-codemirror-issues-502-min95l?fontsize=14&hidenavigation=1&theme=dark with version 4.20.2
The example in the documentation https://uiwjs.github.io/react-codemirror/#/merge/onchange also looks broken
Also, it can be related to updating version of React from 17->18

@jaywcjlove
Copy link
Member

@jaywcjlove
Copy link
Member

<CodeMirrorMerge destroyRerender={false}>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants