@@ -167,44 +167,45 @@ define([
167167 return index ;
168168 }
169169
170- function insertPath ( record , offset ) {
171- var records ;
170+ function insertRecords ( record , offset , records ) {
172171 if ( isTextRecord ( record ) ) {
173- records = spliceContent ( offset , 0 , record , Marker ( ) ) ;
174- return FragmentedText ( record , records ) ;
172+ return spliceContent ( offset , 0 , record , records ) ;
175173 }
176174 if ( isFragmentedText ( record ) ) {
177175 var offsets = fragmentedOffset ( record , offset ) ;
178176 offset = offsets [ 0 ] ;
179177 if ( 2 === offsets . length ) {
180178 var text = record . children ( ) [ offset ] ;
181- var fragments = spliceContent ( offsets [ 1 ] , 0 , text , Marker ( ) ) ;
182- records = spliceContent ( offset , 1 , record , fragments ) ;
183- return record . children ( records ) ;
179+ var fragments = spliceContent ( offsets [ 1 ] , 0 , text , records ) ;
180+ return record . children ( spliceContent ( offset , 1 , record , fragments ) ) ;
184181 }
185182 } else {
186183 offset = normalizeOffset ( record , offset ) ;
187184 }
188- records = spliceContent ( offset , 0 , record , Marker ( ) ) ;
189- return record . children ( records ) ;
185+ return record . children ( spliceContent ( offset , 0 , record , records ) ) ;
190186 }
191187
192- function insertPathInTree ( record , path ) {
188+ function insertRec ( record , path , insertAt ) {
193189 var trail = path . slice ( 1 ) ;
194190 var offset = path [ 0 ] ;
195191 // Because we are at end of the trail: <a/>|<b/> or <#text "foo|bar">
196192 if ( 0 === trail . length ) {
197- return insertPath ( record , offset ) ;
193+ return insertAt ( record , offset ) ;
198194 }
199195 offset = normalizeOffset ( record , offset ) ;
200196 var unchanged = record . children ( ) [ offset ] ;
201- var changed = insertPathInTree ( unchanged , trail ) ;
197+ var changed = insertRec ( unchanged , trail , insertAt ) ;
202198 var children = spliceContent ( offset , 1 , record , changed ) ;
203199 return record . children ( children ) ;
204200 }
205201
206- function insertPathsInTree ( record , paths ) {
207- return paths . reduce ( insertPathInTree , record ) ;
202+ function insertPath ( record , offset ) {
203+ var records = insertRecords ( record , offset , Marker ( ) ) ;
204+ return isTextRecord ( record ) ? FragmentedText ( record , records ) : record ;
205+ }
206+
207+ function insertPathInTree ( record , path ) {
208+ return insertRec ( record , path , insertPath ) ;
208209 }
209210
210211 function boundaryIndexInFragment ( fragments , offset ) {
@@ -229,6 +230,7 @@ define([
229230 var text = fragments . reduce ( function ( strings , string ) {
230231 return strings . concat ( string . text ( ) ) ;
231232 } , [ ] ) . join ( '' ) ;
233+ console . log ( text ) ;
232234 //var doc = record.original.domNode().ownerDocument;
233235 var doc = document ;
234236 return ( record . original && record . original . text ( ) === text )
@@ -315,7 +317,7 @@ define([
315317 var clipped = paths . map ( Fn . partial ( clipCommonRoot , root ) ) . filter ( function ( arr ) {
316318 return arr . length > 0 ;
317319 } ) ;
318- return insertPathsInTree ( Boromir ( element ) , clipped ) ;
320+ return clipped . reduce ( insertPathInTree , Boromir ( element ) ) ;
319321 }
320322
321323 /**
@@ -337,26 +339,16 @@ define([
337339 ] ;
338340 }
339341
340- function insert ( tree , content ) {
341- /*
342- var node = Boromir(
343- ('string' === typeof content)
344- ? Boundaries.document(boundary).createTextNode(content)
345- : content
346- );
347- var container = Boundaries.container(boundary);
348- var editable = Dom.editingHost(container);
349- var path = Paths.fromBoundary(editable, boundary);
350- var unpreserved = boundaries.map(Fn.partial(Paths.fromBoundary, editable));
351- var unchanged = Boromir(container);
352- var offset = Boundaries.offset(boundary);
353- var children = unchanged.children();
354- unchanged.children(
355- children.slice(0, offset).concat(node).concat(children.slice(offset))
356- ).updateDom();
357- var preserved = adjustForInsertBefore(path, unpreserved);
358- return preserved.map(Fn.partial(Paths.toBoundary, editable));
359- */
342+ function insert ( tree , path , content ) {
343+ if ( 0 === content . length ) {
344+ return tree ;
345+ }
346+ var records = content . map ( function ( item ) {
347+ return item instanceof Boromir ? item : Boromir ( item ) ;
348+ } ) ;
349+ return insertRec ( tree , path , function ( record , offset ) {
350+ return insertRecords ( record , offset , records ) ;
351+ } ) ;
360352 }
361353
362354 return {
0 commit comments