@scaleflex/widget-canva 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 scaleflex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # @scaleflex/widget-canva
2
+
3
+ [![Plugins][plugins-image]](#plugins)
4
+ [![Website][filerobot-image]][sfx-url]
5
+ [![Version][filerobot-version]][version-url]
6
+ [![Scaleflex team][made-by-image]][sfx-url]
7
+ [![License][license-image]][license-url]
8
+ [![CodeSandbox][codeSandbox-image]][codeSandbox-url]
9
+
10
+
11
+ <div align='center'>
12
+ <img title="Scaleflex Widget logo" alt="Scaleflex Widget logo" src="https://cdn.scaleflex.com/plugins/filerobot-widget/assets/filerobot_widget_logo_with_fire.png?vh=b2ff09" width="140"/>
13
+ </div>
14
+
15
+ Create your own design & customize it then upload it on the go by integrating this plugin inside [Scaleflex Media Asset Widget](https://www.npmjs.com/package/@scaleflex/widget-core).
16
+
17
+ ## Usage
18
+
19
+ ### NPM
20
+
21
+ ```bash
22
+ npm install --save @scaleflex/widget-canva
23
+ ```
24
+ ### YARN
25
+
26
+ ```bash
27
+ yarn add @scaleflex/widget-canva
28
+ ```
29
+ then
30
+
31
+ ```js
32
+ import Canva from '@scaleflex/widget-canva'
33
+ ...
34
+ ...
35
+ ...
36
+ scaleflexWidget.use(Canva, propertiesObject)
37
+ ```
38
+
39
+ ### CDN
40
+
41
+ The plugin from CDN is found inside `Scaleflex` global object `Scaleflex.Canva`
42
+
43
+ ```js
44
+ const Canva = window.ScaleflexWidget.Canva
45
+ ...
46
+ ...
47
+ ...
48
+ scaleflexWidget.use(Canva, propertiesObject)
49
+ ```
50
+
51
+ ## Properties
52
+
53
+ ### `title`
54
+
55
+ <u>Type:</u> `string`.
56
+
57
+ <u>Default:</u> `'Canva (Design & Upload)'`
58
+
59
+ The title/label that would be shown & used for the plugin.
60
+
61
+ ### `apiKey`
62
+
63
+ <u>Type:</u> `string`.
64
+
65
+ <u>Default:</u> `'scaleflexWidget's canva api key'`
66
+
67
+ The API key used in initializing canva & creating the design through their SDK.
68
+
69
+ ### `designType`
70
+
71
+ <u>Type:</u> `string`.
72
+
73
+ <u>Default:</u> `'A4Document'`
74
+
75
+ The design type of the design workspace that would be opened to create the design, visit [Canva's design types](https://docs.developer.canva.com/button/reference/design-types) for all available types.
76
+
77
+ ### `fileType: string`
78
+
79
+ <u>Type:</u> `string`.
80
+
81
+ <u>Default:</u> `'png'`
82
+
83
+ The type of the final created design's to be saved & uploaded with it.
84
+
85
+ ### `companionHeaders`
86
+
87
+ <u>Type:</u> `null` | `object`.
88
+
89
+ <u>Default:</u> `null`
90
+
91
+ If you need to pass additonal headers in companion requests then pass them in this property (used in retrieving the metadata of the design file).
92
+
93
+ ### `locale`
94
+
95
+ <u>Type:</u> `object`.
96
+
97
+ <u>Default:</u> locales from scaleflexWidget's backend then default locale file with all labels is under `lib/defaultLocale.js`.
98
+
99
+ You can override some labels by specifying a translation object here, such as:
100
+
101
+ ```js
102
+ {
103
+ strings: {
104
+ canvaPluginTitle: 'Canva (Design & upload)' // overrides the default Standard portraits value to standard verticals
105
+ }
106
+ }
107
+ ```
108
+
109
+ <!-- Variables -->
110
+
111
+ [npm-url]: https://www.npmjs.com/package/@scaleflex/widget-canva
112
+ [license-url]: https://opensource.org/licenses/MIT
113
+ [sfx-url]: https://www.scaleflex.com/
114
+ [version-url]: https://www.npmjs.com/package/@scaleflex/widget-canva
115
+ [codeSandbox-url]: https://codesandbox.io/s/filerobot-widget-v3-c5l9th
116
+
117
+
118
+ [npm-image]: https://img.shields.io/npm/v/@telus/remark-config.svg?style=for-the-badge&logo=npm
119
+ [license-image]: https://img.shields.io/badge/license-MIT-blue.svg?style=for-the-badge
120
+ [made-by-image]: https://img.shields.io/badge/%3C%2F%3E%20with%20%E2%99%A5%20by-the%20Scaleflex%20team-6986fa.svg?style=for-the-badge
121
+ [plugins-image]: https://img.shields.io/static/v1?label=Scaleflex&message=Plugins&color=yellow&style=for-the-badge
122
+ [filerobot-image]: https://img.shields.io/static/v1?label=Scaleflex&message=website&color=orange&style=for-the-badge
123
+ [filerobot-version]: https://img.shields.io/npm/v/@scaleflex/widget-canva?label=Version&style=for-the-badge&logo=npm
124
+ [codeSandbox-image]: https://img.shields.io/badge/CodeSandbox-black?style=for-the-badge&logo=CodeSandbox
@@ -0,0 +1,69 @@
1
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
2
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
4
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
5
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
7
+ import { useState, useEffect } from 'react';
8
+ import { LoaderView } from '@scaleflex/widget-common';
9
+ import loadCanvaSDK from './utils/loadCanvaSDK';
10
+ import { jsx as _jsx } from "react/jsx-runtime";
11
+ var openedBlockStyles = {
12
+ display: 'flex',
13
+ justifyContent: 'center',
14
+ alignItems: 'center',
15
+ marginTop: 50
16
+ };
17
+ var CanvaWorkspace = function CanvaWorkspace(_ref) {
18
+ var pluginOpts = _ref.pluginOpts,
19
+ addDesign = _ref.addDesign,
20
+ i18n = _ref.i18n,
21
+ canvaApi = _ref.canvaApi,
22
+ setCanvaApi = _ref.setCanvaApi,
23
+ closePlugin = _ref.closePlugin;
24
+ var apiKey = pluginOpts.apiKey,
25
+ designType = pluginOpts.designType,
26
+ fileType = pluginOpts.fileType;
27
+ var _useState = useState(true),
28
+ _useState2 = _slicedToArray(_useState, 2),
29
+ isLoading = _useState2[0],
30
+ setIsLoading = _useState2[1];
31
+ var createDesign = function createDesign(api) {
32
+ setIsLoading(false);
33
+ if (!canvaApi) {
34
+ setCanvaApi(api);
35
+ }
36
+ api.createDesign({
37
+ design: {
38
+ type: designType
39
+ },
40
+ editor: {
41
+ fileType: fileType
42
+ },
43
+ onDesignPublish: function onDesignPublish(opts) {
44
+ addDesign(opts);
45
+ },
46
+ onDesignClose: function onDesignClose() {
47
+ closePlugin();
48
+ }
49
+ });
50
+ };
51
+ useEffect(function () {
52
+ if (!window.Canva || !window.Canva.DesignButton || !canvaApi) {
53
+ loadCanvaSDK().then(function () {
54
+ window.Canva.DesignButton.initialize({
55
+ apiKey: apiKey
56
+ }).then(createDesign);
57
+ });
58
+ } else {
59
+ createDesign(canvaApi);
60
+ }
61
+ }, []);
62
+ return isLoading ? /*#__PURE__*/_jsx(LoaderView, {
63
+ loaderLabel: i18n('loaderLoadingLabel')
64
+ }) : /*#__PURE__*/_jsx("div", {
65
+ style: openedBlockStyles,
66
+ children: i18n('canvaOpenedInModalHint')
67
+ });
68
+ };
69
+ export default CanvaWorkspace;
@@ -0,0 +1,6 @@
1
+ export default {
2
+ canvaPluginTitle: 'Canva (Design & upload)',
3
+ canvaOpenedInModalHint: 'Canva is opened in an external modal',
4
+ canvaFailedToFetchDesignFileMetaError: 'Failed to fetch the meta of the design file',
5
+ canvaFailedToFetchDesignFileMessage: 'Failed to fetch the design file'
6
+ };
package/lib/index.js ADDED
@@ -0,0 +1,178 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
5
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
6
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
7
+ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
8
+ function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
9
+ function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
10
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
11
+ function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
12
+ function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
13
+ function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
14
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
15
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
16
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
17
+ import { Plugin } from '@scaleflex/widget-core';
18
+ import Explorer from '@scaleflex/widget-explorer';
19
+ import Translator from '@scaleflex/widget-utils/lib/Translator';
20
+ import { CanvaIcon } from '@scaleflex/widget-icons';
21
+ import { RequestClient } from '@scaleflex/widget-companion-client';
22
+ import { PLUGINS_IDS } from '@scaleflex/widget-utils/lib/constants';
23
+ import defaultLocale from './defaultLocale';
24
+ import CanvaWorkspace from './CanvaWorkspace';
25
+ var DEV_API_KEY = '6PlJP_VARP0ey8hLCNQQsp1P';
26
+ var PROD_API_KEY = 'xdXpp6KdBRtRAJbPGs64yEl-';
27
+
28
+ // TODO: find a way to show version of the current plugin
29
+ // why solution below isn't good?
30
+ // first import doesn't work with webpack 5 as it was deprecated
31
+ // second import fixes webpack 5 issue as it was mentioned in their docs
32
+ // but it exposes our package.json to the client and it is mentioned as security rist in mutiple places
33
+ // https://github.com/axelpale/genversion
34
+ // https://stackoverflow.com/questions/64993118/error-should-not-import-the-named-export-version-imported-as-version
35
+ // https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code/10855054#10855054
36
+ // import { version } from '../package.json'
37
+ // import packageInfo from '../package.json'
38
+ var Canva = /*#__PURE__*/function (_Plugin) {
39
+ // static VERSION = packageInfo.version
40
+
41
+ function Canva(filerobot) {
42
+ var _this;
43
+ var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
44
+ _classCallCheck(this, Canva);
45
+ _this = _callSuper(this, Canva, [filerobot, opts]);
46
+ _defineProperty(_this, "i18nInit", function () {
47
+ _this.translator = new Translator([_this.defaultLocale, _this.filerobot.locale, _this.opts.locale]);
48
+ _this.i18n = _this.translator.translate.bind(_this.translator);
49
+ _this.i18nArray = _this.translator.translateArray.bind(_this.translator);
50
+ });
51
+ _defineProperty(_this, "getFileType", function (type) {
52
+ return type.substring(type.lastIndexOf('/') + 1);
53
+ });
54
+ _defineProperty(_this, "getFileNameFromUrl", function (_ref) {
55
+ var url = _ref.url,
56
+ type = _ref.type;
57
+ return url.substring(url.lastIndexOf('/') + 1) + ".".concat(type);
58
+ });
59
+ _defineProperty(_this, "getMeta", function (url) {
60
+ return _this.client.post('url/meta', {
61
+ url: url
62
+ }).then(function (res) {
63
+ if (res.error) {
64
+ _this.filerobot.log('[Canva] Error:');
65
+ _this.filerobot.log(res.error);
66
+ throw new Error(_this.i18n('canvaFailedToFetchDesignFileMetaError'));
67
+ }
68
+ return res;
69
+ });
70
+ });
71
+ _defineProperty(_this, "addDesign", function (design) {
72
+ if (!design.exportUrl) {
73
+ return;
74
+ }
75
+ return _this.getMeta(design.exportUrl).then(function (meta) {
76
+ var tagFile = {
77
+ source: _this.id,
78
+ name: "".concat(design.designTitle, "-").concat(design.designId, ".").concat(_this.getFileType(meta.type)) || _this.getFileNameFromUrl({
79
+ url: design.exportUrl,
80
+ type: _this.getFileType(meta.type)
81
+ }),
82
+ type: meta.type,
83
+ data: {
84
+ size: meta.size
85
+ },
86
+ isRemote: true,
87
+ body: {
88
+ url: design.exportUrl
89
+ },
90
+ preview: design.exportUrl,
91
+ remote: {
92
+ companionUrl: _this.client.companionUrl,
93
+ url: "".concat(_this.hostname, "/url/get"),
94
+ body: {
95
+ fileId: design.designId,
96
+ url: design.exportUrl
97
+ },
98
+ providerOptions: _this.client.opts
99
+ }
100
+ };
101
+ return tagFile;
102
+ }).then(function (tagFile) {
103
+ _this.filerobot.log('[Canva] Adding remote file');
104
+ try {
105
+ _this.filerobot.addFile(tagFile);
106
+ } catch (err) {
107
+ if (!err.isRestriction) {
108
+ _this.filerobot.log(err);
109
+ }
110
+ }
111
+ })["catch"](function (err) {
112
+ _this.filerobot.log(err);
113
+ _this.filerobot.info({
114
+ message: _this.i18n('canvaFailedToFetchDesignFileMessage'),
115
+ details: err
116
+ }, 'error', 5000);
117
+ });
118
+ });
119
+ _defineProperty(_this, "setCanvaApi", function (canvaApi) {
120
+ _this.canvaApi = canvaApi;
121
+ });
122
+ _this.id = PLUGINS_IDS.CANVA;
123
+ _this.type = 'acquirer';
124
+ _this.icon = CanvaIcon;
125
+ _this.title = 'Canva';
126
+ _this.render = _this.render.bind(_this);
127
+ _this.canvaApi = undefined;
128
+ var defaultOptions = {
129
+ apiKey: _this.filerobot.opts.dev ? DEV_API_KEY : PROD_API_KEY,
130
+ designType: 'A4Document',
131
+ fileType: 'png'
132
+ };
133
+ _this.opts = _objectSpread(_objectSpread({}, defaultOptions), opts);
134
+ _this.defaultLocale = {
135
+ strings: defaultLocale
136
+ };
137
+ _this.i18nInit();
138
+
139
+ // !TODO: Check why this.i18n('canvaPluginTitle') doesn't render the proper title.
140
+ // this.title = this.opts.title || this.i18n('canvaPluginTitle')
141
+
142
+ _this.client = new RequestClient(filerobot, {
143
+ companionHeaders: _this.opts.companionHeaders
144
+ });
145
+ _this.hostname = _this.client.companionUrl;
146
+ if (!_this.hostname) {
147
+ throw new Error('Companion hostname is required');
148
+ }
149
+ return _this;
150
+ }
151
+ _inherits(Canva, _Plugin);
152
+ return _createClass(Canva, [{
153
+ key: "install",
154
+ value: function install() {
155
+ if (Explorer) {
156
+ this.mount(Explorer, this);
157
+ }
158
+ }
159
+ }, {
160
+ key: "uninstall",
161
+ value: function uninstall() {
162
+ this.unmount();
163
+ }
164
+ }, {
165
+ key: "render",
166
+ value: function render() {
167
+ return CanvaWorkspace({
168
+ pluginOpts: this.opts,
169
+ addDesign: this.addDesign,
170
+ i18n: this.i18n,
171
+ canvaApi: this.canvaApi,
172
+ setCanvaApi: this.setCanvaApi,
173
+ closePlugin: this.parent.hideAllPanelsAndShowAddFilesPanel
174
+ });
175
+ }
176
+ }]);
177
+ }(Plugin);
178
+ export { Canva as default };
@@ -0,0 +1,11 @@
1
+ var loadCanvaSDK = function loadCanvaSDK() {
2
+ return new Promise(function (resolve, reject) {
3
+ var scriptElement = document.createElement('script');
4
+ scriptElement.src = 'https://sdk.canva.com/designbutton/v2/api.js';
5
+ scriptElement.async = true;
6
+ scriptElement.onload = resolve;
7
+ scriptElement.onerror = reject;
8
+ document.body.appendChild(scriptElement);
9
+ });
10
+ };
11
+ export default loadCanvaSDK;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@scaleflex/widget-canva",
3
+ "description": "Create your own design & customize it then upload it through the widget on the go.",
4
+ "version": "0.0.1",
5
+ "license": "MIT",
6
+ "main": "lib/index.js",
7
+ "types": "types/index.d.ts",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "dependencies": {
12
+ "@scaleflex/widget-common": "^0.0.1",
13
+ "@scaleflex/widget-companion-client": "^0.0.1",
14
+ "@scaleflex/widget-icons": "^0.0.1",
15
+ "@scaleflex/widget-utils": "^0.0.1"
16
+ },
17
+ "devDependencies": {
18
+ "react": "^19.0.0",
19
+ "react-dom": "^19.0.0"
20
+ },
21
+ "peerDependencies": {
22
+ "@scaleflex/widget-core": "^0.0.0",
23
+ "@scaleflex/widget-explorer": "^0.0.0",
24
+ "react": ">=19.0.0",
25
+ "react-dom": ">=19.0.0"
26
+ },
27
+ "files": [
28
+ "/dist",
29
+ "/lib",
30
+ "/types"
31
+ ],
32
+ "gitHead": "64ea82e745b7deda36d6794863350e6671e9010d"
33
+ }