fix newline backspace behaviour
This commit is contained in:
@@ -211,17 +211,31 @@ 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]
|
||||||
const deleteFrom = cursorPosition - lastGrapheme.length
|
// deleteRange doesn't work on newlines, because tiptap
|
||||||
editor?.commands.deleteRange({
|
// treats them as separate 'blocks' and we're using \n
|
||||||
from: deleteFrom,
|
// as a stand-in. bail out if the last grapheme is a newline
|
||||||
to: cursorPosition,
|
// to let the default behavior handle it -sfn
|
||||||
})
|
if (lastGrapheme !== '\n') {
|
||||||
return true
|
// otherwise, delete the last grapheme using deleteRange,
|
||||||
|
// so that emojis are deleted as a whole
|
||||||
|
const deleteFrom = cursorPosition - lastGrapheme.length
|
||||||
|
editor?.commands.deleteRange({
|
||||||
|
from: deleteFrom,
|
||||||
|
to: cursorPosition,
|
||||||
|
})
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user