@quadrats/common 1.1.7 → 1.1.10
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.
|
@@ -2,7 +2,10 @@ import { Editor, NodeEntry, Node } from '@quadrats/core';
|
|
|
2
2
|
import { FileUploader, FileUploaderElement } from './typings';
|
|
3
3
|
export interface CreateFileUploaderOptions {
|
|
4
4
|
type?: string;
|
|
5
|
+
onError?: VoidFunction;
|
|
5
6
|
}
|
|
6
|
-
export declare const createFileUploaderElementByType: (type: string
|
|
7
|
+
export declare const createFileUploaderElementByType: (type: string, uploaderOptions?: {
|
|
8
|
+
onError?: VoidFunction;
|
|
9
|
+
}) => FileUploader<Editor>['createFileUploaderElement'];
|
|
7
10
|
export declare function insertFileUploaderElement(editor: Editor, fileUploaderElement: FileUploaderElement | undefined, currentBlockEntry?: NodeEntry<Node> | undefined): void;
|
|
8
11
|
export declare function createFileUploader(options?: CreateFileUploaderOptions): FileUploader<Editor>;
|
|
@@ -5,7 +5,7 @@ import { FILE_UPLOADER_TYPE, FILE_UPLOADER_PLACEHOLDER_TYPE } from './constants.
|
|
|
5
5
|
import { getFilesFromInput } from './getFilesFromInput.js';
|
|
6
6
|
import { TABLE_CELL_TYPE } from '@quadrats/common/table';
|
|
7
7
|
|
|
8
|
-
const createFileUploaderElementByType = (type) => (editor, file, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
8
|
+
const createFileUploaderElementByType = (type, uploaderOptions) => (editor, file, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
9
9
|
const { createElement, getBody, getHeaders, getUrl, uploader } = options;
|
|
10
10
|
const [mime] = file.type.split('/');
|
|
11
11
|
const createByMime = createElement[mime];
|
|
@@ -34,6 +34,18 @@ const createFileUploaderElementByType = (type) => (editor, file, options) => __a
|
|
|
34
34
|
throw xhr.response;
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
+
if (xhr.addEventListener) {
|
|
38
|
+
xhr.addEventListener('error', () => {
|
|
39
|
+
var _a;
|
|
40
|
+
const path = getPath();
|
|
41
|
+
(_a = uploaderOptions === null || uploaderOptions === void 0 ? void 0 : uploaderOptions.onError) === null || _a === void 0 ? void 0 : _a.call(uploaderOptions);
|
|
42
|
+
if (path) {
|
|
43
|
+
HistoryEditor.withoutSaving(editor, () => {
|
|
44
|
+
Transforms.removeNodes(editor, { at: path });
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
37
49
|
xhr.upload.onprogress = ({ loaded, total }) => onProgress((loaded * 100) / total);
|
|
38
50
|
if (!sent) {
|
|
39
51
|
sent = true;
|
|
@@ -99,8 +111,8 @@ function insertFileUploaderElement(editor, fileUploaderElement, currentBlockEntr
|
|
|
99
111
|
}
|
|
100
112
|
}
|
|
101
113
|
function createFileUploader(options = {}) {
|
|
102
|
-
const { type = FILE_UPLOADER_TYPE } = options;
|
|
103
|
-
const createFileUploaderElement = createFileUploaderElementByType(type);
|
|
114
|
+
const { type = FILE_UPLOADER_TYPE, onError } = options;
|
|
115
|
+
const createFileUploaderElement = createFileUploaderElementByType(type, { onError });
|
|
104
116
|
const removeUploaderPlaceholder = (editor) => {
|
|
105
117
|
const matches = Array.from(Editor.nodes(editor, {
|
|
106
118
|
at: [],
|
|
@@ -68,7 +68,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
68
68
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
69
69
|
};
|
|
70
70
|
|
|
71
|
-
const createFileUploaderElementByType = (type) => (editor, file, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
71
|
+
const createFileUploaderElementByType = (type, uploaderOptions) => (editor, file, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
72
72
|
const { createElement, getBody, getHeaders, getUrl, uploader } = options;
|
|
73
73
|
const [mime] = file.type.split('/');
|
|
74
74
|
const createByMime = createElement[mime];
|
|
@@ -97,6 +97,18 @@ const createFileUploaderElementByType = (type) => (editor, file, options) => __a
|
|
|
97
97
|
throw xhr.response;
|
|
98
98
|
}
|
|
99
99
|
};
|
|
100
|
+
if (xhr.addEventListener) {
|
|
101
|
+
xhr.addEventListener('error', () => {
|
|
102
|
+
var _a;
|
|
103
|
+
const path = getPath();
|
|
104
|
+
(_a = uploaderOptions === null || uploaderOptions === void 0 ? void 0 : uploaderOptions.onError) === null || _a === void 0 ? void 0 : _a.call(uploaderOptions);
|
|
105
|
+
if (path) {
|
|
106
|
+
core.HistoryEditor.withoutSaving(editor, () => {
|
|
107
|
+
core.Transforms.removeNodes(editor, { at: path });
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
100
112
|
xhr.upload.onprogress = ({ loaded, total }) => onProgress((loaded * 100) / total);
|
|
101
113
|
if (!sent) {
|
|
102
114
|
sent = true;
|
|
@@ -162,8 +174,8 @@ function insertFileUploaderElement(editor, fileUploaderElement, currentBlockEntr
|
|
|
162
174
|
}
|
|
163
175
|
}
|
|
164
176
|
function createFileUploader(options = {}) {
|
|
165
|
-
const { type = FILE_UPLOADER_TYPE } = options;
|
|
166
|
-
const createFileUploaderElement = createFileUploaderElementByType(type);
|
|
177
|
+
const { type = FILE_UPLOADER_TYPE, onError } = options;
|
|
178
|
+
const createFileUploaderElement = createFileUploaderElementByType(type, { onError });
|
|
167
179
|
const removeUploaderPlaceholder = (editor) => {
|
|
168
180
|
const matches = Array.from(core.Editor.nodes(editor, {
|
|
169
181
|
at: [],
|
|
@@ -31,6 +31,8 @@ export interface FileUploaderImplement {
|
|
|
31
31
|
open: (method: string, url: string | URL) => void;
|
|
32
32
|
setRequestHeader: (key: string, value: string) => void;
|
|
33
33
|
send(body?: Document | BodyInit | null): void;
|
|
34
|
+
addEventListener?: XMLHttpRequest['addEventListener'];
|
|
35
|
+
removeEventListener?: XMLHttpRequest['removeEventListener'];
|
|
34
36
|
readonly status: number;
|
|
35
37
|
readonly response: any;
|
|
36
38
|
readonly upload: FileUploaderUploadImplement;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quadrats/common",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.10",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "Rytass",
|
|
6
6
|
"homepage": "https://github.com/Quadrats/quadrats#readme",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"url": "https://github.com/Quadrats/quadrats/issues"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@quadrats/core": "^1.1.
|
|
22
|
-
"@quadrats/locales": "^1.
|
|
23
|
-
"@quadrats/utils": "^1.
|
|
21
|
+
"@quadrats/core": "^1.1.9",
|
|
22
|
+
"@quadrats/locales": "^1.1.9",
|
|
23
|
+
"@quadrats/utils": "^1.1.9"
|
|
24
24
|
}
|
|
25
25
|
}
|