@scaleflex/widget-pdf-js 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/CHANGELOG.md +7434 -0
- package/LICENSE +21 -0
- package/README.md +67 -0
- package/lib/components/Footer/Footer.styled.js +48 -0
- package/lib/components/Footer/ZoomControllers.js +47 -0
- package/lib/components/Footer/index.js +101 -0
- package/lib/components/PdfPreview.js +257 -0
- package/lib/components/PdfPreview.styled.js +43 -0
- package/lib/defaultLocale.js +6 -0
- package/lib/index.js +186 -0
- package/package.json +31 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
3
|
+
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); } }
|
|
4
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
5
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
6
|
+
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); }
|
|
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
|
+
import { Plugin, Filerobot } from '@scaleflex/widget-core';
|
|
15
|
+
import Explorer from '@scaleflex/widget-explorer';
|
|
16
|
+
import Translator from '@scaleflex/widget-utils/lib/Translator';
|
|
17
|
+
import { PLUGINS_IDS } from '@scaleflex/widget-utils/lib/constants';
|
|
18
|
+
import getFileLink from '@scaleflex/widget-utils/lib/getFileLink';
|
|
19
|
+
import isUploadableFile from '@scaleflex/widget-utils/lib/isUploadableFile';
|
|
20
|
+
import defaultLocale from './defaultLocale';
|
|
21
|
+
import PdfPreview from './components/PdfPreview';
|
|
22
|
+
|
|
23
|
+
// TODO: find a way to show version of the current plugin
|
|
24
|
+
// why solution below isn't good?
|
|
25
|
+
// first import doesn't work with webpack 5 as it was deprecated
|
|
26
|
+
// second import fixes webpack 5 issue as it was mentioned in their docs
|
|
27
|
+
// but it exposes our package.json to the client and it is mentioned as security rist in mutiple places
|
|
28
|
+
// https://github.com/axelpale/genversion
|
|
29
|
+
// https://stackoverflow.com/questions/64993118/error-should-not-import-the-named-export-version-imported-as-version
|
|
30
|
+
// https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code/10855054#10855054
|
|
31
|
+
// import { version } from '../package.json'
|
|
32
|
+
// import packageInfo from '../package.json'
|
|
33
|
+
var PdfJs = /*#__PURE__*/function (_Plugin) {
|
|
34
|
+
// static VERSION = packageInfo.version
|
|
35
|
+
|
|
36
|
+
function PdfJs(filerobot) {
|
|
37
|
+
var _this;
|
|
38
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
39
|
+
_classCallCheck(this, PdfJs);
|
|
40
|
+
_this = _callSuper(this, PdfJs, [filerobot, opts]);
|
|
41
|
+
_this.id = PLUGINS_IDS.PDF_JS;
|
|
42
|
+
_this.libId = 'pdfjs-lib';
|
|
43
|
+
_this.title = 'Pdf-js';
|
|
44
|
+
_this.type = 'editor';
|
|
45
|
+
_this.defaultLocale = {
|
|
46
|
+
strings: defaultLocale
|
|
47
|
+
};
|
|
48
|
+
_this.opts = opts;
|
|
49
|
+
_this.i18nInit();
|
|
50
|
+
_this.prepareDocument = _this.prepareDocument.bind(_this);
|
|
51
|
+
_this.render = _this.render.bind(_this);
|
|
52
|
+
return _this;
|
|
53
|
+
}
|
|
54
|
+
_inherits(PdfJs, _Plugin);
|
|
55
|
+
return _createClass(PdfJs, [{
|
|
56
|
+
key: "i18nInit",
|
|
57
|
+
value: function i18nInit() {
|
|
58
|
+
this.translator = new Translator([this.defaultLocale, this.filerobot.locale, this.opts.locale]);
|
|
59
|
+
this.i18n = this.translator.translate.bind(this.translator);
|
|
60
|
+
this.i18nArray = this.translator.translateArray.bind(this.translator);
|
|
61
|
+
}
|
|
62
|
+
}, {
|
|
63
|
+
key: "install",
|
|
64
|
+
value: function install() {
|
|
65
|
+
if (Explorer) {
|
|
66
|
+
this.mount(Explorer, this);
|
|
67
|
+
this.loadLib();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}, {
|
|
71
|
+
key: "uninstall",
|
|
72
|
+
value: function uninstall() {
|
|
73
|
+
this.unmount();
|
|
74
|
+
this.removeLib();
|
|
75
|
+
}
|
|
76
|
+
}, {
|
|
77
|
+
key: "loadLib",
|
|
78
|
+
value: function loadLib() {
|
|
79
|
+
var _this2 = this;
|
|
80
|
+
if (this.isPdfJsLibLoading) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.isPdfJsLibLoading = true;
|
|
84
|
+
|
|
85
|
+
// We are using both pdfjs & its worker from CDN as they needs dynamic import, that would cause a change in vite's build target
|
|
86
|
+
// Also would cause to change the bundled JS bundle type from umd to es, so using the CDN avoids that.
|
|
87
|
+
var script = document.createElement('script');
|
|
88
|
+
script.id = this.libId;
|
|
89
|
+
script.src = 'https://scaleflex.cloudimg.io/v7/plugins/filerobot-widget/assets/js/pdf.min.js?vh=a4641a&func=proxy';
|
|
90
|
+
script.async = true;
|
|
91
|
+
script.onload = function () {
|
|
92
|
+
_this2.pdfJsLib = window.pdfjsLib;
|
|
93
|
+
_this2.pdfJsLib.GlobalWorkerOptions.workerSrc = 'https://scaleflex.cloudimg.io/v7/plugins/filerobot-widget/assets/js/pdf.worker.js?vh=49162d&func=proxy';
|
|
94
|
+
_this2.isPdfJsLibLoading = false;
|
|
95
|
+
if (typeof _this2.onLoadPdfJsLib === 'function') {
|
|
96
|
+
_this2.onLoadPdfJsLib();
|
|
97
|
+
_this2.onLoadPdfJsLib = null;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
script.onerror = function () {
|
|
101
|
+
_this2.isPdfJsLibLoading = false;
|
|
102
|
+
_this2.removeLib();
|
|
103
|
+
if (typeof _this2.onErrorPdfJsLib === 'function') {
|
|
104
|
+
_this2.onErrorPdfJsLib();
|
|
105
|
+
_this2.onErrorPdfJsLib = null;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
document.body.appendChild(script);
|
|
109
|
+
}
|
|
110
|
+
}, {
|
|
111
|
+
key: "removeLib",
|
|
112
|
+
value: function removeLib() {
|
|
113
|
+
this.isPdfJsLibLoading = false;
|
|
114
|
+
var element = document.getElementById(this.libId);
|
|
115
|
+
if (element) {
|
|
116
|
+
document.body.removeChild(element);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}, {
|
|
120
|
+
key: "handleLoadingDocumentError",
|
|
121
|
+
value: function handleLoadingDocumentError(setIsFailedToViewPdf, reason) {
|
|
122
|
+
var abortMsgsToDismiss = ['worker was destroyed', 'transport destroyed', 'canceled'];
|
|
123
|
+
if (abortMsgsToDismiss.includes(reason === null || reason === void 0 ? void 0 : reason.toLowerCase())) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
this.info({
|
|
127
|
+
message: this.i18n('pdfJsPdfPreviewErrorLabel'),
|
|
128
|
+
details: reason || ''
|
|
129
|
+
}, 'warning', 5000);
|
|
130
|
+
setIsFailedToViewPdf(true);
|
|
131
|
+
}
|
|
132
|
+
}, {
|
|
133
|
+
key: "waitForLibLoadingThenPrepareDocument",
|
|
134
|
+
value: function waitForLibLoadingThenPrepareDocument(pdfPath) {
|
|
135
|
+
var _this3 = this;
|
|
136
|
+
var isPrepareCancelled = false;
|
|
137
|
+
var destroyPendingDocPrepare = function destroyPendingDocPrepare() {
|
|
138
|
+
isPrepareCancelled = true;
|
|
139
|
+
};
|
|
140
|
+
var waitLibAndPrepareDocPromise = new Promise(function (resolve, reject) {
|
|
141
|
+
_this3.onLoadPdfJsLib = async function () {
|
|
142
|
+
if (isPrepareCancelled) {
|
|
143
|
+
reject(new Error('canceled'));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
var doc = await _this3.prepareDocument(pdfPath).promise;
|
|
147
|
+
resolve(doc);
|
|
148
|
+
};
|
|
149
|
+
_this3.onErrorPdfJsLib = function () {
|
|
150
|
+
reject(new Error('canceled'));
|
|
151
|
+
};
|
|
152
|
+
// Making sure that the loading request is sent (maybe it's failed at some point so we are re-sending it)
|
|
153
|
+
// If it is already loading/in-progress its safe guard condition there won't re-trigger it.
|
|
154
|
+
_this3.loadLib();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Simulating used properties in PdfPreview and same as the ones returned from getDocument of the lib.
|
|
158
|
+
return {
|
|
159
|
+
promise: waitLibAndPrepareDocPromise,
|
|
160
|
+
destroy: destroyPendingDocPrepare
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}, {
|
|
164
|
+
key: "prepareDocument",
|
|
165
|
+
value: function prepareDocument(pdfPath) {
|
|
166
|
+
if (!this.pdfJsLib) {
|
|
167
|
+
return this.waitForLibLoadingThenPrepareDocument(pdfPath);
|
|
168
|
+
}
|
|
169
|
+
return this.pdfJsLib.getDocument(pdfPath);
|
|
170
|
+
}
|
|
171
|
+
}, {
|
|
172
|
+
key: "render",
|
|
173
|
+
value: function render(_ref) {
|
|
174
|
+
var file = _ref.file,
|
|
175
|
+
setIsFailedToViewPdf = _ref.setIsFailedToViewPdf;
|
|
176
|
+
var pdfSource = isUploadableFile(file) ? Filerobot.uploadFilesData[file.id] : getFileLink(file);
|
|
177
|
+
return PdfPreview({
|
|
178
|
+
pdfSource: pdfSource,
|
|
179
|
+
i18n: this.i18n,
|
|
180
|
+
prepareDocument: this.prepareDocument,
|
|
181
|
+
onError: this.handleLoadingDocumentError.bind(this, setIsFailedToViewPdf)
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}]);
|
|
185
|
+
}(Plugin);
|
|
186
|
+
export default PdfJs;
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scaleflex/widget-pdf-js",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Pdf-js integration with Scaleflex",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"/dist",
|
|
9
|
+
"/lib",
|
|
10
|
+
"/types"
|
|
11
|
+
],
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@scaleflex/icons": "^3.0.0-beta.11",
|
|
17
|
+
"@scaleflex/ui": "^3.0.0-beta.11",
|
|
18
|
+
"@scaleflex/widget-common": "^0.0.1"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"react": "^19.0.0",
|
|
22
|
+
"react-dom": "^19.0.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@scaleflex/widget-core": "^0.0.0",
|
|
26
|
+
"@scaleflex/widget-explorer": "^0.0.0",
|
|
27
|
+
"react": ">=19.0.0",
|
|
28
|
+
"react-dom": ">=19.0.0"
|
|
29
|
+
},
|
|
30
|
+
"gitHead": "64ea82e745b7deda36d6794863350e6671e9010d"
|
|
31
|
+
}
|