Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react"
import { NodeViewWrapper, ReactNodeViewRenderer } from "@tiptap/react"
import type { ReactNodeViewProps } from "@tiptap/react"
import { Node, mergeAttributes, type CommandProps } from "@tiptap/core"
import { Node, mergeAttributes } from "@tiptap/core"
import { LearningResourceListCard, styled } from "ol-components"
import { useLearningResourcesDetail } from "api/hooks/learningResources"

Expand Down Expand Up @@ -76,11 +76,39 @@ export const LearningResourceNode = Node.create<LearningResourceOptions>({
return {
insertLearningResource:
(resourceId: number, href?: string) =>
({ commands }: CommandProps) => {
return commands.insertContent({
type: this.name,
attrs: { resourceId, href },
})
({ state, tr, dispatch }) => {
const { $from } = state.selection

const nodeType = state.schema.nodes.learningResource
const paragraphType = state.schema.nodes.paragraph
if (!nodeType) return false

// 🛑 Guard: top-level selection
if ($from.depth === 0) {
tr.insert(
state.selection.from,
nodeType.create({ resourceId, href }),
)

dispatch?.(tr.scrollIntoView())
return true
}

// ✅ Replace the current block
const from = $from.before($from.depth)
const to = $from.after($from.depth)

tr.replaceWith(
from,
to,
[
nodeType.create({ resourceId, href }),
paragraphType?.create(),
].filter(Boolean),
)

dispatch?.(tr.scrollIntoView())
return true
},
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ export const MediaEmbedNode = Node.create({
return {
insertMedia:
(src: string) =>
({ commands }: CommandProps) => {
return commands.insertContent({
type: this.name,
attrs: { src },
})
({ state, chain }: CommandProps) => {
const { from } = state.selection

return (
chain()
.insertContentAt(from, {
type: this.name,
attrs: { src },
})
// 👇 Move cursor AFTER the inserted node
.setTextSelection(from + 2)
.run()
)
},
}
},
Expand Down
Loading