1
1
import { imageInfo } from '../../utils/image.ts' ;
2
- import { replaceTextareaSelection } from '../../utils/dom.ts' ;
3
- import { isUrl } from '../../utils/url.ts' ;
4
2
import { textareaInsertText , triggerEditorContentChanged } from './EditorMarkdown.ts' ;
5
3
import {
6
4
DropzoneCustomEventRemovedFile ,
7
5
DropzoneCustomEventUploadDone ,
8
6
generateMarkdownLinkForAttachment ,
9
7
} from '../dropzone.ts' ;
8
+ import { subscribe } from '@github/paste-markdown' ;
10
9
import type CodeMirror from 'codemirror' ;
11
10
import type EasyMDE from 'easymde' ;
12
11
import type { DropzoneFile } from 'dropzone' ;
@@ -118,46 +117,20 @@ export function removeAttachmentLinksFromMarkdown(text: string, fileUuid: string
118
117
return text ;
119
118
}
120
119
121
- export function pasteAsMarkdownLink ( textarea : { value : string , selectionStart : number , selectionEnd : number } , pastedText : string ) : string | null {
122
- const { value, selectionStart, selectionEnd} = textarea ;
123
- const selectedText = value . substring ( selectionStart , selectionEnd ) ;
124
- const trimmedText = pastedText . trim ( ) ;
125
- const beforeSelection = value . substring ( 0 , selectionStart ) ;
126
- const afterSelection = value . substring ( selectionEnd ) ;
127
- const isInMarkdownLink = beforeSelection . endsWith ( '](' ) && afterSelection . startsWith ( ')' ) ;
128
- const asMarkdownLink = selectedText && isUrl ( trimmedText ) && ! isUrl ( selectedText ) && ! isInMarkdownLink ;
129
- return asMarkdownLink ? `[${ selectedText } ](${ trimmedText } )` : null ;
130
- }
131
-
132
- function handleClipboardText ( textarea : HTMLTextAreaElement , e : ClipboardEvent , pastedText : string , isShiftDown : boolean ) {
133
- // pasting with "shift" means "paste as original content" in most applications
134
- if ( isShiftDown ) return ; // let the browser handle it
135
-
136
- // when pasting links over selected text, turn it into [text](link)
137
- const pastedAsMarkdown = pasteAsMarkdownLink ( textarea , pastedText ) ;
138
- if ( pastedAsMarkdown ) {
139
- e . preventDefault ( ) ;
140
- replaceTextareaSelection ( textarea , pastedAsMarkdown ) ;
141
- }
142
- // else, let the browser handle it
143
- }
144
-
145
- // extract text and images from "paste" event
146
- function getPastedContent ( e : ClipboardEvent ) {
147
- const images = [ ] ;
120
+ function getPastedImages ( e : ClipboardEvent ) {
121
+ const images : Array < File > = [ ] ;
148
122
for ( const item of e . clipboardData ?. items ?? [ ] ) {
149
123
if ( item . type ?. startsWith ( 'image/' ) ) {
150
124
images . push ( item . getAsFile ( ) ) ;
151
125
}
152
126
}
153
- const text = e . clipboardData ?. getData ?.( 'text' ) ?? '' ;
154
- return { text, images} ;
127
+ return images ;
155
128
}
156
129
157
130
export function initEasyMDEPaste ( easyMDE : EasyMDE , dropzoneEl : HTMLElement ) {
158
131
const editor = new CodeMirrorEditor ( easyMDE . codemirror as any ) ;
159
132
easyMDE . codemirror . on ( 'paste' , ( _ , e ) => {
160
- const { images} = getPastedContent ( e ) ;
133
+ const images = getPastedImages ( e ) ;
161
134
if ( ! images . length ) return ;
162
135
handleUploadFiles ( editor , dropzoneEl , images , e ) ;
163
136
} ) ;
@@ -173,19 +146,11 @@ export function initEasyMDEPaste(easyMDE: EasyMDE, dropzoneEl: HTMLElement) {
173
146
}
174
147
175
148
export function initTextareaEvents ( textarea : HTMLTextAreaElement , dropzoneEl : HTMLElement ) {
176
- let isShiftDown = false ;
177
- textarea . addEventListener ( 'keydown' , ( e : KeyboardEvent ) => {
178
- if ( e . shiftKey ) isShiftDown = true ;
179
- } ) ;
180
- textarea . addEventListener ( 'keyup' , ( e : KeyboardEvent ) => {
181
- if ( ! e . shiftKey ) isShiftDown = false ;
182
- } ) ;
149
+ subscribe ( textarea ) ; // enable paste features
183
150
textarea . addEventListener ( 'paste' , ( e : ClipboardEvent ) => {
184
- const { images, text } = getPastedContent ( e ) ;
151
+ const images = getPastedImages ( e ) ;
185
152
if ( images . length && dropzoneEl ) {
186
153
handleUploadFiles ( new TextareaEditor ( textarea ) , dropzoneEl , images , e ) ;
187
- } else if ( text ) {
188
- handleClipboardText ( textarea , e , text , isShiftDown ) ;
189
154
}
190
155
} ) ;
191
156
textarea . addEventListener ( 'drop' , ( e : DragEvent ) => {
0 commit comments