def render_preview_content( file_info:FileInfo, # File to preview file_url:str, # URL to the file config:GalleryConfig, # Gallery configuration prev_url:Optional=None, # URL for previous file handler next_url:Optional=None, # URL for next file handler has_prev:bool=False, # Whether there's a previous file has_next:bool=False, # Whether there's a next file modal_id:Optional=None, # Modal ID to show (for auto-show script) text_content:Optional=None, # Pre-read text content for text files text_error:Optional=None, # Error message if text reading failed)->Any: # Preview modal content with auto-show script
Render the preview modal content with script to show the modal.
from fasthtml.common import to_xml# Test preview contentconfig = GalleryConfig()file_info = FileInfo( name="photo.jpg", path="/photos/photo.jpg", is_directory=False, file_type=FileType.IMAGE, extension="jpg", size=1024000)content = render_preview_content( file_info=file_info, file_url="/media/photo.jpg", config=config, prev_url="/preview_prev", next_url="/preview_next", has_prev=True, has_next=True)html = to_xml(content)assert"photo.jpg"in htmlassert'id="media-preview-content"'in htmlassert"<img"in html # Image playerassert"Download"in htmlassert"Previous"in htmlassert"Next"in htmlassert"File Info"in html # Info panel# Verify the auto-show script is presentassert"<script>"in htmlassert"showModal"in htmlassert"media-preview-modal"in html # Default modal ID in script# Verify hx_vals with path is present for navigationassert'hx-vals'in htmlassert'/photos/photo.jpg'in html # Path should be in hx-vals# Verify hx_target is set for navigation buttonsassert'hx-target="#media-preview-modal"'in html# Test without info panelfrom cjm_fasthtml_media_gallery.core.config import PreviewConfigno_info_config = GalleryConfig(preview=PreviewConfig(show_info_panel=False))content = render_preview_content( file_info=file_info, file_url="/media/photo.jpg", config=no_info_config)html = to_xml(content)assert"File Info"notin html# Test with custom modal IDcontent = render_preview_content( file_info=file_info, file_url="/media/photo.jpg", config=config, modal_id="custom-modal-id", prev_url="/preview_prev", next_url="/preview_next", has_prev=True, has_next=True)html = to_xml(content)assert"custom-modal-id"in htmlassert'hx-target="#custom-modal-id"'in html # Custom modal ID in hx-target# Test with text content (code file)py_file = FileInfo( name="script.py", path="/code/script.py", is_directory=False, file_type=FileType.CODE, extension="py", size=256)content = render_preview_content( file_info=py_file, file_url="/media/script.py", config=config, text_content="def hello():\n print('Hello!')")html = to_xml(content)assert"<pre"in htmlassert"def hello()"in htmlassert"script.py"in html# Test with text errorcontent = render_preview_content( file_info=py_file, file_url="/media/script.py", config=config, text_error="File too large")html = to_xml(content)assert"Unable to preview"in htmlassert"File too large"in htmlprint("render_preview_content tests passed!")