inline-attacher 0.1.0 → 0.1.1

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
@@ -28,7 +28,7 @@ npm i inline-attacher
28
28
  - CodeMirror v6
29
29
 
30
30
  ```ts
31
- import { EditorView } from "codemirror";
31
+ import { EditorView } from "@codemirror/view";
32
32
  import { inlineAttachmentExtension } from "inline-attacher";
33
33
 
34
34
  const editor = new EditorView({
package/dist/core.d.ts CHANGED
@@ -4,9 +4,10 @@ export declare class InlineAttachment<TInstance> {
4
4
  editor: Editor<TInstance>;
5
5
  filename: string;
6
6
  lastValue: string;
7
+ private pendingPlaceholders;
7
8
  constructor(editor: Editor<TInstance>, options: Partial<InlineAttachmentOptions>);
8
9
  /** Uploads file */
9
- uploadFile(file: File): Promise<void>;
10
+ uploadFile(file: File, placeholder?: string): Promise<void>;
10
11
  /**
11
12
  * Returns if the given file is allowed to handle
12
13
  */
@@ -14,16 +15,18 @@ export declare class InlineAttachment<TInstance> {
14
15
  /**
15
16
  * Handles upload response
16
17
  */
17
- onFileUploadSucceed(response: Record<string, unknown>): void;
18
+ onFileUploadSucceed(response: Record<string, unknown>, placeholder?: string, filename?: string): void;
18
19
  /**
19
20
  * Called when a file has failed to upload
20
21
  */
21
- onFileUploadError(error: Error): void;
22
+ onFileUploadError(error: Error, placeholder?: string): void;
22
23
  /**
23
24
  * Called when a file has been inserted, either by drop or paste
24
25
  */
25
- onFileInserted(file: File): void;
26
+ onFileInserted(file: File): string | false;
26
27
  handleFiles(files: FileList): void;
28
+ private insertFile;
29
+ private replacePlaceholder;
27
30
  /**
28
31
  * Called when a paste event occurred
29
32
  */
@@ -1,5 +1,5 @@
1
1
  import { EditorView as w } from "@codemirror/view";
2
- const m = {
2
+ const E = {
3
3
  /**
4
4
  * URL where the file will be send
5
5
  */
@@ -87,7 +87,7 @@ const m = {
87
87
  onFileUploaded() {
88
88
  }
89
89
  };
90
- async function U({
90
+ async function P({
91
91
  url: n,
92
92
  ...t
93
93
  }) {
@@ -106,74 +106,82 @@ async function U({
106
106
  };
107
107
  }
108
108
  }
109
- function x(n) {
109
+ function U(n) {
110
110
  return typeof n == "function";
111
111
  }
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];
112
+ function y(n, t = "", e = "") {
113
+ const s = t.split(".");
114
+ let i = n;
115
+ for (const r of s)
116
+ if (i && typeof i == "object" && r in i)
117
+ i = i[r];
118
118
  else
119
119
  return e;
120
- return r !== void 0 ? r : e;
120
+ return i !== void 0 ? i : e;
121
121
  }
122
- class E {
123
- options = m;
122
+ function T(n, t, e, s = 0) {
123
+ let i = 0;
124
+ for (let r = 0; r <= s; r += 1) {
125
+ const o = n.indexOf(t, i);
126
+ if (o === -1) return n;
127
+ if (r === s)
128
+ return n.slice(0, o) + e + n.slice(o + t.length);
129
+ i = o + t.length;
130
+ }
131
+ return n;
132
+ }
133
+ class F {
134
+ options = E;
124
135
  editor;
125
136
  filename = "";
126
137
  lastValue = "";
138
+ pendingPlaceholders = [];
127
139
  constructor(t, e) {
128
- this.editor = t, this.options = { ...m, ...e };
140
+ this.editor = t, this.options = { ...E, ...e };
129
141
  }
130
- /** Uploads file */
131
- async uploadFile(t) {
142
+ async uploadFile(t, e) {
132
143
  const {
133
- defaultExtension: e,
144
+ defaultExtension: s,
134
145
  remoteFilename: i,
135
146
  uploadFieldName: r,
136
147
  extraParams: o,
137
148
  extraHeaders: a,
138
- uploadUrl: l,
149
+ uploadUrl: c,
139
150
  uploadMethod: d,
140
- beforeFileUpload: F,
141
- uploadHandler: h
142
- } = this.options, c = new FormData();
143
- let f = e;
144
- if (t.name) {
145
- const [s] = t.name.match(/\.(.+)$/) || [];
146
- s && (f = s);
147
- }
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) {
151
+ beforeFileUpload: u,
152
+ uploadHandler: f
153
+ } = this.options, p = new FormData(), x = s.replace(/^\./, ""), h = i?.(t) || t.name || `image-${Date.now()}.${x}`;
154
+ if (this.filename = h, p.append(r, t, h), Object.keys(o).forEach((l) => {
155
+ p.append(l, o[l]);
156
+ }), !u?.(p)) return;
157
+ if (f) {
153
158
  try {
154
- const s = await h({
159
+ const l = await f({
155
160
  file: t,
156
- filename: u,
157
- formData: c,
161
+ filename: h,
162
+ formData: p,
158
163
  options: this.options
159
164
  });
160
- this.onFileUploadSucceed(s);
161
- } catch (s) {
162
- this.onFileUploadError(s instanceof Error ? s : new Error(String(s)));
165
+ this.onFileUploadSucceed(l, e, h);
166
+ } catch (l) {
167
+ this.onFileUploadError(
168
+ l instanceof Error ? l : new Error(String(l)),
169
+ e
170
+ );
163
171
  }
164
172
  return;
165
173
  }
166
- const { ok: v, value: g } = await U({
167
- url: l,
174
+ const { ok: v, value: m } = await P({
175
+ url: c,
168
176
  method: d,
169
- body: c,
177
+ body: p,
170
178
  headers: a
171
179
  });
172
180
  if (!v) {
173
- this.onFileUploadError(g);
181
+ this.onFileUploadError(m, e);
174
182
  return;
175
183
  }
176
- this.onFileUploadSucceed(g);
184
+ this.onFileUploadSucceed(m, e, h);
177
185
  }
178
186
  /**
179
187
  * Returns if the given file is allowed to handle
@@ -182,34 +190,55 @@ class E {
182
190
  const { allowedTypes: e } = this.options;
183
191
  return e.includes("*") || e.includes(t.type);
184
192
  }
185
- /**
186
- * Handles upload response
187
- */
188
- onFileUploadSucceed(t) {
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);
193
+ onFileUploadSucceed(t, e, s = this.filename) {
194
+ const { onFileUploadSucceed: i, urlText: r, responseUrlKey: o } = this.options;
195
+ if (!i?.(t)) return;
196
+ const a = typeof e == "string" ? e : e?.text || this.lastValue;
197
+ if (!a) return;
198
+ const c = y(t, o) || "unknown URL", d = /!\[({[^}]+})]\(([^)]+)\)/, u = U(r) ? r(c, t) : r.replace(r.match(d)[1], s).replace(r.match(d)[2], c), f = this.replacePlaceholder(e, a, u);
199
+ this.editor.setValue(f), this.options.onFileUploaded?.(c);
193
200
  }
194
- /**
195
- * Called when a file has failed to upload
196
- */
197
- onFileUploadError(t) {
198
- if (!this.options.onFileUploadError?.(t) || !this.lastValue) return;
199
- const e = this.editor.getValue().replace(this.lastValue, this.options.errorText);
200
- this.editor.setValue(e);
201
+ onFileUploadError(t, e) {
202
+ if (!this.options.onFileUploadError?.(t)) return;
203
+ const s = typeof e == "string" ? e : e?.text || this.lastValue;
204
+ if (!s) return;
205
+ const i = this.replacePlaceholder(e, s, this.options.errorText);
206
+ this.editor.setValue(i);
201
207
  }
202
208
  /**
203
209
  * Called when a file has been inserted, either by drop or paste
204
210
  */
205
211
  onFileInserted(t) {
206
- this.options.onFileReceived?.(t) && (this.lastValue = this.options.progressText, this.editor.insertValue(this.lastValue));
212
+ const e = this.insertFile(t);
213
+ return e ? e.text : !1;
207
214
  }
208
215
  handleFiles(t) {
209
216
  Array.from(t).forEach((e) => {
210
- this.isFileAllowed(e) && (this.onFileInserted(e), this.uploadFile(e));
217
+ if (!this.isFileAllowed(e)) return;
218
+ const s = this.pendingPlaceholders.length, i = this.onFileInserted(e);
219
+ if (!i) return;
220
+ const r = this.pendingPlaceholders[s];
221
+ this.uploadFile(
222
+ e,
223
+ r?.text === i ? r : i
224
+ );
211
225
  });
212
226
  }
227
+ insertFile(t) {
228
+ if (!this.options.onFileReceived?.(t)) return !1;
229
+ const e = {
230
+ id: /* @__PURE__ */ Symbol("inline-attacher-placeholder"),
231
+ text: this.options.progressText
232
+ };
233
+ return this.pendingPlaceholders.push(e), this.lastValue = e.text, this.editor.insertValue(e.text), e;
234
+ }
235
+ replacePlaceholder(t, e, s) {
236
+ const i = this.editor.getValue();
237
+ if (!t || typeof t == "string")
238
+ return i.replace(e, s);
239
+ const r = this.pendingPlaceholders.findIndex(({ id: o }) => o === t.id);
240
+ return r === -1 ? i.replace(e, s) : (this.pendingPlaceholders.splice(r, 1), T(i, e, s, r));
241
+ }
213
242
  /**
214
243
  * Called when a paste event occurred
215
244
  */
@@ -223,13 +252,13 @@ class E {
223
252
  t.dataTransfer?.files.length && this.handleFiles(t.dataTransfer.files);
224
253
  }
225
254
  }
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();
255
+ function b(n, t) {
256
+ const e = n.scrollTop, s = n.selectionStart ?? 0, i = n.selectionEnd ?? s, r = Math.min(s, i), o = Math.max(s, i), { value: a } = n, c = a.slice(0, r), d = a.slice(o, a.length);
257
+ n.value = c + t + d;
258
+ const u = r + t.length;
259
+ n.selectionStart = u, n.selectionEnd = u, n.scrollTop = e, n.focus();
231
260
  }
232
- class T {
261
+ class D {
233
262
  instance;
234
263
  constructor(t) {
235
264
  this.instance = t;
@@ -238,7 +267,7 @@ class T {
238
267
  return this.instance.value;
239
268
  }
240
269
  insertValue(t) {
241
- D(this.instance, t), this.dispatchInputEvent();
270
+ b(this.instance, t), this.dispatchInputEvent();
242
271
  }
243
272
  setValue(t) {
244
273
  this.instance.value = t, this.dispatchInputEvent();
@@ -250,9 +279,9 @@ class T {
250
279
  }));
251
280
  }
252
281
  }
253
- class b extends E {
282
+ class I extends F {
254
283
  constructor(t, e = {}) {
255
- super(new T(t), e);
284
+ super(new D(t), e);
256
285
  }
257
286
  bind() {
258
287
  this.editor.instance.addEventListener(
@@ -273,10 +302,10 @@ class b extends E {
273
302
  );
274
303
  }
275
304
  }
276
- function P(...n) {
277
- return new b(...n).bind();
305
+ function V(...n) {
306
+ return new I(...n).bind();
278
307
  }
279
- class y {
308
+ class A {
280
309
  instance;
281
310
  constructor(t) {
282
311
  this.instance = t;
@@ -298,9 +327,9 @@ class y {
298
327
  }), this.instance.dispatch({ selection: { anchor: e } });
299
328
  }
300
329
  }
301
- class p extends E {
330
+ class g extends F {
302
331
  constructor(t, e = {}) {
303
- super(new y(t), e);
332
+ super(new A(t), e);
304
333
  }
305
334
  bind() {
306
335
  this.editor.instance.dom.addEventListener(
@@ -321,16 +350,16 @@ class p extends E {
321
350
  );
322
351
  }
323
352
  }
324
- function I(...n) {
325
- return new p(...n).bind();
353
+ function k(...n) {
354
+ return new g(...n).bind();
326
355
  }
327
- function S(n = {}) {
356
+ function L(n = {}) {
328
357
  return w.domEventHandlers({
329
358
  paste: (t, e) => {
330
- new p(e, n).onPaste(t);
359
+ new g(e, n).onPaste(t);
331
360
  },
332
361
  drop: (t, e) => {
333
- t.stopPropagation(), t.preventDefault(), new p(e, n).onDrop(t);
362
+ t.stopPropagation(), t.preventDefault(), new g(e, n).onDrop(t);
334
363
  },
335
364
  dragenter: (t) => {
336
365
  t.stopPropagation(), t.preventDefault();
@@ -341,11 +370,11 @@ function S(n = {}) {
341
370
  });
342
371
  }
343
372
  export {
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
373
+ g as CodeMirrorInlineAttachmentAdapter,
374
+ E as DEFAULT_OPTIONS,
375
+ F as InlineAttachment,
376
+ I as InputInlineAttachmentAdapter,
377
+ V as attach,
378
+ k as attachCodeMirror,
379
+ L as inlineAttachmentExtension
351
380
  };
@@ -1 +1 @@
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"})}));
1
+ (function(s,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("@codemirror/view")):typeof define=="function"&&define.amd?define(["exports","@codemirror/view"],h):(s=typeof globalThis<"u"?globalThis:s||self,h(s.inlineAttacher={},s.CodemirrorView))})(this,(function(s,h){"use strict";const F={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 P({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 y(n,t="",e=""){const o=t.split(".");let i=n;for(const r of o)if(i&&typeof i=="object"&&r in i)i=i[r];else return e;return i!==void 0?i:e}function A(n,t,e,o=0){let i=0;for(let r=0;r<=o;r+=1){const a=n.indexOf(t,i);if(a===-1)return n;if(r===o)return n.slice(0,a)+e+n.slice(a+t.length);i=a+t.length}return n}class v{options=F;editor;filename="";lastValue="";pendingPlaceholders=[];constructor(t,e){this.editor=t,this.options={...F,...e}}async uploadFile(t,e){const{defaultExtension:o,remoteFilename:i,uploadFieldName:r,extraParams:a,extraHeaders:l,uploadUrl:d,uploadMethod:u,beforeFileUpload:p,uploadHandler:E}=this.options,f=new FormData,M=o.replace(/^\./,""),g=i?.(t)||t.name||`image-${Date.now()}.${M}`;if(this.filename=g,f.append(r,t,g),Object.keys(a).forEach(c=>{f.append(c,a[c])}),!p?.(f))return;if(E){try{const c=await E({file:t,filename:g,formData:f,options:this.options});this.onFileUploadSucceed(c,e,g)}catch(c){this.onFileUploadError(c instanceof Error?c:new Error(String(c)),e)}return}const{ok:L,value:w}=await P({url:d,method:u,body:f,headers:l});if(!L){this.onFileUploadError(w,e);return}this.onFileUploadSucceed(w,e,g)}isFileAllowed(t){const{allowedTypes:e}=this.options;return e.includes("*")||e.includes(t.type)}onFileUploadSucceed(t,e,o=this.filename){const{onFileUploadSucceed:i,urlText:r,responseUrlKey:a}=this.options;if(!i?.(t))return;const l=typeof e=="string"?e:e?.text||this.lastValue;if(!l)return;const d=y(t,a)||"unknown URL",u=/!\[({[^}]+})]\(([^)]+)\)/,p=T(r)?r(d,t):r.replace(r.match(u)[1],o).replace(r.match(u)[2],d),E=this.replacePlaceholder(e,l,p);this.editor.setValue(E),this.options.onFileUploaded?.(d)}onFileUploadError(t,e){if(!this.options.onFileUploadError?.(t))return;const o=typeof e=="string"?e:e?.text||this.lastValue;if(!o)return;const i=this.replacePlaceholder(e,o,this.options.errorText);this.editor.setValue(i)}onFileInserted(t){const e=this.insertFile(t);return e?e.text:!1}handleFiles(t){Array.from(t).forEach(e=>{if(!this.isFileAllowed(e))return;const o=this.pendingPlaceholders.length,i=this.onFileInserted(e);if(!i)return;const r=this.pendingPlaceholders[o];this.uploadFile(e,r?.text===i?r:i)})}insertFile(t){if(!this.options.onFileReceived?.(t))return!1;const e={id:Symbol("inline-attacher-placeholder"),text:this.options.progressText};return this.pendingPlaceholders.push(e),this.lastValue=e.text,this.editor.insertValue(e.text),e}replacePlaceholder(t,e,o){const i=this.editor.getValue();if(!t||typeof t=="string")return i.replace(e,o);const r=this.pendingPlaceholders.findIndex(({id:a})=>a===t.id);return r===-1?i.replace(e,o):(this.pendingPlaceholders.splice(r,1),A(i,e,o,r))}onPaste(t){t.clipboardData?.files.length&&this.handleFiles(t.clipboardData.files)}onDrop(t){t.dataTransfer?.files.length&&this.handleFiles(t.dataTransfer.files)}}function U(n,t){const e=n.scrollTop,o=n.selectionStart??0,i=n.selectionEnd??o,r=Math.min(o,i),a=Math.max(o,i),{value:l}=n,d=l.slice(0,r),u=l.slice(a,l.length);n.value=d+t+u;const p=r+t.length;n.selectionStart=p,n.selectionEnd=p,n.scrollTop=e,n.focus()}class I{instance;constructor(t){this.instance=t}getValue(){return this.instance.value}insertValue(t){U(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 x extends v{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 x(...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 m extends v{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 S(...n){return new m(...n).bind()}function V(n={}){return h.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()}})}s.CodeMirrorInlineAttachmentAdapter=m,s.DEFAULT_OPTIONS=F,s.InlineAttachment=v,s.InputInlineAttachmentAdapter=x,s.attach=b,s.attachCodeMirror=S,s.inlineAttachmentExtension=V,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inline-attacher",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "📎 A modern port from Inline Attachment",
5
5
  "type": "module",
6
6
  "files": [
@@ -25,8 +25,8 @@
25
25
  "playground:deploy": "pnpm -C playground run deploy",
26
26
  "lint": "eslint --fix --ext .ts src",
27
27
  "prepublishOnly": "pnpm build",
28
- "release": "pnpx standard-version && git push --follow-tags && pnpm publish",
29
- "size-check": "pnpx vite-bundle-visualizer"
28
+ "release": "npx standard-version && git push --follow-tags && npm publish",
29
+ "size-check": "npx vite-bundle-visualizer"
30
30
  },
31
31
  "keywords": [
32
32
  "inline-attachment",
@@ -56,6 +56,7 @@
56
56
  },
57
57
  "devDependencies": {
58
58
  "@codemirror/state": "^6.7.0",
59
+ "@codemirror/view": "^6.43.4",
59
60
  "@types/node": "^22.20.0",
60
61
  "@typescript-eslint/eslint-plugin": "^6.21.0",
61
62
  "@typescript-eslint/parser": "^6.21.0",
@@ -68,8 +69,5 @@
68
69
  "typescript": "^6.0.2",
69
70
  "vite": "^7.3.0",
70
71
  "vitest": "^4.0.16"
71
- },
72
- "dependencies": {
73
- "@codemirror/view": "^6.43.4"
74
72
  }
75
73
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};