import Bind from "@web-atoms/core/dist/core/Bind" import XNode from "@web-atoms/core/dist/core/XNode" import { BaseService, Get, Query } from "@web-atoms/core/dist/services/http/RestService"; import { AtomControl } from "@web-atoms/core/dist/web/controls/AtomControl"; import styled from "@web-atoms/core/dist/style/styled"; import InjectProperty from "@web-atoms/core/dist/core/InjectProperty"; import "@web-atoms/data-styles/data-styles"; import { CancelToken } from "@web-atoms/core/dist/core/types"; import Action from "@web-atoms/core/dist/view-model/Action"; import DISingleton from "@web-atoms/core/dist/di/DISingleton"; import AtomRepeater from "../basic/AtomRepeater"; import ToggleButtonBar from "../basic/ToggleButtonBar"; import "./Devhost.global.css"; const css = "dev-host-local"; const fileTypes = () => [ { label: "Packed", value: "packed"}, { label: "All", value: "all"} ]; const designModes = () => [ { label: "Design", value: true }, { label: "Live", value: false } ]; const replaceSrc = (src: string): string => { src = src.split("\\").join("/"); const tokens = src.split("/"); if (tokens[0] === "src") { tokens[0] = "dist"; } return tokens.join("/"); } const toAbsoluteUrl = (file: IFilePath, designMode?: boolean) => { let url = `/_view/${replaceSrc(file.dir)}/${file.name}`; if (designMode) { if (url.indexOf("?") === -1) { url += "?"; } url += "&designMode=true"; } return `${location.protocol}//${location.host}${url}`; }; @DISingleton({}) export class FileService extends BaseService { public async getModules(search: string, packed: boolean, cancelToken: CancelToken) { const result = await this.getRemoteModules(search, packed, cancelToken); return result.files; } @Get("/flat-modules") private getRemoteModules( @Query("search") search: string, @Query("packed") packed: boolean, ct?: CancelToken): Promise { return null; } } export default class AppHost extends AtomControl { public search: string = ""; public designMode = false; public fileType = "packed"; @InjectProperty private fileService: FileService; public async init() { this.render(
this.search)} placeholder="Search..."> Mode this.designMode)} /> this.fileType)} />
this.getModules(this.search, this.fileType === "packed", cancelToken))} for="table" itemRenderer={(item: IFilePath) =>
toAbsoluteUrl(x.source, this.designMode))} target="_blank" text="Open"> this.copyUrl(toAbsoluteUrl(item, this.designMode))}> } />
); } async getModules(search: string, packed: boolean, cancelToken: CancelToken) { const result = await this.fileService.getModules(search, packed, cancelToken); if (!result.length && packed) { this.fileType = "all"; } return result; } protected preCreate(): void { super.preCreate(); (this.app).installStyleSheet({ href: "https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.4.0/css/all.min.css", integrity: "sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=", crossOrigin: "anonymous" }); this.runAfterInit(() => this.app.runAsync(() => this.init())); } @Action({ success: "Copied successfully"}) protected async copyUrl(url: string) { await navigator.clipboard.writeText(url); } } export interface IFilePath { name: string; base: string; dir: string; ext: string; root: string; url: string; hostUrl?: string; package?: string; visible: boolean; packed?: boolean; module?: string; } export interface IFilePathResult { files: IFilePath[]; }