@scaleflex/widget-pixaforge 4.4.0 → 4.5.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/lib/index.js ADDED
@@ -0,0 +1,327 @@
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 { BadgeButton, CloseButton } from '@scaleflex/widget-common';
21
+ import { RequestClient } from '@scaleflex/widget-companion-client';
22
+ import { PixaforgeIcon } from '@scaleflex/widget-icons';
23
+ import { PLUGINS_IDS } from '@scaleflex/widget-utils/lib/constants';
24
+ import defaultLocale from './defaultLocale';
25
+ import MainView from './components/MainView';
26
+ import GalleryViewsOptionsMenu from './components/GalleryViewsOptionsMenu';
27
+ import { GALLERIES } from './constants';
28
+ import pixaforgeReducer, { loadingToggled, pixaforgeCommonStateUpdated, selectGallery } from './common.slice';
29
+ // TODO: find a way to show version of the current plugin
30
+ // why solution below isn't good?
31
+ // first import doesn't work with webpack 5 as it was deprecated
32
+ // second import fixes webpack 5 issue as it was mentioned in their docs
33
+ // but it exposes our package.json to the client and it is mentioned as security rist in mutiple places
34
+ // https://github.com/axelpale/genversion
35
+ // https://stackoverflow.com/questions/64993118/error-should-not-import-the-named-export-version-imported-as-version
36
+ // https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code/10855054#10855054
37
+ // import { version } from '../package.json'
38
+ // import packageInfo from '../package.json'
39
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
40
+ var Pixaforge = /*#__PURE__*/function (_Plugin) {
41
+ // static VERSION = packageInfo.version
42
+
43
+ function Pixaforge(filerobot) {
44
+ var _this;
45
+ var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
46
+ _classCallCheck(this, Pixaforge);
47
+ _this = _callSuper(this, Pixaforge, [filerobot, opts]);
48
+ _defineProperty(_this, "i18nInit", function () {
49
+ _this.translator = new Translator([_this.defaultLocale, _this.filerobot.locale, _this.opts.locale]);
50
+ _this.i18n = _this.translator.translate.bind(_this.translator);
51
+ _this.i18nArray = _this.translator.translateArray.bind(_this.translator);
52
+ });
53
+ _defineProperty(_this, "getFileNameFromUrl", function (_ref) {
54
+ var url = _ref.url,
55
+ type = _ref.type;
56
+ return url.substring(url.lastIndexOf('/') + 1) + ".".concat(type.substring(type.lastIndexOf('/') + 1));
57
+ });
58
+ _defineProperty(_this, "getMeta", function (url) {
59
+ return _this.client.post('url/meta', {
60
+ url: url
61
+ }).then(function (res) {
62
+ if (res.error) {
63
+ _this.filerobot.log('[Pixaforge] Error:');
64
+ _this.filerobot.log(res.error);
65
+ throw new Error('Failed to fetch the file');
66
+ }
67
+ return res;
68
+ });
69
+ });
70
+ _defineProperty(_this, "addImage", function () {
71
+ var image = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
72
+ var url = image.src;
73
+ if (!url) {
74
+ return;
75
+ }
76
+ return _this.getMeta(url).then(function (meta) {
77
+ var tagFile = {
78
+ source: _this.id,
79
+ name: _this.getFileNameFromUrl({
80
+ url: url,
81
+ type: meta.type
82
+ }),
83
+ type: meta.type,
84
+ preview: image.preview,
85
+ data: {
86
+ size: meta.size
87
+ },
88
+ isRemote: true,
89
+ body: {
90
+ url: url
91
+ },
92
+ remote: {
93
+ companionUrl: _this.client.companionUrl,
94
+ url: "".concat(_this.hostname, "/url/get"),
95
+ body: {
96
+ fileId: image.id,
97
+ url: url
98
+ },
99
+ providerOptions: _this.client.opts
100
+ }
101
+ };
102
+ return tagFile;
103
+ }).then(function (tagFile) {
104
+ _this.filerobot.log('[Pixaforge] Adding remote file');
105
+ try {
106
+ _this.filerobot.addFile(tagFile);
107
+ } catch (err) {
108
+ if (!err.isRestriction) {
109
+ _this.filerobot.log(err);
110
+ }
111
+ }
112
+ })["catch"](function (err) {
113
+ _this.filerobot.log(err);
114
+ _this.filerobot.info({
115
+ message: _this.i18n('pixaforgeFailedToFetchMessage'),
116
+ details: (err === null || err === void 0 ? void 0 : err.message) || (err === null || err === void 0 ? void 0 : err.toString()) || err
117
+ }, 'error', 5000);
118
+ });
119
+ });
120
+ _defineProperty(_this, "toggleLoader", function () {
121
+ _this.dispatch(loadingToggled());
122
+ });
123
+ _defineProperty(_this, "addImages", function () {
124
+ var images = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
125
+ var imagesLength = images.length;
126
+ if (imagesLength === 0) {
127
+ return;
128
+ }
129
+ _this.toggleLoader();
130
+ Promise.all(images.map(_this.addImage))["finally"](_this.toggleLoader);
131
+ });
132
+ _defineProperty(_this, "getIconFileDescriptorToBeAdded", function () {
133
+ var _ref2, _ref3, _icon$name;
134
+ var icon = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
135
+ var currentDate = Date.now();
136
+ var iconStringReplacedWithFinalColor = (icon.svgStringified || '').replace('color="currentColor"', "color=\"".concat(icon.color, "\""));
137
+ if (!iconStringReplacedWithFinalColor) {
138
+ return;
139
+ }
140
+ var iconName = (_ref2 = (_ref3 = (_icon$name = icon.name) !== null && _icon$name !== void 0 ? _icon$name : icon.alt) !== null && _ref3 !== void 0 ? _ref3 : icon.uid) !== null && _ref2 !== void 0 ? _ref2 : currentDate;
141
+ var iconFileProperties = {
142
+ type: 'image/svg+xml',
143
+ lastModified: currentDate
144
+ };
145
+ var iconFile = new File([iconStringReplacedWithFinalColor], iconName, iconFileProperties);
146
+ var previewUrl = URL.createObjectURL(iconFile);
147
+ var fileDescriptor = {
148
+ source: _this.id,
149
+ name: iconFile.name,
150
+ type: iconFile.type,
151
+ data: iconFile,
152
+ preview: previewUrl
153
+ };
154
+ setTimeout(function () {
155
+ URL.revokeObjectURL(previewUrl);
156
+ }, 3000);
157
+ return fileDescriptor;
158
+ });
159
+ _defineProperty(_this, "addIcons", function () {
160
+ var icons = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
161
+ var iconsLength = icons.length;
162
+ if (iconsLength === 0) {
163
+ return;
164
+ }
165
+ _this.toggleLoader();
166
+ var iconsFilesDescriptors = icons.map(_this.getIconFileDescriptorToBeAdded);
167
+ try {
168
+ _this.filerobot.addFiles(iconsFilesDescriptors);
169
+ } catch (err) {
170
+ _this.filerobot.log(err);
171
+ }
172
+ _this.toggleLoader();
173
+ });
174
+ _defineProperty(_this, "sendSelectedMediaToUpload", function () {
175
+ var _this$getPluginCommon = _this.getPluginCommonState(),
176
+ _this$getPluginCommon2 = _this$getPluginCommon.currentSelection,
177
+ currentSelection = _this$getPluginCommon2 === void 0 ? [] : _this$getPluginCommon2,
178
+ _this$getPluginCommon3 = _this$getPluginCommon.customizedIcons,
179
+ customizedIcons = _this$getPluginCommon3 === void 0 ? [] : _this$getPluginCommon3;
180
+ if (customizedIcons.length > 0) {
181
+ _this.addIcons(customizedIcons);
182
+ } else {
183
+ _this.addImages(currentSelection);
184
+ }
185
+ _this.setPluginCommonState({
186
+ currentSelection: [],
187
+ customizedIcons: []
188
+ });
189
+ });
190
+ _defineProperty(_this, "sendSelectedIconsToCustomize", function () {
191
+ var _this$getPluginCommon4 = _this.getPluginCommonState(),
192
+ _this$getPluginCommon5 = _this$getPluginCommon4.currentSelection,
193
+ currentSelection = _this$getPluginCommon5 === void 0 ? [] : _this$getPluginCommon5;
194
+ if (currentSelection.length > 0) {
195
+ _this.dispatch(pixaforgeCommonStateUpdated({
196
+ customizedIcons: currentSelection,
197
+ currentSelection: []
198
+ }));
199
+ }
200
+ });
201
+ _defineProperty(_this, "clearSelection", function () {
202
+ _this.dispatch(pixaforgeCommonStateUpdated({
203
+ currentSelection: [],
204
+ customizedIcons: [],
205
+ isAllSelected: false
206
+ }));
207
+ });
208
+ _defineProperty(_this, "listenToElementResize", function (elementSelectorOrRef, callback) {
209
+ _this.stopListeningToElementResize();
210
+ _this.resizeObserver = new ResizeObserver(function (entries) {
211
+ var filerobotPixaforgeInnerEl = entries[0];
212
+ var _filerobotPixaforgeIn = filerobotPixaforgeInnerEl.contentRect,
213
+ width = _filerobotPixaforgeIn.width,
214
+ height = _filerobotPixaforgeIn.height;
215
+ _this.filerobot.log("[Pixaforge] resized: ".concat(width, " / ").concat(height), 'debug');
216
+ if (typeof callback === 'function') {
217
+ callback(width, height);
218
+ }
219
+ });
220
+ _this.resizeObserver.observe(typeof elementSelectorOrRef === 'string' ? _this.el.querySelector(elementSelectorOrRef) : elementSelectorOrRef);
221
+ });
222
+ _defineProperty(_this, "stopListeningToElementResize", function () {
223
+ if (_this.resizeObserver) {
224
+ _this.resizeObserver.disconnect();
225
+ }
226
+ });
227
+ _defineProperty(_this, "renderPanelBarTopLeftComponent", function () {
228
+ var _this$getPluginCommon6 = _this.getPluginCommonState(),
229
+ _this$getPluginCommon7 = _this$getPluginCommon6.currentSelection,
230
+ currentSelection = _this$getPluginCommon7 === void 0 ? [] : _this$getPluginCommon7,
231
+ _this$getPluginCommon8 = _this$getPluginCommon6.customizedIcons,
232
+ customizedIcons = _this$getPluginCommon8 === void 0 ? [] : _this$getPluginCommon8,
233
+ gallery = _this$getPluginCommon6.gallery;
234
+ var selectionCount = currentSelection.length;
235
+ var customizedIconsCount = customizedIcons.length;
236
+ var isIconsGallery = gallery === GALLERIES.ICONS;
237
+ var hasCustomizedIcons = customizedIconsCount > 0;
238
+ var isInIconsSelectionStep = isIconsGallery && !hasCustomizedIcons;
239
+ var isDisabled = selectionCount === 0 && !hasCustomizedIcons;
240
+ var onClickHandler = isInIconsSelectionStep ? _this.sendSelectedIconsToCustomize : _this.sendSelectedMediaToUpload;
241
+ return /*#__PURE__*/_jsxs(_Fragment, {
242
+ children: [/*#__PURE__*/_jsx(BadgeButton, {
243
+ onClick: isDisabled ? undefined : onClickHandler,
244
+ background: "accent",
245
+ disabled: isDisabled,
246
+ label: _this.filerobot.i18n(isInIconsSelectionStep ? 'mutualizedSelectLabel' : 'mutualizedImportLabel'),
247
+ badgeContent: selectionCount === 0 && customizedIconsCount === 0 ? undefined : customizedIconsCount || selectionCount
248
+ }), !isDisabled && /*#__PURE__*/_jsx(CloseButton, {
249
+ onClick: _this.clearSelection,
250
+ hideDivider: true
251
+ })]
252
+ });
253
+ });
254
+ _defineProperty(_this, "renderPanelBarTopRightComponent", function () {
255
+ if (selectGallery(_this.getGlobalState()) !== GALLERIES.IMAGES) {
256
+ return null;
257
+ }
258
+ return /*#__PURE__*/_jsx(GalleryViewsOptionsMenu, {
259
+ i18n: _this.i18n
260
+ });
261
+ });
262
+ _this.id = PLUGINS_IDS.PIXAFORGE;
263
+ _this.type = 'acquirer';
264
+ _this.icon = function () {
265
+ return /*#__PURE__*/_jsx(PixaforgeIcon, {});
266
+ };
267
+ _this.title = _this.opts.title || 'Free images & icons';
268
+ _this.render = _this.render.bind(_this);
269
+ _this.defaultLocale = {
270
+ strings: defaultLocale
271
+ };
272
+ var defaultOptions = {
273
+ noIconsGallery: false
274
+ };
275
+ _this.opts = _objectSpread(_objectSpread({}, defaultOptions), opts);
276
+ _this.i18nInit();
277
+
278
+ // !TODO: Check why this.i18n('pixaforgeTitle') doesn't render the proper title.
279
+ // this.title = this.opts.title || this.i18n('pixaforgeTitle')
280
+
281
+ _this.client = new RequestClient(filerobot, {
282
+ companionHeaders: _this.opts.companionHeaders
283
+ });
284
+ _this.hostname = _this.client.companionUrl;
285
+ if (!_this.hostname) {
286
+ throw new Error('Companion hostname is required');
287
+ }
288
+ return _this;
289
+ }
290
+ _inherits(Pixaforge, _Plugin);
291
+ return _createClass(Pixaforge, [{
292
+ key: "install",
293
+ value: function install() {
294
+ if (Explorer) {
295
+ this.mount(Explorer, this);
296
+ }
297
+ }
298
+ }, {
299
+ key: "uninstall",
300
+ value: function uninstall() {
301
+ this.unmount();
302
+ this.stopListeningToElementResize();
303
+ }
304
+ }, {
305
+ key: "setPluginCommonState",
306
+ value: function setPluginCommonState(update) {
307
+ return this.dispatch(pixaforgeCommonStateUpdated(update));
308
+ }
309
+ }, {
310
+ key: "getPluginReducer",
311
+ value: function getPluginReducer() {
312
+ return pixaforgeReducer;
313
+ }
314
+ }, {
315
+ key: "render",
316
+ value: function render() {
317
+ return /*#__PURE__*/_jsx(MainView, {
318
+ listenToElementResize: this.listenToElementResize,
319
+ stopListeningToElementResize: this.stopListeningToElementResize,
320
+ clearSelection: this.clearSelection,
321
+ i18n: this.i18n,
322
+ info: this.filerobot.info
323
+ });
324
+ }
325
+ }]);
326
+ }(Plugin);
327
+ export { Pixaforge as default };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@scaleflex/widget-pixaforge",
3
3
  "description": "Import free images & icons from Pixaforge that has free gallery for both.",
4
- "version": "4.4.0",
4
+ "version": "4.5.1",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
7
7
  "types": "types/index.d.ts",
@@ -11,10 +11,10 @@
11
11
  "dependencies": {
12
12
  "@scaleflex/icons": "^3.0.0-beta.11",
13
13
  "@scaleflex/ui": "^3.0.0-beta.11",
14
- "@scaleflex/widget-common": "^4.4.0",
15
- "@scaleflex/widget-companion-client": "^4.4.0",
16
- "@scaleflex/widget-icons": "^4.4.0",
17
- "@scaleflex/widget-utils": "^4.4.0"
14
+ "@scaleflex/widget-common": "^4.5.1",
15
+ "@scaleflex/widget-companion-client": "^4.5.1",
16
+ "@scaleflex/widget-icons": "^4.5.1",
17
+ "@scaleflex/widget-utils": "^4.5.1"
18
18
  },
19
19
  "devDependencies": {
20
20
  "react": "^19.0.0",
@@ -31,5 +31,5 @@
31
31
  "/lib",
32
32
  "/types"
33
33
  ],
34
- "gitHead": "04b8ec545c4f10be89f5a44eed954acf8433360c"
34
+ "gitHead": "b211f9961ad30d3eaa01138f34e190ad5d38fb91"
35
35
  }