fix newline backspace behaviour
This commit is contained in:
@@ -211,11 +211,24 @@ export function TextInput({
|
|||||||
const isNotSelection = view.state.selection.empty
|
const isNotSelection = view.state.selection.empty
|
||||||
if (isNotSelection) {
|
if (isNotSelection) {
|
||||||
const cursorPosition = view.state.selection.$anchor.pos
|
const cursorPosition = view.state.selection.$anchor.pos
|
||||||
const textBefore = view.state.doc.textBetween(0, cursorPosition)
|
const textBefore = view.state.doc.textBetween(
|
||||||
|
0,
|
||||||
|
cursorPosition,
|
||||||
|
// important - use \n as a block separator, otherwise
|
||||||
|
// all the lines get mushed together -sfn
|
||||||
|
'\n',
|
||||||
|
)
|
||||||
const graphemes = new Graphemer().splitGraphemes(textBefore)
|
const graphemes = new Graphemer().splitGraphemes(textBefore)
|
||||||
|
|
||||||
if (graphemes.length > 0) {
|
if (graphemes.length > 0) {
|
||||||
const lastGrapheme = graphemes[graphemes.length - 1]
|
const lastGrapheme = graphemes[graphemes.length - 1]
|
||||||
|
// deleteRange doesn't work on newlines, because tiptap
|
||||||
|
// treats them as separate 'blocks' and we're using \n
|
||||||
|
// as a stand-in. bail out if the last grapheme is a newline
|
||||||
|
// to let the default behavior handle it -sfn
|
||||||
|
if (lastGrapheme !== '\n') {
|
||||||
|
// otherwise, delete the last grapheme using deleteRange,
|
||||||
|
// so that emojis are deleted as a whole
|
||||||
const deleteFrom = cursorPosition - lastGrapheme.length
|
const deleteFrom = cursorPosition - lastGrapheme.length
|
||||||
editor?.commands.deleteRange({
|
editor?.commands.deleteRange({
|
||||||
from: deleteFrom,
|
from: deleteFrom,
|
||||||
@@ -225,6 +238,7 @@ export function TextInput({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
content: generateJSON(richtext.text.toString(), extensions, {
|
content: generateJSON(richtext.text.toString(), extensions, {
|
||||||
|
|||||||
Reference in New Issue
Block a user