|
| 1 | +import adrapid from "../../adrapid.app.mjs"; |
| 2 | +import { parseObject } from "../../common/utils.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "adrapid-create-banner", |
| 6 | + name: "Create Banner", |
| 7 | + description: "Generates a new banner using provided data. This action can create different types of banners, such as animated HTML5, image, or video banners. [See the documentation](https://docs.adrapid.com/api/overview)", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + adrapid, |
| 12 | + templateId: { |
| 13 | + propDefinition: [ |
| 14 | + adrapid, |
| 15 | + "templateId", |
| 16 | + ], |
| 17 | + reloadProps: true, |
| 18 | + }, |
| 19 | + modes: { |
| 20 | + type: "string[]", |
| 21 | + label: "Modes", |
| 22 | + description: "Modes for the resulting banners, previously set on configuration block.", |
| 23 | + options: [ |
| 24 | + "html5", |
| 25 | + "amp", |
| 26 | + "png", |
| 27 | + "jpeg", |
| 28 | + "pdf", |
| 29 | + "webp", |
| 30 | + "video", |
| 31 | + "gif", |
| 32 | + ], |
| 33 | + reloadProps: true, |
| 34 | + optional: true, |
| 35 | + }, |
| 36 | + overrides: { |
| 37 | + type: "object", |
| 38 | + label: "Overrides", |
| 39 | + description: "With this parameter we override the default template content. We use the name of the item we want to override, valid properties are text, url (for external images) and css. [See the documentation](https://docs.adrapid.com/api/overview).", |
| 40 | + optional: true, |
| 41 | + }, |
| 42 | + sync: { |
| 43 | + type: "boolean", |
| 44 | + label: "Sync", |
| 45 | + description: "Get banner url in the same response. Delay the response until the banner is ready. Usually in a few seconds with a maxtime of two minutes.", |
| 46 | + optional: true, |
| 47 | + }, |
| 48 | + editable: { |
| 49 | + type: "boolean", |
| 50 | + label: "Editable", |
| 51 | + description: "Make banner editable, creating a template that is a copy of the banner.", |
| 52 | + optional: true, |
| 53 | + }, |
| 54 | + excludeBaseSize: { |
| 55 | + type: "boolean", |
| 56 | + label: "Exclude Base Size", |
| 57 | + description: "Exclude base size from the banner. Only applies to multisize banners.", |
| 58 | + optional: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + async additionalProps() { |
| 62 | + const props = {}; |
| 63 | + if (this.templateId) { |
| 64 | + const { sizes } = await this.adrapid.getTemplate({ |
| 65 | + templateId: this.templateId, |
| 66 | + }); |
| 67 | + props.sizeIds = { |
| 68 | + type: "string[]", |
| 69 | + label: "Size Ids", |
| 70 | + description: "The template sizes. To use all of them you can set this parameter to 'all'. If you remove this parameter only the default size of the template will be used.", |
| 71 | + reloadProps: true, |
| 72 | + optional: true, |
| 73 | + options: sizes.map(({ |
| 74 | + id: value, name: label, |
| 75 | + }) => ({ |
| 76 | + value, |
| 77 | + label, |
| 78 | + })), |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + if (this.modes) { |
| 83 | + if (this.modes.includes("html5")) { |
| 84 | + props.html5Packed = { |
| 85 | + type: "boolean", |
| 86 | + label: "HTML5 Packed", |
| 87 | + description: "Set banner in a single file with embedded images.", |
| 88 | + }; |
| 89 | + props.html5Flexible = { |
| 90 | + type: "boolean", |
| 91 | + label: "HTML5 Flexible", |
| 92 | + description: "Set a flexible banner that will adapt to his container.", |
| 93 | + }; |
| 94 | + } |
| 95 | + if (this.modes.includes("video")) { |
| 96 | + props.videoFps = { |
| 97 | + type: "integer", |
| 98 | + label: "Video FPS", |
| 99 | + description: "Frame Per Second of resulting video.", |
| 100 | + default: 14, |
| 101 | + max: 60, |
| 102 | + }; |
| 103 | + props.videoCrf = { |
| 104 | + type: "integer", |
| 105 | + label: "Video CRF", |
| 106 | + description: "Constant Rate Factor. Less is more quality.", |
| 107 | + default: 18, |
| 108 | + max: 60, |
| 109 | + }; |
| 110 | + props.videoOffset = { |
| 111 | + type: "string", |
| 112 | + label: "Audio Offset", |
| 113 | + description: "Audio offset. Can be negative number.", |
| 114 | + }; |
| 115 | + props.videoSrc = { |
| 116 | + type: "string", |
| 117 | + label: "Audio SRC", |
| 118 | + description: "URL of the audio file.", |
| 119 | + }; |
| 120 | + } |
| 121 | + if (this.modes.includes("gif")) { |
| 122 | + props.gifFps = { |
| 123 | + type: "boolean", |
| 124 | + label: "GIF FPS", |
| 125 | + description: "Frame per second of resulting gif.", |
| 126 | + }; |
| 127 | + props.gifFrames = { |
| 128 | + type: "integer[]", |
| 129 | + label: "GIF Frames", |
| 130 | + description: "Select specific seconds in the timeline to be used as frames.", |
| 131 | + }; |
| 132 | + } |
| 133 | + } |
| 134 | + return props; |
| 135 | + }, |
| 136 | + async run({ $ }) { |
| 137 | + |
| 138 | + const modes = {}; |
| 139 | + |
| 140 | + if (this.modes) { |
| 141 | + this.modes.forEach((item) => { |
| 142 | + modes[item] = true; |
| 143 | + }); |
| 144 | + |
| 145 | + if (this.modes.includes("html5")) { |
| 146 | + modes.html5 = { |
| 147 | + packed: this.html5Packed, |
| 148 | + flexibe: this.html5Flexible, |
| 149 | + }; |
| 150 | + } |
| 151 | + if (this.modes.includes("video")) { |
| 152 | + modes.video = { |
| 153 | + fps: this.videoFps, |
| 154 | + crf: this.videoCrf, |
| 155 | + audio: { |
| 156 | + offset: parseInt(this.videoOffset), |
| 157 | + src: this.videoSrc, |
| 158 | + }, |
| 159 | + }; |
| 160 | + } |
| 161 | + if (this.modes.includes("gif")) { |
| 162 | + modes.html5 = { |
| 163 | + fps: this.gifFps, |
| 164 | + frames: this.gifFrames, |
| 165 | + }; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + const response = await this.adrapid.createBanner({ |
| 170 | + $, |
| 171 | + data: { |
| 172 | + sizeIds: this.sizeIds, |
| 173 | + templateId: this.templateId, |
| 174 | + modes, |
| 175 | + overrides: parseObject(this.overrides), |
| 176 | + sync: this.sync, |
| 177 | + editable: this.editable, |
| 178 | + excludeBaseSize: this.excludeBaseSize, |
| 179 | + }, |
| 180 | + }); |
| 181 | + |
| 182 | + $.export("$summary", `Banner created successfully with ID: ${response.id}`); |
| 183 | + return response; |
| 184 | + }, |
| 185 | +}; |
0 commit comments