Просмотр исходного кода

add `onMount` to Plugin from @uppy/core

Called when plugin is mounted, whether in DOM or into another plugin. Needed because sometimes plugins are mounted separately/after `install`, so this.el and this.parent might not be available in `install`. This is the case with @uppy/react plugins, for example.
Artur Paikin 6 лет назад
Родитель
Сommit
b9b53da4ec
1 измененных файлов с 15 добавлено и 2 удалено
  1. 15 2
      packages/@uppy/core/src/Plugin.js

+ 15 - 2
packages/@uppy/core/src/Plugin.js

@@ -72,6 +72,16 @@ module.exports = class Plugin {
     }
     }
   }
   }
 
 
+  /**
+  * Called when plugin is mounted, whether in DOM or into another plugin.
+  * Needed because sometimes plugins are mounted separately/after `install`,
+  * so this.el and this.parent might not be available in `install`.
+  * This is the case with @uppy/react plugins, for example.
+  */
+  onMount () {
+
+  }
+
   /**
   /**
    * Check if supplied `target` is a DOM element or an `object`.
    * Check if supplied `target` is a DOM element or an `object`.
    * If it’s an object — target is a plugin, and we search `plugins`
    * If it’s an object — target is a plugin, and we search `plugins`
@@ -107,6 +117,7 @@ module.exports = class Plugin {
 
 
       this.el = preact.render(this.render(this.uppy.getState()), targetElement)
       this.el = preact.render(this.render(this.uppy.getState()), targetElement)
 
 
+      this.onMount()
       return this.el
       return this.el
     }
     }
 
 
@@ -127,9 +138,11 @@ module.exports = class Plugin {
     }
     }
 
 
     if (targetPlugin) {
     if (targetPlugin) {
-      const targetPluginName = targetPlugin.id
-      this.uppy.log(`Installing ${callerPluginName} to ${targetPluginName}`)
+      this.uppy.log(`Installing ${callerPluginName} to ${targetPlugin.id}`)
+      this.parent = targetPlugin.id
       this.el = targetPlugin.addTarget(plugin)
       this.el = targetPlugin.addTarget(plugin)
+
+      this.onMount()
       return this.el
       return this.el
     }
     }