0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Auto delink if backspacing inside auto-linked URL

This means if you make a mistake and backspace, you don't end up
accidentally fixing the text but leaving the link to the wrong URL.
This commit is contained in:
Neil Jenkins 2023-02-01 14:20:53 +11:00
parent b85754ca50
commit 1904405ab8

View file

@ -10,6 +10,7 @@ import {
getStartBlockOfRange,
rangeDoesStartAtBlockBoundary,
} from '../range/Block';
import { moveRangeBoundariesDownTree } from '../range/Boundaries';
import { deleteContentsOfRange } from '../range/InsertDelete';
import { afterDelete, detachUneditableNode } from './KeyHelpers';
@ -20,13 +21,13 @@ const Backspace = (self: Squire, event: KeyboardEvent, range: Range): void => {
self._removeZWS();
// Record undo checkpoint.
self.saveUndoState(range);
// If not collapsed, delete contents
if (!range.collapsed) {
// If not collapsed, delete contents
event.preventDefault();
deleteContentsOfRange(range, root);
afterDelete(self, range);
// If at beginning of block, merge with previous
} else if (rangeDoesStartAtBlockBoundary(range, root)) {
// If at beginning of block, merge with previous
event.preventDefault();
const startBlock = getStartBlockOfRange(range, root);
if (!startBlock) {
@ -77,13 +78,30 @@ const Backspace = (self: Squire, event: KeyboardEvent, range: Range): void => {
self.setSelection(range);
self._updatePath(range, true);
}
// Otherwise, leave to browser but check afterwards whether it has
// left behind an empty inline tag.
} else {
self.setSelection(range);
setTimeout(() => {
afterDelete(self);
}, 0);
// If deleting text inside a link that looks like a URL, delink.
// This is to allow you to easily correct auto-linked text.
moveRangeBoundariesDownTree(range);
const text = range.startContainer;
const offset = range.startOffset;
const a = text.parentNode;
if (
text instanceof Text &&
a instanceof HTMLAnchorElement &&
offset &&
a.href.includes(text.data)
) {
text.deleteData(offset - 1, 1);
self.setSelection(range);
self.removeLink();
} else {
// Otherwise, leave to browser but check afterwards whether it has
// left behind an empty inline tag.
self.setSelection(range);
setTimeout(() => {
afterDelete(self);
}, 0);
}
}
};