11import '#css/view.css' ;
2+ import { rendy } from 'rendy' ;
23import currify from 'currify' ;
34import wraptile from 'wraptile' ;
5+ import { tryToCatch } from 'try-to-catch' ;
46import load from 'load.js' ;
57import * as _modal from '@cloudcmd/modal' ;
68import _createElement from '@cloudcmd/create-element' ;
79import { time } from '#common/util' ;
10+ import * as Files from '#dom/files' ;
811import * as Events from '#dom/events' ;
12+ import { FS } from '#common/cloudfunc' ;
913import * as Images from '#dom/images' ;
14+ import { encode } from '#common/entity' ;
15+ import {
16+ isImage ,
17+ isAudio ,
18+ getType ,
19+ } from './types.js' ;
1020
1121const CloudCmd = globalThis . CloudCmd || { } ;
1222const DOM = globalThis . DOM || { } ;
13-
23+ const isString = ( a ) => typeof a === 'string' ;
1424const { assign} = Object ;
1525const { isArray} = Array ;
1626
@@ -40,8 +50,12 @@ CloudCmd[Name] = {
4050const Info = DOM . CurrentInfo ;
4151const { Key} = CloudCmd ;
4252
43- let El ;
53+ const basename = ( a ) => a
54+ . split ( '/' )
55+ . pop ( ) ;
4456
57+ let El ;
58+ let TemplateAudio ;
4559let Overlay ;
4660
4761const Config = {
@@ -85,6 +99,8 @@ export async function init() {
8599}
86100
87101export async function show ( data , options = { } ) {
102+ const prefixURL = CloudCmd . prefixURL + FS ;
103+
88104 if ( Loading )
89105 return ;
90106
@@ -110,7 +126,28 @@ export async function show(data, options = {}) {
110126
111127 Images . show . load ( ) ;
112128
113- return await viewFile ( ) ;
129+ const path = prefixURL + Info . path ;
130+ const type = options . raw ? '' : await getType ( path ) ;
131+
132+ switch ( type ) {
133+ default :
134+ return await viewFile ( ) ;
135+
136+ case 'markdown' :
137+ return await CloudCmd . Markdown . show ( Info . path ) ;
138+
139+ case 'html' :
140+ return viewHtml ( path ) ;
141+
142+ case 'image' :
143+ return viewImage ( Info . path , prefixURL ) ;
144+
145+ case 'media' :
146+ return await viewMedia ( path ) ;
147+
148+ case 'pdf' :
149+ return viewPDF ( path ) ;
150+ }
114151}
115152
116153export const _createIframe = createIframe ;
@@ -140,6 +177,38 @@ function viewHtml(src, overrides = {}) {
140177 modal . open ( createIframe ( src ) , Config ) ;
141178}
142179
180+ function viewPDF ( src ) {
181+ const element = createIframe ( src ) ;
182+
183+ const options = assign ( { } , Config ) ;
184+
185+ if ( CloudCmd . config ( 'showFileName' ) )
186+ options . title = Info . name ;
187+
188+ _modal . open ( element , options ) ;
189+ }
190+
191+ async function viewMedia ( path ) {
192+ const [ e , element ] = await getMediaElement ( path ) ;
193+
194+ if ( e )
195+ return alert ( e ) ;
196+
197+ const allConfig = {
198+ ...Config ,
199+ ...{
200+ autoSize : true ,
201+ afterShow : ( ) => {
202+ element
203+ . querySelector ( 'audio, video' )
204+ . focus ( ) ;
205+ } ,
206+ } ,
207+ } ;
208+
209+ _modal . open ( element , allConfig ) ;
210+ }
211+
143212async function viewFile ( ) {
144213 const [ error , data ] = await Info . getData ( ) ;
145214
@@ -189,6 +258,73 @@ export function hide() {
189258 _modal . close ( ) ;
190259}
191260
261+ function viewImage ( path , prefixURL ) {
262+ const isSupportedImage = ( a ) => isImage ( a ) || a === path ;
263+ const makeTitle = ( path ) => ( {
264+ href : `${ prefixURL } ${ path } ` ,
265+ title : encode ( basename ( path ) ) ,
266+ } ) ;
267+
268+ const names = Info . files
269+ . map ( DOM . getCurrentPath )
270+ . filter ( isSupportedImage ) ;
271+
272+ const titles = names . map ( makeTitle ) ;
273+
274+ const index = names . indexOf ( Info . path ) ;
275+
276+ const imageConfig = {
277+ index,
278+ autoSize : true ,
279+ arrows : true ,
280+ keys : true ,
281+ helpers : {
282+ title : { } ,
283+ } ,
284+ } ;
285+
286+ const config = {
287+ ...Config ,
288+ ...imageConfig ,
289+ } ;
290+
291+ _modal . open ( titles , config ) ;
292+ }
293+
294+ async function getMediaElement ( src ) {
295+ check ( src ) ;
296+
297+ const [ error , template ] = await tryToCatch ( Files . get , 'view/media-tmpl' ) ;
298+
299+ if ( error )
300+ return [ error ] ;
301+
302+ const { name} = Info ;
303+
304+ if ( ! TemplateAudio )
305+ TemplateAudio = template ;
306+
307+ const is = isAudio ( name ) ;
308+ const type = is ? 'audio' : 'video' ;
309+
310+ const innerHTML = rendy ( TemplateAudio , {
311+ src,
312+ type,
313+ name,
314+ } ) ;
315+
316+ const element = _createElement ( 'div' , {
317+ innerHTML,
318+ } ) ;
319+
320+ return [ null , element ] ;
321+ }
322+
323+ function check ( src ) {
324+ if ( ! isString ( src ) )
325+ throw Error ( 'src should be a string!' ) ;
326+ }
327+
192328async function loadAll ( ) {
193329 const { DIR_DIST } = CloudCmd ;
194330
0 commit comments