Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ export function useMarkdownFind({
*/
const revealActiveMatch = useCallback(() => {
requestAnimationFrame(() => {
editorRef.current?.view.dom
.querySelector(`.${ACTIVE_MATCH_CLASS}`)
?.scrollIntoView({ block: 'center' })
const current = editorRef.current
if (!current || current.isDestroyed) return
current.view.dom.querySelector(`.${ACTIVE_MATCH_CLASS}`)?.scrollIntoView({ block: 'center' })
})
}, [])

Expand All @@ -99,7 +99,7 @@ export function useMarkdownFind({
*/
const syncTally = useCallback(() => {
const current = editorRef.current
if (!current) return
if (!current || current.isDestroyed) return
const { matches, activeIndex, truncated } = getFindTally(current.state)
setTally((previous) =>
previous.count === matches.length &&
Expand All @@ -116,7 +116,7 @@ export function useMarkdownFind({
* on each of those. Not subscribed while the bar is closed, so typing costs nothing then.
*/
useEffect(() => {
if (!editor || !isOpen) return
if (!editor || editor.isDestroyed || !isOpen) return
syncTally()
editor.on('transaction', syncTally)
return () => {
Expand All @@ -132,15 +132,15 @@ export function useMarkdownFind({
const queryRef = useRef(query)
queryRef.current = query
useEffect(() => {
if (!editor || queryRef.current.length === 0) return
if (!editor || editor.isDestroyed || queryRef.current.length === 0) return
setFindQuery(editor, queryRef.current)
}, [editor])

const setQuery = useCallback(
(next: string) => {
setQueryState(next)
const current = editorRef.current
if (!current) return
if (!current || current.isDestroyed) return
setFindQuery(current, next)
revealActiveMatch()
},
Expand All @@ -150,7 +150,7 @@ export function useMarkdownFind({
const step = useCallback(
(delta: number) => {
const current = editorRef.current
if (!current) return
if (!current || current.isDestroyed) return
stepFindMatch(current, delta)
revealActiveMatch()
},
Expand All @@ -162,13 +162,18 @@ export function useMarkdownFind({

const replaceCurrent = useCallback(() => {
const current = editorRef.current
if (!current || !replaceActiveFindMatch(current, replacement, warnReplacementLimit)) return
if (
!current ||
current.isDestroyed ||
!replaceActiveFindMatch(current, replacement, warnReplacementLimit)
)
return
revealActiveMatch()
}, [replacement, revealActiveMatch])

const replaceAll = useCallback(() => {
const current = editorRef.current
if (!current) return
if (!current || current.isDestroyed) return
replaceAllFindMatches(current, replacement, warnReplacementLimit)
}, [replacement])

Expand All @@ -179,7 +184,7 @@ export function useMarkdownFind({
setReplacement('')
setTally(EMPTY_TALLY)
const current = editorRef.current
if (current) setFindQuery(current, '')
if (current && !current.isDestroyed) setFindQuery(current, '')
requestAnimationFrame(() => {
if (current && !current.isDestroyed) current.commands.focus()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export function EditorBubbleMenu({
* preventing a lost pointer release from suppressing later keyboard selections.
*/
useEffect(() => {
if (editor.isDestroyed) return
const dom = editor.view.dom
const onPointerDown = () => {
isPointerDownRef.current = true
Expand Down Expand Up @@ -255,6 +256,7 @@ export function EditorBubbleMenu({
}

useEffect(() => {
if (editor.isDestroyed) return
const dom = editor.view.dom
const openLinkOnShortcut = (event: KeyboardEvent) => {
if (!editor.isEditable) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export function LinkHoverCard({ editor }: LinkHoverCardProps) {
}, [cancelHide, isEditing])

useEffect(() => {
// Cached routes reconnect child effects before useEditor replaces the disposed editor.
if (editor.isDestroyed) return
const dom = editor.view.dom
const onOver = (event: Event) => {
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function SuggestionList<T>({
const activeOptionId = hasOptions ? `${optionIdPrefix}-${activeIndex}` : null

useEffect(() => {
if (editor.isDestroyed) return
const dom = editor.view.dom
dom.setAttribute('aria-haspopup', 'listbox')
dom.setAttribute('aria-expanded', 'true')
Expand All @@ -76,6 +77,7 @@ export function SuggestionList<T>({
}, [editor])

useEffect(() => {
if (editor.isDestroyed) return
const dom = editor.view.dom
if (activeOptionId) {
dom.setAttribute('aria-controls', listboxId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function bubbleMenuAnchorRange(selection: Selection): { from: number; to:
* editor mounts so the caller skips positioning.
*/
function selectionVirtualElement(editor: Editor) {
if (editor.isDestroyed) return null
const { view, state } = editor
if (!view.dom.isConnected) return null
const { from, to } = bubbleMenuAnchorRange(state.selection)
Expand Down Expand Up @@ -53,10 +54,11 @@ export function useBubbleMenuFloating(

useEffect(() => {
const menu = menuRef.current
if (!menu) return
if (!menu || editor.isDestroyed) return
let stop: (() => void) | undefined
const trackSelection = () => {
stop?.()
if (editor.isDestroyed) return
const { node } = editor.view.domAtPos(editor.state.selection.from)
const reference = node instanceof Element ? node : (node.parentElement ?? editor.view.dom)
stop = autoUpdate(reference, menu, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function useEditorToolbar({
})

useEffect(() => {
if (editor.isDestroyed) return
let frame: number | undefined
const enterToolbar = (event: globalThis.KeyboardEvent) => {
if (
Expand Down
Loading
Loading