|
| 1 | +/** |
| 2 | + * Copyright (c) 2018-present, Renderforest, LLC. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the license found in the |
| 6 | + * LICENSE file in the root directory. |
| 7 | + */ |
| 8 | + |
| 9 | +const PackageJson = require('../../package.json') |
| 10 | + |
| 11 | +class ProjectData { |
| 12 | + /** |
| 13 | + * @constructor |
| 14 | + * @param {Object} projectDataJson |
| 15 | + */ |
| 16 | + constructor (projectDataJson) { |
| 17 | + this.projectDataJson = projectDataJson |
| 18 | + this.setGenerator() |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @description Set the generator. |
| 23 | + */ |
| 24 | + setGenerator () { |
| 25 | + this.projectDataJson.data['generator'] = ProjectData.generator |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @returns {number} |
| 30 | + * @description Get the project id. |
| 31 | + */ |
| 32 | + getProjectId () { |
| 33 | + return this.projectDataJson.projectId |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @returns {number} |
| 38 | + * @description Get the template id. |
| 39 | + */ |
| 40 | + getTemplateId () { |
| 41 | + return this.projectDataJson.data['templateId'] |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @returns {boolean} |
| 46 | + * @description Check whether is equalizer or not. |
| 47 | + */ |
| 48 | + isEqualizer () { |
| 49 | + return this.projectDataJson.data['equalizer'] |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @returns {boolean} |
| 54 | + * @description Check whether is lego or not. |
| 55 | + */ |
| 56 | + isLego () { |
| 57 | + return this.projectDataJson.data['isLego'] |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @returns {boolean} |
| 62 | + * @description Get the project muteMusic property. |
| 63 | + */ |
| 64 | + getMuteMusic () { |
| 65 | + return this.projectDataJson.data['muteMusic'] |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param {boolean} muteMusic |
| 70 | + * @description Set the project muteMusic property. |
| 71 | + */ |
| 72 | + setMuteMusic (muteMusic) { |
| 73 | + this.projectDataJson.data['muteMusic'] = muteMusic |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @returns {Array} |
| 78 | + * @description Get the project colors. |
| 79 | + */ |
| 80 | + getProjectColors () { |
| 81 | + return this.projectDataJson.data['projectColors'] |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param {Array} projectColors |
| 86 | + * @description Set the project colors. |
| 87 | + */ |
| 88 | + setProjectColors (projectColors) { |
| 89 | + this.projectDataJson.data['projectColors'] = projectColors |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @returns {Object} |
| 94 | + * @description Get the project theme. |
| 95 | + */ |
| 96 | + getTheme () { |
| 97 | + return { |
| 98 | + themeVariableName: this.projectDataJson.data['themeVariableName'], |
| 99 | + themeVariableValue: this.projectDataJson.data['themeVariableValue'] |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param {Object} payload |
| 105 | + * @param {string} payload.themeVariableName |
| 106 | + * @param {string} payload.themeVariableValue |
| 107 | + * @description Set the project theme. |
| 108 | + */ |
| 109 | + setTheme (payload) { |
| 110 | + this.projectDataJson.data['themeVariableName'] = payload.themeVariableName |
| 111 | + this.projectDataJson.data['themeVariableValue'] = payload.themeVariableValue |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @returns {Array} |
| 116 | + * @description Get the project sounds. |
| 117 | + */ |
| 118 | + getSounds () { |
| 119 | + return this.projectDataJson.data['sounds'] |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @param {Array} sounds |
| 124 | + * @description Set the project sounds. |
| 125 | + */ |
| 126 | + setSounds (sounds) { |
| 127 | + this.projectDataJson.data['sounds'] = sounds |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @returns {string} |
| 132 | + * @description Get the project title. |
| 133 | + */ |
| 134 | + getTitle () { |
| 135 | + return this.projectDataJson.data['title'] |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @returns {Array} |
| 140 | + * @description Get screens (add methods on screens & screen areas). |
| 141 | + */ |
| 142 | + getScreens () { |
| 143 | + const screens = this.projectDataJson.data['screens'] |
| 144 | + return screens.map((screen) => { |
| 145 | + return this.constructScreen(screen) |
| 146 | + }) |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @param {Object} screen |
| 151 | + * @returns {Object} |
| 152 | + * @description Construct screen. |
| 153 | + */ |
| 154 | + constructScreen (screen) { |
| 155 | + const { |
| 156 | + id, characterBasedDuration, compositionName, duration, extraVideoSecond, gifBigPath, gifPath, gifThumbnailPath, |
| 157 | + hidden, iconAdjustable, isFull, maxDuration, order, path, tags, title, type, areas |
| 158 | + } = screen |
| 159 | + |
| 160 | + return { |
| 161 | + id, |
| 162 | + characterBasedDuration, |
| 163 | + compositionName, |
| 164 | + duration, |
| 165 | + extraVideoSecond, |
| 166 | + gifBigPath, |
| 167 | + gifPath, |
| 168 | + gifThumbnailPath, |
| 169 | + hidden, |
| 170 | + iconAdjustable, |
| 171 | + isFull, |
| 172 | + maxDuration, |
| 173 | + order, |
| 174 | + path, |
| 175 | + tags, |
| 176 | + title, |
| 177 | + type, |
| 178 | + getAreas: () => { |
| 179 | + return areas.map((area) => { |
| 180 | + return this.constructArea(area) |
| 181 | + }) |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * @param {Object} area |
| 188 | + * @returns {Object} |
| 189 | + * @description Construct area. |
| 190 | + */ |
| 191 | + constructArea (area) { |
| 192 | + const { |
| 193 | + id, fileName, height, width, value, cords, title, wordCount, originalHeight, originalWidth, order, type, |
| 194 | + mimeType, webpPath, fileType, thumbnailPath, imageCropParams, videoCropParams |
| 195 | + } = area |
| 196 | + |
| 197 | + const result = { id, height, width, value, cords, title, wordCount, order, type } |
| 198 | + |
| 199 | + if (area.type === 'text') { |
| 200 | + result.setText = (text) => { |
| 201 | + area.value = text |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + if (area.type === 'image') { |
| 206 | + Object.assign(result, { |
| 207 | + fileName, originalHeight, originalWidth, mimeType, webpPath, fileType, thumbnailPath, imageCropParams |
| 208 | + }) |
| 209 | + result.setImage = (image) => { |
| 210 | + ProjectData.setAreaImage(area, image) |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + if (area.type === 'video') { |
| 215 | + Object.assign(result, { |
| 216 | + fileName, originalHeight, originalWidth, mimeType, webpPath, fileType, thumbnailPath, videoCropParams |
| 217 | + }) |
| 218 | + result.setVideo = (video) => { |
| 219 | + ProjectData.setAreaVideo(area, video) |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return result |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * @param {Object} area |
| 228 | + * @param {{fileName, mime, filePath, webpPath, fileType, thumbnailPath, imageCropParams}} image |
| 229 | + * @param {{transform, top, left, width, height}} image.imageCropParams |
| 230 | + * @description Set image on area. |
| 231 | + */ |
| 232 | + static setAreaImage (area, image) { |
| 233 | + const { fileName, mime, filePath, webpPath, fileType, thumbnailPath, imageCropParams } = image |
| 234 | + const { transform, top, left, width, height } = imageCropParams |
| 235 | + |
| 236 | + area.fileName = fileName |
| 237 | + area.mimeType = mime |
| 238 | + area.value = filePath |
| 239 | + area.webpPath = webpPath |
| 240 | + area.fileType = fileType |
| 241 | + area.thumbnailPath = thumbnailPath |
| 242 | + area.imageCropParams = { |
| 243 | + transform: `rotate(${transform}deg)`, |
| 244 | + top: `${top}px`, |
| 245 | + left: `${left}px`, |
| 246 | + width, |
| 247 | + height |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * @param {Object} area |
| 253 | + * @param {{fileName, mime, filePath, webpPath, fileType, videoCropParams}} video |
| 254 | + * @param {{duration, end, mime, start, stockFootageId, thumbnail, thumbnailVideo, videoVoiceTreatment}} video.videoCropParams |
| 255 | + * @description Set video on area. |
| 256 | + */ |
| 257 | + static setAreaVideo (area, video) { |
| 258 | + const { fileName, mime, filePath, webpPath, fileType, videoCropParams } = video |
| 259 | + |
| 260 | + Object.assign(area, { |
| 261 | + fileName, mimeType: mime, value: filePath, webpPath, fileType, videoCropParams |
| 262 | + }) |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +ProjectData.generator = `renderforest/sdk-node/${PackageJson.version}` |
| 267 | + |
| 268 | +module.exports = ProjectData |
0 commit comments