From ab7d17509e0431706a1f066d2350a2e190ba9083 Mon Sep 17 00:00:00 2001 From: sibiraj-s Date: Fri, 9 Jul 2021 00:14:34 +0530 Subject: [PATCH] feat: add markdown shortcuts for bold and italics --- projects/ngx-editor/helpers/markInputRule.ts | 28 +++++++++++++++++++ projects/ngx-editor/helpers/public_api.ts | 1 + projects/ngx-editor/src/lib/defaultPlugins.ts | 14 +++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 projects/ngx-editor/helpers/markInputRule.ts diff --git a/projects/ngx-editor/helpers/markInputRule.ts b/projects/ngx-editor/helpers/markInputRule.ts new file mode 100644 index 00000000..ee796330 --- /dev/null +++ b/projects/ngx-editor/helpers/markInputRule.ts @@ -0,0 +1,28 @@ +import { InputRule } from "prosemirror-inputrules" +import { MarkType } from "prosemirror-model" + +export const markInputRule = (regexp: RegExp, markType: MarkType, attrs: Record): InputRule => { + + return new InputRule(regexp, (state, match, start, end) => { + + const tr = state.tr + + if (match[2]) { + const textStart = start + match[0].indexOf(match[2]) + const textEnd = textStart + match[2].length + + if (textEnd < end) { + tr.delete(textEnd, end) + } + + if (textStart > start) { + tr.delete(start, textStart) + } + + end = start + match[2].length + } + return tr.addMark(start, end, markType.create(attrs)) + }) +} + +export default markInputRule diff --git a/projects/ngx-editor/helpers/public_api.ts b/projects/ngx-editor/helpers/public_api.ts index b7fb204d..cc4fa722 100644 --- a/projects/ngx-editor/helpers/public_api.ts +++ b/projects/ngx-editor/helpers/public_api.ts @@ -5,3 +5,4 @@ export * from './getSelectionMarks'; export * from './bubblePosition'; export * from './getSelectionNodes'; export * from './markApplies'; +export * from './markInputRule'; diff --git a/projects/ngx-editor/src/lib/defaultPlugins.ts b/projects/ngx-editor/src/lib/defaultPlugins.ts index 6859166c..9723656f 100644 --- a/projects/ngx-editor/src/lib/defaultPlugins.ts +++ b/projects/ngx-editor/src/lib/defaultPlugins.ts @@ -1,4 +1,4 @@ -import { NodeType, Schema } from 'prosemirror-model'; +import { MarkType, NodeType, Schema } from 'prosemirror-model'; import { Plugin } from 'prosemirror-state'; import { keymap } from 'prosemirror-keymap'; import { toggleMark, baseKeymap, chainCommands, exitCode } from 'prosemirror-commands'; @@ -9,6 +9,8 @@ import { smartQuotes, emDash, ellipsis, InputRule } from 'prosemirror-inputrules'; +import { markInputRule } from 'ngx-editor/helpers'; + interface Options { history: boolean; keyboardShortcuts: boolean; @@ -70,12 +72,22 @@ const headingRule = (nodeType: NodeType, maxLevel: number): InputRule => { ); }; +const boldRule = (markType: MarkType): InputRule => { + return markInputRule(/(?:^|\s)((?:\*\*|__)((?:[^*_]+))(?:\*\*|__))$/, markType, {}) +} + +const emRule = (markType: MarkType): InputRule => { + return markInputRule(/(?:^|\s)((?:\*|_)((?:[^*_]+))(?:\*|_))$/, markType, {}) +} + // : (Schema) → Plugin // A set of input rules for creating the basic block quotes, lists, // code blocks, and heading. const buildInputRules = (schema: Schema): Plugin => { const rules = smartQuotes.concat(ellipsis, emDash); + rules.push(boldRule(schema.marks.strong)); + rules.push(emRule(schema.marks.em)); rules.push(blockQuoteRule(schema.nodes.blockquote)); rules.push(orderedListRule(schema.nodes.ordered_list)); rules.push(bulletListRule(schema.nodes.bullet_list));