focusreactive / focusreactive/headless-cms-yaml-input
YamlEditor: replacing text throws runtime error if currentText is longer than new text
- Dominant language
- TypeScript
- Stars
- 8
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
Description
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(null);
...
{ yamlEditorRef.current.replaceValue({ text: "foo: bar" }); }}>Reset
```
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 };
...
{ setExampleYaml("foo: bar"); }}>Reset
```
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.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.