predikt/oracle/web/components/editor/floating-format-menu.tsx
caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

150 lines
4.3 KiB
TypeScript

import {
CheckIcon,
EyeOffIcon,
LinkIcon,
TrashIcon,
} from '@heroicons/react/solid'
import { Editor } from '@tiptap/core'
import { BubbleMenu } from '@tiptap/react'
import clsx from 'clsx'
import { getUrl } from 'common/util/parse'
import { useState } from 'react'
import BoldIcon from 'web/lib/icons/bold-icon.svg'
import ItalicIcon from 'web/lib/icons/italic-icon.svg'
import TypeIcon from 'web/lib/icons/type-icon.svg'
import { isAndroid } from 'web/lib/util/device'
// see https://tiptap.dev/guide/menus
export function FloatingFormatMenu(props: {
editor: Editor | null
/** show more formatting options */
advanced?: boolean
}) {
const { editor, advanced } = props
const [url, setUrl] = useState<string | null>(null)
if (!editor) return null
const setLink = () => {
const href = url && getUrl(url)
if (href) {
editor.chain().focus().extendMarkRange('link').setLink({ href }).run()
}
}
const unsetLink = () => editor.chain().focus().unsetLink().run()
// On Android, adjust the menu position to appear above the native text selection menu
const tippyOptions = isAndroid()
? // const tippyOptions = true
{
offset: [0, 5] as [number, number],
placement: 'bottom' as const,
}
: {}
return (
<BubbleMenu
editor={editor}
className="text-ink-0 bg-ink-700 flex gap-2 rounded-sm p-1"
tippyOptions={tippyOptions}
>
{url === null ? (
<>
{advanced && (
<>
<IconButton
icon={TypeIcon}
label="Toggle heading level 1"
onClick={() =>
editor.chain().focus().toggleHeading({ level: 1 }).run()
}
isActive={editor.isActive('heading', { level: 1 })}
/>
<IconButton
icon={TypeIcon}
label="Toggle heading level 2"
onClick={() =>
editor.chain().focus().toggleHeading({ level: 2 }).run()
}
isActive={editor.isActive('heading', { level: 2 })}
className="!h-4"
/>
<Divider />
</>
)}
<IconButton
icon={BoldIcon}
label="Toggle bold"
onClick={() => editor.chain().focus().toggleBold().run()}
isActive={editor.isActive('bold')}
/>
<IconButton
icon={ItalicIcon}
label="Toggle italic"
onClick={() => editor.chain().focus().toggleItalic().run()}
isActive={editor.isActive('italic')}
/>
<IconButton
icon={LinkIcon}
label={editor.isActive('link') ? 'Remove link' : 'Add link'}
onClick={() => (editor.isActive('link') ? unsetLink() : setUrl(''))}
isActive={editor.isActive('link')}
/>
<IconButton
icon={EyeOffIcon}
label="Toggle spoiler"
onClick={() => editor.chain().focus().toggleSpoiler().run()}
isActive={editor.isActive('spoiler')}
/>
</>
) : (
<>
<input
type="text"
inputMode="url"
className="h-5 border-0 bg-inherit text-sm !shadow-none !ring-0"
placeholder="Type or paste a link"
onChange={(e) => setUrl(e.target.value)}
/>
<button
type="button"
aria-label="Save link"
onClick={() => (setLink(), setUrl(null))}
>
<CheckIcon className="h-5 w-5" />
</button>
<button
type="button"
aria-label="Remove link"
onClick={() => (unsetLink(), setUrl(null))}
>
<TrashIcon className="h-5 w-5" />
</button>
</>
)}
</BubbleMenu>
)
}
const IconButton = (props: {
icon: React.FC<React.SVGProps<SVGSVGElement>>
onClick: () => any
isActive?: boolean
className?: string
label: string
}) => {
const { icon: Icon, onClick, isActive, className, label } = props
return (
<button onClick={onClick} type="button" aria-label={label}>
<Icon
className={clsx('h-5', isActive && 'text-primary-200', className)}
/>
</button>
)
}
const Divider = () => <div className="bg-ink-400 mx-0.5 w-[1px]" />