Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves the list item backspace behaviour. #1232

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import BulletList from "@tiptap/extension-bullet-list";

export default BulletList.extend({
addKeyboardShortcuts() {
return {
// This will handle the backspace press in both bulletList and orderedList.
Backspace: () =>
this.editor.commands.command(({ state, tr, dispatch }) => {
const { $from, empty } = state.selection;
const inList =
this.editor.isActive("bulletList") ||
this.editor.isActive("orderedList");

if (empty && inList && $from.parent.type.name === "paragraph") {
const isTrailingParagraph = $from.parent.content.size === 0;

const $list = $from.node($from.depth - 2);
const $listItem = $from.node($from.depth - 1);

const isInListItem = $listItem.type.name === "listItem";
const isLastParagraphInListItem =
$from.index($from.depth - 1) === $listItem.childCount - 1;

// Check for list items above and below
const currentItemIndex = $from.index($from.depth - 2);
const hasListItemsAbove = currentItemIndex > 0;
const hasListItemsBelow = currentItemIndex < $list.childCount - 1;

if (
isTrailingParagraph &&
isInListItem &&
isLastParagraphInListItem &&
!hasListItemsBelow &&
hasListItemsAbove
) {
if (dispatch) {
const range = $from.blockRange($from);
if (range) {
tr.lift(range, 0);
dispatch(tr);
}
}

return true;
}
}

return false;
}),
};
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EDITOR_OPTIONS } from "common/constants";
import { isEmpty } from "ramda";

import HighlightInternal from "../BackgroundColor/ExtensionConfig";
import BulletList from "../BulletList/ExtensionConfig";
import CodeBlock from "../CodeBlock/ExtensionConfig";
import CustomCommands from "../CustomCommands/ExtensionConfig";
import Embeds from "../Embeds/ExtensionConfig";
Expand Down Expand Up @@ -78,9 +79,9 @@ const useCustomExtensions = ({
document: false,
codeBlock: false,
code: false,
bulletList: false,
blockquote: options.includes(EDITOR_OPTIONS.BLOCKQUOTE),
orderedList: options.includes(EDITOR_OPTIONS.LIST_ORDERED),
bulletList: options.includes(EDITOR_OPTIONS.LIST_BULLETS),
history: !collaborationProvider,
}),
TextStyle,
Expand Down Expand Up @@ -108,6 +109,10 @@ const useCustomExtensions = ({
customExtensions.push(Color);
}

if (options.includes(EDITOR_OPTIONS.LIST_BULLETS)) {
customExtensions.push(BulletList);
}

if (isSlashCommandsActive) {
customExtensions.push(
SlashCommands.configure({
Expand Down