From 578fb72ddc0f538d18a1f5caa6b1ee7c8a45d92c Mon Sep 17 00:00:00 2001 From: Muhammad Ali Date: Tue, 25 Jul 2023 19:14:24 +0500 Subject: [PATCH] editor: infer dir from parent when adding heading --- .../editor/src/extensions/heading/heading.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/editor/src/extensions/heading/heading.ts b/packages/editor/src/extensions/heading/heading.ts index f0ee16dbf..e973324ad 100644 --- a/packages/editor/src/extensions/heading/heading.ts +++ b/packages/editor/src/extensions/heading/heading.ts @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +import { textblockTypeInputRule } from "@tiptap/core"; import { Heading as TiptapHeading } from "@tiptap/extension-heading"; export const Heading = TiptapHeading.extend({ @@ -40,5 +41,31 @@ export const Heading = TiptapHeading.extend({ }); } }; + }, + + addKeyboardShortcuts() { + return this.options.levels.reduce( + (items, level) => ({ + ...items, + ...{ + [`Mod-Alt-${level}`]: () => this.editor.commands.setHeading({ level }) + } + }), + {} + ); + }, + + addInputRules() { + return this.options.levels.map((level) => { + return textblockTypeInputRule({ + find: new RegExp(`^(#{1,${level}})\\s$`), + type: this.type, + getAttributes: () => { + const { textAlign, textDirection } = + this.editor.state.selection.$from.parent.attrs; + return { level, textAlign, textDirection }; + } + }); + }); } });