Skip to content

Commit 9b47fca

Browse files
authored
Merge pull request #517 from milaiwi/fix-pasting
Fix pasting
2 parents 8f1674f + ad9638a commit 9b47fca

File tree

3 files changed

+83
-29
lines changed

3 files changed

+83
-29
lines changed

package-lock.json

Lines changed: 66 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/blocknote/core/BlockNoteExtensions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const getBlockNoteExtensions = <BSchema extends HMBlockSchema>(opts: {
6868

6969
// copy paste:
7070
// @ts-ignore
71-
createMarkdownExtension(),
71+
createMarkdownExtension(opts.editor),
7272

7373
// block manupulations:
7474
BlockManipulationExtension,

src/lib/blocknote/core/extensions/Markdown/MarkdownExtension.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { Editor, Extension } from '@tiptap/core'
22
import { Fragment, Node } from '@tiptap/pm/model'
33
import { Plugin } from 'prosemirror-state'
44
import { youtubeParser } from '@/components/Editor/types/utils'
5+
import { BlockNoteEditor } from '../../BlockNoteEditor'
6+
import { getBlockInfoFromPos } from '@/lib/utils'
7+
import * as BlockUtils from '@/lib/utils/block-utils'
58

69
function containsMarkdownSymbols(pastedText: string) {
710
// Regex to detect unique Markdown symbols at the start of a line
@@ -53,7 +56,7 @@ function getPastedNodes(parent: Node | Fragment, editor: Editor) {
5356
return nodes
5457
}
5558

56-
const createMarkdownExtension = () => {
59+
const createMarkdownExtension = (bnEditor: BlockNoteEditor) => {
5760
const MarkdownExtension = Extension.create({
5861
name: 'MarkdownPasteHandler',
5962
priority: 900,
@@ -76,6 +79,7 @@ const createMarkdownExtension = () => {
7679
const { selection } = state
7780

7881
const isMarkdown = pastedHtml ? containsMarkdownSymbols(pastedText) : true
82+
7983
if (!isMarkdown) {
8084
if (hasList) {
8185
const firstBlockGroup = slice.content.firstChild?.type.name === 'blockGroup'
@@ -96,7 +100,6 @@ const createMarkdownExtension = () => {
96100
}
97101
return false
98102
}
99-
100103
if (hasVideo) {
101104
let embedUrl = 'https://www.youtube.com/embed/'
102105
if (pastedText.includes('youtu.be') || pastedText.includes('youtube')) {
@@ -110,19 +113,18 @@ const createMarkdownExtension = () => {
110113
view.dispatch(view.state.tr.replaceSelectionWith(node))
111114
}
112115
}
116+
} else {
117+
// This is not a media file, just plaintext
118+
bnEditor.markdownToBlocks(pastedText).then((organizedBlocks: any) => {
119+
const blockInfo = getBlockInfoFromPos(state.doc, selection.from)
120+
bnEditor.replaceBlocks(
121+
[blockInfo.node.attrs.id],
122+
// @ts-ignore
123+
organizedBlocks,
124+
)
125+
BlockUtils.setGroupTypes(bnEditor._tiptapEditor, organizedBlocks)
126+
})
113127
}
114-
115-
// bnEditor.markdownToBlocks(pastedText).then((organizedBlocks) => {
116-
// const blockInfo = getBlockInfoFromPos(state.doc, selection.from)
117-
// console.log(`BLockINfo type: `, blockInfo.node.type.name)
118-
// bnEditor.replaceBlocks(
119-
// [blockInfo.node.attrs.id],
120-
// // @ts-ignore
121-
// organizedBlocks,
122-
// )
123-
// BlockUtils.setGroupTypes(bnEditor._tiptapEditor, organizedBlocks)
124-
// })
125-
126128
return true
127129
},
128130
},

0 commit comments

Comments
 (0)