Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: add markdown shortcut for checklist (#7087)

01zulfi6672db6ac2e7
diff --git a/packages/editor/src/extensions/check-list/check-list.ts b/packages/editor/src/extensions/check-list/check-list.ts
index d7a904358..854cba890 100644
--- a/packages/editor/src/extensions/check-list/check-list.ts
+++ b/packages/editor/src/extensions/check-list/check-list.ts
@@ -16,7 +16,10 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { mergeAttributes, Node } from "@tiptap/core";
+import { mergeAttributes, Node, wrappingInputRule } from "@tiptap/core";
+import { inputRegex } from "@tiptap/extension-task-item";
+import { getParentAttributes } from "../../utils/prosemirror";
+import { ListItem } from "../list-item";
export interface CheckListOptions {
itemTypeName: string;
@@ -79,6 +82,45 @@ export const CheckList = Node.create<CheckListOptions>({
};
},
+ addInputRules() {
+ const inputRule = wrappingInputRule({
+ find: inputRegex,
+ type: this.type,
+ getAttributes: () => {
+ return getParentAttributes(this.editor, true, true);
+ }
+ });
+ const oldHandler = inputRule.handler;
+ inputRule.handler = ({ state, range, match, chain, can, commands }) => {
+ const $from = state.selection.$from;
+ const parentNode = $from.node($from.depth - 1);
+ if (parentNode.type.name !== ListItem.name) {
+ return;
+ }
+
+ const tr = state.tr;
+ // reset nodes before converting them to a check list.
+ commands.clearNodes();
+
+ oldHandler({
+ state,
+ range: {
+ from: tr.mapping.map(range.from),
+ to: tr.mapping.map(range.to)
+ },
+ match,
+ chain,
+ can,
+ commands
+ });
+
+ tr.setNodeMarkup(state.tr.selection.to - 2, undefined, {
+ checked: match[match.length - 1] === "x"
+ });
+ };
+ return [inputRule];
+ },
+
addKeyboardShortcuts() {
return {
"Mod-Shift-9": () => this.editor.commands.toggleCheckList()
diff --git a/packages/editor/src/extensions/task-list/task-list.ts b/packages/editor/src/extensions/task-list/task-list.ts
index daa8b928a..fa7e3a5d9 100644
--- a/packages/editor/src/extensions/task-list/task-list.ts
+++ b/packages/editor/src/extensions/task-list/task-list.ts
@@ -39,6 +39,7 @@ import {
} from "./utils.js";
import { Node as ProsemirrorNode } from "@tiptap/pm/model";
import { TaskItemNode } from "../task-item/index.js";
+import { ListItem } from "../list-item/list-item.js";
type TaskListStats = { checked: number; total: number };
export type TaskListAttributes = {
@@ -335,6 +336,12 @@ export const TaskListNode = TaskList.extend({
});
const oldHandler = inputRule.handler;
inputRule.handler = ({ state, range, match, chain, can, commands }) => {
+ const $from = state.selection.$from;
+ const parentNode = $from.node($from.depth - 1);
+ if (parentNode.type.name === ListItem.name) {
+ return;
+ }
+
const tr = state.tr;
// reset nodes before converting them to a task list.
commands.clearNodes();