UNPKG

10.7 kBSource Map (JSON)View Raw
1{"version":3,"sources":["Plugin.js"],"names":["preact","require","findDOMElement","debounce","fn","calling","latestArgs","args","Promise","resolve","then","module","exports","uppy","opts","update","bind","mount","install","uninstall","getPluginState","getState","plugins","id","setPluginState","setState","setOptions","newOpts","state","el","_updateUI","afterUpdate","onMount","target","plugin","callerPluginName","targetElement","isTargetDOMEl","rerender","getPlugin","render","log","replaceTargetContent","innerHTML","targetPlugin","Plugin","Target","iteratePlugins","parent","addTarget","message","Error","unmount","parentNode","removeChild"],"mappings":";;AAAA,IAAMA,MAAM,GAAGC,OAAO,CAAC,QAAD,CAAtB;;AACA,IAAMC,cAAc,GAAGD,OAAO,CAAC,gCAAD,CAA9B;AAEA;AACA;AACA;;;AACA,SAASE,QAAT,CAAmBC,EAAnB,EAAuB;AACrB,MAAIC,OAAO,GAAG,IAAd;AACA,MAAIC,UAAU,GAAG,IAAjB;AACA,SAAO,YAAa;AAAA,sCAATC,IAAS;AAATA,MAAAA,IAAS;AAAA;;AAClBD,IAAAA,UAAU,GAAGC,IAAb;;AACA,QAAI,CAACF,OAAL,EAAc;AACZA,MAAAA,OAAO,GAAGG,OAAO,CAACC,OAAR,GAAkBC,IAAlB,CAAuB,YAAM;AACrCL,QAAAA,OAAO,GAAG,IAAV,CADqC,CAErC;AACA;AACA;AACA;;AACA,eAAOD,EAAE,MAAF,SAAME,UAAN,CAAP;AACD,OAPS,CAAV;AAQD;;AACD,WAAOD,OAAP;AACD,GAbD;AAcD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACAM,MAAM,CAACC,OAAP;AACE,kBAAaC,IAAb,EAAmBC,IAAnB,EAAyB;AACvB,SAAKD,IAAL,GAAYA,IAAZ;AACA,SAAKC,IAAL,GAAYA,IAAI,IAAI,EAApB;AAEA,SAAKC,MAAL,GAAc,KAAKA,MAAL,CAAYC,IAAZ,CAAiB,IAAjB,CAAd;AACA,SAAKC,KAAL,GAAa,KAAKA,KAAL,C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,GA1JH;;AAAA,SA4JEX,OA5JF,GA4JE,mBAAW,CAEV,CA9JH;;AAAA,SAgKEC,SAhKF,GAgKE,qBAAa;AACX,SAAKiC,OAAL;AACD,GAlKH;;AAAA;AAAA","sourcesContent":["const preact = require('preact')\nconst findDOMElement = require('@uppy/utils/lib/findDOMElement')\n\n/**\n * Defer a frequent call to the microtask queue.\n */\nfunction debounce (fn) {\n let calling = null\n let latestArgs = null\n return (...args) => {\n latestArgs = args\n if (!calling) {\n calling = Promise.resolve().then(() => {\n calling = null\n // At this point `args` may be different from the most\n // recent state, if multiple calls happened since this task\n // was queued. So we use the `latestArgs`, which definitely\n // is the most recent call.\n return fn(...latestArgs)\n })\n }\n return calling\n }\n}\n\n/**\n * Boilerplate that all Plugins share - and should not be used\n * directly. It also shows which methods final plugins should implement/override,\n * this deciding on structure.\n *\n * @param {object} main Uppy core object\n * @param {object} object with plugin options\n * @returns {Array|string} files or success/fail message\n */\nmodule.exports = class Plugin {\n constructor (uppy, opts) {\n this.uppy = uppy\n this.opts = opts || {}\n\n this.update = this.update.bind(this)\n this.mount = this.mount.bind(this)\n this.install = this.install.bind(this)\n this.uninstall = this.uninstall.bind(this)\n }\n\n getPluginState () {\n const { plugins } = this.uppy.getState()\n return plugins[this.id] || {}\n }\n\n setPluginState (update) {\n const { plugins } = this.uppy.getState()\n\n this.uppy.setState({\n plugins: {\n ...plugins,\n [this.id]: {\n ...plugins[this.id],\n ...update,\n },\n },\n })\n }\n\n setOptions (newOpts) {\n this.opts = { ...this.opts, ...newOpts }\n this.setPluginState() // so that UI re-renders with new options\n }\n\n update (state) {\n if (typeof this.el === 'undefined') {\n return\n }\n\n if (this._updateUI) {\n this._updateUI(state)\n }\n }\n\n // Called after every state update, after everything's mounted. Debounced.\n afterUpdate () {\n\n }\n\n /**\n * Called when plugin is mounted, whether in DOM or into another plugin.\n * Needed because sometimes plugins are mounted separately/after `install`,\n * so this.el and this.parent might not be available in `install`.\n * This is the case with @uppy/react plugins, for example.\n */\n onMount () {\n\n }\n\n /**\n * Check if supplied `target` is a DOM element or an `object`.\n * If it’s an object — target is a plugin, and we search `plugins`\n * for a plugin with same name and return its target.\n *\n * @param {string|object} target\n *\n */\n mount (target, plugin) {\n const callerPluginName = plugin.id\n\n const targetElement = findDOMElement(target)\n\n if (targetElement) {\n this.isTargetDOMEl = true\n\n // API for plugins that require a synchronous rerender.\n this.rerender = (state) => {\n // plugin could be removed, but this.rerender is debounced below,\n // so it could still be called even after uppy.removePlugin or uppy.close\n // hence the check\n if (!this.uppy.getPlugin(this.id)) return\n this.el = preact.render(this.render(state), targetElement, this.el)\n this.afterUpdate()\n }\n this._updateUI = debounce(this.rerender)\n\n this.uppy.log(`Installing ${callerPluginName} to a DOM element '${target}'`)\n\n // clear everything inside the target container\n if (this.opts.replaceTargetContent) {\n targetElement.innerHTML = ''\n }\n\n this.el = preact.render(this.render(this.uppy.getState()), targetElement)\n\n this.onMount()\n return this.el\n }\n\n let targetPlugin\n if (typeof target === 'object' && target instanceof Plugin) {\n // Targeting a plugin *instance*\n targetPlugin = target\n } else if (typeof target === 'function') {\n // Targeting a plugin type\n const Target = target\n // Find the target plugin instance.\n this.uppy.iteratePlugins((plugin) => {\n if (plugin instanceof Target) {\n targetPlugin = plugin\n return false\n }\n })\n }\n\n if (targetPlugin) {\n this.uppy.log(`Installing ${callerPluginName} to ${targetPlugin.id}`)\n this.parent = targetPlugin\n this.el = targetPlugin.addTarget(plugin)\n\n this.onMount()\n return this.el\n }\n\n this.uppy.log(`Not installing ${callerPluginName}`)\n\n let message = `Invalid target option given to ${callerPluginName}.`\n if (typeof target === 'function') {\n message += ' The given target is not a Plugin class. '\n + 'Please check that you\\'re not specifying a React Component instead of a plugin. '\n + 'If you are using @uppy/* packages directly, make sure you have only 1 version of @uppy/core installed: '\n + 'run `npm ls @uppy/core` on the command line and verify that all the versions match and are deduped correctly.'\n } else {\n message += 'If you meant to target an HTML element, please make sure that the element exists. '\n + 'Check that the <script> tag initializing Uppy is right before the closing </body> tag at the end of the page. '\n + '(see https://github.com/transloadit/uppy/issues/1042)\\n\\n'\n + 'If you meant to target a plugin, please confirm that your `import` statements or `require` calls are correct.'\n }\n throw new Error(message)\n }\n\n render (state) {\n throw (new Error('Extend the render method to add your plugin to a DOM element'))\n }\n\n addTarget (plugin) {\n throw (new Error('Extend the addTarget method to add your plugin to another plugin\\'s target'))\n }\n\n unmount () {\n if (this.isTargetDOMEl && this.el && this.el.parentNode) {\n this.el.parentNode.removeChild(this.el)\n }\n }\n\n install () {\n\n }\n\n uninstall () {\n this.unmount()\n }\n}\n"]}
\No newline at end of file