okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
743a6b3c6
main
1import { useEffect , useMemo , useState } from 'react' ; 2import { tr } from '../lib/i18n' ; 3import type { TimeSkipFile } from '../lib/time-skip' ; 4import { HTML_PREVIEW_SANDBOX , diffStats , fileKind , htmlPreviewDocument , needsOutsideFiles , lineDiff , segmentLines , timeSkipImageUrl , wordDiff , type Segment } from '../lib/time-skip-compare' ; 5import { Btn , Dialog , Icon } from './ui' ; 6 7const baseName = ( path :string ) => path . split ( '/' ). pop () || path ; 8const statusText = ( status :TimeSkipFile [ 'status' ]) => tr ( status === 'added' ?'new' :status === 'removed' ?'removed' :'changed' ); 9 10/** Lines of marked words laid out as the document they are: headings, list items, code fences and paragraphs (Markdown), or plain lines. */ 11function MarkedDoc ({ segments, markdown} :{ segments :Segment []; markdown :boolean }) { 12const lines = segmentLines ( segments ); 13let fenced = false ; 14return < div className = { markdown ?'tsc-doc tsc-doc--md' :'tsc-doc' }> 15{ lines . map (( line , i ) => { 16const plain = line . map ( s => s . text ). join ( '' ); 17const marks = ( parts :Segment []) => parts . map (( s , k ) => s . changed ?< mark key = { k }>{ s . text }</ mark > :< span key = { k }>{ s . text }</ span >); 18if ( ! markdown ) return < p key = { i }>{ plain ?marks ( line ) :< br />}</ p >; 19if ( / ^\s*``` / . test ( plain )) { fenced = ! fenced ; return < p key = { i } className = "tsc-fence" >{ plain ?marks ( line ) :null }</ p >; } 20if ( fenced ) return < p key = { i } className = "tsc-code" >{ plain ?marks ( line ) :< br />}</ p >; 21const heading = / ^(#{1,6})\s+ / . exec ( plain ); 22if ( heading ) return < p key = { i } className = { `tsc-h tsc-h ${ Math . min ( 3 , heading [ 1 ]. length )} ` }>{ marks ( stripLead ( line , heading [ 0 ]. length ))}</ p >; 23const item = / ^(\s*)([-*+]|\d+[.)])\s+ / . exec ( plain ); 24if ( item ) return < p key = { i } className = "tsc-li" >{ marks ( stripLead ( line , item [ 0 ]. length ))}</ p >; 25return < p key = { i }>{ plain ?marks ( line ) :< br />}</ p >; 26})} 27</ div >; 28} 29 30/** Drop the first `count` characters (the Markdown marker) from the front of a line of segments. */ 31function stripLead ( line :Segment [], count :number ) :Segment [] { 32const out :Segment [] = []; let left = count ; 33for ( const seg of line ) { 34if ( left >= seg . text . length ) { left -= seg . text . length ; continue ; } 35out . push ({ text :seg . text . slice ( left ), changed :seg . changed }); left = 0 ; 36} 37return out ; 38} 39 40function DiffView ({ original, updated} :{ original :string ; updated :string }) { 41const rows = useMemo (() => lineDiff ( original , updated ), [ original , updated ]); 42const stats = diffStats ( rows ); 43return < div className = "tsc-diff" role = "table" aria-label = { tr ( "Changes" )}> 44< div className = "tsc-diff__stats" >< span className = "tsc-add" > + { stats . added }</ span >< span className = "tsc-del" > − { stats . removed }</ span ></ div > 45{ rows . map (( r , i ) => r . t === 'gap' 46 ?< div className = "tsc-diff__gap" role = "row" key = { i }>{ tr ( '{0} unchanged lines' , [ r . n ])}</ div > 47 :< div className = { `tsc-diff__row tsc-diff__row-- ${ r . t } ` } role = "row" key = { i }>< span className = "tsc-diff__no" aria-hidden = "true" >{ r . a ?? '' }</ span >< span className = "tsc-diff__no" aria-hidden = "true" >{ r . b ?? '' }</ span >< span className = "tsc-diff__sign" aria-hidden = "true" >{ r . t === 'add' ?'+' :r . t === 'del' ?'−' :'' }</ span >< code >{ r . text || ' ' }</ code ></ div >)} 48</ div >; 49} 50 51function Picture ({ src, label, missing, onOpen} :{ src :string ; label :string ; missing ?:string ; onOpen :() => void }) { 52const [ failed , setFailed ] = useState ( false ); 53useEffect (() => { setFailed ( false ); }, [ src ]); 54if ( missing || failed ) return < div className = "tsc-picture tsc-picture--empty" >< span >{ missing ?? tr ( "This picture could not be loaded." )}</ span ></ div >; 55return < button type = "button" className = "tsc-picture" onClick = { onOpen } aria-label = { tr ( "Enlarge the {0} picture" , [ label ])}>< img src = { src } alt = { label } onError = {() => setFailed ( true )}/></ button >; 56} 57 58function FileView ({ jobId, file} :{ jobId :string ; file :TimeSkipFile }) { 59const kind = fileKind ( file . path , file ); 60const [ code , setCode ] = useState ( false ); 61const [ big , setBig ] = useState < 'original' | 'updated' | null >( null ); 62useEffect (() => { setCode ( false ); setBig ( null ); }, [ file . path ]); 63const original = file . original ?? '' , updated = file . updated ?? '' ; 64const words = useMemo (() => ( kind === 'markdown' || kind === 'text' ) ?wordDiff ( original , updated ) :null , [ kind , original , updated ]); 65const heads = < div className = "tsc-heads" >< span >< b > A </ b > · { tr ( "Original" )}</ span >< span >< b > B </ b > · { tr ( "Updated" )}</ span ></ div >; 66if ( kind === 'other' ) return < p className = "tsc-note" >{ tr ( "This file is too big, or not text, so it is listed but not shown." )}</ p >; 67if ( kind === 'image' ) { 68const a = timeSkipImageUrl ( jobId , file . path , 'original' ), b = timeSkipImageUrl ( jobId , file . path , 'updated' ); 69return <>{ heads }< div className = "tsc-pair tsc-pair--pictures" > 70< Picture src = { a } label = { tr ( "Original" )} missing = { file . status === 'added' ?tr ( "Not there before." ) :undefined } onOpen = {() => setBig ( 'original' )}/> 71< Picture src = { b } label = { tr ( "Updated" )} missing = { file . status === 'removed' ?tr ( "Removed." ) :undefined } onOpen = {() => setBig ( 'updated' )}/> 72</ div > 73< Dialog open = { !! big } title = { ` ${ baseName ( file . path )} · ${ big === 'original' ? tr ( 'Original' ) : tr ( 'Updated' )} ` } onClose = {() => setBig ( null )} sheet > 74{ big ?< img className = "tsc-enlarged" src = { big === 'original' ?a :b } alt = { baseName ( file . path )}/> :null } 75</ Dialog ></>; 76} 77if ( kind === 'html' ) return <> 78< div className = "tsc-tools" >< Btn size = "sm" variant = "ghost" icon = { code ?'image' :'file' } aria-pressed = { code } onClick = {() => setCode ( v => ! v )}>{ tr ( code ?'View preview' :'View code' )}</ Btn ></ div > 79{ code ?< DiffView original = { original } updated = { updated }/> :<>{ heads }< div className = "tsc-pair tsc-pair--html" > 80{ /* The page may run its own scripts in an isolated frame; the policy inside blocks every outside request. */ } 81{ file . status === 'added' ?< div className = "tsc-picture tsc-picture--empty" >< span >{ tr ( "Not there before." )}</ span ></ div > 82 :< iframe title = { tr ( "Original page" )} sandbox = { HTML_PREVIEW_SANDBOX } srcDoc = { htmlPreviewDocument ( original )} loading = "lazy" />} 83{ file . status === 'removed' ?< div className = "tsc-picture tsc-picture--empty" >< span >{ tr ( "Removed." )}</ span ></ div > 84 :< iframe title = { tr ( "Updated page" )} sandbox = { HTML_PREVIEW_SANDBOX } srcDoc = { htmlPreviewDocument ( updated )} loading = "lazy" />} 85</ div >{ needsOutsideFiles ( updated ) ?< p className = "tsc-note" >{ tr ( "This page asks for files from the internet, which the preview does not load, so it may look incomplete. View code shows exactly what was written." )}</ p > :null }</>} 86</>; 87if ( words ) return <>{ heads }< div className = "tsc-pair tsc-pair--text" > 88< MarkedDoc segments = { words . original } markdown = { kind === 'markdown' }/> 89< MarkedDoc segments = { words . updated } markdown = { kind === 'markdown' }/> 90</ div ></>; 91return < DiffView original = { original } updated = { updated }/>; 92} 93 94/** 95* The finished A versus B, in one window of a fixed, modest size whatever was made: text and Markdown rendered with the changed words marked, 96* a web page as two sandboxed previews (with a code toggle), pictures side by side to enlarge, anything else as a diff, and a compact switcher 97* when several files changed. It scrolls inside itself; it never grows to fill the screen. 98*/ 99export function TimeSkipCompare ({ jobId, files, more= 0 } :{ jobId :string ; files :TimeSkipFile []; more ?:number }) { 100const [ at , setAt ] = useState ( 0 ); 101useEffect (() => { setAt ( 0 ); }, [ jobId ]); 102const file = files [ Math . min ( at , files . length - 1 )]; 103if ( ! file ) return null ; 104return < section className = "tsc" aria-label = { tr ( "Original and updated, side by side" )}> 105{ files . length > 1 ?< div className = "tsc-files" role = "tablist" aria-label = { tr ( "Changed files" )}> 106{ files . map (( f , i ) => < button key = { f . path } type = "button" role = "tab" aria-selected = { i === at } tabIndex = { i === at ?0 :- 1 } title = { f . path } onClick = {() => setAt ( i )} 107onKeyDown = { e => { if ( e . key === 'ArrowRight' || e . key === 'ArrowLeft' ) { e . preventDefault (); setAt (( i + ( e . key === 'ArrowRight' ?1 :files . length - 1 )) % files . length ); } }}> 108< Icon name = { fileKind ( f . path , f ) === 'image' ?'image' :'file' } size = { 13 }/>< span >{ baseName ( f . path )}</ span >< small >{ statusText ( f . status )}</ small ></ button >)} 109{ more > 0 ?< span className = "tsc-more" >{ tr ( '+{0} more' , [ more ])}</ span > :null } 110</ div > :< div className = "tsc-one" >< Icon name = { fileKind ( file . path , file ) === 'image' ?'image' :'file' } size = { 14 }/>< strong title = { file . path }>{ baseName ( file . path )}</ strong >< small >{ statusText ( file . status )}</ small ></ div >} 111< div className = "tsc-stage" >< FileView jobId = { jobId } file = { file }/></ div > 112</ section >; 113}