-
-
Notifications
You must be signed in to change notification settings - Fork 13
Added logo upload feature #81
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
Open
ValdoTR
wants to merge
10
commits into
workadventure:main
Choose a base branch
from
ValdoTR:logo-upload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6c9b63e
WIP: design but no storage yet
3f6c8e3
Final WIP (no endpoint yet to upload)
e7a6437
Switched to local upload endpont
38d6df3
Merge branch 'main' of github.com:ValdoTR/scripting-api-extra into lo…
a23d174
Added service and store
ddff10b
Added WA.player.userRoomToken in the upload-file headers
3fea7f6
Merge branch 'main' of github.com:ValdoTR/scripting-api-extra into lo…
57d6398
Fixed linter errors
2e887e9
Fixed wrong throw error
54bf86f
Added the documentation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** | ||
| * The base URL for the default API. | ||
| * This file is updated dynamically during the package build process. | ||
| */ | ||
| export const defaultApiUrl = "http://workadventure.localhost/api"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { writable } from "svelte/store"; | ||
|
|
||
| interface Form { | ||
| error: string, | ||
| image: HTMLImageElement, | ||
| showImage: boolean, | ||
| formData: FormData, | ||
| } | ||
|
|
||
| const defaultFormState: Form = { | ||
| error: '', | ||
| image: new Image(0, 0), | ||
| showImage: false, | ||
| formData: new FormData() | ||
| } | ||
|
|
||
| function createFormAction() { | ||
| const { subscribe, set, update } = writable<Form>(defaultFormState); | ||
|
|
||
| return { | ||
| subscribe, | ||
| setError: (error: string): void => { | ||
| update((form: Form) => { | ||
| form.error = error | ||
| return form | ||
| }) | ||
| }, | ||
| clearError: (): void => { | ||
| update((form: Form) => { | ||
| form.error = '' | ||
| return form | ||
| }) | ||
| }, | ||
| setImagePreviewMaxWidth: (width: string): void => { | ||
| update((form: Form) => { | ||
| form.image.style.maxWidth = "" + width + "px"; | ||
| return form | ||
| }) | ||
| }, | ||
| setImagePreviewMaxHeight: (height: string): void => { | ||
| update((form: Form) => { | ||
| form.image.style.maxHeight = "" + height + "px"; | ||
| return form | ||
| }) | ||
| }, | ||
| setImageSource: (source: string): void => { | ||
| update((form: Form) => { | ||
| form.image.setAttribute("src", source); | ||
| return form | ||
| }) | ||
| }, | ||
| changeImageVisibility: (mustShowImage: boolean): void => { | ||
| update((form: Form) => { | ||
| form.showImage = mustShowImage | ||
| return form | ||
| }) | ||
| }, | ||
| setFormDataFile: (file: File): void => { | ||
| update((form: Form) => { | ||
| form.formData.append('file', file) | ||
| return form | ||
| }) | ||
| }, | ||
| setFormDataProperty: (propertyName: string, propertyValue: any): void => { | ||
| update((form: Form) => { | ||
| form.formData.set(propertyName, propertyValue) | ||
| return form | ||
| }) | ||
| }, | ||
| clearForm: (): void => { | ||
| set(defaultFormState); | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export const formStore = createFormAction(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /// <reference path="../node_modules/@workadventure/iframe-api-typings/iframe_api.d.ts" /> | ||
|
|
||
| import {defaultApiUrl} from "./Features/default_api_url"; | ||
| import { formStore } from "./Iframes/Configuration/Stores/form" | ||
| import type {VariableDescriptor} from "./VariablesExtra"; | ||
|
|
||
| let formData: FormData | ||
| const unsubscribe = formStore.subscribe(form => { | ||
| formData = form.formData; | ||
| }) | ||
|
|
||
| /** | ||
| * Builds the image preview and the form data | ||
| */ | ||
| export function prepareUpload(event: Event, variable: VariableDescriptor) { | ||
| const files = (event.target as HTMLInputElement).files; | ||
| const file = files ? files[0] : null; | ||
| formStore.clearError() | ||
|
|
||
| if (file) { | ||
| formStore.changeImageVisibility(true) | ||
| const reader = new FileReader(); | ||
| reader.onload = () => { | ||
| if (typeof reader.result === "string") { | ||
| formStore.setFormDataFile(file) | ||
| formStore.setFormDataProperty('identifier', variable.name) | ||
| if (variable.properties.getString('imageWidth')) { | ||
| const width = variable.properties.mustGetString('imageWidth') | ||
| // Just for the rendering, doesn't resize the actual file | ||
| formStore.setImagePreviewMaxWidth(width) | ||
| // This will resize the file, on server side | ||
| formStore.setFormDataProperty('imageWidth', width) | ||
| } | ||
| if (variable.properties.getString('imageHeight')) { | ||
| const height = variable.properties.mustGetString('imageHeight') | ||
| // Just for the rendering, doesn't resize the actual file | ||
| formStore.setImagePreviewMaxHeight(height) | ||
| // This will resize the file, on server side | ||
| formStore.setFormDataProperty('imageHeight', height) | ||
| } | ||
| formStore.setImageSource(reader.result) | ||
| } | ||
| }; | ||
| reader.readAsDataURL(file); | ||
| return; | ||
| } | ||
| formStore.changeImageVisibility(false) | ||
| } | ||
|
|
||
| /** | ||
| * Performs the upload and update the url variable | ||
| */ | ||
| export async function uploadFile(): Promise<string> { | ||
| const token = WA.player.userRoomToken | ||
| formStore.clearError() | ||
| //!\ if you are self-hosting WA, this API must be added by your side | ||
| const response = await fetch(defaultApiUrl + '/upload-file', { | ||
| method: 'POST', | ||
| body: formData, | ||
| headers: { | ||
| 'Authorization': `Bearer ${token}`, | ||
| }, | ||
| }).catch(e => { | ||
| formStore.setError('Upload error. Please contact the support.') | ||
| throw new Error(e) | ||
| }) | ||
|
|
||
| unsubscribe() | ||
| const data = await response.json() | ||
| return data.url | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.