inline-attacher 0.0.8 → 0.1.0

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/README.md CHANGED
@@ -38,3 +38,27 @@ npm i inline-attacher
38
38
  parent: document.body,
39
39
  });
40
40
  ```
41
+
42
+ - Custom upload handler
43
+
44
+ ```ts
45
+ import { attach } from "inline-attacher";
46
+
47
+ const textarea = document.querySelector("textarea");
48
+
49
+ attach(textarea, {
50
+ async uploadHandler({ file, filename, formData }) {
51
+ const response = await myUploader.upload({
52
+ file,
53
+ filename,
54
+ formData,
55
+ });
56
+
57
+ return { url: response.publicUrl };
58
+ },
59
+ });
60
+ ```
61
+
62
+ The returned object is handled the same way as the built-in upload response:
63
+ `responseUrlKey` is used to read the uploaded file URL before replacing the
64
+ placeholder text.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1,8 +1,5 @@
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 V } from "@codemirror/view";
5
- const E = {
1
+ import { EditorView as w } from "@codemirror/view";
2
+ const m = {
6
3
  /**
7
4
  * URL where the file will be send
8
5
  */
@@ -90,7 +87,7 @@ const E = {
90
87
  onFileUploaded() {
91
88
  }
92
89
  };
93
- async function D({
90
+ async function U({
94
91
  url: n,
95
92
  ...t
96
93
  }) {
@@ -109,60 +106,74 @@ async function D({
109
106
  };
110
107
  }
111
108
  }
112
- function T(n) {
109
+ function x(n) {
113
110
  return typeof n == "function";
114
111
  }
115
- function A(n, t = "", e = "") {
116
- const s = t.split(".");
117
- let i = n;
118
- for (const r of s)
119
- if (i && typeof i == "object" && r in i)
120
- i = i[r];
112
+ function V(n, t = "", e = "") {
113
+ const i = t.split(".");
114
+ let r = n;
115
+ for (const o of i)
116
+ if (r && typeof r == "object" && o in r)
117
+ r = r[o];
121
118
  else
122
119
  return e;
123
- return i !== void 0 ? i : e;
120
+ return r !== void 0 ? r : e;
124
121
  }
125
- class v {
122
+ class E {
123
+ options = m;
124
+ editor;
125
+ filename = "";
126
+ lastValue = "";
126
127
  constructor(t, e) {
127
- o(this, "options", E);
128
- o(this, "editor");
129
- o(this, "filename", "");
130
- o(this, "lastValue", "");
131
- this.editor = t, this.options = { ...E, ...e };
128
+ this.editor = t, this.options = { ...m, ...e };
132
129
  }
133
130
  /** Uploads file */
134
131
  async uploadFile(t) {
135
132
  const {
136
133
  defaultExtension: e,
137
- remoteFilename: s,
138
- uploadFieldName: i,
139
- extraParams: r,
140
- extraHeaders: l,
141
- uploadUrl: c,
142
- uploadMethod: p,
143
- beforeFileUpload: d
144
- } = this.options, a = new FormData();
134
+ remoteFilename: i,
135
+ uploadFieldName: r,
136
+ extraParams: o,
137
+ extraHeaders: a,
138
+ uploadUrl: l,
139
+ uploadMethod: d,
140
+ beforeFileUpload: F,
141
+ uploadHandler: h
142
+ } = this.options, c = new FormData();
145
143
  let f = e;
146
144
  if (t.name) {
147
- const [u] = t.name.match(/\.(.+)$/) || [];
148
- u && (f = u);
145
+ const [s] = t.name.match(/\.(.+)$/) || [];
146
+ s && (f = s);
149
147
  }
150
- const g = (s == null ? void 0 : s(t)) || `image-${Date.now()}.${f}`;
151
- if (this.filename = g, a.append(i, t, g), Object.keys(r).forEach((u) => {
152
- a.append(u, r[u]);
153
- }), !(d != null && d(a)))
148
+ const u = i?.(t) || `image-${Date.now()}.${f}`;
149
+ if (this.filename = u, c.append(r, t, u), Object.keys(o).forEach((s) => {
150
+ c.append(s, o[s]);
151
+ }), !F?.(c)) return;
152
+ if (h) {
153
+ try {
154
+ const s = await h({
155
+ file: t,
156
+ filename: u,
157
+ formData: c,
158
+ options: this.options
159
+ });
160
+ this.onFileUploadSucceed(s);
161
+ } catch (s) {
162
+ this.onFileUploadError(s instanceof Error ? s : new Error(String(s)));
163
+ }
154
164
  return;
155
- const { ok: F, value: m } = await D({
156
- url: c,
157
- method: p,
158
- body: a,
159
- headers: l
165
+ }
166
+ const { ok: v, value: g } = await U({
167
+ url: l,
168
+ method: d,
169
+ body: c,
170
+ headers: a
160
171
  });
161
- if (!F) {
162
- this.onFileUploadError(m);
172
+ if (!v) {
173
+ this.onFileUploadError(g);
163
174
  return;
164
175
  }
165
- this.onFileUploadSucceed(m);
176
+ this.onFileUploadSucceed(g);
166
177
  }
167
178
  /**
168
179
  * Returns if the given file is allowed to handle
@@ -175,23 +186,16 @@ class v {
175
186
  * Handles upload response
176
187
  */
177
188
  onFileUploadSucceed(t) {
178
- var d, a;
179
- const { onFileUploadSucceed: e, urlText: s, responseUrlKey: i } = this.options;
180
- if (!(e != null && e(t)) || !this.lastValue)
181
- return;
182
- const r = A(t, i) || "unknown URL";
183
- if (!r)
184
- return;
185
- const l = /!\[({[^}]+})]\(([^)]+)\)/, c = T(s) ? s(r, t) : s.replace(s.match(l)[1], this.filename).replace(s.match(l)[2], r), p = this.editor.getValue().replace(this.lastValue, c);
186
- this.editor.setValue(p), (a = (d = this.options).onFileUploaded) == null || a.call(d, r);
189
+ const { onFileUploadSucceed: e, urlText: i, responseUrlKey: r } = this.options;
190
+ if (!e?.(t) || !this.lastValue) return;
191
+ const o = V(t, r) || "unknown URL", a = /!\[({[^}]+})]\(([^)]+)\)/, l = x(i) ? i(o, t) : i.replace(i.match(a)[1], this.filename).replace(i.match(a)[2], o), d = this.editor.getValue().replace(this.lastValue, l);
192
+ this.editor.setValue(d), this.options.onFileUploaded?.(o);
187
193
  }
188
194
  /**
189
195
  * Called when a file has failed to upload
190
196
  */
191
197
  onFileUploadError(t) {
192
- var s, i;
193
- if (!((i = (s = this.options).onFileUploadError) != null && i.call(s, t)) || !this.lastValue)
194
- return;
198
+ if (!this.options.onFileUploadError?.(t) || !this.lastValue) return;
195
199
  const e = this.editor.getValue().replace(this.lastValue, this.options.errorText);
196
200
  this.editor.setValue(e);
197
201
  }
@@ -199,8 +203,7 @@ class v {
199
203
  * Called when a file has been inserted, either by drop or paste
200
204
  */
201
205
  onFileInserted(t) {
202
- var e, s;
203
- (s = (e = this.options).onFileReceived) != null && s.call(e, t) && (this.lastValue = this.options.progressText, this.editor.insertValue(this.lastValue));
206
+ this.options.onFileReceived?.(t) && (this.lastValue = this.options.progressText, this.editor.insertValue(this.lastValue));
204
207
  }
205
208
  handleFiles(t) {
206
209
  Array.from(t).forEach((e) => {
@@ -211,33 +214,31 @@ class v {
211
214
  * Called when a paste event occurred
212
215
  */
213
216
  onPaste(t) {
214
- var e;
215
- (e = t.clipboardData) != null && e.files.length && this.handleFiles(t.clipboardData.files);
217
+ t.clipboardData?.files.length && this.handleFiles(t.clipboardData.files);
216
218
  }
217
219
  /**
218
220
  * Called when a drop event occurred
219
221
  */
220
222
  onDrop(t) {
221
- var e;
222
- (e = t.dataTransfer) != null && e.files.length && this.handleFiles(t.dataTransfer.files);
223
+ t.dataTransfer?.files.length && this.handleFiles(t.dataTransfer.files);
223
224
  }
224
225
  }
225
- function U(n, t) {
226
- const e = n.scrollTop, s = n.selectionStart || 0, { value: i } = n, r = i.slice(0, s), l = i.slice(s, i.length);
227
- n.value = r + t + l;
228
- const c = s + t.length;
229
- n.selectionStart = c, n.selectionEnd = c, n.scrollTop = e, n.focus();
226
+ function D(n, t) {
227
+ const e = n.scrollTop, i = n.selectionStart || 0, { value: r } = n, o = r.slice(0, i), a = r.slice(i, r.length);
228
+ n.value = o + t + a;
229
+ const l = i + t.length;
230
+ n.selectionStart = l, n.selectionEnd = l, n.scrollTop = e, n.focus();
230
231
  }
231
- class b {
232
+ class T {
233
+ instance;
232
234
  constructor(t) {
233
- o(this, "instance");
234
235
  this.instance = t;
235
236
  }
236
237
  getValue() {
237
238
  return this.instance.value;
238
239
  }
239
240
  insertValue(t) {
240
- U(this.instance, t), this.dispatchInputEvent();
241
+ D(this.instance, t), this.dispatchInputEvent();
241
242
  }
242
243
  setValue(t) {
243
244
  this.instance.value = t, this.dispatchInputEvent();
@@ -249,9 +250,9 @@ class b {
249
250
  }));
250
251
  }
251
252
  }
252
- class y extends v {
253
+ class b extends E {
253
254
  constructor(t, e = {}) {
254
- super(new b(t), e);
255
+ super(new T(t), e);
255
256
  }
256
257
  bind() {
257
258
  this.editor.instance.addEventListener(
@@ -272,12 +273,12 @@ class y extends v {
272
273
  );
273
274
  }
274
275
  }
275
- function L(...n) {
276
- return new y(...n).bind();
276
+ function P(...n) {
277
+ return new b(...n).bind();
277
278
  }
278
- class P {
279
+ class y {
280
+ instance;
279
281
  constructor(t) {
280
- o(this, "instance");
281
282
  this.instance = t;
282
283
  }
283
284
  getValue() {
@@ -297,9 +298,9 @@ class P {
297
298
  }), this.instance.dispatch({ selection: { anchor: e } });
298
299
  }
299
300
  }
300
- class h extends v {
301
+ class p extends E {
301
302
  constructor(t, e = {}) {
302
- super(new P(t), e);
303
+ super(new y(t), e);
303
304
  }
304
305
  bind() {
305
306
  this.editor.instance.dom.addEventListener(
@@ -320,16 +321,16 @@ class h extends v {
320
321
  );
321
322
  }
322
323
  }
323
- function S(...n) {
324
- return new h(...n).bind();
324
+ function I(...n) {
325
+ return new p(...n).bind();
325
326
  }
326
- function M(n = {}) {
327
- return V.domEventHandlers({
327
+ function S(n = {}) {
328
+ return w.domEventHandlers({
328
329
  paste: (t, e) => {
329
- new h(e, n).onPaste(t);
330
+ new p(e, n).onPaste(t);
330
331
  },
331
332
  drop: (t, e) => {
332
- t.stopPropagation(), t.preventDefault(), new h(e, n).onDrop(t);
333
+ t.stopPropagation(), t.preventDefault(), new p(e, n).onDrop(t);
333
334
  },
334
335
  dragenter: (t) => {
335
336
  t.stopPropagation(), t.preventDefault();
@@ -340,11 +341,11 @@ function M(n = {}) {
340
341
  });
341
342
  }
342
343
  export {
343
- h as CodeMirrorInlineAttachmentAdapter,
344
- E as DEFAULT_OPTIONS,
345
- v as InlineAttachment,
346
- y as InputInlineAttachmentAdapter,
347
- L as attach,
348
- S as attachCodeMirror,
349
- M as inlineAttachmentExtension
344
+ p as CodeMirrorInlineAttachmentAdapter,
345
+ m as DEFAULT_OPTIONS,
346
+ E as InlineAttachment,
347
+ b as InputInlineAttachmentAdapter,
348
+ P as attach,
349
+ I as attachCodeMirror,
350
+ S as inlineAttachmentExtension
350
351
  };
@@ -1 +1 @@
1
- (function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("@codemirror/view")):typeof define=="function"&&define.amd?define(["exports","@codemirror/view"],o):(r=typeof globalThis<"u"?globalThis:r||self,o(r.inlineAttacher={},r.CodemirrorView))})(this,function(r,o){"use strict";var L=Object.defineProperty;var M=(r,o,l)=>o in r?L(r,o,{enumerable:!0,configurable:!0,writable:!0,value:l}):r[o]=l;var d=(r,o,l)=>(M(r,typeof o!="symbol"?o+"":o,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:"![{alt}]({url})",errorText:"Error uploading file",extraParams:{},extraHeaders:{},beforeFileUpload(){return!0},onFileReceived(){return!0},onFileUploadSucceed(){return!0},onFileUploadError(){return!0},onFileUploaded(){}};async function T({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 V(n){return typeof n=="function"}function y(n,t="",e=""){const i=t.split(".");let s=n;for(const a of i)if(s&&typeof s=="object"&&a in s)s=s[a];else return e;return s!==void 0?s:e}class g{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:s,extraParams:a,extraHeaders:u,uploadUrl:h,uploadMethod:E,beforeFileUpload:p}=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(s,t,F),Object.keys(a).forEach(f=>{c.append(f,a[f])}),!(p!=null&&p(c)))return;const{ok:S,value:A}=await T({url:h,method:E,body:c,headers:u});if(!S){this.onFileUploadError(A);return}this.onFileUploadSucceed(A)}isFileAllowed(t){const{allowedTypes:e}=this.options;return e.includes("*")||e.includes(t.type)}onFileUploadSucceed(t){var p,c;const{onFileUploadSucceed:e,urlText:i,responseUrlKey:s}=this.options;if(!(e!=null&&e(t))||!this.lastValue)return;const a=y(t,s)||"unknown URL";if(!a)return;const u=/!\[({[^}]+})]\(([^)]+)\)/,h=V(i)?i(a,t):i.replace(i.match(u)[1],this.filename).replace(i.match(u)[2],a),E=this.editor.getValue().replace(this.lastValue,h);this.editor.setValue(E),(c=(p=this.options).onFileUploaded)==null||c.call(p,a)}onFileUploadError(t){var i,s;if(!((s=(i=this.options).onFileUploadError)!=null&&s.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;(e=t.clipboardData)!=null&&e.files.length&&this.handleFiles(t.clipboardData.files)}onDrop(t){var e;(e=t.dataTransfer)!=null&&e.files.length&&this.handleFiles(t.dataTransfer.files)}}function D(n,t){const e=n.scrollTop,i=n.selectionStart||0,{value:s}=n,a=s.slice(0,i),u=s.slice(i,s.length);n.value=a+t+u;const h=i+t.length;n.selectionStart=h,n.selectionEnd=h,n.scrollTop=e,n.focus()}class I{constructor(t){d(this,"instance");this.instance=t}getValue(){return this.instance.value}insertValue(t){D(this.instance,t),this.dispatchInputEvent()}setValue(t){this.instance.value=t,this.dispatchInputEvent()}dispatchInputEvent(){this.instance.dispatchEvent(new InputEvent("input",{bubbles:!0,cancelable:!0}))}}class v extends g{constructor(t,e={}){super(new I(t),e)}bind(){this.editor.instance.addEventListener("paste",t=>{this.onPaste(t)}),this.editor.instance.addEventListener("drop",t=>{t.preventDefault(),this.onDrop(t)}),this.editor.instance.addEventListener("dragover",t=>{t.preventDefault()})}}function b(...n){return new v(...n).bind()}class x{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 m extends g{constructor(t,e={}){super(new x(t),e)}bind(){this.editor.instance.dom.addEventListener("paste",t=>{this.onPaste(t)}),this.editor.instance.dom.addEventListener("drop",t=>{t.preventDefault(),this.onDrop(t)}),this.editor.instance.dom.addEventListener("dragover",t=>{t.preventDefault()})}}function P(...n){return new m(...n).bind()}function U(n={}){return o.EditorView.domEventHandlers({paste:(t,e)=>{new m(e,n).onPaste(t)},drop:(t,e)=>{t.stopPropagation(),t.preventDefault(),new m(e,n).onDrop(t)},dragenter:t=>{t.stopPropagation(),t.preventDefault()},dragover:t=>{t.stopPropagation(),t.preventDefault()}})}r.CodeMirrorInlineAttachmentAdapter=m,r.DEFAULT_OPTIONS=l,r.InlineAttachment=g,r.InputInlineAttachmentAdapter=v,r.attach=b,r.attachCodeMirror=P,r.inlineAttachmentExtension=U,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
1
+ (function(o,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("@codemirror/view")):typeof define=="function"&&define.amd?define(["exports","@codemirror/view"],d):(o=typeof globalThis<"u"?globalThis:o||self,d(o.inlineAttacher={},o.CodemirrorView))})(this,(function(o,d){"use strict";const h={uploadUrl:"/upload",uploadMethod:"POST",uploadFieldName:"file",defaultExtension:"png",responseUrlKey:"url",allowedTypes:["image/jpeg","image/png","image/jpg","image/gif"],progressText:"![Uploading file...]()",urlText:"![{alt}]({url})",errorText:"Error uploading file",extraParams:{},extraHeaders:{},beforeFileUpload(){return!0},onFileReceived(){return!0},onFileUploadSucceed(){return!0},onFileUploadError(){return!0},onFileUploaded(){}};async function A({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 U(n,t="",e=""){const i=t.split(".");let r=n;for(const s of i)if(r&&typeof r=="object"&&s in r)r=r[s];else return e;return r!==void 0?r:e}class f{options=h;editor;filename="";lastValue="";constructor(t,e){this.editor=t,this.options={...h,...e}}async uploadFile(t){const{defaultExtension:e,remoteFilename:i,uploadFieldName:r,extraParams:s,extraHeaders:l,uploadUrl:c,uploadMethod:m,beforeFileUpload:P,uploadHandler:F}=this.options,u=new FormData;let v=e;if(t.name){const[a]=t.name.match(/\.(.+)$/)||[];a&&(v=a)}const g=i?.(t)||`image-${Date.now()}.${v}`;if(this.filename=g,u.append(r,t,g),Object.keys(s).forEach(a=>{u.append(a,s[a])}),!P?.(u))return;if(F){try{const a=await F({file:t,filename:g,formData:u,options:this.options});this.onFileUploadSucceed(a)}catch(a){this.onFileUploadError(a instanceof Error?a:new Error(String(a)))}return}const{ok:S,value:w}=await A({url:c,method:m,body:u,headers:l});if(!S){this.onFileUploadError(w);return}this.onFileUploadSucceed(w)}isFileAllowed(t){const{allowedTypes:e}=this.options;return e.includes("*")||e.includes(t.type)}onFileUploadSucceed(t){const{onFileUploadSucceed:e,urlText:i,responseUrlKey:r}=this.options;if(!e?.(t)||!this.lastValue)return;const s=U(t,r)||"unknown URL",l=/!\[({[^}]+})]\(([^)]+)\)/,c=T(i)?i(s,t):i.replace(i.match(l)[1],this.filename).replace(i.match(l)[2],s),m=this.editor.getValue().replace(this.lastValue,c);this.editor.setValue(m),this.options.onFileUploaded?.(s)}onFileUploadError(t){if(!this.options.onFileUploadError?.(t)||!this.lastValue)return;const e=this.editor.getValue().replace(this.lastValue,this.options.errorText);this.editor.setValue(e)}onFileInserted(t){this.options.onFileReceived?.(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){t.clipboardData?.files.length&&this.handleFiles(t.clipboardData.files)}onDrop(t){t.dataTransfer?.files.length&&this.handleFiles(t.dataTransfer.files)}}function V(n,t){const e=n.scrollTop,i=n.selectionStart||0,{value:r}=n,s=r.slice(0,i),l=r.slice(i,r.length);n.value=s+t+l;const c=i+t.length;n.selectionStart=c,n.selectionEnd=c,n.scrollTop=e,n.focus()}class y{instance;constructor(t){this.instance=t}getValue(){return this.instance.value}insertValue(t){V(this.instance,t),this.dispatchInputEvent()}setValue(t){this.instance.value=t,this.dispatchInputEvent()}dispatchInputEvent(){this.instance.dispatchEvent(new InputEvent("input",{bubbles:!0,cancelable:!0}))}}class E extends f{constructor(t,e={}){super(new y(t),e)}bind(){this.editor.instance.addEventListener("paste",t=>{this.onPaste(t)}),this.editor.instance.addEventListener("drop",t=>{t.preventDefault(),this.onDrop(t)}),this.editor.instance.addEventListener("dragover",t=>{t.preventDefault()})}}function b(...n){return new E(...n).bind()}class D{instance;constructor(t){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 p extends f{constructor(t,e={}){super(new D(t),e)}bind(){this.editor.instance.dom.addEventListener("paste",t=>{this.onPaste(t)}),this.editor.instance.dom.addEventListener("drop",t=>{t.preventDefault(),this.onDrop(t)}),this.editor.instance.dom.addEventListener("dragover",t=>{t.preventDefault()})}}function I(...n){return new p(...n).bind()}function x(n={}){return d.EditorView.domEventHandlers({paste:(t,e)=>{new p(e,n).onPaste(t)},drop:(t,e)=>{t.stopPropagation(),t.preventDefault(),new p(e,n).onDrop(t)},dragenter:t=>{t.stopPropagation(),t.preventDefault()},dragover:t=>{t.stopPropagation(),t.preventDefault()}})}o.CodeMirrorInlineAttachmentAdapter=p,o.DEFAULT_OPTIONS=h,o.InlineAttachment=f,o.InputInlineAttachmentAdapter=E,o.attach=b,o.attachCodeMirror=I,o.inlineAttachmentExtension=x,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
package/dist/types.d.ts CHANGED
@@ -1,3 +1,9 @@
1
+ export interface UploadHandlerContext {
2
+ file: File;
3
+ filename: string;
4
+ formData: FormData;
5
+ options: InlineAttachmentOptions;
6
+ }
1
7
  export interface InlineAttachmentOptions {
2
8
  uploadUrl: string;
3
9
  uploadMethod: string;
@@ -15,6 +21,7 @@ export interface InlineAttachmentOptions {
15
21
  [name: string]: any;
16
22
  };
17
23
  beforeFileUpload?: (formData: FormData) => boolean;
24
+ uploadHandler?: (context: UploadHandlerContext) => Promise<Record<string, unknown>> | Record<string, unknown>;
18
25
  remoteFilename?: (file: File) => string;
19
26
  onFileReceived?: (file: File) => boolean;
20
27
  onFileUploadSucceed?: (response: Record<string, unknown>) => boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inline-attacher",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "description": "📎 A modern port from Inline Attachment",
5
5
  "type": "module",
6
6
  "files": [
@@ -11,10 +11,23 @@
11
11
  "types": "./dist/index.d.ts",
12
12
  "exports": {
13
13
  ".": {
14
+ "types": "./dist/index.d.ts",
14
15
  "import": "./dist/inline-attacher.js",
15
16
  "require": "./dist/inline-attacher.umd.cjs"
16
17
  }
17
18
  },
19
+ "scripts": {
20
+ "dev": "vite",
21
+ "build": "rimraf dist && tsc && vite build",
22
+ "preview": "vite preview",
23
+ "test": "vitest run",
24
+ "playground:dev": "pnpm -C playground dev",
25
+ "playground:deploy": "pnpm -C playground run deploy",
26
+ "lint": "eslint --fix --ext .ts src",
27
+ "prepublishOnly": "pnpm build",
28
+ "release": "pnpx standard-version && git push --follow-tags && pnpm publish",
29
+ "size-check": "pnpx vite-bundle-visualizer"
30
+ },
18
31
  "keywords": [
19
32
  "inline-attachment",
20
33
  "attachment",
@@ -24,7 +37,7 @@
24
37
  "image",
25
38
  "file"
26
39
  ],
27
- "author": "Michael Wang 汪東陽 <michael19920327@gmail.com> (https://github.com/EastSun5566)",
40
+ "author": "Michael Wang <hi@eastsun.me> (https://github.com/EastSun5566)",
28
41
  "license": "MIT",
29
42
  "repository": {
30
43
  "type": "git",
@@ -34,36 +47,29 @@
34
47
  "url": "https://github.com/EastSun5566/inline-attachment/issues/new"
35
48
  },
36
49
  "homepage": "https://github.com/EastSun5566/inline-attachment",
50
+ "packageManager": "pnpm@10.34.4",
37
51
  "engines": {
38
- "node": ">=16.0.0"
52
+ "node": ">=22"
39
53
  },
40
54
  "peerDependencies": {
41
55
  "@codemirror/view": ">=6.0.0"
42
56
  },
43
57
  "devDependencies": {
44
- "@codemirror/state": "^6.4.1",
45
- "@types/node": "^20.16.11",
58
+ "@codemirror/state": "^6.7.0",
59
+ "@types/node": "^22.20.0",
46
60
  "@typescript-eslint/eslint-plugin": "^6.21.0",
47
61
  "@typescript-eslint/parser": "^6.21.0",
48
62
  "eslint": "^8.57.1",
49
63
  "eslint-config-airbnb-base": "^15.0.0",
50
64
  "eslint-config-airbnb-typescript": "^17.1.0",
51
65
  "eslint-plugin-import": "^2.31.0",
66
+ "happy-dom": "^20.0.2",
52
67
  "rimraf": "^5.0.10",
53
- "typescript": "^5.6.3",
54
- "vite": "^4.5.5"
68
+ "typescript": "^6.0.2",
69
+ "vite": "^7.3.0",
70
+ "vitest": "^4.0.16"
55
71
  },
56
72
  "dependencies": {
57
- "@codemirror/view": "^6.34.1"
58
- },
59
- "scripts": {
60
- "dev": "vite",
61
- "build": "rimraf dist && tsc && vite build",
62
- "preview": "vite preview",
63
- "playground:dev": "pnpm -C playground dev",
64
- "playground:deploy": "pnpm -C playground deploy",
65
- "lint": "eslint --fix --ext .ts src",
66
- "release": "pnpx standard-version && git push --follow-tags && pnpm publish && pnpm dlx jsr publish",
67
- "size-check": "pnpx vite-bundle-visualizer"
73
+ "@codemirror/view": "^6.43.4"
68
74
  }
69
- }
75
+ }