Example: link-mark-view
Install this example with
shadcn:npx shadcn@latest add @prosekit/react-example-link-mark-viewnpx shadcn@latest add @prosekit/preact-example-link-mark-viewnpx shadcn@latest add @prosekit/svelte-example-link-mark-viewnpx shadcn@latest add @prosekit/vue-example-link-mark-viewimport 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import { useMemo } from 'preact/hooks'
import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/preact'
import { defaultContent } from '../../sample/sample-doc-link-mark-view'
import { defineExtension } from './extension'
export default function Editor() {
const editor = useMemo(() => {
const extension = defineExtension()
return createEditor({ extension, defaultContent })
}, [])
return (
<ProseKit editor={editor}>
<div className="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div className="relative w-full flex-1 box-border overflow-y-auto">
<div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>
)
}import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { definePreactMarkView } from 'prosekit/preact'
import LinkView from './link-view'
export function defineExtension() {
return union(
defineBasicExtension(),
definePreactMarkView({
name: 'link',
component: LinkView,
}),
)
}
export type EditorExtension = ReturnType<typeof defineExtension>export { default as ExampleEditor } from './editor'import {
useEffect,
useState,
} from 'preact/hooks'
import type { PreactMarkViewProps } from 'prosekit/preact'
const colors = [
'#f06292',
'#ba68c8',
'#9575cd',
'#7986cb',
'#64b5f6',
'#4fc3f7',
'#4dd0e1',
'#4db6ac',
'#81c784',
'#aed581',
'#ffb74d',
'#ffa726',
'#ff8a65',
'#d4e157',
'#ffd54f',
'#ffecb3',
] as const
function pickRandomColor() {
return colors[Math.floor(Math.random() * colors.length)]
}
export default function Link(props: PreactMarkViewProps) {
const [color, setColor] = useState<string>(colors[0])
const href = props.mark.attrs.href as string
useEffect(() => {
const interval = setInterval(() => {
setColor(pickRandomColor())
}, 1000)
return () => clearInterval(interval)
}, [])
return (
<a
href={href}
ref={props.contentRef}
style={{ color, transition: 'color 1s ease-in-out' }}
>
</a>
)
}import type { NodeJSON } from 'prosekit/core'
export const defaultContent: NodeJSON = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Here is a link that changes color every second: ',
},
{
type: 'text',
marks: [
{
type: 'link',
attrs: {
href: 'https://www.example.com',
target: null,
rel: null,
},
},
],
text: 'example link',
},
],
},
],
}import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'
import { defaultContent } from '../../sample/sample-doc-link-mark-view'
import { defineExtension } from './extension'
export default function Editor() {
const editor = useMemo(() => {
const extension = defineExtension()
return createEditor({ extension, defaultContent })
}, [])
return (
<ProseKit editor={editor}>
<div className="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div className="relative w-full flex-1 box-border overflow-y-auto">
<div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>
)
}import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { defineReactMarkView } from 'prosekit/react'
import LinkView from './link-view'
export function defineExtension() {
return union(
defineBasicExtension(),
defineReactMarkView({
name: 'link',
component: LinkView,
}),
)
}
export type EditorExtension = ReturnType<typeof defineExtension>'use client'
export { default as ExampleEditor } from './editor'import type { ReactMarkViewProps } from 'prosekit/react'
import {
useEffect,
useState,
} from 'react'
const colors = [
'#f06292',
'#ba68c8',
'#9575cd',
'#7986cb',
'#64b5f6',
'#4fc3f7',
'#4dd0e1',
'#4db6ac',
'#81c784',
'#aed581',
'#ffb74d',
'#ffa726',
'#ff8a65',
'#d4e157',
'#ffd54f',
'#ffecb3',
]
function pickRandomColor() {
return colors[Math.floor(Math.random() * colors.length)]
}
export default function Link(props: ReactMarkViewProps) {
const [color, setColor] = useState(colors[0])
const href = props.mark.attrs.href as string
useEffect(() => {
const interval = setInterval(() => {
setColor(pickRandomColor())
}, 1000)
return () => clearInterval(interval)
}, [])
return (
<a
href={href}
ref={props.contentRef}
style={{ color, transition: 'color 1s ease-in-out' }}
>
</a>
)
}import type { NodeJSON } from 'prosekit/core'
export const defaultContent: NodeJSON = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Here is a link that changes color every second: ',
},
{
type: 'text',
marks: [
{
type: 'link',
attrs: {
href: 'https://www.example.com',
target: null,
rel: null,
},
},
],
text: 'example link',
},
],
},
],
}<script lang="ts">
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/svelte'
import { defaultContent } from '../../sample/sample-doc-link-mark-view'
import { defineExtension } from './extension'
const extension = defineExtension()
const editor = createEditor({ extension, defaultContent })
const mount = (element: HTMLElement) => {
editor.mount(element)
return { destroy: () => editor.unmount() }
}
</script>
<ProseKit {editor}>
<div class="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div class="relative w-full flex-1 box-border overflow-y-auto">
<div use:mount class="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
</div>
</div>
</ProseKit>import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import { defineSvelteMarkView } from 'prosekit/svelte'
import LinkView from './link-view.svelte'
export function defineExtension() {
return union(
defineBasicExtension(),
defineSvelteMarkView({
name: 'link',
component: LinkView,
}),
)
}
export type EditorExtension = ReturnType<typeof defineExtension>export { default as ExampleEditor } from './editor.svelte'<script lang="ts">
import type { SvelteMarkViewProps } from 'prosekit/svelte'
import { onDestroy } from 'svelte'
const props: SvelteMarkViewProps = $props()
const colors = [
'#f06292',
'#ba68c8',
'#9575cd',
'#7986cb',
'#64b5f6',
'#4fc3f7',
'#4dd0e1',
'#4db6ac',
'#81c784',
'#aed581',
'#ffb74d',
'#ffa726',
'#ff8a65',
'#d4e157',
'#ffd54f',
'#ffecb3',
]
function pickRandomColor() {
return colors[Math.floor(Math.random() * colors.length)]
}
let color = $state(colors[0])
const mark = props.mark
const href = $derived($mark.attrs.href as string)
const interval = setInterval(() => {
color = pickRandomColor()
}, 1000)
onDestroy(() => {
clearInterval(interval)
})
function bindContentRef(element: HTMLAnchorElement) {
props.contentRef(element)
}
</script>
<!-- svelte-ignore a11y_consider_explicit_label -->
<a use:bindContentRef {href} style:color={color} style:transition="color 1s ease-in-out"></a>import type { NodeJSON } from 'prosekit/core'
export const defaultContent: NodeJSON = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Here is a link that changes color every second: ',
},
{
type: 'text',
marks: [
{
type: 'link',
attrs: {
href: 'https://www.example.com',
target: null,
rel: null,
},
},
],
text: 'example link',
},
],
},
],
}<script setup lang="ts">
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/vue'
import {
ref,
watchPostEffect,
} from 'vue'
import { defaultContent } from '../../sample/sample-doc-link-mark-view'
import { defineExtension } from './extension'
const extension = defineExtension()
const editor = createEditor({ extension, defaultContent })
const editorRef = ref<HTMLDivElement | null>(null)
watchPostEffect((onCleanup) => {
editor.mount(editorRef.value)
onCleanup(() => editor.unmount())
})
</script>
<template>
<ProseKit :editor="editor">
<div class="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
<div class="relative w-full flex-1 box-border overflow-y-auto">
<div ref="editorRef" class="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500" />
</div>
</div>
</ProseKit>
</template>import { defineBasicExtension } from 'prosekit/basic'
import { union } from 'prosekit/core'
import {
defineVueMarkView,
type VueMarkViewComponent,
} from 'prosekit/vue'
import LinkView from './link-view.vue'
export function defineExtension() {
return union(
defineBasicExtension(),
defineVueMarkView({
name: 'link',
component: LinkView as VueMarkViewComponent,
}),
)
}
export type EditorExtension = ReturnType<typeof defineExtension>export { default as ExampleEditor } from './editor.vue'<script setup lang="ts">
import type { VueMarkViewProps } from 'prosekit/vue'
import {
onBeforeUnmount,
ref,
} from 'vue'
const props = defineProps<VueMarkViewProps>()
const colors = [
'#f06292',
'#ba68c8',
'#9575cd',
'#7986cb',
'#64b5f6',
'#4fc3f7',
'#4dd0e1',
'#4db6ac',
'#81c784',
'#aed581',
'#ffb74d',
'#ffa726',
'#ff8a65',
'#d4e157',
'#ffd54f',
'#ffecb3',
]
function pickRandomColor() {
return colors[Math.floor(Math.random() * colors.length)]
}
const color = ref(colors[0])
const href = props.mark.value.attrs.href as string
const interval = setInterval(() => {
color.value = pickRandomColor()
}, 1000)
onBeforeUnmount(() => {
clearInterval(interval)
})
</script>
<template>
<a
:ref="props.contentRef"
:href="href"
:style="{ color: color, transition: 'color 1s ease-in-out' }"
>
</a>
</template>import type { NodeJSON } from 'prosekit/core'
export const defaultContent: NodeJSON = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Here is a link that changes color every second: ',
},
{
type: 'text',
marks: [
{
type: 'link',
attrs: {
href: 'https://www.example.com',
target: null,
rel: null,
},
},
],
text: 'example link',
},
],
},
],
}