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

YamlEditor: replacing text throws runtime error if currentText is longer than new text #8

Open
wccbuck opened this issue Feb 25, 2024 · 0 comments

Comments

@wccbuck
Copy link

wccbuck commented Feb 25, 2024

Programmatically updating the text of the yaml editor, either by using ref.current.replaceValue or by using merge={({text}) => {return {text: text}}), fails if the text currently within the editor is more characters than the new text. The error message is RangeError: Selection points outside of document, and is thrown by codemirror/text/dist/index.cjs/Transaction constructor.

replaceText example:

const yamlEditorRef = useRef<any | null>(null);
...
<button onClick={() => { yamlEditorRef.current.replaceValue({ text: "foo: bar" }); }}>Reset</button>
<YamlEditor text={exampleYaml} ref={yamlEditorRef} />

merge example:

const [exampleYaml, setExampleYaml] = useState("");
const handleYamlChange = ({ json, text }: { json: any, text: string }) => {
    setExampleYaml(text);
}
const handleMerge = ({ json, text, currentText }: { json: any, text: string, currentText: string }) => {
    return { text: text };
...
<button onClick={() => { setExampleYaml("foo: bar"); }}>Reset</button>
<YamlEditor text={exampleYaml} onChange={handleYamlChange} merge={handleMerge}/>

In both of these cases, if the text box currently has something shorter than or equal to "foo: bar", such as "12345678", this will succeed. If the text box has more characters, it will fail with that RangeError.

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

1 participant