-
Notifications
You must be signed in to change notification settings - Fork 2k
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
@uppy/core: migrate to TS #4811
Merged
Changes from all commits
Commits
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 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 was deleted.
Oops, something went wrong.
This file contains 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,106 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
|
||
/** | ||
* Core plugin logic that all plugins share. | ||
* | ||
* BasePlugin does not contain DOM rendering so it can be used for plugins | ||
* without a user interface. | ||
* | ||
* See `Plugin` for the extended version with Preact rendering for interfaces. | ||
*/ | ||
|
||
import Translator from '@uppy/utils/lib/Translator' | ||
import type { I18n, Locale } from '@uppy/utils/lib/Translator' | ||
import type { Body, Meta } from '@uppy/utils/lib/UppyFile' | ||
import type { Uppy } from '.' | ||
|
||
export type PluginOpts = { locale?: Locale; [key: string]: unknown } | ||
|
||
export default class BasePlugin< | ||
Opts extends PluginOpts, | ||
M extends Meta, | ||
B extends Body, | ||
> { | ||
uppy: Uppy<M, B> | ||
|
||
opts: Opts | ||
|
||
id: string | ||
|
||
defaultLocale: Locale | ||
|
||
i18n: I18n | ||
|
||
i18nArray: Translator['translateArray'] | ||
|
||
type: string | ||
|
||
VERSION: string | ||
|
||
constructor(uppy: Uppy<M, B>, opts: Opts) { | ||
this.uppy = uppy | ||
this.opts = opts ?? {} | ||
} | ||
|
||
getPluginState(): Record<string, unknown> { | ||
const { plugins } = this.uppy.getState() | ||
return plugins?.[this.id] || {} | ||
} | ||
|
||
setPluginState(update: unknown): void { | ||
if (!update) return | ||
const { plugins } = this.uppy.getState() | ||
|
||
this.uppy.setState({ | ||
plugins: { | ||
...plugins, | ||
[this.id]: { | ||
...plugins[this.id], | ||
...update, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
setOptions(newOpts: Partial<Opts>): void { | ||
this.opts = { ...this.opts, ...newOpts } | ||
this.setPluginState(undefined) // so that UI re-renders with new options | ||
this.i18nInit() | ||
} | ||
|
||
i18nInit(): void { | ||
const translator = new Translator([ | ||
this.defaultLocale, | ||
this.uppy.locale, | ||
this.opts.locale, | ||
]) | ||
this.i18n = translator.translate.bind(translator) | ||
this.i18nArray = translator.translateArray.bind(translator) | ||
this.setPluginState(undefined) // so that UI re-renders and we see the updated locale | ||
} | ||
|
||
/** | ||
* Extendable methods | ||
* ================== | ||
* These methods are here to serve as an overview of the extendable methods as well as | ||
* making them not conditional in use, such as `if (this.afterUpdate)`. | ||
*/ | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
addTarget(plugin: unknown): HTMLElement { | ||
throw new Error( | ||
"Extend the addTarget method to add your plugin to another plugin's target", | ||
) | ||
} | ||
|
||
install(): void {} | ||
|
||
uninstall(): void {} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
update(state: any): void {} | ||
|
||
// Called after every state update, after everything's mounted. Debounced. | ||
afterUpdate(): void {} | ||
} |
This file contains 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,114 @@ | ||
import type { Meta, Body, UppyFile } from '@uppy/utils/lib/UppyFile' | ||
import type { | ||
DeprecatedUppyEventMap, | ||
Uppy, | ||
UppyEventMap, | ||
_UppyEventMap, | ||
} from './Uppy' | ||
|
||
/** | ||
* Create a wrapper around an event emitter with a `remove` method to remove | ||
* all events that were added using the wrapped emitter. | ||
*/ | ||
export default class EventManager<M extends Meta, B extends Body> { | ||
#uppy: Uppy<M, B> | ||
|
||
#events: Array<[keyof UppyEventMap<M, B>, (...args: any[]) => void]> = [] | ||
|
||
constructor(uppy: Uppy<M, B>) { | ||
this.#uppy = uppy | ||
} | ||
|
||
on<K extends keyof _UppyEventMap<M, B>>( | ||
event: K, | ||
fn: _UppyEventMap<M, B>[K], | ||
): Uppy<M, B> | ||
|
||
/** @deprecated */ | ||
on<K extends keyof DeprecatedUppyEventMap<M, B>>( | ||
event: K, | ||
fn: DeprecatedUppyEventMap<M, B>[K], | ||
): Uppy<M, B> | ||
|
||
on<K extends keyof UppyEventMap<M, B>>( | ||
event: K, | ||
fn: UppyEventMap<M, B>[K], | ||
): Uppy<M, B> { | ||
this.#events.push([event, fn]) | ||
return this.#uppy.on(event as keyof _UppyEventMap<M, B>, fn) | ||
} | ||
|
||
remove(): void { | ||
for (const [event, fn] of this.#events.splice(0)) { | ||
this.#uppy.off(event, fn) | ||
} | ||
} | ||
|
||
onFilePause( | ||
fileID: UppyFile<M, B>['id'], | ||
cb: (isPaused: boolean) => void, | ||
): void { | ||
this.on('upload-pause', (targetFileID, isPaused) => { | ||
if (fileID === targetFileID) { | ||
cb(isPaused) | ||
} | ||
}) | ||
} | ||
|
||
onFileRemove( | ||
fileID: UppyFile<M, B>['id'], | ||
cb: (isPaused: UppyFile<M, B>['id']) => void, | ||
): void { | ||
this.on('file-removed', (file) => { | ||
if (fileID === file.id) cb(file.id) | ||
}) | ||
} | ||
|
||
onPause(fileID: UppyFile<M, B>['id'], cb: (isPaused: boolean) => void): void { | ||
this.on('upload-pause', (targetFileID, isPaused) => { | ||
if (fileID === targetFileID) { | ||
// const isPaused = this.#uppy.pauseResume(fileID) | ||
cb(isPaused) | ||
} | ||
}) | ||
} | ||
|
||
onRetry(fileID: UppyFile<M, B>['id'], cb: () => void): void { | ||
this.on('upload-retry', (targetFileID) => { | ||
if (fileID === targetFileID) { | ||
cb() | ||
} | ||
}) | ||
} | ||
|
||
onRetryAll(fileID: UppyFile<M, B>['id'], cb: () => void): void { | ||
this.on('retry-all', () => { | ||
if (!this.#uppy.getFile(fileID)) return | ||
cb() | ||
}) | ||
} | ||
|
||
onPauseAll(fileID: UppyFile<M, B>['id'], cb: () => void): void { | ||
this.on('pause-all', () => { | ||
if (!this.#uppy.getFile(fileID)) return | ||
cb() | ||
}) | ||
} | ||
|
||
onCancelAll( | ||
fileID: UppyFile<M, B>['id'], | ||
eventHandler: UppyEventMap<M, B>['cancel-all'], | ||
): void { | ||
this.on('cancel-all', (...args) => { | ||
if (!this.#uppy.getFile(fileID)) return | ||
eventHandler(...args) | ||
}) | ||
} | ||
|
||
onResumeAll(fileID: UppyFile<M, B>['id'], cb: () => void): void { | ||
this.on('resume-all', () => { | ||
if (!this.#uppy.getFile(fileID)) return | ||
cb() | ||
}) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why three
on
s?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One is deprecated