From 89e80a4e128700d8d65bb413c373dacf609ed6c5 Mon Sep 17 00:00:00 2001 From: holazz <2418184580@qq.com> Date: Wed, 3 Aug 2022 19:55:52 +0800 Subject: [PATCH] Refactor HeadingBlock using tiptap --- src/components/Block.vue | 12 ++++-- src/components/Lotion.vue | 60 ++++++++++++++++++++++------- src/components/Main.vue | 2 +- src/components/Markdown.vue | 4 +- src/components/blocks/TextBlock.vue | 24 +++++++++++- src/components/elements/Editor.vue | 4 ++ src/utils/types.ts | 11 +++++- 7 files changed, 95 insertions(+), 22 deletions(-) diff --git a/src/components/Block.vue b/src/components/Block.vue index e49560c..90e4256 100644 --- a/src/components/Block.vue +++ b/src/components/Block.vue @@ -97,12 +97,18 @@ function getInnerContent () { } function getTextContent () { + if (props.block.type === BlockType.Text || props.block.type === BlockType.Quote || props.block.type === BlockType.H1) { + return content.value.$el.querySelector('.ProseMirror').editor.getText() + } const innerContent = getInnerContent() if (innerContent) return innerContent.parentElement ? innerContent.parentElement.textContent : innerContent.textContent else return '' } function getHtmlContent () { + if (props.block.type === BlockType.Text || props.block.type === BlockType.Quote || props.block.type === BlockType.H1) { + return content.value.$el.querySelector('.ProseMirror').editor.getHTML() + } const innerContent = getInnerContent() if (innerContent) return innerContent.parentElement.innerHTML else return '' @@ -299,9 +305,9 @@ function getCaretCoordinates () { function getCaretPos () { const selection = window.getSelection() if (selection) { + let selectedNode = selection.anchorNode if (props.block.type === BlockType.Text || props.block.type === BlockType.Quote) { let offsetNode, offset = 0, tag = null - let selectedNode = selection.anchorNode if (['STRONG', 'EM'].includes(selectedNode?.parentElement?.tagName as string)) { selectedNode = selectedNode?.parentElement as Node tag = (selectedNode as HTMLElement).tagName.toLowerCase() @@ -323,7 +329,7 @@ function getCaretPos () { } return { pos: offset + selection.anchorOffset + (selectedNode?.parentElement?.tagName === 'P' ? 3 : 0), tag } } else { - return { pos: selection.anchorOffset } + return { pos: selection.anchorOffset + (selectedNode?.parentElement?.tagName === 'H1' ? 4 : 0) } } } else { return { pos: 0 } @@ -365,7 +371,7 @@ function getCaretPosWithoutTags () { function setCaretPos (caretPos:number) { const innerContent = getInnerContent() if (innerContent) { - if (props.block.type === BlockType.Text || props.block.type === BlockType.Quote) { + if (props.block.type === BlockType.Text || props.block.type === BlockType.Quote || props.block.type === BlockType.H1) { let offsetNode, offset = 0 const numNodes = (content.value as any).$el.firstChild.firstChild.childNodes.length for (const [i, node] of (content.value as any).$el.firstChild.firstChild.childNodes.entries()) { diff --git a/src/components/Lotion.vue b/src/components/Lotion.vue index ab16eed..85df787 100644 --- a/src/components/Lotion.vue +++ b/src/components/Lotion.vue @@ -26,9 +26,9 @@ diff --git a/src/components/Main.vue b/src/components/Main.vue index 277ae71..06787b9 100644 --- a/src/components/Main.vue +++ b/src/components/Main.vue @@ -20,7 +20,7 @@ const page = ref({ blocks:[{ type: BlockType.H1, details: { - value: 'Get Started' + value: '

Get Started

' }, }, { type: BlockType.Divider, diff --git a/src/components/Markdown.vue b/src/components/Markdown.vue index 10a16fe..171b799 100644 --- a/src/components/Markdown.vue +++ b/src/components/Markdown.vue @@ -17,13 +17,15 @@ const props = defineProps({ const markdownBlocks = computed(() => { return props.page.blocks.map(block => { - if (block.type === BlockType.Text || block.type === BlockType.Quote) { + if (block.type === BlockType.Text || block.type === BlockType.Quote || block.type === BlockType.H1) { return { type: block.type, details: { value: (block.details.value as string) .replaceAll('

', '') .replaceAll('

', '') + .replaceAll('

', '') + .replaceAll('

', '') .replaceAll('', '**') .replaceAll('', '**') .replaceAll('', '*') diff --git a/src/components/blocks/TextBlock.vue b/src/components/blocks/TextBlock.vue index a56b0cb..7de709a 100644 --- a/src/components/blocks/TextBlock.vue +++ b/src/components/blocks/TextBlock.vue @@ -1,11 +1,13 @@ diff --git a/src/components/elements/Editor.vue b/src/components/elements/Editor.vue index 5a96023..a7fa523 100644 --- a/src/components/elements/Editor.vue +++ b/src/components/elements/Editor.vue @@ -8,6 +8,7 @@ import { watch } from 'vue' import Document from '@tiptap/extension-document' import Paragraph from '@tiptap/extension-paragraph' import Text from '@tiptap/extension-text' +import Heading from '@tiptap/extension-heading' import Bold from '@tiptap/extension-bold' import Italic from '@tiptap/extension-italic' import History from '@tiptap/extension-history' @@ -27,6 +28,9 @@ const editor = useEditor({ Document, Paragraph, Text, + Heading.configure({ + levels: [1], + }), Bold, Italic, History, diff --git a/src/utils/types.ts b/src/utils/types.ts index 34eb7bc..e9d2823 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -24,9 +24,18 @@ export interface Details { export const BlockComponents = { [BlockType.Text]: TextBlock, - [BlockType.H1]: HeadingBlock, + [BlockType.H1]: TextBlock, [BlockType.H2]: HeadingBlock, [BlockType.H3]: HeadingBlock, [BlockType.Divider]: DividerBlock, [BlockType.Quote]: QuoteBlock, } + +export const blockTagMap = { + [BlockType.Text]: 'p', + [BlockType.H1]: 'h1', + [BlockType.H2]: '', + [BlockType.H3]: '', + [BlockType.Quote]: 'p', + [BlockType.Divider]: '', +}