diff --git a/core/lib/utils/html/editor_script/list_keyboard_handler_script.dart b/core/lib/utils/html/editor_script/list_keyboard_handler_script.dart index 5363f50fa..25c0bd3fc 100644 --- a/core/lib/utils/html/editor_script/list_keyboard_handler_script.dart +++ b/core/lib/utils/html/editor_script/list_keyboard_handler_script.dart @@ -53,6 +53,13 @@ abstract final class ListKeyboardHandlerSource { return listItem; } + // Summernote `indent` wraps an item with no previous sibling in an + // empty parent item, so each Tab would add a stray bullet. + function canNestListItem(item) { + const previous = item.previousElementSibling; + return !!previous && previous.tagName === 'LI'; + } + function isNestedListItem(item) { const list = item.parentElement; let ancestor = list ? list.parentElement : null; @@ -116,7 +123,12 @@ abstract final class ListKeyboardHandlerSource { || event.metaKey) return; if (event.key === 'Tab') { - if (!findCaretListItem()) return; + const item = findCaretListItem(); + if (!item) return; + if (!event.shiftKey && !canNestListItem(item)) { + event.preventDefault(); + return; + } runListCommand(event, event.shiftKey ? 'outdent' : 'indent'); return; } diff --git a/core/test/utils/html/editor_script/list_keyboard_handler_script_browser_test.dart b/core/test/utils/html/editor_script/list_keyboard_handler_script_browser_test.dart index 7d5b30caf..48b352430 100644 --- a/core/test/utils/html/editor_script/list_keyboard_handler_script_browser_test.dart +++ b/core/test/utils/html/editor_script/list_keyboard_handler_script_browser_test.dart @@ -79,6 +79,26 @@ void main() { expect(_commands(), ['outdent']); }); + test('Tab on the first item of a list keeps focus without nesting it', () { + final item = _byId(editable, 'first'); + _placeCaret(item); + + final defaultPrevented = _dispatchKey(item, 'Tab'); + + expect(defaultPrevented, isTrue); + expect(_commands(), isEmpty); + }); + + test('Shift+Tab on the first item of a list outdents it', () { + final item = _byId(editable, 'first'); + _placeCaret(item); + + final defaultPrevented = _dispatchKey(item, 'Tab', shiftKey: true); + + expect(defaultPrevented, isTrue); + expect(_commands(), ['outdent']); + }); + test('Tab outside a list keeps the browser behaviour', () { final paragraph = _byId(editable, 'paragraph'); _placeCaret(paragraph);