BasePlugin.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. /**
  3. * Core plugin logic that all plugins share.
  4. *
  5. * BasePlugin does not contain DOM rendering so it can be used for plugins
  6. * without a user interface.
  7. *
  8. * See `Plugin` for the extended version with Preact rendering for interfaces.
  9. */
  10. const Translator = require("@uppy/utils/lib/Translator");
  11. class BasePlugin {
  12. constructor(uppy, opts) {
  13. if (opts === void 0) {
  14. opts = {};
  15. }
  16. this.uppy = uppy;
  17. this.opts = opts;
  18. }
  19. getPluginState() {
  20. const {
  21. plugins
  22. } = this.uppy.getState();
  23. return plugins[this.id] || {};
  24. }
  25. setPluginState(update) {
  26. const {
  27. plugins
  28. } = this.uppy.getState();
  29. this.uppy.setState({
  30. plugins: { ...plugins,
  31. [this.id]: { ...plugins[this.id],
  32. ...update
  33. }
  34. }
  35. });
  36. }
  37. setOptions(newOpts) {
  38. this.opts = { ...this.opts,
  39. ...newOpts
  40. };
  41. this.setPluginState(); // so that UI re-renders with new options
  42. this.i18nInit();
  43. }
  44. i18nInit() {
  45. const translator = new Translator([this.defaultLocale, this.uppy.locale, this.opts.locale]);
  46. this.i18n = translator.translate.bind(translator);
  47. this.i18nArray = translator.translateArray.bind(translator);
  48. this.setPluginState(); // so that UI re-renders and we see the updated locale
  49. }
  50. /**
  51. * Extendable methods
  52. * ==================
  53. * These methods are here to serve as an overview of the extendable methods as well as
  54. * making them not conditional in use, such as `if (this.afterUpdate)`.
  55. */
  56. // eslint-disable-next-line class-methods-use-this
  57. addTarget() {
  58. throw new Error('Extend the addTarget method to add your plugin to another plugin\'s target');
  59. } // eslint-disable-next-line class-methods-use-this
  60. install() {} // eslint-disable-next-line class-methods-use-this
  61. uninstall() {}
  62. /**
  63. * Called when plugin is mounted, whether in DOM or into another plugin.
  64. * Needed because sometimes plugins are mounted separately/after `install`,
  65. * so this.el and this.parent might not be available in `install`.
  66. * This is the case with @uppy/react plugins, for example.
  67. */
  68. render() {
  69. throw new Error('Extend the render method to add your plugin to a DOM element');
  70. } // TODO: remove in the next major version. It's not feasible to
  71. // try to use plugins with other frameworks.
  72. // eslint-disable-next-line class-methods-use-this
  73. update() {} // Called after every state update, after everything's mounted. Debounced.
  74. // eslint-disable-next-line class-methods-use-this
  75. afterUpdate() {}
  76. }
  77. module.exports = BasePlugin;