'use client'
import { useEffect, useState } from 'react'
import { createReactBlockSpec, createReactInlineContentSpec } from '@blocknote/react'
import { useTranslation } from 'react-i18next'
import {
Info,
Lightbulb,
AlertTriangle,
ChevronRight,
Globe,
ExternalLink,
Pencil,
ListTree,
FileCode,
} from 'lucide-react'
import dynamic from 'next/dynamic'
import type { NoteBlock } from '@/lib/notes-api'
import { safeNoteUrl } from '../model/urls'
const TaskView = dynamic(() => import('./NoteTaskView'), { ssr: false })
export const callout = createReactBlockSpec(
{ type: 'callout', propSchema: { tone: { default: 'tip' } }, content: 'inline' },
{
render: function Callout({ block, editor, contentRef }) {
const { t } = useTranslation()
const icons = { tip: Lightbulb, info: Info, warning: AlertTriangle }
const tone = block.props.tone as keyof typeof icons
const Icon = icons[tone] || Info
return (
)
},
}
)
export const toggleHeading = createReactBlockSpec(
{
type: 'toggleHeading',
propSchema: { level: { default: 2 }, collapsed: { default: false } },
content: 'inline',
},
{
render: ({ block, contentRef }) => (
),
renderFrame: function Toggle({ block, editor, slotRef }) {
const { t } = useTranslation()
return (
)
},
}
)
export const noteColumn = createReactBlockSpec(
{ type: 'noteColumn', content: 'none', propSchema: {}, layoutOnly: true, placeable: 'namedOnly' },
{ renderFrame: ({ slotRef }) => }
)
export const noteColumns = createReactBlockSpec(
{
type: 'noteColumns',
content: 'none',
propSchema: {},
layoutOnly: true,
children: { allow: ['noteColumn'], min: 2, dissolve: true },
},
{ renderFrame: ({ slotRef }) => }
)
export const tableOfContents = createReactBlockSpec(
{ type: 'tableOfContents', content: 'none', propSchema: {} },
{
render: function Contents({ editor }) {
const { t } = useTranslation()
const [headings, setHeadings] = useState([])
useEffect(() => {
const refresh = () => {
const result: NoteBlock[] = []
const walk = (blocks: NoteBlock[]) =>
blocks.forEach(b => {
if (b.type === 'heading' || b.type === 'toggleHeading') result.push(b)
walk(b.children || [])
})
walk(editor.document as NoteBlock[])
setHeadings(result)
}
refresh()
return editor.onChange(refresh)
}, [editor])
return (
)
},
}
)
export const webCard = createReactBlockSpec(
{
type: 'webCard',
content: 'none',
propSchema: {
url: { default: '' },
title: { default: '' },
description: { default: '' },
mode: { default: 'bookmark' },
},
},
{
render: function WebCard({ block, editor }) {
const { t } = useTranslation()
const [edit, setEdit] = useState(!block.props.url)
const [url, setUrl] = useState(block.props.url)
const [title, setTitle] = useState(block.props.title)
const [description, setDescription] = useState(block.props.description)
const [error, setError] = useState('')
const safe = safeNoteUrl(block.props.url)
const host = safe && /^https?:/.test(safe) ? new URL(safe).hostname : ''
return (
{edit ? (
) : (
<>
{block.props.mode === 'embed' && safe && (
<>
{t('If the site blocks embedding, open the link above')}
>
)}
>
)}
)
},
}
)
export const htmlEmbed = createReactBlockSpec(
{ type: 'htmlEmbed', content: 'none', propSchema: { source: { default: '' } } },
{
render: function HtmlEmbed({ block, editor }) {
const { t } = useTranslation()
const [editing, setEditing] = useState(!block.props.source)
return (
{t('HTML')}
{editing ? (
)
},
}
)
export const taskView = createReactBlockSpec(
{ type: 'taskView', content: 'none', propSchema: { view: { default: 'table' } } },
{
render: ({ block, editor }) => (
editor.updateBlock(block, { props: { view } })}
/>
),
}
)
export const noteDate = createReactInlineContentSpec(
{ type: 'noteDate', content: 'none', propSchema: { date: { default: '' } } },
{
render: function DateInline({ inlineContent, updateInlineContent }) {
const { t } = useTranslation()
return (
updateInlineContent({ type: 'noteDate', props: { date: e.target.value } })}
/>
)
},
}
)