import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Platform } from 'react-native';
import { useForm, FormProvider } from 'react-hook-form';
import { View, Pressable } from 'app/design/view';
import Image, { POST_ENTRY_COVER_SIZES, POST_ENTRY_COVER_WIDTH_CAP } from 'app/ui/atoms/image';
import { getCoverAspectClass } from 'app/lib/image/upload-settings';
import Html from 'app/ui/atoms/html';
import { splitInlineMedia } from 'app/lib/editor/inline-video';
import { Text, H1C } from 'app/design/typography';
import { Input } from 'app/design/controls';
import { appSetting, clearLinks, getYouTubeVideoId, cn } from 'app/lib/util'
import { useEditableRequest } from 'app/lib/form/form-helpers'
import { ContentMore } from 'app/ui/molecules/content/content-more';
import EntityAttachments from './entity-attachments';
import TextMore from 'app/ui/molecules/content/text-more';
import Video from 'app/ui/atoms/video';
import Youtube from 'app/ui/molecules/content/youtube'
import InlineVideo from 'app/ui/molecules/content/inline-video'
import InlineEmbed from 'app/ui/molecules/content/inline-embed'
import InlineTable from 'app/ui/molecules/content/inline-table'
import { BlockWrapper } from 'app/components/block-wrapper'
import RftText from 'app/components/form-fields/editor-rft-text'
const isWeb = Platform.OS === 'web'
function useClickOutside(enabled, ref, onOutside) {
const onOutsideRef = useRef(onOutside)
onOutsideRef.current = onOutside
useEffect(() => {
if (!enabled || !isWeb || typeof document === 'undefined') return
const handle = (event) => {
const node = ref.current
if (!node) return
const target = event.target
if (node.contains?.(target)) return
onOutsideRef.current?.()
}
// Skip the same click that opened edit mode
const timer = setTimeout(() => {
document.addEventListener('mousedown', handle)
document.addEventListener('touchstart', handle)
}, 0)
return () => {
clearTimeout(timer)
document.removeEventListener('mousedown', handle)
document.removeEventListener('touchstart', handle)
}
}, [enabled, ref])
}
function EditableTitleField({
editable,
requestUrl,
fieldName = 'title',
initialValue,
inputClassName,
children,
}) {
const { value, commit } = useEditableRequest({
initialValue: initialValue ?? '',
requestUrl,
fieldName,
toParam: (v) => encodeURIComponent(String(v ?? '')),
})
const [editing, setEditing] = useState(false)
const draftRef = useRef(value ?? '')
const wrapRef = useRef(null)
const saveAndClose = useCallback(async () => {
await commit(draftRef.current)
setEditing(false)
}, [commit])
useClickOutside(editing, wrapRef, saveAndClose)
if (!editable) {
return typeof children === 'function' ? children(initialValue) : children
}
if (editing) {
return (