{
  "version": 3,
  "sources": ["../../../../../src/lib/ui/components/HelperButtons/BackToContent.tsx"],
  "sourcesContent": ["import { Editor, EditorAtom, useEditor, useQuickReactor } from '@tldraw/editor'\nimport { useEffect, useRef, useState } from 'react'\nimport { useActions } from '../../context/actions'\nimport { TldrawUiMenuActionItem } from '../primitives/menus/TldrawUiMenuActionItem'\n\nconst backToContentSuppressed = new EditorAtom('backToContentSuppressed', () => false)\nconst suppressTimeout = new EditorAtom<number | null>('backToContentSuppressTimeout', () => null)\n\n/**\n * How long all content must stay off-screen before the \"back to content\" button\n * appears. Delaying the appearance stops the button from flickering during brief\n * off-screen moments while panning or zooming.\n * @internal\n */\nexport const BACK_TO_CONTENT_APPEAR_DELAY_MS = 1000\n\n/**\n * Hide the \"back to content\" helper button right away, regardless of where the\n * camera currently is. This is used when the user has explicitly chosen to\n * navigate back to the content (e.g. via the \"move focus to canvas\" button): the\n * button should disappear on intent rather than waiting for the camera animation\n * to physically bring a shape back into the viewport. The button falls back to\n * its normal reactive logic after `durationMs`, by which point the camera has\n * arrived and the content is visible.\n * @internal\n */\nexport function suppressBackToContent(editor: Editor, durationMs: number) {\n\tbackToContentSuppressed.set(editor, true)\n\t// Cancel any pending timeout so an earlier (shorter) timer can't clear the\n\t// suppression while a newer camera animation is still running.\n\tconst pending = suppressTimeout.get(editor)\n\tif (pending !== null) clearTimeout(pending)\n\tconst id = editor.timers.setTimeout(() => {\n\t\tbackToContentSuppressed.set(editor, false)\n\t\tsuppressTimeout.set(editor, null)\n\t}, durationMs)\n\tsuppressTimeout.set(editor, id)\n}\n\nexport function BackToContent() {\n\tconst editor = useEditor()\n\n\tconst actions = useActions()\n\n\tconst [showBackToContent, setShowBackToContent] = useState(false)\n\tconst rIsShowing = useRef(false)\n\tconst rAppearTimeout = useRef<number | null>(null)\n\n\tuseQuickReactor(\n\t\t'toggle showback to content',\n\t\t() => {\n\t\t\tlet showBackToContentNow = false\n\t\t\tif (!backToContentSuppressed.get(editor)) {\n\t\t\t\tconst shapeIds = editor.getCurrentPageShapeIds()\n\t\t\t\tif (shapeIds.size) {\n\t\t\t\t\tshowBackToContentNow = shapeIds.size === editor.getNotVisibleShapes().size\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (showBackToContentNow === rIsShowing.current) {\n\t\t\t\t// Already in the desired state. Cancel any pending appearance timer\n\t\t\t\t// that's no longer needed (e.g. content came back into view before\n\t\t\t\t// the delay elapsed).\n\t\t\t\tif (rAppearTimeout.current !== null) {\n\t\t\t\t\tclearTimeout(rAppearTimeout.current)\n\t\t\t\t\trAppearTimeout.current = null\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif (showBackToContentNow) {\n\t\t\t\t// Delay the appearance so the button doesn't flicker during brief\n\t\t\t\t// off-screen moments. The timer cancels above if content returns.\n\t\t\t\tif (rAppearTimeout.current === null) {\n\t\t\t\t\trAppearTimeout.current = editor.timers.setTimeout(() => {\n\t\t\t\t\t\trAppearTimeout.current = null\n\t\t\t\t\t\trIsShowing.current = true\n\t\t\t\t\t\tsetShowBackToContent(true)\n\t\t\t\t\t}, BACK_TO_CONTENT_APPEAR_DELAY_MS)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Hiding stays immediate.\n\t\t\t\tif (rAppearTimeout.current !== null) {\n\t\t\t\t\tclearTimeout(rAppearTimeout.current)\n\t\t\t\t\trAppearTimeout.current = null\n\t\t\t\t}\n\t\t\t\trIsShowing.current = false\n\t\t\t\tsetShowBackToContent(false)\n\t\t\t}\n\t\t},\n\t\t[editor]\n\t)\n\n\t// Clear any pending appearance timer if we unmount before it fires.\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tif (rAppearTimeout.current !== null) {\n\t\t\t\tclearTimeout(rAppearTimeout.current)\n\t\t\t\trAppearTimeout.current = null\n\t\t\t}\n\t\t}\n\t}, [])\n\n\tif (!showBackToContent) return null\n\n\treturn (\n\t\t<TldrawUiMenuActionItem\n\t\t\tactionId=\"back-to-content\"\n\t\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