7
7
Markdown editor
8
8
*/
9
9
10
- // Note -- the old file upload used .chat-images for everything,
11
- // rather than a directory for each file.
12
- const AUX_FILE_EXT = "upload" ;
13
-
14
10
import * as CodeMirror from "codemirror" ;
15
11
import { debounce , isEqual } from "lodash" ;
16
- import { join } from "path" ;
17
12
import {
18
13
CSSProperties ,
19
14
MutableRefObject ,
@@ -25,7 +20,6 @@ import {
25
20
useRef ,
26
21
useState ,
27
22
} from "react" ;
28
-
29
23
import { alert_message } from "@cocalc/frontend/alerts" ;
30
24
import {
31
25
ReactDOM ,
@@ -36,21 +30,16 @@ import {
36
30
import { SubmitMentionsRef } from "@cocalc/frontend/chat/types" ;
37
31
import { A } from "@cocalc/frontend/components" ;
38
32
import { IS_MOBILE } from "@cocalc/frontend/feature" ;
39
- import { Dropzone , FileUploadWrapper } from "@cocalc/frontend/file-upload" ;
33
+ import { Dropzone , BlobUpload } from "@cocalc/frontend/file-upload" ;
40
34
import { Cursors , CursorsType } from "@cocalc/frontend/jupyter/cursors" ;
41
35
import Fragment , { FragmentId } from "@cocalc/frontend/misc/fragment-id" ;
42
36
import { useProjectHasInternetAccess } from "@cocalc/frontend/project/settings/has-internet-access-hook" ;
43
- import {
44
- aux_file ,
45
- len ,
46
- path_split ,
47
- trunc ,
48
- trunc_middle ,
49
- } from "@cocalc/util/misc" ;
37
+ import { len , trunc , trunc_middle } from "@cocalc/util/misc" ;
50
38
import { Complete , Item } from "./complete" ;
51
39
import { useMentionableUsers } from "./mentionable-users" ;
52
40
import { submit_mentions } from "./mentions" ;
53
41
import { EditorFunctions } from "./multimode" ;
42
+ import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context" ;
54
43
55
44
type EventHandlerFunction = ( cm : CodeMirror . Editor ) => void ;
56
45
@@ -162,6 +151,7 @@ export function MarkdownInput(props: Props) {
162
151
unregisterEditor,
163
152
value,
164
153
} = props ;
154
+ const { actions } = useFrameContext ( ) ;
165
155
const cm = useRef < CodeMirror . Editor > ( ) ;
166
156
const textarea_ref = useRef < HTMLTextAreaElement > ( null ) ;
167
157
const editor_settings = useRedux ( [ "account" , "editor_settings" ] ) ;
@@ -579,7 +569,7 @@ export function MarkdownInput(props: Props) {
579
569
}
580
570
if ( cm . current == null ) return ;
581
571
const input = cm . current . getValue ( ) ;
582
- const s = upload_temp_link ( path , file ) ;
572
+ const s = upload_temp_link ( file ) ;
583
573
if ( input . indexOf ( s ) != - 1 ) {
584
574
// already have link.
585
575
return ;
@@ -588,11 +578,7 @@ export function MarkdownInput(props: Props) {
588
578
saveValue ( ) ;
589
579
}
590
580
591
- function upload_complete ( file : {
592
- type : string ;
593
- name : string ;
594
- status : string ;
595
- } ) : void {
581
+ function upload_complete ( file ) : void {
596
582
if ( path == null ) {
597
583
throw Error ( "path must be set if enableUploads is set." ) ;
598
584
}
@@ -608,7 +594,7 @@ export function MarkdownInput(props: Props) {
608
594
}
609
595
if ( cm . current == null ) return ;
610
596
const input = cm . current . getValue ( ) ;
611
- const s0 = upload_temp_link ( path , file ) ;
597
+ const s0 = upload_temp_link ( file ) ;
612
598
let s1 : string ;
613
599
if ( file . status == "error" ) {
614
600
s1 = "" ;
@@ -617,43 +603,13 @@ export function MarkdownInput(props: Props) {
617
603
// users can cancel files when they are being uploaded.
618
604
s1 = "" ;
619
605
} else {
620
- s1 = upload_link ( path , file ) ;
606
+ s1 = upload_link ( file ) ;
621
607
}
622
608
const newValue = input . replace ( s0 , s1 ) ;
623
609
setValueNoJump ( newValue ) ;
624
610
saveValue ( ) ;
625
611
}
626
612
627
- function upload_removed ( file : { name : string ; type : string } ) : void {
628
- if ( cm . current == null ) return ;
629
- if ( project_id == null || path == null ) {
630
- throw Error ( "project_id and path must be set if enableUploads is set." ) ;
631
- }
632
- if ( ! current_uploads_ref . current ?. [ file . name ] ) {
633
- // it actually succeeded if this is not set -- it was removed
634
- // via upload_complete above.
635
- return ;
636
- }
637
- delete current_uploads_ref . current [ file . name ] ;
638
- if ( onUploadEnd != null ) {
639
- onUploadEnd ( ) ;
640
- }
641
-
642
- const input = cm . current . getValue ( ) ;
643
- const s = upload_link ( path , file ) ;
644
- if ( input . indexOf ( s ) == - 1 ) {
645
- // not there anymore; maybe user already submitted -- do nothing further.
646
- return ;
647
- }
648
- const newValue = input . replace ( s , "" ) ;
649
- setValueNoJump ( newValue ) ;
650
- saveValue ( ) ;
651
- // delete from project itself
652
- const target = join ( aux_file ( path , AUX_FILE_EXT ) , file . name ) ;
653
- // console.log("deleting target", target, { paths: [target] });
654
- redux . getProjectActions ( project_id ) . delete_files ( { paths : [ target ] } ) ;
655
- }
656
-
657
613
function handle_paste_event ( _ , e ) : void {
658
614
// console.log("handle_paste_event", e);
659
615
const items = e . clipboardData . items ;
@@ -926,49 +882,45 @@ export function MarkdownInput(props: Props) {
926
882
const event_handlers = {
927
883
complete : upload_complete ,
928
884
sending : upload_sending ,
929
- removedfile : upload_removed ,
885
+ error : ( _ , message ) => {
886
+ actions ?. set_error ( `${ message } ` ) ;
887
+ } ,
930
888
} ;
931
889
if ( project_id == null || path == null ) {
932
890
throw Error ( "project_id and path must be set if enableUploads is set." ) ;
933
891
}
934
892
body = (
935
- < FileUploadWrapper
893
+ < BlobUpload
894
+ show_upload = { false }
936
895
project_id = { project_id }
937
- dest_path = { aux_file ( path , AUX_FILE_EXT ) }
938
896
event_handlers = { event_handlers }
939
897
style = { { height : "100%" , width : "100%" } }
940
898
dropzone_ref = { dropzone_ref }
941
899
close_preview_ref = { upload_close_preview_ref }
900
+ config = {
901
+ // only images work since when pasting other doc types
902
+ // things blow up (due to conflict with codemirror?)
903
+ { acceptedFiles : "image/*" }
904
+ }
942
905
>
943
906
{ body }
944
- </ FileUploadWrapper >
907
+ </ BlobUpload >
945
908
) ;
946
909
}
947
910
948
911
return body ;
949
912
}
950
913
951
- function upload_target ( path : string , file : { name : string } ) : string {
952
- // path to our upload target, but relative to path.
953
- return join ( path_split ( aux_file ( path , AUX_FILE_EXT ) ) . tail , file . name ) ;
954
- }
955
-
956
- function upload_temp_link ( path : string , file : { name : string } ) : string {
957
- return `[Uploading...]\(${ upload_target ( path , file ) } \)` ;
914
+ function upload_temp_link ( file ) : string {
915
+ return `[Uploading...]\(${ file . name ?? file . upload ?. filename ?? "" } \)` ;
958
916
}
959
917
960
- function upload_link (
961
- path : string ,
962
- file : { name : string ; type : string } ,
963
- ) : string {
964
- const target = upload_target ( path , file ) ;
965
- if ( file . type . indexOf ( "image" ) !== - 1 ) {
966
- return `<img src=\"${ target } \" style="max-width:100%" />` ;
918
+ function upload_link ( file ) : string {
919
+ const { url, dataURL, height, upload } = file ;
920
+ if ( ! height && ! dataURL ?. startsWith ( "data:image" ) ) {
921
+ return `[${ upload . filename ? upload . filename : "file" } ](${ url } )` ;
967
922
} else {
968
- // We use an a tag instead of [${file.name}](${target}) because for
969
- // some files (e.g,. word doc files) our markdown renderer inexplicably
970
- // does NOT render them as links!? a tags work though.
971
- return `<a href=\"${ target } \">${ file . name } </a>` ;
923
+ return `` ;
972
924
}
973
925
}
974
926
0 commit comments