Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asset support to :play #19

Merged
merged 16 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
380 changes: 233 additions & 147 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
"@codemirror/lint": "6.5.0",
"@codemirror/state": "6.4.1",
"@codemirror/view": "6.26.3",
"@devvit/previews": "0.10.20",
"@devvit/protos": "0.10.20",
"@devvit/public-api": "0.10.20",
"@devvit/runtime-lite": "0.10.20",
"@devvit/shared-types": "0.10.20",
"@devvit/ui-renderer": "0.10.20",
"@devvit/previews": "0.10.21-next-2024-05-13-b48ce196f.0",
"@devvit/protos": "0.10.21-next-2024-05-13-b48ce196f.0",
"@devvit/public-api": "0.10.21-next-2024-05-13-b48ce196f.0",
"@devvit/runtime-lite": "0.10.21-next-2024-05-13-b48ce196f.0",
"@devvit/shared-types": "0.10.21-next-2024-05-13-b48ce196f.0",
"@devvit/ui-renderer": "0.10.21-next-2024-05-13-b48ce196f.0",
"@esm-bundle/chai": "4.3.4-fix.0",
"@types/jsdom": "21.1.6",
"@types/mocha": "10.0.6",
"@typescript/vfs": "1.5.0",
"@web/dev-server-esbuild": "1.0.2",
"@web/test-runner": "0.18.1",
"@zenfs/core": "0.9.7",
"@zenfs/dom": "0.2.6",
"@zenfs/zip": "0.3.1",
"codemirror": "6.0.1",
"esbuild": "0.20.2",
"idb-keyval": "6.2.1",
"jsdom": "24.0.0",
"lit": "3.1.3",
"lit-analyzer": "2.0.3",
Expand Down Expand Up @@ -56,8 +60,8 @@
"gzip": "3.5 KB"
},
"dist/play-pen.js": {
"none": "35000 KB",
"gzip": "5200 KB"
"none": "37000 KB",
"gzip": "5400 KB"
}
},
"typesVersions": {
Expand Down
10 changes: 8 additions & 2 deletions src/bundler/linker.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import type {LinkedBundle, SerializableServiceDefinition} from '@devvit/protos'
import type {AssetMap} from '@devvit/shared-types/Assets.js'

/**
* @arg es JavaScript
* @arg hostname Arbitrary but something unique to the window like
* hello-world.local may allow concurrent sessions with the
* remote.
* @arg assets AssetMap describing how to map project assets to URLs
*/
export function link(es: string, hostname: string): LinkedBundle {
export function link(
es: string,
hostname: string,
assets?: AssetMap
ObsidianSnoo marked this conversation as resolved.
Show resolved Hide resolved
): LinkedBundle {
return {
actor: {name: 'pen', owner: 'play', version: '0.0.0.0'},
assets: {},
assets: assets ?? {},
code: es,
hostname,
provides: provides(),
Expand Down
7 changes: 7 additions & 0 deletions src/elements/play-assets-dialog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {assert} from '@esm-bundle/chai'
import {PlayAssetsDialog} from './play-assets-dialog'

test('tag is defined', () => {
const el = document.createElement('play-assets-dialog')
assert.instanceOf(el, PlayAssetsDialog)
})
145 changes: 145 additions & 0 deletions src/elements/play-assets-dialog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import {customElement, property, query, state} from 'lit/decorators.js'
import {
css,
type CSSResultGroup,
html,
LitElement,
type TemplateResult
} from 'lit'
import {PlayDialog} from './play-dialog/play-dialog.js'
import {choose} from 'lit-html/directives/choose.js'
import {when} from 'lit-html/directives/when.js'
import {
type AssetFilesystemType,
PlayAssets
} from './play-assets/play-assets.js'

import './play-assets/play-assets.js'
import './play-assets/play-assets-virtual-fs.js'
import './play-assets/play-assets-local-directory.js'
import './play-assets/play-assets-local-archive.js'
import './play-dialog/play-dialog.js'

declare global {
interface HTMLElementEventMap {}
interface HTMLElementTagNameMap {
'play-assets-dialog': PlayAssetsDialog
}
}

@customElement('play-assets-dialog')
export class PlayAssetsDialog extends LitElement {
static override readonly styles: CSSResultGroup = css`
${PlayDialog.styles}

fieldset {
margin-bottom: var(--space);
}

#localFs {
display: flex;
flex-direction: column;
gap: 8px;
}
`

@property({attribute: 'enable-local-assets', type: Boolean})
enableLocalAssets: boolean = false

@state()
private _filesystemType: AssetFilesystemType = 'virtual'

@query('play-assets')
private _assets: PlayAssets | undefined

@query('play-dialog', true)
private _dialog!: PlayDialog

open(): void {
this._dialog.open()
}

close(): void {
this._dialog.close()
}

protected override render(): TemplateResult {
return html`
<play-assets @assets-updated=${this.#assetsUpdated}></play-assets>
<play-dialog
title="Assets"
description="Manage static assets available to blocks in :play"
>
${when(this.enableLocalAssets, this.#renderFilesystemPicker)}

<fieldset>
<legend>${this.#filesystemTitle}:</legend>
${choose(this._filesystemType, [
[
'virtual',
() => html`<play-assets-virtual-fs></play-assets-virtual-fs>`
],
['local', this.#renderLocalFs]
])}
</fieldset>
</play-dialog>
`
}

#renderFilesystemPicker = (): TemplateResult => {
return html`<fieldset>
<legend>Filesystem type:</legend>
<label>
<input
name="filesystemType"
type="radio"
?checked="${this._filesystemType === 'virtual'}"
@change=${this.#setFilesystem}
value="virtual"
/>
Virtual
</label>
<label>
<input
name="filesystemType"
type="radio"
?checked="${this._filesystemType === 'local'}"
@change=${this.#setFilesystem}
value="local"
/>
Local
</label>
</fieldset>`
}

#setFilesystem = (ev: InputEvent & {currentTarget: HTMLInputElement}) => {
if (this._assets) {
this._assets.filesystemType = ev.currentTarget
niedzielski marked this conversation as resolved.
Show resolved Hide resolved
.value as AssetFilesystemType
this.requestUpdate()
}
}

#renderLocalFs = () => {
return html`
<div id="localFs">
${when(
PlayAssets.hasFileAccessAPI,
() =>
html`<play-assets-local-directory></play-assets-local-directory>`
)}
<play-assets-local-archive></play-assets-local-archive>
</div>
`
}

#assetsUpdated = () => {
this._filesystemType = this._assets?.filesystemType ?? 'virtual'
}

get #filesystemTitle(): string {
return this._filesystemType === 'virtual'
? 'Manage files'
: 'Filesystem source'
}
}
7 changes: 7 additions & 0 deletions src/elements/play-assets/file-upload-dropper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {assert} from '@esm-bundle/chai'
import {FileUploadDropper} from './file-upload-dropper'

test('tag is defined', () => {
const el = document.createElement('file-upload-dropper')
assert.instanceOf(el, FileUploadDropper)
})
Loading