@@ -27,9 +27,8 @@ impl Default for Document {
27
27
}
28
28
29
29
fn split_path ( path : & [ LayerId ] ) -> Result < ( & [ LayerId ] , LayerId ) , DocumentError > {
30
- let id = path. last ( ) . ok_or ( DocumentError :: InvalidPath ) ?;
31
- let folder_path = & path[ 0 ..path. len ( ) - 1 ] ;
32
- Ok ( ( folder_path, * id) )
30
+ let ( id, path) = path. split_last ( ) . ok_or ( DocumentError :: InvalidPath ) ?;
31
+ Ok ( ( path, * id) )
33
32
}
34
33
35
34
impl Document {
@@ -221,24 +220,8 @@ impl Document {
221
220
/// Deletes the layer specified by `path`.
222
221
pub fn delete ( & mut self , path : & [ LayerId ] ) -> Result < ( ) , DocumentError > {
223
222
let ( path, id) = split_path ( path) ?;
224
- if let Ok ( layer) = self . layer_mut ( path) {
225
- layer. cache_dirty = true ;
226
- }
227
- self . document_folder_mut ( path) ?. as_folder_mut ( ) ?. remove_layer ( id) ?;
228
- Ok ( ( ) )
229
- }
230
-
231
- pub fn reorder_layers ( & mut self , source_paths : & [ Vec < LayerId > ] , target_path : & [ LayerId ] ) -> Result < ( ) , DocumentError > {
232
- // TODO: Detect when moving between folders and handle properly
233
-
234
- let source_layer_ids = source_paths
235
- . iter ( )
236
- . map ( |x| x. last ( ) . cloned ( ) . ok_or ( DocumentError :: LayerNotFound ) )
237
- . collect :: < Result < Vec < LayerId > , DocumentError > > ( ) ?;
238
-
239
- self . root . as_folder_mut ( ) ?. reorder_layers ( source_layer_ids, * target_path. last ( ) . ok_or ( DocumentError :: LayerNotFound ) ?) ?;
240
-
241
- Ok ( ( ) )
223
+ let _ = self . layer_mut ( path) . map ( |x| x. cache_dirty = true ) ;
224
+ self . document_folder_mut ( path) ?. as_folder_mut ( ) ?. remove_layer ( id)
242
225
}
243
226
244
227
pub fn layer_axis_aligned_bounding_box ( & self , path : & [ LayerId ] ) -> Result < Option < [ DVec2 ; 2 ] > , DocumentError > {
@@ -268,6 +251,16 @@ impl Document {
268
251
Ok ( ( ) )
269
252
}
270
253
254
+ fn working_paths ( & mut self ) -> Vec < Vec < LayerId > > {
255
+ self . work
256
+ . as_folder ( )
257
+ . unwrap ( )
258
+ . layer_ids
259
+ . iter ( )
260
+ . map ( |id| self . work_mount_path . iter ( ) . chain ( [ * id] . iter ( ) ) . cloned ( ) . collect ( ) )
261
+ . collect ( )
262
+ }
263
+
271
264
/// Mutate the document by applying the `operation` to it. If the operation necessitates a
272
265
/// reaction from the frontend, responses may be returned.
273
266
pub fn handle_operation ( & mut self , operation : Operation ) -> Result < Option < Vec < DocumentResponse > > , DocumentError > {
@@ -276,19 +269,19 @@ impl Document {
276
269
let id = self . add_layer ( & path, Layer :: new ( LayerDataTypes :: Ellipse ( layers:: Ellipse :: new ( ) ) , * transform, * style) , * insert_index) ?;
277
270
let path = [ path. clone ( ) , vec ! [ id] ] . concat ( ) ;
278
271
279
- Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: SelectLayer { path } ] )
272
+ Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: CreatedLayer { path } ] )
280
273
}
281
274
Operation :: AddRect { path, insert_index, transform, style } => {
282
275
let id = self . add_layer ( & path, Layer :: new ( LayerDataTypes :: Rect ( Rect ) , * transform, * style) , * insert_index) ?;
283
276
let path = [ path. clone ( ) , vec ! [ id] ] . concat ( ) ;
284
277
285
- Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: SelectLayer { path } ] )
278
+ Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: CreatedLayer { path } ] )
286
279
}
287
280
Operation :: AddLine { path, insert_index, transform, style } => {
288
281
let id = self . add_layer ( & path, Layer :: new ( LayerDataTypes :: Line ( Line ) , * transform, * style) , * insert_index) ?;
289
282
let path = [ path. clone ( ) , vec ! [ id] ] . concat ( ) ;
290
283
291
- Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: SelectLayer { path } ] )
284
+ Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: CreatedLayer { path } ] )
292
285
}
293
286
Operation :: AddPen {
294
287
path,
@@ -301,7 +294,7 @@ impl Document {
301
294
let polyline = PolyLine :: new ( points) ;
302
295
let id = self . add_layer ( & path, Layer :: new ( LayerDataTypes :: PolyLine ( polyline) , * transform, * style) , * insert_index) ?;
303
296
let path = [ path. clone ( ) , vec ! [ id] ] . concat ( ) ;
304
- Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: SelectLayer { path } ] )
297
+ Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: CreatedLayer { path } ] )
305
298
}
306
299
Operation :: AddShape {
307
300
path,
@@ -315,20 +308,29 @@ impl Document {
315
308
let id = self . add_layer ( & path, Layer :: new ( LayerDataTypes :: Shape ( s) , * transform, * style) , * insert_index) ?;
316
309
let path = [ path. clone ( ) , vec ! [ id] ] . concat ( ) ;
317
310
318
- Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: SelectLayer { path } ] )
311
+ Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: CreatedLayer { path } ] )
319
312
}
320
313
Operation :: DeleteLayer { path } => {
321
314
self . delete ( & path) ?;
322
315
323
- let ( path, _) = split_path ( path. as_slice ( ) ) . unwrap_or_else ( |_| ( & [ ] , 0 ) ) ;
324
- Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: FolderChanged { path: path. to_vec( ) } ] )
316
+ let ( folder, _) = split_path ( path. as_slice ( ) ) . unwrap_or_else ( |_| ( & [ ] , 0 ) ) ;
317
+ Some ( vec ! [
318
+ DocumentResponse :: DocumentChanged ,
319
+ DocumentResponse :: DeletedLayer { path: path. clone( ) } ,
320
+ DocumentResponse :: FolderChanged { path: folder. to_vec( ) } ,
321
+ ] )
325
322
}
326
- Operation :: PasteLayer { path, layer } => {
323
+ Operation :: PasteLayer { path, layer, insert_index } => {
327
324
let folder = self . folder_mut ( path) ?;
328
325
//FIXME: This clone of layer should be avoided somehow
329
- folder. add_layer ( layer. clone ( ) , -1 ) . ok_or ( DocumentError :: IndexOutOfBounds ) ?;
330
-
331
- Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: FolderChanged { path: path. clone( ) } ] )
326
+ let id = folder. add_layer ( layer. clone ( ) , * insert_index) . ok_or ( DocumentError :: IndexOutOfBounds ) ?;
327
+ let full_path = [ path. clone ( ) , vec ! [ id] ] . concat ( ) ;
328
+
329
+ Some ( vec ! [
330
+ DocumentResponse :: DocumentChanged ,
331
+ DocumentResponse :: CreatedLayer { path: full_path } ,
332
+ DocumentResponse :: FolderChanged { path: path. clone( ) } ,
333
+ ] )
332
334
}
333
335
Operation :: DuplicateLayer { path } => {
334
336
let layer = self . layer ( & path) ?. clone ( ) ;
@@ -343,11 +345,13 @@ impl Document {
343
345
Some ( vec ! [ DocumentResponse :: DocumentChanged , DocumentResponse :: FolderChanged { path: path. clone( ) } ] )
344
346
}
345
347
Operation :: MountWorkingFolder { path } => {
348
+ let mut responses: Vec < _ > = self . working_paths ( ) . into_iter ( ) . map ( |path| DocumentResponse :: DeletedLayer { path } ) . collect ( ) ;
346
349
self . work_mount_path = path. clone ( ) ;
347
350
self . work_operations . clear ( ) ;
348
351
self . work = Layer :: new ( LayerDataTypes :: Folder ( Folder :: default ( ) ) , DAffine2 :: IDENTITY . to_cols_array ( ) , PathStyle :: default ( ) ) ;
349
352
self . work_mounted = true ;
350
- None
353
+ responses. push ( DocumentResponse :: DocumentChanged ) ;
354
+ Some ( responses)
351
355
}
352
356
Operation :: TransformLayer { path, transform } => {
353
357
let layer = self . document_layer_mut ( path) . unwrap ( ) ;
@@ -365,26 +369,30 @@ impl Document {
365
369
Some ( vec ! [ DocumentResponse :: DocumentChanged ] )
366
370
}
367
371
Operation :: DiscardWorkingFolder => {
372
+ let mut responses: Vec < _ > = self . working_paths ( ) . into_iter ( ) . map ( |path| DocumentResponse :: DeletedLayer { path } ) . collect ( ) ;
368
373
self . work_operations . clear ( ) ;
369
374
self . work_mount_path = vec ! [ ] ;
370
375
self . work = Layer :: new ( LayerDataTypes :: Folder ( Folder :: default ( ) ) , DAffine2 :: IDENTITY . to_cols_array ( ) , PathStyle :: default ( ) ) ;
371
376
self . work_mounted = false ;
372
- Some ( vec ! [ DocumentResponse :: DocumentChanged ] )
377
+ responses. push ( DocumentResponse :: DocumentChanged ) ;
378
+ Some ( responses)
373
379
}
374
380
Operation :: ClearWorkingFolder => {
381
+ let mut responses: Vec < _ > = self . working_paths ( ) . into_iter ( ) . map ( |path| DocumentResponse :: DeletedLayer { path } ) . collect ( ) ;
375
382
self . work_operations . clear ( ) ;
376
383
self . work = Layer :: new ( LayerDataTypes :: Folder ( Folder :: default ( ) ) , DAffine2 :: IDENTITY . to_cols_array ( ) , PathStyle :: default ( ) ) ;
377
- Some ( vec ! [ DocumentResponse :: DocumentChanged ] )
384
+ responses. push ( DocumentResponse :: DocumentChanged ) ;
385
+ Some ( responses)
378
386
}
379
387
Operation :: CommitTransaction => {
388
+ let mut responses: Vec < _ > = self . working_paths ( ) . into_iter ( ) . map ( |path| DocumentResponse :: DeletedLayer { path } ) . collect ( ) ;
380
389
let mut ops = Vec :: new ( ) ;
381
390
let mut path: Vec < LayerId > = vec ! [ ] ;
382
391
std:: mem:: swap ( & mut path, & mut self . work_mount_path ) ;
383
392
std:: mem:: swap ( & mut ops, & mut self . work_operations ) ;
384
393
self . work_mounted = false ;
385
394
self . work_mount_path = vec ! [ ] ;
386
395
self . work = Layer :: new ( LayerDataTypes :: Folder ( Folder :: default ( ) ) , DAffine2 :: IDENTITY . to_cols_array ( ) , PathStyle :: default ( ) ) ;
387
- let mut responses = vec ! [ ] ;
388
396
for operation in ops. into_iter ( ) {
389
397
if let Some ( mut op_responses) = self . handle_operation ( operation) ? {
390
398
responses. append ( & mut op_responses) ;
@@ -416,11 +424,6 @@ impl Document {
416
424
self . mark_as_dirty ( path) ?;
417
425
Some ( vec ! [ DocumentResponse :: DocumentChanged ] )
418
426
}
419
- Operation :: ReorderLayers { source_paths, target_path } => {
420
- self . reorder_layers ( source_paths, target_path) ?;
421
-
422
- Some ( vec ! [ DocumentResponse :: DocumentChanged ] )
423
- }
424
427
} ;
425
428
if !matches ! (
426
429
operation,
0 commit comments