inline-attacher 0.0.7
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/LICENSE +21 -0
- package/README.md +38 -0
- package/dist/adapters/codemirror.d.ts +10 -0
- package/dist/adapters/index.d.ts +2 -0
- package/dist/adapters/input.d.ts +9 -0
- package/dist/constants.d.ts +4 -0
- package/dist/core.d.ts +36 -0
- package/dist/index.d.ts +4 -0
- package/dist/inline-attacher.js +359 -0
- package/dist/inline-attacher.umd.cjs +1 -0
- package/dist/types.d.ts +29 -0
- package/dist/utils.d.ts +12 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 - present Michael Wang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# 📎 Inline Attachment Next (WIP)
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/inline-attacher)
|
|
4
|
+
[](https://www.npmjs.com/package/inline-attacher)
|
|
5
|
+
[](https://github.com/EastSun5566/inline-attachment/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
> A modern port of [Inline Attachment](https://github.com/Rovak/InlineAttachment)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm i inline-attacher
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
- Input / Textarea
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { attach } from "inline-attacher";
|
|
21
|
+
|
|
22
|
+
const textarea = document.querySelector("textarea");
|
|
23
|
+
attach(textarea, { uploadUrl: "https://example.com/upload" });
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- CodeMirror v6
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { EditorView } from "codemirror";
|
|
30
|
+
import { inlineAttachmentExtension } from "inline-attacher";
|
|
31
|
+
|
|
32
|
+
const editor = new EditorView({
|
|
33
|
+
extensions: [
|
|
34
|
+
inlineAttachmentExtension({ uploadUrl: "https://example.com/upload" }),
|
|
35
|
+
],
|
|
36
|
+
parent: document.body,
|
|
37
|
+
});
|
|
38
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EditorView } from '@codemirror/view';
|
|
2
|
+
import type { Extension } from '@codemirror/state';
|
|
3
|
+
import { InlineAttachmentOptions } from '../types';
|
|
4
|
+
import { InlineAttachment } from '../core';
|
|
5
|
+
export declare class CodeMirrorInlineAttachmentAdapter<TInstance extends EditorView> extends InlineAttachment<TInstance> {
|
|
6
|
+
constructor(editorView: TInstance, options?: Partial<InlineAttachmentOptions>);
|
|
7
|
+
bind(): void;
|
|
8
|
+
}
|
|
9
|
+
export declare function attachCodeMirror(...params: [EditorView, Partial<InlineAttachmentOptions>?]): void;
|
|
10
|
+
export declare function inlineAttachmentExtension(options?: Partial<InlineAttachmentOptions>): Extension;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InlineAttachment } from '../core';
|
|
2
|
+
import type { InlineAttachmentOptions } from '../types';
|
|
3
|
+
type InputElement = HTMLInputElement | HTMLTextAreaElement;
|
|
4
|
+
export declare class InputInlineAttachmentAdapter<TInstance extends InputElement> extends InlineAttachment<TInstance> {
|
|
5
|
+
constructor(element: TInstance, options?: Partial<InlineAttachmentOptions>);
|
|
6
|
+
bind(): void;
|
|
7
|
+
}
|
|
8
|
+
export declare function attach(...params: [InputElement, Partial<InlineAttachmentOptions>?]): void;
|
|
9
|
+
export {};
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Editor, InlineAttachmentOptions } from './types';
|
|
2
|
+
export declare class InlineAttachment<TInstance> {
|
|
3
|
+
options: InlineAttachmentOptions;
|
|
4
|
+
editor: Editor<TInstance>;
|
|
5
|
+
filename: string;
|
|
6
|
+
lastValue: string;
|
|
7
|
+
constructor(editor: Editor<TInstance>, options: Partial<InlineAttachmentOptions>);
|
|
8
|
+
/** Uploads file */
|
|
9
|
+
uploadFile(file: File): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Returns if the given file is allowed to handle
|
|
12
|
+
*/
|
|
13
|
+
isFileAllowed(file: File): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Handles upload response
|
|
16
|
+
*/
|
|
17
|
+
onFileUploadSucceed(response: Record<string, unknown>): void;
|
|
18
|
+
/**
|
|
19
|
+
* Called when a file has failed to upload
|
|
20
|
+
*/
|
|
21
|
+
onFileUploadError(error: Error): void;
|
|
22
|
+
/**
|
|
23
|
+
* Called when a file has been inserted, either by drop or paste
|
|
24
|
+
*/
|
|
25
|
+
onFileInserted(file: File): void;
|
|
26
|
+
handleFiles(files: FileList): void;
|
|
27
|
+
/**
|
|
28
|
+
* Called when a paste event occurred
|
|
29
|
+
*/
|
|
30
|
+
onPaste(event: ClipboardEvent): void;
|
|
31
|
+
/**
|
|
32
|
+
* Called when a drop event occurred
|
|
33
|
+
*/
|
|
34
|
+
onDrop(event: DragEvent): void;
|
|
35
|
+
}
|
|
36
|
+
export default InlineAttachment;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
var w = Object.defineProperty;
|
|
2
|
+
var x = (n, t, e) => t in n ? w(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var o = (n, t, e) => (x(n, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
import { EditorView as D } from "@codemirror/view";
|
|
5
|
+
const v = {
|
|
6
|
+
/**
|
|
7
|
+
* URL where the file will be send
|
|
8
|
+
*/
|
|
9
|
+
uploadUrl: "/upload",
|
|
10
|
+
/**
|
|
11
|
+
* Which method will be used to send the file to the upload URL
|
|
12
|
+
*/
|
|
13
|
+
uploadMethod: "POST",
|
|
14
|
+
/**
|
|
15
|
+
* Name in which the file will be placed
|
|
16
|
+
*/
|
|
17
|
+
uploadFieldName: "file",
|
|
18
|
+
/**
|
|
19
|
+
* Extension which will be used when a file extension could not
|
|
20
|
+
* be detected
|
|
21
|
+
*/
|
|
22
|
+
defaultExtension: "png",
|
|
23
|
+
/**
|
|
24
|
+
* JSON field which refers to the uploaded file URL
|
|
25
|
+
*/
|
|
26
|
+
responseUrlKey: "url",
|
|
27
|
+
/**
|
|
28
|
+
* Allowed MIME types
|
|
29
|
+
*/
|
|
30
|
+
allowedTypes: [
|
|
31
|
+
"image/jpeg",
|
|
32
|
+
"image/png",
|
|
33
|
+
"image/jpg",
|
|
34
|
+
"image/gif"
|
|
35
|
+
],
|
|
36
|
+
/**
|
|
37
|
+
* Text which will be inserted when dropping or pasting a file.
|
|
38
|
+
* Acts as a placeholder which will be replaced when the file is done with uploading
|
|
39
|
+
*/
|
|
40
|
+
progressText: "![Uploading file...]()",
|
|
41
|
+
/**
|
|
42
|
+
* When a file has successfully been uploaded the progressText
|
|
43
|
+
* will be replaced by the urlText, the {url} tag will be replaced response url
|
|
44
|
+
*/
|
|
45
|
+
urlText: "",
|
|
46
|
+
/**
|
|
47
|
+
* Text which will be used when uploading has failed
|
|
48
|
+
*/
|
|
49
|
+
errorText: "Error uploading file",
|
|
50
|
+
/**
|
|
51
|
+
* Extra parameters which will be send when uploading a file
|
|
52
|
+
*/
|
|
53
|
+
extraParams: {},
|
|
54
|
+
/**
|
|
55
|
+
* Extra headers which will be send when uploading a file
|
|
56
|
+
*/
|
|
57
|
+
extraHeaders: {},
|
|
58
|
+
/**
|
|
59
|
+
* Before the file is send
|
|
60
|
+
*/
|
|
61
|
+
beforeFileUpload() {
|
|
62
|
+
return !0;
|
|
63
|
+
},
|
|
64
|
+
/**
|
|
65
|
+
* Triggers when a file is dropped or pasted
|
|
66
|
+
*/
|
|
67
|
+
onFileReceived() {
|
|
68
|
+
return !0;
|
|
69
|
+
},
|
|
70
|
+
/**
|
|
71
|
+
* Custom upload handler
|
|
72
|
+
*
|
|
73
|
+
* @return {Boolean} when false is returned it will prevent default upload behavior
|
|
74
|
+
*/
|
|
75
|
+
onFileUploadSucceed() {
|
|
76
|
+
return !0;
|
|
77
|
+
},
|
|
78
|
+
/**
|
|
79
|
+
* Custom error handler. Runs after removing the placeholder text and before the alert().
|
|
80
|
+
* Return false from this function to prevent the alert dialog.
|
|
81
|
+
*
|
|
82
|
+
* @return {Boolean} when false is returned it will prevent default error behavior
|
|
83
|
+
*/
|
|
84
|
+
onFileUploadError() {
|
|
85
|
+
return !0;
|
|
86
|
+
},
|
|
87
|
+
/**
|
|
88
|
+
* When a file has successfully been uploaded
|
|
89
|
+
*/
|
|
90
|
+
onFileUploaded() {
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
async function P({
|
|
94
|
+
url: n,
|
|
95
|
+
...t
|
|
96
|
+
}) {
|
|
97
|
+
try {
|
|
98
|
+
const e = await fetch(n, t);
|
|
99
|
+
if (!e.ok)
|
|
100
|
+
throw new Error(e.statusText);
|
|
101
|
+
return {
|
|
102
|
+
ok: !0,
|
|
103
|
+
value: await e.json()
|
|
104
|
+
};
|
|
105
|
+
} catch (e) {
|
|
106
|
+
return {
|
|
107
|
+
ok: !1,
|
|
108
|
+
value: e
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function V(n) {
|
|
113
|
+
return typeof n == "function";
|
|
114
|
+
}
|
|
115
|
+
function T(n, t = "", e = "") {
|
|
116
|
+
const r = t.split(".");
|
|
117
|
+
let a = n;
|
|
118
|
+
for (const s of r)
|
|
119
|
+
if (a && typeof a == "object" && s in a)
|
|
120
|
+
a = a[s];
|
|
121
|
+
else
|
|
122
|
+
return e;
|
|
123
|
+
return a !== void 0 ? a : e;
|
|
124
|
+
}
|
|
125
|
+
class E {
|
|
126
|
+
constructor(t, e) {
|
|
127
|
+
o(this, "options", v);
|
|
128
|
+
o(this, "editor");
|
|
129
|
+
o(this, "filename", "");
|
|
130
|
+
o(this, "lastValue", "");
|
|
131
|
+
this.editor = t, this.options = { ...v, ...e };
|
|
132
|
+
}
|
|
133
|
+
/** Uploads file */
|
|
134
|
+
async uploadFile(t) {
|
|
135
|
+
const {
|
|
136
|
+
defaultExtension: e,
|
|
137
|
+
remoteFilename: r,
|
|
138
|
+
uploadFieldName: a,
|
|
139
|
+
extraParams: s,
|
|
140
|
+
extraHeaders: l,
|
|
141
|
+
uploadUrl: d,
|
|
142
|
+
uploadMethod: p,
|
|
143
|
+
beforeFileUpload: c
|
|
144
|
+
} = this.options, i = new FormData();
|
|
145
|
+
let f = e;
|
|
146
|
+
if (t.name) {
|
|
147
|
+
const [u] = t.name.match(/\.(.+)$/) || [];
|
|
148
|
+
u && (f = u);
|
|
149
|
+
}
|
|
150
|
+
const g = (r == null ? void 0 : r(t)) || `image-${Date.now()}.${f}`;
|
|
151
|
+
if (this.filename = g, i.append(a, t, g), Object.keys(s).forEach((u) => {
|
|
152
|
+
i.append(u, s[u]);
|
|
153
|
+
}), !(c != null && c(i)))
|
|
154
|
+
return;
|
|
155
|
+
const { ok: F, value: m } = await P({
|
|
156
|
+
url: d,
|
|
157
|
+
method: p,
|
|
158
|
+
body: i,
|
|
159
|
+
headers: l
|
|
160
|
+
});
|
|
161
|
+
if (!F) {
|
|
162
|
+
this.onFileUploadError(m);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
this.onFileUploadSucceed(m);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Returns if the given file is allowed to handle
|
|
169
|
+
*/
|
|
170
|
+
isFileAllowed(t) {
|
|
171
|
+
const { allowedTypes: e } = this.options;
|
|
172
|
+
return e.includes("*") || e.includes(t.type);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Handles upload response
|
|
176
|
+
*/
|
|
177
|
+
onFileUploadSucceed(t) {
|
|
178
|
+
var c, i;
|
|
179
|
+
const { onFileUploadSucceed: e, urlText: r, responseUrlKey: a } = this.options;
|
|
180
|
+
if (!(e != null && e(t)) || !this.lastValue)
|
|
181
|
+
return;
|
|
182
|
+
const s = T(t, a) || "unknown URL";
|
|
183
|
+
if (!s)
|
|
184
|
+
return;
|
|
185
|
+
const l = /!\[({[^}]+})]\(([^)]+)\)/, d = V(r) ? r(s, t) : r.replace(r.match(l)[1], this.filename).replace(r.match(l)[2], s), p = this.editor.getValue().replace(this.lastValue, d);
|
|
186
|
+
this.editor.setValue(p), (i = (c = this.options).onFileUploaded) == null || i.call(c, s);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Called when a file has failed to upload
|
|
190
|
+
*/
|
|
191
|
+
onFileUploadError(t) {
|
|
192
|
+
var r, a;
|
|
193
|
+
if (!((a = (r = this.options).onFileUploadError) != null && a.call(r, t)) || !this.lastValue)
|
|
194
|
+
return;
|
|
195
|
+
const e = this.editor.getValue().replace(this.lastValue, this.options.errorText);
|
|
196
|
+
this.editor.setValue(e);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Called when a file has been inserted, either by drop or paste
|
|
200
|
+
*/
|
|
201
|
+
onFileInserted(t) {
|
|
202
|
+
var e, r;
|
|
203
|
+
(r = (e = this.options).onFileReceived) != null && r.call(e, t) && (this.lastValue = this.options.progressText, this.editor.insertValue(this.lastValue));
|
|
204
|
+
}
|
|
205
|
+
handleFiles(t) {
|
|
206
|
+
Array.from(t).forEach((e) => {
|
|
207
|
+
this.isFileAllowed(e) && (this.onFileInserted(e), this.uploadFile(e));
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Called when a paste event occurred
|
|
212
|
+
*/
|
|
213
|
+
onPaste(t) {
|
|
214
|
+
var e;
|
|
215
|
+
t.preventDefault(), (e = t.clipboardData) != null && e.files.length && this.handleFiles(t.clipboardData.files);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Called when a drop event occurred
|
|
219
|
+
*/
|
|
220
|
+
onDrop(t) {
|
|
221
|
+
var e;
|
|
222
|
+
t.preventDefault(), (e = t.dataTransfer) != null && e.files.length && this.handleFiles(t.dataTransfer.files);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function A(n, t) {
|
|
226
|
+
const e = n.scrollTop;
|
|
227
|
+
let r = 0, a = !1, s;
|
|
228
|
+
n.selectionStart || n.selectionStart === 0 ? a = "ff" : document.selection && (a = "ie"), a === "ie" ? (n.focus(), s = document.selection.createRange(), s.moveStart("character", -n.value.length), r = s.text.length) : a === "ff" && (r = n.selectionStart || 0);
|
|
229
|
+
const l = n.value.substring(0, r), d = n.value.substring(r, n.value.length);
|
|
230
|
+
n.value = l + t + d, r += t.length, a === "ie" ? (n.focus(), s = document.selection.createRange(), s.moveStart("character", -n.value.length), s.moveStart("character", r), s.moveEnd("character", 0), s.select()) : a === "ff" && (n.selectionStart = r, n.selectionEnd = r, n.focus()), n.scrollTop = e;
|
|
231
|
+
}
|
|
232
|
+
class U {
|
|
233
|
+
constructor(t) {
|
|
234
|
+
o(this, "instance");
|
|
235
|
+
this.instance = t;
|
|
236
|
+
}
|
|
237
|
+
getValue() {
|
|
238
|
+
return this.instance.value;
|
|
239
|
+
}
|
|
240
|
+
insertValue(t) {
|
|
241
|
+
A(this.instance, t);
|
|
242
|
+
}
|
|
243
|
+
setValue(t) {
|
|
244
|
+
this.instance.value = t;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
class y extends E {
|
|
248
|
+
constructor(t, e = {}) {
|
|
249
|
+
super(new U(t), e);
|
|
250
|
+
}
|
|
251
|
+
bind() {
|
|
252
|
+
this.editor.instance.addEventListener(
|
|
253
|
+
"paste",
|
|
254
|
+
(t) => {
|
|
255
|
+
this.onPaste(t);
|
|
256
|
+
},
|
|
257
|
+
!1
|
|
258
|
+
), this.editor.instance.addEventListener(
|
|
259
|
+
"drop",
|
|
260
|
+
(t) => {
|
|
261
|
+
t.stopPropagation(), t.preventDefault(), this.onDrop(t);
|
|
262
|
+
},
|
|
263
|
+
!1
|
|
264
|
+
), this.editor.instance.addEventListener(
|
|
265
|
+
"dragenter",
|
|
266
|
+
(t) => {
|
|
267
|
+
t.stopPropagation(), t.preventDefault();
|
|
268
|
+
}
|
|
269
|
+
), this.editor.instance.addEventListener(
|
|
270
|
+
"dragover",
|
|
271
|
+
(t) => {
|
|
272
|
+
t.stopPropagation(), t.preventDefault();
|
|
273
|
+
}
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function k(...n) {
|
|
278
|
+
return new y(...n).bind();
|
|
279
|
+
}
|
|
280
|
+
class b {
|
|
281
|
+
constructor(t) {
|
|
282
|
+
o(this, "instance");
|
|
283
|
+
this.instance = t;
|
|
284
|
+
}
|
|
285
|
+
getValue() {
|
|
286
|
+
return this.instance.state.doc.toString();
|
|
287
|
+
}
|
|
288
|
+
insertValue(t) {
|
|
289
|
+
this.instance.dispatch(this.instance.state.replaceSelection(t));
|
|
290
|
+
}
|
|
291
|
+
setValue(t) {
|
|
292
|
+
const e = this.instance.state.selection.main.head;
|
|
293
|
+
this.instance.dispatch({
|
|
294
|
+
changes: {
|
|
295
|
+
from: 0,
|
|
296
|
+
to: this.instance.state.doc.length,
|
|
297
|
+
insert: t
|
|
298
|
+
}
|
|
299
|
+
}), this.instance.dispatch({ selection: { anchor: e } });
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
class h extends E {
|
|
303
|
+
constructor(t, e = {}) {
|
|
304
|
+
super(new b(t), e);
|
|
305
|
+
}
|
|
306
|
+
bind() {
|
|
307
|
+
this.editor.instance.dom.addEventListener(
|
|
308
|
+
"paste",
|
|
309
|
+
(t) => {
|
|
310
|
+
this.onPaste(t);
|
|
311
|
+
},
|
|
312
|
+
!1
|
|
313
|
+
), this.editor.instance.dom.addEventListener(
|
|
314
|
+
"drop",
|
|
315
|
+
(t) => {
|
|
316
|
+
t.stopPropagation(), t.preventDefault(), this.onDrop(t);
|
|
317
|
+
},
|
|
318
|
+
!1
|
|
319
|
+
), this.editor.instance.dom.addEventListener(
|
|
320
|
+
"dragenter",
|
|
321
|
+
(t) => {
|
|
322
|
+
t.stopPropagation(), t.preventDefault();
|
|
323
|
+
}
|
|
324
|
+
), this.editor.instance.dom.addEventListener(
|
|
325
|
+
"dragover",
|
|
326
|
+
(t) => {
|
|
327
|
+
t.stopPropagation(), t.preventDefault();
|
|
328
|
+
}
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
function I(...n) {
|
|
333
|
+
return new h(...n).bind();
|
|
334
|
+
}
|
|
335
|
+
function M(n = {}) {
|
|
336
|
+
return D.domEventHandlers({
|
|
337
|
+
paste: (t, e) => {
|
|
338
|
+
new h(e, n).onPaste(t);
|
|
339
|
+
},
|
|
340
|
+
drop: (t, e) => {
|
|
341
|
+
t.stopPropagation(), t.preventDefault(), new h(e, n).onDrop(t);
|
|
342
|
+
},
|
|
343
|
+
dragenter: (t) => {
|
|
344
|
+
t.stopPropagation(), t.preventDefault();
|
|
345
|
+
},
|
|
346
|
+
dragover: (t) => {
|
|
347
|
+
t.stopPropagation(), t.preventDefault();
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
export {
|
|
352
|
+
h as CodeMirrorInlineAttachmentAdapter,
|
|
353
|
+
v as DEFAULT_OPTIONS,
|
|
354
|
+
E as InlineAttachment,
|
|
355
|
+
y as InputInlineAttachmentAdapter,
|
|
356
|
+
k as attach,
|
|
357
|
+
I as attachCodeMirror,
|
|
358
|
+
M as inlineAttachmentExtension
|
|
359
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(a,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("@codemirror/view")):typeof define=="function"&&define.amd?define(["exports","@codemirror/view"],s):(a=typeof globalThis<"u"?globalThis:a||self,s(a.inlineAttacher={},a.CodemirrorView))})(this,function(a,s){"use strict";var L=Object.defineProperty;var M=(a,s,l)=>s in a?L(a,s,{enumerable:!0,configurable:!0,writable:!0,value:l}):a[s]=l;var d=(a,s,l)=>(M(a,typeof s!="symbol"?s+"":s,l),l);const l={uploadUrl:"/upload",uploadMethod:"POST",uploadFieldName:"file",defaultExtension:"png",responseUrlKey:"url",allowedTypes:["image/jpeg","image/png","image/jpg","image/gif"],progressText:"![Uploading file...]()",urlText:"",errorText:"Error uploading file",extraParams:{},extraHeaders:{},beforeFileUpload(){return!0},onFileReceived(){return!0},onFileUploadSucceed(){return!0},onFileUploadError(){return!0},onFileUploaded(){}};async function D({url:n,...t}){try{const e=await fetch(n,t);if(!e.ok)throw new Error(e.statusText);return{ok:!0,value:await e.json()}}catch(e){return{ok:!1,value:e}}}function T(n){return typeof n=="function"}function P(n,t="",e=""){const i=t.split(".");let r=n;for(const o of i)if(r&&typeof r=="object"&&o in r)r=r[o];else return e;return r!==void 0?r:e}class m{constructor(t,e){d(this,"options",l);d(this,"editor");d(this,"filename","");d(this,"lastValue","");this.editor=t,this.options={...l,...e}}async uploadFile(t){const{defaultExtension:e,remoteFilename:i,uploadFieldName:r,extraParams:o,extraHeaders:u,uploadUrl:p,uploadMethod:v,beforeFileUpload:h}=this.options,c=new FormData;let w=e;if(t.name){const[f]=t.name.match(/\.(.+)$/)||[];f&&(w=f)}const F=(i==null?void 0:i(t))||`image-${Date.now()}.${w}`;if(this.filename=F,c.append(r,t,F),Object.keys(o).forEach(f=>{c.append(f,o[f])}),!(h!=null&&h(c)))return;const{ok:I,value:A}=await D({url:p,method:v,body:c,headers:u});if(!I){this.onFileUploadError(A);return}this.onFileUploadSucceed(A)}isFileAllowed(t){const{allowedTypes:e}=this.options;return e.includes("*")||e.includes(t.type)}onFileUploadSucceed(t){var h,c;const{onFileUploadSucceed:e,urlText:i,responseUrlKey:r}=this.options;if(!(e!=null&&e(t))||!this.lastValue)return;const o=P(t,r)||"unknown URL";if(!o)return;const u=/!\[({[^}]+})]\(([^)]+)\)/,p=T(i)?i(o,t):i.replace(i.match(u)[1],this.filename).replace(i.match(u)[2],o),v=this.editor.getValue().replace(this.lastValue,p);this.editor.setValue(v),(c=(h=this.options).onFileUploaded)==null||c.call(h,o)}onFileUploadError(t){var i,r;if(!((r=(i=this.options).onFileUploadError)!=null&&r.call(i,t))||!this.lastValue)return;const e=this.editor.getValue().replace(this.lastValue,this.options.errorText);this.editor.setValue(e)}onFileInserted(t){var e,i;(i=(e=this.options).onFileReceived)!=null&&i.call(e,t)&&(this.lastValue=this.options.progressText,this.editor.insertValue(this.lastValue))}handleFiles(t){Array.from(t).forEach(e=>{this.isFileAllowed(e)&&(this.onFileInserted(e),this.uploadFile(e))})}onPaste(t){var e;t.preventDefault(),(e=t.clipboardData)!=null&&e.files.length&&this.handleFiles(t.clipboardData.files)}onDrop(t){var e;t.preventDefault(),(e=t.dataTransfer)!=null&&e.files.length&&this.handleFiles(t.dataTransfer.files)}}function V(n,t){const e=n.scrollTop;let i=0,r=!1,o;n.selectionStart||n.selectionStart===0?r="ff":document.selection&&(r="ie"),r==="ie"?(n.focus(),o=document.selection.createRange(),o.moveStart("character",-n.value.length),i=o.text.length):r==="ff"&&(i=n.selectionStart||0);const u=n.value.substring(0,i),p=n.value.substring(i,n.value.length);n.value=u+t+p,i+=t.length,r==="ie"?(n.focus(),o=document.selection.createRange(),o.moveStart("character",-n.value.length),o.moveStart("character",i),o.moveEnd("character",0),o.select()):r==="ff"&&(n.selectionStart=i,n.selectionEnd=i,n.focus()),n.scrollTop=e}class y{constructor(t){d(this,"instance");this.instance=t}getValue(){return this.instance.value}insertValue(t){V(this.instance,t)}setValue(t){this.instance.value=t}}class E extends m{constructor(t,e={}){super(new y(t),e)}bind(){this.editor.instance.addEventListener("paste",t=>{this.onPaste(t)},!1),this.editor.instance.addEventListener("drop",t=>{t.stopPropagation(),t.preventDefault(),this.onDrop(t)},!1),this.editor.instance.addEventListener("dragenter",t=>{t.stopPropagation(),t.preventDefault()}),this.editor.instance.addEventListener("dragover",t=>{t.stopPropagation(),t.preventDefault()})}}function x(...n){return new E(...n).bind()}class S{constructor(t){d(this,"instance");this.instance=t}getValue(){return this.instance.state.doc.toString()}insertValue(t){this.instance.dispatch(this.instance.state.replaceSelection(t))}setValue(t){const e=this.instance.state.selection.main.head;this.instance.dispatch({changes:{from:0,to:this.instance.state.doc.length,insert:t}}),this.instance.dispatch({selection:{anchor:e}})}}class g extends m{constructor(t,e={}){super(new S(t),e)}bind(){this.editor.instance.dom.addEventListener("paste",t=>{this.onPaste(t)},!1),this.editor.instance.dom.addEventListener("drop",t=>{t.stopPropagation(),t.preventDefault(),this.onDrop(t)},!1),this.editor.instance.dom.addEventListener("dragenter",t=>{t.stopPropagation(),t.preventDefault()}),this.editor.instance.dom.addEventListener("dragover",t=>{t.stopPropagation(),t.preventDefault()})}}function U(...n){return new g(...n).bind()}function b(n={}){return s.EditorView.domEventHandlers({paste:(t,e)=>{new g(e,n).onPaste(t)},drop:(t,e)=>{t.stopPropagation(),t.preventDefault(),new g(e,n).onDrop(t)},dragenter:t=>{t.stopPropagation(),t.preventDefault()},dragover:t=>{t.stopPropagation(),t.preventDefault()}})}a.CodeMirrorInlineAttachmentAdapter=g,a.DEFAULT_OPTIONS=l,a.InlineAttachment=m,a.InputInlineAttachmentAdapter=E,a.attach=x,a.attachCodeMirror=U,a.inlineAttachmentExtension=b,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface InlineAttachmentOptions {
|
|
2
|
+
uploadUrl: string;
|
|
3
|
+
uploadMethod: string;
|
|
4
|
+
uploadFieldName: string;
|
|
5
|
+
defaultExtension: string;
|
|
6
|
+
responseUrlKey: string;
|
|
7
|
+
allowedTypes: string[];
|
|
8
|
+
progressText: string;
|
|
9
|
+
urlText: string | ((url: string, result: unknown) => string);
|
|
10
|
+
errorText: string;
|
|
11
|
+
extraParams: {
|
|
12
|
+
[name: string]: any;
|
|
13
|
+
};
|
|
14
|
+
extraHeaders: {
|
|
15
|
+
[name: string]: any;
|
|
16
|
+
};
|
|
17
|
+
beforeFileUpload?: (formData: FormData) => boolean;
|
|
18
|
+
remoteFilename?: (file: File) => string;
|
|
19
|
+
onFileReceived?: (file: File) => boolean;
|
|
20
|
+
onFileUploadSucceed?: (response: Record<string, unknown>) => boolean;
|
|
21
|
+
onFileUploadError?: (error: Error) => boolean;
|
|
22
|
+
onFileUploaded?: (filename: string) => void;
|
|
23
|
+
}
|
|
24
|
+
export interface Editor<TInstance> {
|
|
25
|
+
instance: TInstance;
|
|
26
|
+
getValue(): string;
|
|
27
|
+
setValue(value: string): void;
|
|
28
|
+
insertValue(value: string): void;
|
|
29
|
+
}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface UploadOptions extends RequestInit {
|
|
2
|
+
url: string;
|
|
3
|
+
}
|
|
4
|
+
export type Response = Record<string, unknown>;
|
|
5
|
+
interface Result<TResponse = Response> {
|
|
6
|
+
ok: boolean;
|
|
7
|
+
value: TResponse | Error;
|
|
8
|
+
}
|
|
9
|
+
export declare function upload<TResponse = Response>({ url, ...restOptions }: UploadOptions): Promise<Result<TResponse>>;
|
|
10
|
+
export declare function isFunction(value: unknown): value is Function;
|
|
11
|
+
export declare function get(object: Record<string, unknown>, path?: string, defaultValue?: string): string | Record<string, unknown>;
|
|
12
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "inline-attacher",
|
|
3
|
+
"version": "0.0.7",
|
|
4
|
+
"description": "📎 A modern port from Inline Attachment",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/inline-attacher.umd.cjs",
|
|
10
|
+
"module": "./dist/inline-attacher.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/inline-attacher.js",
|
|
15
|
+
"require": "./dist/inline-attacher.umd.cjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"inline-attachment",
|
|
20
|
+
"attachment",
|
|
21
|
+
"upload",
|
|
22
|
+
"paste",
|
|
23
|
+
"drop",
|
|
24
|
+
"image",
|
|
25
|
+
"file"
|
|
26
|
+
],
|
|
27
|
+
"author": "Michael Wang 汪東陽 <michael19920327@gmail.com> (https://github.com/EastSun5566)",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/EastSun5566/inline-attachment"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/EastSun5566/inline-attachment/issues/new"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/EastSun5566/inline-attachment",
|
|
37
|
+
"packageManager": "pnpm@8.6.0",
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=16.0.0",
|
|
40
|
+
"pnpm": ">=7.0.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@codemirror/view": ">=6.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@codemirror/state": "^6.2.1",
|
|
47
|
+
"@types/node": "^20.5.0",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^6.4.0",
|
|
49
|
+
"@typescript-eslint/parser": "^6.4.0",
|
|
50
|
+
"eslint": "^8.47.0",
|
|
51
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
52
|
+
"eslint-config-airbnb-typescript": "^17.1.0",
|
|
53
|
+
"eslint-plugin-import": "^2.28.0",
|
|
54
|
+
"rimraf": "^5.0.1",
|
|
55
|
+
"typescript": "^5.1.6",
|
|
56
|
+
"vite": "^4.4.9"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@codemirror/view": "^6.16.0"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"dev": "vite",
|
|
63
|
+
"build": "rimraf dist && tsc && vite build",
|
|
64
|
+
"preview": "vite preview",
|
|
65
|
+
"playground:dev": "pnpm -C playground dev",
|
|
66
|
+
"lint": "eslint --fix --ext .ts src",
|
|
67
|
+
"release": "pnpx standard-version && git push --follow-tags && pnpm publish"
|
|
68
|
+
}
|
|
69
|
+
}
|