From b32182fc8d373a62450c2c2711bcfbf17f2e3399 Mon Sep 17 00:00:00 2001 From: pipipi-pikachu Date: Sat, 8 Jul 2023 10:43:56 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=96=87=E6=9C=AC=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/prosemirror/plugins/inputrules.ts | 15 +++++++++++++++ src/utils/prosemirror/plugins/keymap.ts | 1 + src/utils/prosemirror/schema/marks.ts | 8 ++++---- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/utils/prosemirror/plugins/inputrules.ts b/src/utils/prosemirror/plugins/inputrules.ts index 003b33ab..00cc06fc 100644 --- a/src/utils/prosemirror/plugins/inputrules.ts +++ b/src/utils/prosemirror/plugins/inputrules.ts @@ -6,6 +6,7 @@ import { smartQuotes, emDash, ellipsis, + InputRule, } from 'prosemirror-inputrules' const blockQuoteRule = (nodeType: NodeType) => wrappingInputRule(/^\s*>\s$/, nodeType) @@ -23,6 +24,19 @@ const bulletListRule = (nodeType: NodeType) => wrappingInputRule(/^\s*([-+*])\s$ const codeBlockRule = (nodeType: NodeType) => textblockTypeInputRule(/^```$/, nodeType) +const linkRule = () => { + const urlRegEx = /(?:https?:\/\/)?[\w-]+(?:\.[\w-]+)+\.?(?:\d+)?(?:\/\S*)?$/ + + return new InputRule(urlRegEx, (state, match, start, end) => { + const { schema } = state + + const tr = state.tr.insertText(match[0], start, end) + const mark = schema.marks.link.create({ href: match[0], title: match[0] }) + + return tr.addMark(start, start + match[0].length, mark) + }) +} + export const buildInputRules = (schema: Schema) => { const rules = [ ...smartQuotes, @@ -33,6 +47,7 @@ export const buildInputRules = (schema: Schema) => { rules.push(orderedListRule(schema.nodes.ordered_list)) rules.push(bulletListRule(schema.nodes.bullet_list)) rules.push(codeBlockRule(schema.nodes.code_block)) + rules.push(linkRule()) return inputRules({ rules }) } \ No newline at end of file diff --git a/src/utils/prosemirror/plugins/keymap.ts b/src/utils/prosemirror/plugins/keymap.ts index 1978249b..fd493ba5 100644 --- a/src/utils/prosemirror/plugins/keymap.ts +++ b/src/utils/prosemirror/plugins/keymap.ts @@ -38,6 +38,7 @@ export const buildKeymap = (schema: Schema) => { )) bind('Mod-[', liftListItem(schema.nodes.list_item)) bind('Mod-]', sinkListItem(schema.nodes.list_item)) + bind('Tab', sinkListItem(schema.nodes.list_item)) return keys } \ No newline at end of file diff --git a/src/utils/prosemirror/schema/marks.ts b/src/utils/prosemirror/schema/marks.ts index 14c7fe9f..77d84057 100644 --- a/src/utils/prosemirror/schema/marks.ts +++ b/src/utils/prosemirror/schema/marks.ts @@ -37,7 +37,7 @@ const strikethrough: MarkSpec = { getAttrs: value => value === 'line-through' && null }, ], - toDOM: () => ['span', { style: 'text-decoration-line: line-through' }, 0], + toDOM: () => ['span', { style: 'text-decoration-line: line-through;' }, 0], } const underline: MarkSpec = { @@ -52,7 +52,7 @@ const underline: MarkSpec = { getAttrs: value => value === 'underline' && null }, ], - toDOM: () => ['span', { style: 'text-decoration: underline' }, 0], + toDOM: () => ['span', { style: 'text-decoration: underline;' }, 0], } const forecolor: MarkSpec = { @@ -110,7 +110,7 @@ const fontsize: MarkSpec = { toDOM: mark => { const { fontsize } = mark.attrs let style = '' - if (fontsize) style += `font-size: ${fontsize}` + if (fontsize) style += `font-size: ${fontsize};` return ['span', { style }, 0] }, } @@ -132,7 +132,7 @@ const fontname: MarkSpec = { toDOM: mark => { const { fontname } = mark.attrs let style = '' - if (fontname) style += `font-family: ${fontname}` + if (fontname) style += `font-family: ${fontname};` return ['span', { style }, 0] }, }