[Question] How to replace all content but keep undo history?
I tried the following to progamatically update Trix Content:
const trix = document.querySelector('trix-editor');
trix.value = 'new content';
// or
trix.editor.loadHTML('new content');
But both methods don't keep the undo history, digging in the source code both of this methods create a new Document instance and replace the old one, so undo history is lost.
So currently I am doing the following:
const trix = document.querySelector('trix-editor');
const editor = trix.editor;
editor.recordUndoEntry( 'Content updated' );
editor.setSelectedRange( [ 0, trix.value.length ] );
editor.deleteInDirection( 'forward' );
editor.insertHTML( 'new content' );
It feels a bit hacky and I noticed there is a method in the models/composition.coffee called replaceHTML that seems to be a better way to do this, but unfortunately it is not exposed through the editor, document or the element.
So: Is there a better way to replace the content progamatically while keeping undo history? If the replaceHTML is the best way to do this, would you consider a PR to expose it through the editor object?
I did not write a PR because the only place where this method is called (controllers/editor_controller.coffee) is right below a comment which says private. So I prefered to ask first.
Thanks.
You can actually get Trix.Composition via
const trix = document.querySelector('trix-editor');
const composition = trix.editor.editorController.composition;
If you go to the constructor of EditorController you can see how the objects fit together and who delegates to what.
Hey @rodrigopedra, what you're doing isn't hacky. I'd make a couple little changes:
- Use
editor.getDocument().getLength()instead oftrix.value.lengthfor the range's end position.valueis an HTML string, which will usually be much longer than the document's length. - Skip the
editor.deleteInDirection( 'forward' ).editor.insertHTML( 'new content' )will handle that since it overwrites the selected range.
const { editor } = document.querySelector('trix-editor')
editor.recordUndoEntry('Content updated')
editor.setSelectedRange([0, editor.getDocument().getLength()])
editor.insertHTML('new content')
Thanks @javan it works as expected!
One more question:
After replacing the content the trix-editor steal the focus to itself. How could I avoid that?
Example:
<html>
<head>
<link rel="stylesheet" href="trix.css">
<script src="trix.js"></script>
<script>
document.addEventListener( 'DOMContentLoaded', function () {
var textarea = document.querySelector( 'textarea' );
textarea.addEventListener( 'input', function () {
var editor = document.querySelector( 'trix-editor' ).editor;
var content = textarea.value;
editor.recordUndoEntry( 'Content updated' );
editor.setSelectedRange( [ 0, editor.getDocument().getLength() ] );
editor.insertHTML( content );
} );
} );
</script>
</head>
<body>
<trix-editor></trix-editor>
<textarea></textarea>
</body>
</html>
if you prefer I can open a new issue
UPDATE:
I tried using replaceHTML without selecting the range first, as this:
var editor = document.querySelector( 'trix-editor' ).editor;
var content = textarea.value;
// editor.recordUndoEntry( 'Content updated' );
// editor.setSelectedRange( [ 0, editor.getDocument().getLength() ] );
editor.composition.replaceHTML( content );
But it also steals focus. Also, if the range is not selected it doesn't replace the old content.
UPDATE 2:
I managed to work out what I am trying to do using document.activeElement:
var activeElement = window.document.activeElement;
editor.recordUndoEntry( 'Content updated' );
editor.setSelectedRange( [ 0, editor.getDocument().getLength() ] );
editor.insertHTML( content );
activeElement && activeElement.focus && activeElement.focus();
It works, but I wonder if there is a better way to avoid trix stelaing focus after inserting new content programmatically.
Background: I am writing a Vue controlled component that wraps trix. The part I am struggling with is to enable reactivity. As this is an odd scenario (having two-way binding with another sources) it will be enabled as an opt-in attribute. But I wanted to handle it for completeness.
I wonder if there is a better way to avoid trix stelaing focus after inserting new content programmatically
There isn't, currently. "UPDATE 2" looks good. :)
Thanks a lot, when I publish the component I ping you to have a look if you are interested.