mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-27 12:53:13 +03:00
Fixed autofocus cursor position when editing comments
This commit is contained in:
parent
e002904ce7
commit
072574ee71
@ -1,4 +1,4 @@
|
||||
import React, {useContext} from 'react';
|
||||
import React, {useContext, useEffect} from 'react';
|
||||
import {Transition} from '@headlessui/react';
|
||||
import AppContext from '../AppContext';
|
||||
import Avatar from './Avatar';
|
||||
@ -17,7 +17,9 @@ const Form = (props) => {
|
||||
} else if (props.isEdit) {
|
||||
config = {
|
||||
placeholder: 'Edit this comment',
|
||||
autofocus: true,
|
||||
// warning: we cannot use autofocus on the edit field, because that sets
|
||||
// the cursor position at the beginning of the text field instead of the end
|
||||
autofocus: false,
|
||||
content: props.comment.html
|
||||
};
|
||||
} else {
|
||||
@ -31,6 +33,30 @@ const Form = (props) => {
|
||||
...getEditorConfig(config)
|
||||
});
|
||||
|
||||
// Set the cursor position at the end of the form, instead of the beginning (= when using autofocus)
|
||||
useEffect(() => {
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Focus editor + jump to end
|
||||
if (!props.isEdit) {
|
||||
return;
|
||||
}
|
||||
|
||||
// jump to end
|
||||
editor
|
||||
.chain()
|
||||
.focus()
|
||||
.command(({tr, commands}) => {
|
||||
return commands.setTextSelection({
|
||||
from: tr.doc.content.size,
|
||||
to: tr.doc.content.size
|
||||
});
|
||||
})
|
||||
.run();
|
||||
}, [editor, props.isEdit]);
|
||||
|
||||
const focused = editor?.isFocused || !editor?.isEmpty;
|
||||
|
||||
const submitForm = async (event) => {
|
||||
|
Loading…
Reference in New Issue
Block a user