@@ -145,8 +145,9 @@ define(function (require, exports, module) {
145145 * it is called when there is drag-drop in the live preview
146146 * @param {Number } sourceId - the data-brackets-id of the element being moved
147147 * @param {Number } targetId - the data-brackets-id of the target element where to move
148+ * @param {Boolean } insertAfter - whether to insert the source element after the target element
148149 */
149- function _moveElementInSource ( sourceId , targetId ) {
150+ function _moveElementInSource ( sourceId , targetId , insertAfter ) {
150151 // this is to get the currently live document that is being served in the live preview
151152 const currLiveDoc = LiveDevMultiBrowser . getCurrentLiveDoc ( ) ;
152153 if ( ! currLiveDoc ) {
@@ -160,32 +161,44 @@ define(function (require, exports, module) {
160161
161162 // position of source and target elements in the editor
162163 const sourceRange = HTMLInstrumentation . getPositionFromTagId ( editor , sourceId ) ;
164+ const targetRange = HTMLInstrumentation . getPositionFromTagId ( editor , targetId ) ;
163165
164- if ( ! sourceRange ) {
166+ if ( ! sourceRange || ! targetRange ) {
165167 return ;
166168 }
167169
168170 const sourceText = editor . getTextBetween ( sourceRange . from , sourceRange . to ) ;
171+ const targetIndent = editor . getTextBetween ( { line : targetRange . from . line , ch : 0 } , targetRange . from ) ;
169172
170173 // creating a batch operation so that undo in live preview works fine
171174 editor . document . batchOperation ( function ( ) {
172175 // first, we need to remove the source code from its initial position
173176 editor . replaceRange ( "" , sourceRange . from , sourceRange . to ) ;
174177
175- // get the target range, this is where we want to insert the text
176- const targetRange = HTMLInstrumentation . getPositionFromTagId ( editor , targetId ) ;
177- if ( ! targetRange ) {
178+ // recalculate the target range, as the source text is not removed
179+ const updatedTargetRange = HTMLInstrumentation . getPositionFromTagId ( editor , targetId ) ;
180+ if ( ! updatedTargetRange ) {
178181 return ;
179182 }
180- const targetText = editor . getTextBetween ( targetRange . from , targetRange . to ) ;
181- const targetIndent = editor . getTextBetween ( { line : targetRange . from . line , ch : 0 } , targetRange . from ) ;
182183
183- // to check if there is only indentation and no text before it
184- if ( targetIndent . trim ( ) === "" ) {
185- const finalText = sourceText + '\n' + targetIndent + targetText ;
186- editor . replaceRange ( finalText , targetRange . from , targetRange . to ) ;
184+ if ( insertAfter ) {
185+ const insertPos = {
186+ line : updatedTargetRange . to . line ,
187+ ch : updatedTargetRange . to . ch
188+ } ;
189+
190+ editor . replaceRange ( "\n" + targetIndent + sourceText , insertPos ) ;
187191 } else {
188- editor . replaceRange ( sourceText + targetText , targetRange . from , targetRange . to ) ;
192+ // insert before
193+ const targetText = editor . getTextBetween ( updatedTargetRange . from , updatedTargetRange . to ) ;
194+
195+ // to check if there is only indentation and no text before it
196+ if ( targetIndent . trim ( ) === "" ) {
197+ const finalText = sourceText + '\n' + targetIndent + targetText ;
198+ editor . replaceRange ( finalText , updatedTargetRange . from , updatedTargetRange . to ) ;
199+ } else {
200+ editor . replaceRange ( sourceText + targetText , updatedTargetRange . from , updatedTargetRange . to ) ;
201+ }
189202 }
190203 } ) ;
191204 }
@@ -206,14 +219,15 @@ define(function (require, exports, module) {
206219
207220 sourceId: sourceId, (these are for move (drag & drop))
208221 targetId: targetId,
222+ insertAfter: boolean, (whether to insert after the target element)
209223 move: true
210224 }
211225 * these are the main properties that are passed through the message
212226 */
213227 function handleLivePreviewEditOperation ( message ) {
214228 // handle move(drag & drop)
215229 if ( message . move && message . sourceId && message . targetId ) {
216- _moveElementInSource ( message . sourceId , message . targetId ) ;
230+ _moveElementInSource ( message . sourceId , message . targetId , message . insertAfter ) ;
217231 return ;
218232 }
219233
0 commit comments