|
| 1 | +/* |
| 2 | + React Studio wrapper for the 'react-tsparticles' npm package. |
| 3 | +
|
| 4 | + - 2020 / Nitin Khanna / @nitinthewiz / automedia.ai |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +// -- plugin info requested by host app -- |
| 9 | + |
| 10 | +this.describePlugin = function(id, lang) { |
| 11 | + switch (id) { |
| 12 | + case 'displayName': |
| 13 | + return "tsparticles"; |
| 14 | + |
| 15 | + case 'shortDisplayText': |
| 16 | + return "tsparticles!"; |
| 17 | + |
| 18 | + case 'defaultNameForNewInstance': |
| 19 | + return "tsparticles"; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +// -- private variables -- |
| 25 | + |
| 26 | +this._data = { |
| 27 | + declarative: "" |
| 28 | +}; |
| 29 | + |
| 30 | + |
| 31 | +// -- persistence, i.e. saving and loading -- |
| 32 | + |
| 33 | +this.persist = function() { |
| 34 | + return this._data; |
| 35 | +} |
| 36 | + |
| 37 | +this.unpersist = function(data) { |
| 38 | + this._data = data; |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +// -- inspector UI -- |
| 43 | + |
| 44 | +this.inspectorUIDefinition = [ |
| 45 | + { |
| 46 | + "type": "label", |
| 47 | + "text": "Paste the tsParticles config JSON below.\nDimensions come from React Studio canvas", |
| 48 | + "height": 40, |
| 49 | + }, |
| 50 | + { |
| 51 | + "type": "textinput", |
| 52 | + "id": "declarative", |
| 53 | + "label": "JSON Declaration", |
| 54 | + "actionBinding": "this.onUIChange", |
| 55 | + "multiline": true, |
| 56 | + "height": 600 |
| 57 | + } |
| 58 | +]; |
| 59 | + |
| 60 | +this._uiTextFields = [ 'declarative' ]; |
| 61 | +this._uiCheckboxes = []; |
| 62 | +this._uiNumberFields = []; |
| 63 | +this._uiColorPickers = []; |
| 64 | +this._uiComponentPickers = []; |
| 65 | + |
| 66 | +this._accessorForDataKey = function(key) { |
| 67 | + if (this._uiTextFields.includes(key)) return 'text'; |
| 68 | + else if (this._uiCheckboxes.includes(key)) return 'checked'; |
| 69 | + else if (this._uiNumberFields.includes(key)) return 'numberValue'; |
| 70 | + else if (this._uiColorPickers.includes(key)) return 'rgbaArrayValue'; |
| 71 | + else if (this._uiComponentPickers.includes(key)) return 'componentName'; |
| 72 | + return null; |
| 73 | +} |
| 74 | + |
| 75 | +this.onCreateUI = function() { |
| 76 | + var ui = this.getUI(); |
| 77 | + for (var controlId in this._data) { |
| 78 | + var prop = this._accessorForDataKey(controlId); |
| 79 | + if (prop) { |
| 80 | + try { |
| 81 | + ui.getChildById(controlId)[prop] = this._data[controlId]; |
| 82 | + } catch (e) { |
| 83 | + console.log("** can't set ui value for key "+controlId+", prop "+prop); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +this.onUIChange = function(controlId) { |
| 90 | + var ui = this.getUI(); |
| 91 | + var prop = this._accessorForDataKey(controlId); |
| 92 | + if (prop) { |
| 93 | + this._data[controlId] = ui.getChildById(controlId)[prop]; |
| 94 | + } else { |
| 95 | + console.log("** no data property found for controlId "+controlId); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +// -- plugin preview -- |
| 101 | + |
| 102 | +this.renderIcon = function(canvas) { |
| 103 | + var ctx = canvas.getContext('2d'); |
| 104 | + var w = canvas.width; |
| 105 | + var h = canvas.height; |
| 106 | + ctx.save(); |
| 107 | + if (this.icon == null) { |
| 108 | + // got the Particles logo online |
| 109 | + var path = Plugin.getPathForResource("particles_logo.png"); |
| 110 | + this.icon = Plugin.loadImage(path); |
| 111 | + } |
| 112 | + var iconW = this.icon.width; |
| 113 | + var iconH = this.icon.height; |
| 114 | + var aspectScale = Math.min(w/iconW, h/iconH); |
| 115 | + var scale = 0.9 * aspectScale; // add some margin around icon |
| 116 | + iconW *= scale; |
| 117 | + iconH *= scale; |
| 118 | + ctx.drawImage(this.icon, (w-iconW)*0.5, (h-iconH)*0.5, iconW, iconH); |
| 119 | + ctx.restore(); |
| 120 | +}; |
| 121 | + |
| 122 | +this.renderEditingCanvasPreview = function(canvas, controller) { |
| 123 | + this._renderPreview(canvas, controller); |
| 124 | +} |
| 125 | + |
| 126 | +this._renderPreview = function(canvas, controller) { |
| 127 | + var ctx = canvas.getContext('2d'); |
| 128 | + var w = canvas.width; |
| 129 | + var h = canvas.height; |
| 130 | + ctx.save(); |
| 131 | + |
| 132 | + if (this.icon == null) { |
| 133 | + var path = Plugin.getPathForResource("particles_logo.png"); |
| 134 | + this.icon = Plugin.loadImage(path); |
| 135 | + } |
| 136 | + var iconW = this.icon.width; |
| 137 | + var iconH = this.icon.height; |
| 138 | + var aspectScale = Math.min(w/iconW, h/iconH); |
| 139 | + var scale = 0.9 * aspectScale; // add some margin around icon |
| 140 | + iconW *= scale; |
| 141 | + iconH *= scale; |
| 142 | + ctx.drawImage(this.icon, (w-iconW)*0.5, (h-iconH)*0.5, iconW, iconH); |
| 143 | + ctx.restore(); |
| 144 | + |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +// -- code generation, React web -- |
| 149 | + |
| 150 | +this.getReactWebPackages = function() { |
| 151 | + // Return dependencies that need to be included in the exported project's package.json file. |
| 152 | + // Each key is an npm package name that must be imported, and the value is the package version. |
| 153 | + // |
| 154 | + // Example: |
| 155 | + // return { "somepackage": "^1.2.3" } |
| 156 | + |
| 157 | + return { |
| 158 | + "react-tsparticles": "^1.17.10" |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +this.getReactWebImports = function(exporter) { |
| 163 | + var arr = [ |
| 164 | + { varName: "Particles", path: "react-tsparticles" } |
| 165 | + ]; |
| 166 | + |
| 167 | + return arr; |
| 168 | +} |
| 169 | + |
| 170 | +this.writesCustomReactWebComponent = false; |
| 171 | + |
| 172 | +this.getReactWebJSXCode = function(exporter) { |
| 173 | + var json_declaration = this._data.declarative; |
| 174 | + |
| 175 | + var jsx = `<Particles id="tsparticles" options={${JSON.parse(json_declaration)}} />`; |
| 176 | + |
| 177 | + return jsx; |
| 178 | +} |
| 179 | + |
0 commit comments