@quadrats/common 1.1.9 → 1.1.11

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) => FileUploader<Editor>['createFileUploaderElement'];
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];
@@ -21,6 +21,7 @@ const createFileUploaderElementByType = (type) => (editor, file, options) => __a
21
21
  type,
22
22
  register: (getPath, onProgress) => {
23
23
  xhr.onload = () => {
24
+ var _a;
24
25
  if (xhr.status < 400) {
25
26
  const path = getPath();
26
27
  if (path) {
@@ -31,9 +32,27 @@ const createFileUploaderElementByType = (type) => (editor, file, options) => __a
31
32
  }
32
33
  }
33
34
  else {
34
- throw xhr.response;
35
+ const path = getPath();
36
+ (_a = uploaderOptions === null || uploaderOptions === void 0 ? void 0 : uploaderOptions.onError) === null || _a === void 0 ? void 0 : _a.call(uploaderOptions);
37
+ if (path) {
38
+ HistoryEditor.withoutSaving(editor, () => {
39
+ Transforms.removeNodes(editor, { at: path });
40
+ });
41
+ }
35
42
  }
36
43
  };
44
+ if (xhr.addEventListener) {
45
+ xhr.addEventListener('error', () => {
46
+ var _a;
47
+ const path = getPath();
48
+ (_a = uploaderOptions === null || uploaderOptions === void 0 ? void 0 : uploaderOptions.onError) === null || _a === void 0 ? void 0 : _a.call(uploaderOptions);
49
+ if (path) {
50
+ HistoryEditor.withoutSaving(editor, () => {
51
+ Transforms.removeNodes(editor, { at: path });
52
+ });
53
+ }
54
+ });
55
+ }
37
56
  xhr.upload.onprogress = ({ loaded, total }) => onProgress((loaded * 100) / total);
38
57
  if (!sent) {
39
58
  sent = true;
@@ -99,8 +118,8 @@ function insertFileUploaderElement(editor, fileUploaderElement, currentBlockEntr
99
118
  }
100
119
  }
101
120
  function createFileUploader(options = {}) {
102
- const { type = FILE_UPLOADER_TYPE } = options;
103
- const createFileUploaderElement = createFileUploaderElementByType(type);
121
+ const { type = FILE_UPLOADER_TYPE, onError } = options;
122
+ const createFileUploaderElement = createFileUploaderElementByType(type, { onError });
104
123
  const removeUploaderPlaceholder = (editor) => {
105
124
  const matches = Array.from(Editor.nodes(editor, {
106
125
  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];
@@ -84,6 +84,7 @@ const createFileUploaderElementByType = (type) => (editor, file, options) => __a
84
84
  type,
85
85
  register: (getPath, onProgress) => {
86
86
  xhr.onload = () => {
87
+ var _a;
87
88
  if (xhr.status < 400) {
88
89
  const path = getPath();
89
90
  if (path) {
@@ -94,9 +95,27 @@ const createFileUploaderElementByType = (type) => (editor, file, options) => __a
94
95
  }
95
96
  }
96
97
  else {
97
- throw xhr.response;
98
+ const path = getPath();
99
+ (_a = uploaderOptions === null || uploaderOptions === void 0 ? void 0 : uploaderOptions.onError) === null || _a === void 0 ? void 0 : _a.call(uploaderOptions);
100
+ if (path) {
101
+ core.HistoryEditor.withoutSaving(editor, () => {
102
+ core.Transforms.removeNodes(editor, { at: path });
103
+ });
104
+ }
98
105
  }
99
106
  };
107
+ if (xhr.addEventListener) {
108
+ xhr.addEventListener('error', () => {
109
+ var _a;
110
+ const path = getPath();
111
+ (_a = uploaderOptions === null || uploaderOptions === void 0 ? void 0 : uploaderOptions.onError) === null || _a === void 0 ? void 0 : _a.call(uploaderOptions);
112
+ if (path) {
113
+ core.HistoryEditor.withoutSaving(editor, () => {
114
+ core.Transforms.removeNodes(editor, { at: path });
115
+ });
116
+ }
117
+ });
118
+ }
100
119
  xhr.upload.onprogress = ({ loaded, total }) => onProgress((loaded * 100) / total);
101
120
  if (!sent) {
102
121
  sent = true;
@@ -162,8 +181,8 @@ function insertFileUploaderElement(editor, fileUploaderElement, currentBlockEntr
162
181
  }
163
182
  }
164
183
  function createFileUploader(options = {}) {
165
- const { type = FILE_UPLOADER_TYPE } = options;
166
- const createFileUploaderElement = createFileUploaderElementByType(type);
184
+ const { type = FILE_UPLOADER_TYPE, onError } = options;
185
+ const createFileUploaderElement = createFileUploaderElementByType(type, { onError });
167
186
  const removeUploaderPlaceholder = (editor) => {
168
187
  const matches = Array.from(core.Editor.nodes(editor, {
169
188
  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.9",
3
+ "version": "1.1.11",
4
4
  "description": "",
5
5
  "author": "Rytass",
6
6
  "homepage": "https://github.com/Quadrats/quadrats#readme",