UNPKG

@uppy/core

Version:

Core module for the extensible JavaScript file upload widget with support for drag&drop, resumable uploads, previews, restrictions, file processing/encoding, remote providers like Instagram, Dropbox, Google Drive, S3 and more :dog:

77 lines (69 loc) 2.12 kB
/* eslint-disable class-methods-use-this */ /** * 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'; /** * DefinePluginOpts marks all of the passed AlwaysDefinedKeys as “required” or “always defined”. */ export default class BasePlugin { constructor(uppy, opts) { this.uppy = uppy; this.opts = opts != null ? opts : {}; } getPluginState() { const { plugins } = this.uppy.getState(); return (plugins == null ? void 0 : plugins[this.id]) || {}; } setPluginState(update) { const { plugins } = this.uppy.getState(); this.uppy.setState({ plugins: { ...plugins, [this.id]: { ...plugins[this.id], ...update } } }); } setOptions(newOpts) { this.opts = { ...this.opts, ...newOpts }; this.setPluginState(undefined); // so that UI re-renders with new options this.i18nInit(); } i18nInit() { 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) { throw new Error("Extend the addTarget method to add your plugin to another plugin's target"); } install() {} uninstall() {} // eslint-disable-next-line @typescript-eslint/no-unused-vars update(state) {} // Called after every state update, after everything's mounted. Debounced. afterUpdate() {} }