/
config.ts
145 lines (117 loc) · 3.08 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2024 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
/**
* @module modules/uploader
*/
import type {
IUploader,
IUploaderAnswer,
IUploaderData,
IUploaderOptions
} from 'jodit/types';
import { isArray } from 'jodit/core/helpers/checker/is-array';
import { isJoditObject } from 'jodit/core/helpers/checker/is-jodit-object';
import { Config } from 'jodit/config';
declare module 'jodit/config' {
interface Config {
/**
* Enable drag and drop file editor
*/
enableDragAndDropFileToEditor: boolean;
uploader: IUploaderOptions<IUploader>;
}
}
/**
* Module for processing download documents and images by Drag and Drop
* Drag and Drop files
*/
Config.prototype.enableDragAndDropFileToEditor = true;
Config.prototype.uploader = {
url: '',
insertImageAsBase64URI: false,
imagesExtensions: ['jpg', 'png', 'jpeg', 'gif'],
headers: null,
data: null,
filesVariableName(i: number): string {
return `files[${i}]`;
},
withCredentials: false,
pathVariableName: 'path',
format: 'json',
method: 'POST',
prepareData(this: IUploader, formData: FormData) {
return formData;
},
isSuccess(this: IUploader, resp: IUploaderAnswer): boolean {
return resp.success;
},
getMessage(this: IUploader, resp: IUploaderAnswer) {
return resp.data.messages !== undefined && isArray(resp.data.messages)
? resp.data.messages.join(' ')
: '';
},
/**
* @see [[IUploader.processFileName]]
*/
processFileName(
this: IUploader,
key: string,
file: File,
name: string
): [string, File, string] {
return [key, file, name];
},
process(this: IUploader, resp: IUploaderAnswer): IUploaderData {
return resp.data;
},
error(this: IUploader, e: Error) {
this.j.message.error(e.message, 4000);
},
getDisplayName(this: IUploader, baseurl: string, filename: string): string {
return baseurl + filename;
},
defaultHandlerSuccess(this: IUploader, resp: IUploaderData) {
const j = this.j || this;
if (!isJoditObject(j)) {
return;
}
if (resp.files && resp.files.length) {
resp.files.forEach((filename, index: number) => {
const [tagName, attr]: string[] =
resp.isImages && resp.isImages[index]
? ['img', 'src']
: ['a', 'href'];
const elm = j.createInside.element(tagName);
elm.setAttribute(attr, resp.baseurl + filename);
if (tagName === 'a') {
elm.textContent = j.o.uploader.getDisplayName.call(
this,
resp.baseurl,
filename
);
}
if (tagName === 'img') {
j.s.insertImage(
elm as HTMLImageElement,
null,
j.o.imageDefaultWidth
);
} else {
j.s.insertNode(elm);
}
});
}
},
defaultHandlerError(this: IUploader, e: Error) {
this.j.message.error(e.message);
},
contentType(this: IUploader, requestData: any) {
return (this.ow as any).FormData !== undefined &&
typeof requestData !== 'string'
? false
: 'application/x-www-form-urlencoded; charset=UTF-8';
}
} as IUploaderOptions<IUploader>;