11import { imageInfo } from '../../utils/image.ts' ;
2- import { replaceTextareaSelection } from '../../utils/dom.ts' ;
3- import { isUrl } from '../../utils/url.ts' ;
42import { textareaInsertText , triggerEditorContentChanged } from './EditorMarkdown.ts' ;
53import {
64 DropzoneCustomEventRemovedFile ,
75 DropzoneCustomEventUploadDone ,
86 generateMarkdownLinkForAttachment ,
97} from '../dropzone.ts' ;
8+ import { subscribe } from '@github/paste-markdown' ;
109import type CodeMirror from 'codemirror' ;
1110import type EasyMDE from 'easymde' ;
1211import type { DropzoneFile } from 'dropzone' ;
@@ -118,46 +117,20 @@ export function removeAttachmentLinksFromMarkdown(text: string, fileUuid: string
118117 return text ;
119118}
120119
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 > = [ ] ;
148122 for ( const item of e . clipboardData ?. items ?? [ ] ) {
149123 if ( item . type ?. startsWith ( 'image/' ) ) {
150124 images . push ( item . getAsFile ( ) ) ;
151125 }
152126 }
153- const text = e . clipboardData ?. getData ?.( 'text' ) ?? '' ;
154- return { text, images} ;
127+ return images ;
155128}
156129
157130export function initEasyMDEPaste ( easyMDE : EasyMDE , dropzoneEl : HTMLElement ) {
158131 const editor = new CodeMirrorEditor ( easyMDE . codemirror as any ) ;
159132 easyMDE . codemirror . on ( 'paste' , ( _ , e ) => {
160- const { images} = getPastedContent ( e ) ;
133+ const images = getPastedImages ( e ) ;
161134 if ( ! images . length ) return ;
162135 handleUploadFiles ( editor , dropzoneEl , images , e ) ;
163136 } ) ;
@@ -173,19 +146,11 @@ export function initEasyMDEPaste(easyMDE: EasyMDE, dropzoneEl: HTMLElement) {
173146}
174147
175148export 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
183150 textarea . addEventListener ( 'paste' , ( e : ClipboardEvent ) => {
184- const { images, text } = getPastedContent ( e ) ;
151+ const images = getPastedImages ( e ) ;
185152 if ( images . length && dropzoneEl ) {
186153 handleUploadFiles ( new TextareaEditor ( textarea ) , dropzoneEl , images , e ) ;
187- } else if ( text ) {
188- handleClipboardText ( textarea , e , text , isShiftDown ) ;
189154 }
190155 } ) ;
191156 textarea . addEventListener ( 'drop' , ( e : DragEvent ) => {
0 commit comments