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: '
', '') .replaceAll('
', '') + .replaceAll('