Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do no close popover menu after creating a link #17977

Merged
merged 10 commits into from
Oct 26, 2023
7 changes: 6 additions & 1 deletion frontend/src/scenes/notebooks/Marks/NotebookMarkLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,10 @@ export const NotebookMarkLink = Mark.create({
})

const isPostHogLink = (href: string): boolean => {
return new URL(href, window.location.origin).origin === window.location.origin
try {
const url = new URL(href, window.location.origin)
return url.origin === window.location.origin
} catch {
return false
}
}
16 changes: 11 additions & 5 deletions frontend/src/scenes/notebooks/Notebook/InlineMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Editor, isTextSelection } from '@tiptap/core'
import { BubbleMenu } from '@tiptap/react'
import { IconBold, IconDelete, IconItalic, IconLink, IconOpenInNew } from 'lib/lemon-ui/icons'
import { isURL } from 'lib/utils'
import { useRef } from 'react'
import NotebookIconHeading from './NotebookIconHeading'

export const InlineMenu = ({ editor }: { editor: Editor }): JSX.Element => {
const { href, target } = editor.getAttributes('link')
const menuRef = useRef<HTMLDivElement>(null)

const setLink = (href: string): void => {
editor.commands.setMark('link', { href: href })
Expand All @@ -19,25 +21,29 @@ export const InlineMenu = ({ editor }: { editor: Editor }): JSX.Element => {
return (
<BubbleMenu
editor={editor}
shouldShow={({ editor, view, state, from, to }) => {
const hasEditorFocus = view.hasFocus()
shouldShow={({ editor: { isEditable }, view, state, from, to }) => {
const isChildOfMenu = menuRef.current?.contains(document.activeElement)
const focused = view.hasFocus() || isChildOfMenu
const isTextBlock = isTextSelection(state.selection)

if (!hasEditorFocus || !editor.isEditable || !isTextBlock) {
if (!focused || !isEditable || !isTextBlock) {
return false
}

return state.doc.textBetween(from, to).length > 0
}}
>
<div className="NotebookInlineMenu flex bg-white rounded border items-center text-muted-alt p-1 space-x-0.5">
<div
ref={menuRef}
className="NotebookInlineMenu flex bg-white rounded border items-center text-muted-alt p-1 space-x-0.5"
>
{editor.isActive('link') ? (
<>
<LemonInput
size="small"
placeholder="https://posthog.com"
onChange={setLink}
value={href}
value={href ?? ''}
className="border-0"
autoFocus
/>
Expand Down
Loading