'use client'
import { Node, mergeAttributes } from '@tiptap/core'
import { ReactNodeViewRenderer, NodeViewWrapper } from '@tiptap/react'
import { FileText, Trash2, ChevronRight } from 'lucide-react'
import { useLanguage } from '@/lib/i18n'
import { useRouter } from 'next/navigation'
const SubPageView = ({ node, deleteNode, selected }: any) => {
const { t } = useLanguage()
const router = useRouter()
const noteId = node.attrs.noteId as string
const title = (node.attrs.title as string) || t('notes.untitled') || 'Sans titre'
const open = () => {
if (noteId) router.push(`/home?openNote=${noteId}`)
}
return (
)
}
export const SubPageExtension = Node.create({
name: 'subPageBlock',
group: 'block',
atom: true,
defining: true,
addAttributes() {
return {
noteId: {
default: '',
parseHTML: (el) => el.getAttribute('data-note-id') || '',
renderHTML: (attrs) => ({ 'data-note-id': attrs.noteId }),
},
title: {
default: '',
parseHTML: (el) => el.getAttribute('data-title') || '',
renderHTML: (attrs) => ({ 'data-title': attrs.title }),
},
}
},
parseHTML() {
return [{ tag: 'div[data-type="sub-page-block"]' }]
},
renderHTML({ node, HTMLAttributes }) {
return [
'div',
mergeAttributes(HTMLAttributes, {
'data-type': 'sub-page-block',
'data-note-id': node.attrs.noteId,
'data-title': node.attrs.title,
class: 'sub-page-block',
}),
]
},
addNodeView() {
return ReactNodeViewRenderer(SubPageView)
},
addKeyboardShortcuts() {
return {
'Mod-Shift-P': () => this.editor.commands.insertContent({
type: this.name,
attrs: { noteId: '', title: '' },
}),
}
},
})
export async function insertSubPageBlock(editor: any): Promise {
const notebookId = (editor.storage as any).structuredViewBlock?.notebookId as string | null
try {
const res = await fetch('/api/notes', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: 'Sans titre',
content: '',
notebookId: notebookId || undefined,
}),
})
const data = await res.json()
if (!res.ok || !data.data) return
const note = data.data
editor.chain().focus().insertContent({
type: 'subPageBlock',
attrs: {
noteId: note.id,
title: note.title || 'Sans titre',
},
}).run()
} catch (e) {
console.error('[SubPage] Failed to create note:', e)
}
}