@capillarytech/creatives-library 7.12.93 → 7.12.94

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@capillarytech/creatives-library",
3
3
  "author": "meharaj",
4
- "version": "7.12.93",
4
+ "version": "7.12.94",
5
5
  "description": "Capillary creatives ui",
6
6
  "main": "./index.js",
7
7
  "module": "./index.es.js",
@@ -24,6 +24,7 @@
24
24
  "jest-date-mock": "^1.0.8",
25
25
  "jquery": "^3.3.1",
26
26
  "load-script": "^1.0.0",
27
+ "node-html-parser": "^5.4.2-0",
27
28
  "normalizr": "^3.2.3",
28
29
  "papaparse": "^5.3.1",
29
30
  "pre-push": "^0.1.2",
@@ -0,0 +1,220 @@
1
+ import cloneDeep from "lodash/cloneDeep";
2
+ import isEmpty from "lodash/isEmpty";
3
+ import forOwn from "lodash/forOwn";
4
+ import isNumber from "lodash/isNumber";
5
+ /* eslint-disable no-unused-expressions */
6
+ import { parse } from "node-html-parser";
7
+
8
+ // https://storage.crm.n.content-cdn.io/cdn-cgi/image/width=100,height=100,quality=75,fit=cover,g=top,format=auto/intouch_creative_assets/005710a7-6412-44fe-a19f-e72456b1.jpg
9
+
10
+ const CDN_BASE_URL = `https://storage.crm.n.content-cdn.io`;
11
+ const CDN_TRANSFORMATION_URL_SUFFIX = `/cdn-cgi/image/`;
12
+ const QUALITY = 75;
13
+ // const regex = /https:\/\/.*intouch_creative_assets.*(?:jpg|png|jpeg|gif)$/;
14
+ const regex = /https:\/\/.*intouch_creative_assets.*$/;
15
+ const INTOUCH_CREATIVE_ASSETS = "intouch_creative_assets";
16
+ const SPECIFIED = "specified";
17
+ const FORMAT_TYPES = { AUTO: "auto" };
18
+ const QUALITY_TYPE = {
19
+ DECREASE: "decrease",
20
+ PRESERVE: "preserve",
21
+ };
22
+
23
+ const CHANNEL_CONFIGS = {
24
+ EMAIL: {
25
+ transformations: {
26
+ width: SPECIFIED,
27
+ height: SPECIFIED,
28
+ format: FORMAT_TYPES.AUTO,
29
+ quality: QUALITY_TYPE.DECREASE,
30
+ },
31
+ },
32
+ RCS: {
33
+ transformations: {
34
+ width: 1440,
35
+ height: 720,
36
+ quality: QUALITY_TYPE.DECREASE,
37
+ },
38
+ },
39
+ VIBER: {
40
+ transformations: {
41
+ width: 300,
42
+ height: 400,
43
+ quality: QUALITY_TYPE.DECREASE,
44
+ },
45
+ },
46
+ WHATSAPP: {
47
+ transformations: {
48
+ quality: QUALITY_TYPE.DECREASE, //TODO whatsapp itself might reduce quality. To be checked
49
+ },
50
+ },
51
+ MOBILE_PUSH: {
52
+ transformations: {
53
+ quality: QUALITY_TYPE.DECREASE,
54
+ },
55
+ },
56
+ FACEBOOK: {
57
+ IMAGE: {
58
+ transformations: {
59
+ width: 1080,
60
+ height: 1080,
61
+ quality: QUALITY_TYPE.DECREASE,
62
+ },
63
+ }
64
+ },
65
+ LINE: {
66
+ IMAGE: {
67
+ transformations: {
68
+ width: 300,
69
+ height: 400,
70
+ quality: QUALITY_TYPE.DECREASE,
71
+ },
72
+ },
73
+ CARD: {
74
+ transformations: {
75
+ width: 1024,
76
+ height: 1024,
77
+ quality: QUALITY_TYPE.DECREASE,
78
+ },
79
+ },
80
+ RICH_MESSAGE: {
81
+ //Rich Message in Line has Square and Custom templates.
82
+ //Width (1040px) is same in both whereas height varies. Hence including width only.
83
+ transformations: {
84
+ width: 1040,
85
+ quality: QUALITY_TYPE.DECREASE,
86
+ },
87
+ }
88
+ },
89
+ };
90
+
91
+ /**
92
+ * getCdnUrl : utility function to replace and return s3/cdn url with cdn url.
93
+ */
94
+ export const getCdnUrl = ({
95
+ url,
96
+ height,
97
+ width,
98
+ channelName: receivedChannelName,
99
+ channelSubType: receivedChannelSubType,
100
+ }) => {
101
+ const channelName = receivedChannelName?.toUpperCase();
102
+ const channelSubType = receivedChannelSubType?.toUpperCase();
103
+
104
+ if (
105
+ !regex.test(url) ||
106
+ !CHANNEL_CONFIGS?.[channelName] ||
107
+ (channelSubType && !CHANNEL_CONFIGS?.[channelName]?.[channelSubType])
108
+ )
109
+ return url;
110
+
111
+ const [, assetKey] = url.split(INTOUCH_CREATIVE_ASSETS);
112
+
113
+ let newUrl = `${CDN_BASE_URL}`;
114
+
115
+ let applicableTransformations;
116
+
117
+ if (channelSubType) {
118
+ applicableTransformations =
119
+ CHANNEL_CONFIGS?.[channelName]?.[channelSubType]?.transformations;
120
+ } else {
121
+ applicableTransformations = CHANNEL_CONFIGS?.[channelName]?.transformations;
122
+ }
123
+
124
+ if (!isEmpty(applicableTransformations)) {
125
+
126
+ newUrl += CDN_TRANSFORMATION_URL_SUFFIX;
127
+
128
+ // Object.keys(applicableTransformations)?.forEach((transformationParam) => {
129
+ forOwn(applicableTransformations, (transformationParamVal, transformationParam)=>{
130
+ try {
131
+ switch (transformationParam) {
132
+ case "height":
133
+ if (transformationParamVal === SPECIFIED && height) {
134
+ newUrl += `${transformationParam}=${height},`;
135
+ } else if(transformationParamVal !== SPECIFIED && isNumber(transformationParamVal)) {
136
+ newUrl += `${transformationParam}=${transformationParamVal},`;
137
+ }
138
+ break;
139
+ case "width":
140
+ if (transformationParamVal === SPECIFIED && width) {
141
+ newUrl += `${transformationParam}=${width},`;
142
+ } else if(transformationParamVal !== SPECIFIED && isNumber(transformationParamVal)) {
143
+ newUrl += `${transformationParam}=${transformationParamVal},`;
144
+ }
145
+ break;
146
+ case "format":
147
+ if(transformationParamVal){
148
+ newUrl += `${transformationParam}=${transformationParamVal},`;
149
+ }
150
+ break;
151
+ case "quality":
152
+ if (transformationParamVal === QUALITY_TYPE?.DECREASE && QUALITY) {
153
+ newUrl += `${transformationParam}=${QUALITY},`;
154
+ }
155
+ break;
156
+ default:
157
+ break;
158
+ }
159
+ } catch (e) {
160
+ console.log("some error occured while applying transformation",e);
161
+ }
162
+ })
163
+ }
164
+
165
+ newUrl += `/${INTOUCH_CREATIVE_ASSETS}${assetKey}`;
166
+ return newUrl;
167
+ };
168
+
169
+ /**
170
+ *
171
+ * @param {*} htmlContents
172
+ * @returns htmlContents with updated urls in image tag
173
+ */
174
+ export const updateImagesInHtml = (htmlContents) => {
175
+ const copiedHtmlContents = htmlContents;
176
+ try {
177
+ const root = parse(htmlContents);
178
+ root.querySelectorAll("img").forEach((element) => {
179
+ const imageSrc = element.getAttribute("src");
180
+ const isAsset = regex.test(imageSrc);
181
+ if (isAsset) {
182
+ const height = element.getAttribute("height");
183
+ const width = element.getAttribute("width");
184
+ const newUrl = getCdnUrl({
185
+ url: imageSrc,
186
+ height,
187
+ width,
188
+ channelName: "EMAIL",
189
+ });
190
+ element.setAttribute("src", newUrl);
191
+ }
192
+ });
193
+ return root.toString();
194
+ } catch (e) {
195
+ console.log("An error occured while updating images in html", e);
196
+ return copiedHtmlContents; //return received input directly in case an exception is thrown
197
+ }
198
+ };
199
+
200
+ /**
201
+ *
202
+ * @param {*} contents
203
+ * @returns
204
+ */
205
+ export const transformEmailTemplates = (contents) => {
206
+ const contentsCopy = cloneDeep(contents);
207
+ try {
208
+ const base = contentsCopy?.versions?.base;
209
+ const languages = base?.selectedLanguages;
210
+ languages?.forEach((lang) => {
211
+ const htmlContents = base?.[lang]?.["template-content"];
212
+ const newHtmlContents = updateImagesInHtml(htmlContents);
213
+ base[lang]["template-content"] = newHtmlContents;
214
+ });
215
+ return contentsCopy;
216
+ } catch (e) {
217
+ console.log("Some error has occured while transforming email templates", e);
218
+ return contents; //return received input directly in case an exception is thrown
219
+ }
220
+ };
@@ -0,0 +1,298 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`transformEmailTemplate() should transform email template 1`] = `
4
+ Object {
5
+ "name": "test template1",
6
+ "type": "EMAIL",
7
+ "versions": Object {
8
+ "base": Object {
9
+ "activeTab": "en",
10
+ "base": true,
11
+ "en": Object {
12
+ "drag_drop_id": "",
13
+ "id": "5f7b61605f09ebe595ff404e",
14
+ "is_drag_drop": false,
15
+ "iso_code": "en",
16
+ "lang_id": 69,
17
+ "language": "English",
18
+ "tabKey": "6",
19
+ "template-content": "<!DOCTYPE html PUBLIC \\"-//W3C//DTD XHTML 1.0 Transitional //EN\\" \\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\\">
20
+ <html xmlns=\\"http://www.w3.org/1999/xhtml\\" xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\">
21
+ <head><meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=utf-8\\"><meta name=\\"viewport\\" content=\\"width=device-width\\">
22
+ <title></title>
23
+
24
+ <style type=\\"text/css\\">body {
25
+ margin: 0;
26
+ padding: 0; }
27
+
28
+ table,
29
+ td,
30
+ tr {
31
+ vertical-align: top;
32
+ border-collapse: collapse; }
33
+
34
+ * {
35
+ line-height: inherit; }
36
+
37
+ a[x-apple-data-detectors=true] {
38
+ color: inherit !important;
39
+ text-decoration: none !important; }
40
+ </style>
41
+ <style id=\\"media-query\\" type=\\"text/css\\">@media (max-width: 520px) {
42
+ .block-grid,
43
+ .col {
44
+ min-width: 320px !important;
45
+ max-width: 100% !important;
46
+ display: block !important;
47
+ }
48
+
49
+ .block-grid {
50
+ width: 100% !important;
51
+ }
52
+
53
+ .col {
54
+ width: 100% !important;
55
+ }
56
+
57
+ .col > div {
58
+ margin: 0 auto;
59
+ }
60
+
61
+ img.fullwidth,
62
+ img.fullwidthOnMobile {
63
+ max-width: 100% !important;
64
+ }
65
+
66
+ .no-stack .col {
67
+ min-width: 0 !important;
68
+ display: table-cell !important;
69
+ }
70
+
71
+ .no-stack.two-up .col {
72
+ width: 50% !important;
73
+ }
74
+
75
+ .no-stack .col.num4 {
76
+ width: 33% !important;
77
+ }
78
+
79
+ .no-stack .col.num8 {
80
+ width: 66% !important;
81
+ }
82
+
83
+ .no-stack .col.num4 {
84
+ width: 33% !important;
85
+ }
86
+
87
+ .no-stack .col.num3 {
88
+ width: 25% !important;
89
+ }
90
+
91
+ .no-stack .col.num6 {
92
+ width: 50% !important;
93
+ }
94
+
95
+ .no-stack .col.num9 {
96
+ width: 75% !important;
97
+ }
98
+
99
+ .video-block {
100
+ max-width: none !important;
101
+ }
102
+
103
+ .mobile_hide {
104
+ min-height: 0px;
105
+ max-height: 0px;
106
+ max-width: 0px;
107
+ display: none;
108
+ overflow: hidden;
109
+ font-size: 0px;
110
+ }
111
+
112
+ .desktop_hide {
113
+ display: block !important;
114
+ max-height: none !important;
115
+ }
116
+ }
117
+ </style>
118
+ </head>
119
+ <body class=\\"clean-body\\" style=\\"margin: 0; padding: 0; -webkit-text-size-adjust: 100%; background-color: #FFFFFF;\\">
120
+ <p>hello world</p>
121
+
122
+ <p><img alt=\\"Capillary Image\\" height=\\"171\\" src=\\"https://storage.crm.n.content-cdn.io/cdn-cgi/image/width=257,height=171,format=auto,quality=75,/intouch_creative_assets/b4b63b22-8a81-41ee-a8df-6db967fc.jpg\\" width=\\"257\\"></p>
123
+
124
+ <table bgcolor=\\"#FFFFFF\\" cellpadding=\\"0\\" cellspacing=\\"0\\" class=\\"nl-container\\" role=\\"presentation\\" style=\\"background-color:#ffffff; border-collapse:collapse; border-spacing:0; margin:0 auto; min-width:320px; mso-table-lspace:0pt; mso-table-rspace:0pt; table-layout:fixed; vertical-align:top; width:100%\\" valign=\\"top\\">
125
+ <tbody>
126
+ <tr style=\\"vertical-align:top\\" valign=\\"top\\">
127
+ <td style=\\"vertical-align:top; word-break:break-word\\">
128
+ <div style=\\"background-color:transparent\\">
129
+ <div class=\\"block-grid\\" style=\\"background-color:transparent; margin-bottom:0; margin-left:auto; margin-right:auto; margin-top:0; max-width:500px; min-width:320px; overflow-wrap:break-word; word-break:break-word; word-wrap:break-word\\">
130
+ <div style=\\"background-color:transparent; border-collapse:collapse; display:table; width:100%\\">
131
+ <div class=\\"col num12\\" style=\\"display:table-cell; max-width:500px; min-width:320px; vertical-align:top; width:500px\\">
132
+ <div style=\\"width:100% !important\\"></div>
133
+ </div>
134
+ </div>
135
+ </div>
136
+ </div>
137
+ </td>
138
+ </tr>
139
+ </tbody>
140
+ </table>
141
+ </body>
142
+ </html>
143
+ ",
144
+ },
145
+ "selectedLanguages": Array [
146
+ "en",
147
+ ],
148
+ "subject": "hello subject",
149
+ "tabKey": "6",
150
+ },
151
+ "history": Array [
152
+ Object {
153
+ "activeTab": "en",
154
+ "base": true,
155
+ "en": Object {
156
+ "drag_drop_id": "",
157
+ "id": "5f7b61605f09ebe595ff404e",
158
+ "is_drag_drop": false,
159
+ "iso_code": "en",
160
+ "lang_id": 69,
161
+ "language": "English",
162
+ "tabKey": "6",
163
+ "template-content": "<!DOCTYPE html PUBLIC \\"-//W3C//DTD XHTML 1.0 Transitional //EN\\" \\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\\">
164
+ <html xmlns=\\"http://www.w3.org/1999/xhtml\\" xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\">
165
+ <head><!--[if gte mso 9]><xml><o:OfficeDocumentSettings><o:AllowPNG/><o:PixelsPerInch>96</o:PixelsPerInch></o:OfficeDocumentSettings></xml><![endif]--><meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=utf-8\\"><meta name=\\"viewport\\" content=\\"width=device-width\\"><!--[if !mso]><meta http-equiv=\\"X-UA-Compatible\\" content=\\"IE=edge\\"><!--<![endif]-->
166
+ <title></title>
167
+ <!--[if !mso]><!--<![endif]-->
168
+ <style type=\\"text/css\\">body {
169
+ margin: 0;
170
+ padding: 0; }
171
+
172
+ table,
173
+ td,
174
+ tr {
175
+ vertical-align: top;
176
+ border-collapse: collapse; }
177
+
178
+ * {
179
+ line-height: inherit; }
180
+
181
+ a[x-apple-data-detectors=true] {
182
+ color: inherit !important;
183
+ text-decoration: none !important; }
184
+ </style>
185
+ <style id=\\"media-query\\" type=\\"text/css\\">@media (max-width: 520px) {
186
+ .block-grid,
187
+ .col {
188
+ min-width: 320px !important;
189
+ max-width: 100% !important;
190
+ display: block !important;
191
+ }
192
+
193
+ .block-grid {
194
+ width: 100% !important;
195
+ }
196
+
197
+ .col {
198
+ width: 100% !important;
199
+ }
200
+
201
+ .col > div {
202
+ margin: 0 auto;
203
+ }
204
+
205
+ img.fullwidth,
206
+ img.fullwidthOnMobile {
207
+ max-width: 100% !important;
208
+ }
209
+
210
+ .no-stack .col {
211
+ min-width: 0 !important;
212
+ display: table-cell !important;
213
+ }
214
+
215
+ .no-stack.two-up .col {
216
+ width: 50% !important;
217
+ }
218
+
219
+ .no-stack .col.num4 {
220
+ width: 33% !important;
221
+ }
222
+
223
+ .no-stack .col.num8 {
224
+ width: 66% !important;
225
+ }
226
+
227
+ .no-stack .col.num4 {
228
+ width: 33% !important;
229
+ }
230
+
231
+ .no-stack .col.num3 {
232
+ width: 25% !important;
233
+ }
234
+
235
+ .no-stack .col.num6 {
236
+ width: 50% !important;
237
+ }
238
+
239
+ .no-stack .col.num9 {
240
+ width: 75% !important;
241
+ }
242
+
243
+ .video-block {
244
+ max-width: none !important;
245
+ }
246
+
247
+ .mobile_hide {
248
+ min-height: 0px;
249
+ max-height: 0px;
250
+ max-width: 0px;
251
+ display: none;
252
+ overflow: hidden;
253
+ font-size: 0px;
254
+ }
255
+
256
+ .desktop_hide {
257
+ display: block !important;
258
+ max-height: none !important;
259
+ }
260
+ }
261
+ </style>
262
+ </head>
263
+ <body class=\\"clean-body\\" style=\\"margin: 0; padding: 0; -webkit-text-size-adjust: 100%; background-color: #FFFFFF;\\">
264
+ <p><!--[if IE]><div class=\\"ie-browser\\"><![endif]-->hello world</p>
265
+
266
+ <p><img alt=\\"Capillary Image\\" height=\\"171\\" src=\\"https://crm-nightly-new-fileservice.s3.amazonaws.com/intouch_creative_assets/b4b63b22-8a81-41ee-a8df-6db967fc.jpg\\" width=\\"257\\" /></p>
267
+
268
+ <table bgcolor=\\"#FFFFFF\\" cellpadding=\\"0\\" cellspacing=\\"0\\" class=\\"nl-container\\" role=\\"presentation\\" style=\\"background-color:#ffffff; border-collapse:collapse; border-spacing:0; margin:0 auto; min-width:320px; mso-table-lspace:0pt; mso-table-rspace:0pt; table-layout:fixed; vertical-align:top; width:100%\\" valign=\\"top\\">
269
+ <tbody>
270
+ <tr style=\\"vertical-align:top\\" valign=\\"top\\">
271
+ <td style=\\"vertical-align:top; word-break:break-word\\"><!--[if (mso)|(IE)]><table width=\\"100%\\" cellpadding=\\"0\\" cellspacing=\\"0\\" border=\\"0\\"><tr><td align=\\"center\\" style=\\"background-color:#FFFFFF\\"><![endif]-->
272
+ <div style=\\"background-color:transparent\\">
273
+ <div class=\\"block-grid\\" style=\\"background-color:transparent; margin-bottom:0; margin-left:auto; margin-right:auto; margin-top:0; max-width:500px; min-width:320px; overflow-wrap:break-word; word-break:break-word; word-wrap:break-word\\">
274
+ <div style=\\"background-color:transparent; border-collapse:collapse; display:table; width:100%\\"><!--[if (mso)|(IE)]><table width=\\"100%\\" cellpadding=\\"0\\" cellspacing=\\"0\\" border=\\"0\\" style=\\"background-color:transparent;\\"><tr><td align=\\"center\\"><table cellpadding=\\"0\\" cellspacing=\\"0\\" border=\\"0\\" style=\\"width:500px\\"><tr class=\\"layout-full-width\\" style=\\"background-color:transparent\\"><![endif]--><!--[if (mso)|(IE)]><td align=\\"center\\" width=\\"500\\" style=\\"background-color:transparent;width:500px; border-top: 0px solid transparent; border-left: 0px solid transparent; border-bottom: 0px solid transparent; border-right: 0px solid transparent;\\" valign=\\"top\\"><table width=\\"100%\\" cellpadding=\\"0\\" cellspacing=\\"0\\" border=\\"0\\"><tr><td style=\\"padding-right: 0px; padding-left: 0px; padding-top:5px; padding-bottom:5px;\\"><![endif]-->
275
+ <div class=\\"col num12\\" style=\\"display:table-cell; max-width:500px; min-width:320px; vertical-align:top; width:500px\\">
276
+ <div style=\\"width:100% !important\\"><!--[if (!mso)&(!IE)]><div style=\\"border-top:0px solid transparent; border-left:0px solid transparent; border-bottom:0px solid transparent; border-right:0px solid transparent; padding-top:5px; padding-bottom:5px; padding-right: 0px; padding-left: 0px;\\"><!--<![endif]--><!--[if (!mso)&(!IE)]></div><!--<![endif]--></div>
277
+ </div>
278
+ <!--[if (mso)|(IE)]></td></tr></table><![endif]--><!--[if (mso)|(IE)]></td></tr></table></td></tr></table><![endif]--></div>
279
+ </div>
280
+ </div>
281
+ <!--[if (mso)|(IE)]></td></tr></table><![endif]--></td>
282
+ </tr>
283
+ </tbody>
284
+ </table>
285
+ <!--[if (IE)]></div><![endif]--></body>
286
+ </html>
287
+ ",
288
+ },
289
+ "selectedLanguages": Array [
290
+ "en",
291
+ ],
292
+ "subject": "hello subject",
293
+ "tabKey": "6",
294
+ },
295
+ ],
296
+ },
297
+ }
298
+ `;