Skip to content

Commit

Permalink
Extract BrowserDialog into an interface so that it can be referenced …
Browse files Browse the repository at this point in the history
…without pulling in the implementation (as that was resulting in browser-only side effects being emitted in Node-only tools)
  • Loading branch information
curiousdannii committed Sep 30, 2024
1 parent 89d9ade commit 079ab4b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/dialog/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ https://github.com/curiousdannii/asyncglk

import path from 'path-browserify-esm'

Check failure on line 12 in src/dialog/browser/browser.ts

View workflow job for this annotation

GitHub Actions / test (Node v18)

Cannot find module 'path-browserify-esm' or its corresponding type declarations.

Check failure on line 12 in src/dialog/browser/browser.ts

View workflow job for this annotation

GitHub Actions / test (Node v20)

Cannot find module 'path-browserify-esm' or its corresponding type declarations.

Check failure on line 12 in src/dialog/browser/browser.ts

View workflow job for this annotation

GitHub Actions / test (Node v22)

Cannot find module 'path-browserify-esm' or its corresponding type declarations.

import type {AsyncDialog, DialogDirectories, DialogOptions} from '../common/interface.js'
import {type DownloadOptions, DownloadProvider, type ProgressCallback} from './download.js'
import type {Provider} from './interface.js'
import type {DialogDirectories, DialogOptions} from '../common/interface.js'
import {DownloadProvider} from './download.js'
import type {BrowserDialog, DownloadOptions, ProgressCallback, Provider} from './interface.js'
import {WebStorageProvider} from './storage.js'
import DialogUI from './ui/FileDialog.svelte'

Check failure on line 18 in src/dialog/browser/browser.ts

View workflow job for this annotation

GitHub Actions / test (Node v18)

Cannot find module './ui/FileDialog.svelte' or its corresponding type declarations.

Check failure on line 18 in src/dialog/browser/browser.ts

View workflow job for this annotation

GitHub Actions / test (Node v20)

Cannot find module './ui/FileDialog.svelte' or its corresponding type declarations.

Check failure on line 18 in src/dialog/browser/browser.ts

View workflow job for this annotation

GitHub Actions / test (Node v22)

Cannot find module './ui/FileDialog.svelte' or its corresponding type declarations.

export class BrowserDialog implements AsyncDialog {
export class ProviderBasedBrowserDialog implements BrowserDialog {
'async' = true as const
private dialog: DialogUI | undefined
private dirs: DialogDirectories = {
Expand Down
13 changes: 1 addition & 12 deletions src/dialog/browser/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,9 @@ https://github.com/curiousdannii/asyncglk
// The download provider stores its own files just in a map (maybe to be cached in the future), but if files are written next to them, then they need to be done so in another provider

import {NullProvider} from './common.js'
import {type DirBrowser, type Provider} from './interface.js'
import type {DirBrowser, DownloadOptions, ProgressCallback, Provider} from './interface.js'
import {utf8decoder} from '../../common/misc.js'

export interface DownloadOptions {
/** Domains to access directly: should always have both Access-Control-Allow-Origin and compression headers */
direct_domains: string[],
/** URL of Proxy */
proxy_url: string,
/** Disable the file proxy, which may mean that some files can't be loaded */
use_proxy?: boolean | number,
}

export type ProgressCallback = (bytes: number) => void

export class DownloadProvider implements Provider {
next = new NullProvider()
private options: DownloadOptions
Expand Down
17 changes: 17 additions & 0 deletions src/dialog/browser/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ https://github.com/curiousdannii/asyncglk
*/

import type {AsyncDialog} from '../common/interface.js'

export type ProgressCallback = (bytes: number) => void

export interface BrowserDialog extends AsyncDialog {
download(url: string, progress_callback?: ProgressCallback): Promise<string>
}

export interface DownloadOptions {
/** Domains to access directly: should always have both Access-Control-Allow-Origin and compression headers */
direct_domains: string[],
/** URL of Proxy */
proxy_url: string,
/** Disable the file proxy, which may mean that some files can't be loaded */
use_proxy?: boolean | number,
}

/** A provider handles part of the filesystem, and can cascade down to another provider for files it doesn't handle */
export interface Provider {
/** A link to the next provider */
Expand Down
15 changes: 7 additions & 8 deletions src/dialog/browser/ui/AlertDialog.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script context="module" lang="ts">
export const enum AlertMode {
ALERT = 0,
CONFIRM = 1,
PROMPT = 2,
}
export const ALERT_MODE_ALERT = 0
export const ALERT_MODE_CONFIRM = 1
export const ALERT_MODE_PROMPT = 2
export type AlertMode = typeof ALERT_MODE_ALERT | typeof ALERT_MODE_CONFIRM | typeof ALERT_MODE_PROMPT
</script>
<script lang="ts">
Expand Down Expand Up @@ -43,7 +42,7 @@
}
function on_submit() {
if (mode === AlertMode.PROMPT) {
if (mode === ALERT_MODE_PROMPT) {
const val = val_input.value.trim()
base_dialog.resolve(val || false)
}
Expand All @@ -63,13 +62,13 @@
<BaseDialog bind:this={base_dialog}>
<p>{message}</p>
<div>
{#if mode === AlertMode.PROMPT}
{#if mode === ALERT_MODE_PROMPT}
<input id="val_input" on:keydown={on_input_keydown} use:on_create_input>
{/if}
</div>
<div class="foot">
<div>
{#if mode !== AlertMode.ALERT}
{#if mode !== ALERT_MODE_ALERT}
<button class="close" on:click={on_cancel}>Cancel</button>
{/if}
<button class="submit" on:click={on_submit}>Ok</button>
Expand Down
6 changes: 3 additions & 3 deletions src/dialog/browser/ui/FileDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</script>

<script lang="ts">
import AlertDialog, {AlertMode} from './AlertDialog.svelte'
import AlertDialog, {ALERT_MODE_CONFIRM, ALERT_MODE_PROMPT} from './AlertDialog.svelte'
import BaseDialog from './BaseDialog.svelte'
import DirTree from './DirTree.svelte'
import FileList from './FileList.svelte'
Expand Down Expand Up @@ -107,7 +107,7 @@
}
async function on_new_folder() {
let new_folder_name = await alert_dialog.open(AlertMode.PROMPT, 'New folder', 'Enter new folder name')
let new_folder_name = await alert_dialog.open(ALERT_MODE_PROMPT, 'New folder', 'Enter new folder name')
if (new_folder_name) {
update_direntry(cur_dir + '/' + new_folder_name)
}
Expand All @@ -118,7 +118,7 @@
const filename = filename_input.value.trim()
for (const entry of cur_direntry) {
if (!entry.dir && filename === entry.name) {
const overwrite = await alert_dialog.open(AlertMode.CONFIRM, 'Overwrite file', `Are you sure you want to overwrite ${filename}?`)
const overwrite = await alert_dialog.open(ALERT_MODE_CONFIRM, 'Overwrite file', `Are you sure you want to overwrite ${filename}?`)
if (!overwrite) {
return
}
Expand Down
5 changes: 2 additions & 3 deletions src/index-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
AsyncGlk exports
================
Copyright (c) 2022 Dannii Willis
Copyright (c) 2024 Dannii Willis
MIT licenced
https://github.com/curiousdannii/asyncglk
*/

export * from './index-common.js'

export {BrowserDialog} from './dialog/browser/browser.js'
export type {DownloadOptions, ProgressCallback} from './dialog/browser/download.js'
export {ProviderBasedBrowserDialog} from './dialog/browser/browser.js'
export {parse_base64, read_response} from './dialog/browser/download.js'

export {default as WebGlkOte} from './glkote/web/web.js'
2 changes: 2 additions & 0 deletions src/index-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export * as protocol from './common/protocol.js'
export {filetype_to_extension, filters_for_usage, path_native_to_posix, path_posix_to_native} from './dialog/common/common.js'
export type {AsyncDialog, AutosaveData, ClassicFileStream, ClassicStreamingDialog, ClassicSyncDialog, Dialog, DialogDirectories, DialogOptions} from './dialog/common/interface.js'

export type {BrowserDialog, DownloadOptions, ProgressCallback} from './dialog/browser/interface.js'

export type {GiDispa, GlkApi, GlkApiAsync, GlkApiOptions, GlkClassName, GlkFref, GlkObject, GlkSchannel, GlkStream, GlkVM, GlkWindow} from './glkapi/interface.js'
export {AsyncGlk, RefBox, RefStruct} from './glkapi/glkapi.js'

Expand Down

0 comments on commit 079ab4b

Please sign in to comment.