11import ComposerEditor from "discourse/components/composer-editor" ;
22import {
3+ bind ,
34 default as discourseComputed ,
45 on ,
56} from "discourse-common/utils/decorators" ;
@@ -12,6 +13,9 @@ import Site from "discourse/models/site";
1213import { uploadIcon } from "discourse/lib/uploads" ;
1314import { dasherize } from "@ember/string" ;
1415
16+ const IMAGE_MARKDOWN_REGEX =
17+ / ! \[ ( .* ?) \| ( \d { 1 , 4 } x \d { 1 , 4 } ) ( , \s * \d { 1 , 3 } % ) ? ( .* ?) \] \( ( u p l o a d : \/ \/ .* ?) \) (? ! ( .* ` ) ) / g;
18+
1519export default ComposerEditor . extend ( {
1620 classNameBindings : [ "fieldClass" ] ,
1721 allowUpload : true ,
@@ -25,7 +29,7 @@ export default ComposerEditor.extend({
2529 popupMenuOptions : [ ] ,
2630 draftStatus : "null" ,
2731 replyPlaceholder : alias ( "field.translatedPlaceholder" ) ,
28- uploadingFieldId : null ,
32+ wizardEventFieldId : null ,
2933
3034 @on ( "didInsertElement" )
3135 _composerEditorInit ( ) {
@@ -76,7 +80,6 @@ export default ComposerEditor.extend({
7680
7781 const wizardEventNames = [ "insert-text" , "replace-text" ] ;
7882 const eventPrefix = this . eventPrefix ;
79- const session = this . get ( "session" ) ;
8083 this . appEvents . reopen ( {
8184 trigger ( name , ...args ) {
8285 let eventParts = name . split ( ":" ) ;
@@ -87,26 +90,8 @@ export default ComposerEditor.extend({
8790 currentEventPrefix !== "wizard-editor" &&
8891 wizardEventNames . some ( ( wen ) => wen === currentEventName )
8992 ) {
90- let wizardName = name . replace ( eventPrefix , "wizard-editor" ) ;
91- if ( currentEventName === "insert-text" ) {
92- args = {
93- text : args [ 0 ] ,
94- } ;
95- }
96- if ( currentEventName === "replace-text" ) {
97- args = {
98- oldVal : args [ 0 ] ,
99- newVal : args [ 1 ] ,
100- } ;
101- }
102- let wizardArgs = Object . assign (
103- { } ,
104- {
105- fieldId : session . get ( "uploadingFieldId" ) ,
106- } ,
107- args
108- ) ;
109- return this . _super ( wizardName , wizardArgs ) ;
93+ let wizardEventName = name . replace ( eventPrefix , "wizard-editor" ) ;
94+ return this . _super ( wizardEventName , ...args ) ;
11095 } else {
11196 return this . _super ( name , ...args ) ;
11297 }
@@ -138,6 +123,28 @@ export default ComposerEditor.extend({
138123 }
139124 } ,
140125
126+ @bind
127+ _handleImageDeleteButtonClick ( event ) {
128+ if ( ! event . target . classList . contains ( "delete-image-button" ) ) {
129+ return ;
130+ }
131+
132+ const index = parseInt (
133+ event . target . closest ( ".button-wrapper" ) . dataset . imageIndex ,
134+ 10
135+ ) ;
136+ const matchingPlaceholder =
137+ this . get ( "composer.reply" ) . match ( IMAGE_MARKDOWN_REGEX ) ;
138+
139+ this . session . set ( "wizardEventFieldId" , this . field . id ) ;
140+ this . appEvents . trigger (
141+ "composer:replace-text" ,
142+ matchingPlaceholder [ index ] ,
143+ "" ,
144+ { regex : IMAGE_MARKDOWN_REGEX , index }
145+ ) ;
146+ } ,
147+
141148 actions : {
142149 extraButtons ( toolbar ) {
143150 const component = this ;
@@ -213,7 +220,7 @@ export default ComposerEditor.extend({
213220 } ,
214221
215222 showUploadModal ( ) {
216- this . session . set ( "uploadingFieldId " , this . field . id ) ;
223+ this . session . set ( "wizardEventFieldId " , this . field . id ) ;
217224 document . getElementById ( this . fileUploadElementId ) . click ( ) ;
218225 } ,
219226 } ,
0 commit comments