11use crate :: document_metadata:: { is_artboard, DocumentMetadata , LayerNodeIdentifier } ;
2- use crate :: layers:: folder_layer:: FolderLegacyLayer ;
3- use crate :: layers:: layer_info:: { LegacyLayer , LegacyLayerType } ;
4- use crate :: DocumentError ;
52
63use graph_craft:: document:: { DocumentNode , DocumentNodeImplementation , NodeId , NodeNetwork , NodeOutput } ;
74use graphene_core:: renderer:: ClickTarget ;
@@ -23,9 +20,6 @@ pub type LayerId = u64;
2320pub struct Document {
2421 #[ serde( default ) ]
2522 pub document_network : NodeNetwork ,
26- /// The root layer, usually a [FolderLegacyLayer](layers::folder_layer::FolderLegacyLayer) that contains all other [LegacyLayers](layers::layer_info::LegacyLayer).
27- #[ serde( skip) ]
28- pub root : LegacyLayer ,
2923 /// The state_identifier serves to provide a way to uniquely identify a particular state that the document is in.
3024 /// This identifier is not a hash and is not guaranteed to be equal for equivalent documents.
3125 #[ serde( skip) ]
@@ -43,11 +37,6 @@ impl PartialEq for Document {
4337impl Default for Document {
4438 fn default ( ) -> Self {
4539 Self {
46- root : LegacyLayer {
47- name : None ,
48- visible : true ,
49- data : LegacyLayerType :: Folder ( FolderLegacyLayer :: default ( ) ) ,
50- } ,
5140 state_identifier : DefaultHasher :: new ( ) ,
5241 document_network : {
5342 use graph_craft:: document:: { value:: TaggedValue , NodeInput } ;
@@ -156,100 +145,4 @@ impl Document {
156145 pub fn current_state_identifier ( & self ) -> u64 {
157146 self . state_identifier . finish ( )
158147 }
159-
160- /// Returns a reference to the requested folder. Fails if the path does not exist,
161- /// or if the requested layer is not of type folder.
162- pub fn folder ( & self , path : impl AsRef < [ LayerId ] > ) -> Result < & FolderLegacyLayer , DocumentError > {
163- let mut root = & self . root ;
164- for id in path. as_ref ( ) {
165- root = root. as_folder ( ) ?. layer ( * id) . ok_or_else ( || DocumentError :: LayerNotFound ( path. as_ref ( ) . into ( ) ) ) ?;
166- }
167- root. as_folder ( )
168- }
169-
170- /// Returns a mutable reference to the requested folder. Fails if the path does not exist,
171- /// or if the requested layer is not of type folder.
172- fn folder_mut ( & mut self , path : & [ LayerId ] ) -> Result < & mut FolderLegacyLayer , DocumentError > {
173- let mut root = & mut self . root ;
174- for id in path {
175- root = root. as_folder_mut ( ) ?. layer_mut ( * id) . ok_or_else ( || DocumentError :: LayerNotFound ( path. into ( ) ) ) ?;
176- }
177- root. as_folder_mut ( )
178- }
179-
180- /// Returns a reference to the layer or folder at the path.
181- pub fn layer ( & self , path : & [ LayerId ] ) -> Result < & LegacyLayer , DocumentError > {
182- if path. is_empty ( ) {
183- return Ok ( & self . root ) ;
184- }
185- let ( path, id) = split_path ( path) ?;
186- self . folder ( path) ?. layer ( id) . ok_or_else ( || DocumentError :: LayerNotFound ( path. into ( ) ) )
187- }
188-
189- /// Returns a mutable reference to the layer or folder at the path.
190- pub fn layer_mut ( & mut self , path : & [ LayerId ] ) -> Result < & mut LegacyLayer , DocumentError > {
191- if path. is_empty ( ) {
192- return Ok ( & mut self . root ) ;
193- }
194- let ( path, id) = split_path ( path) ?;
195- self . folder_mut ( path) ?. layer_mut ( id) . ok_or_else ( || DocumentError :: LayerNotFound ( path. into ( ) ) )
196- }
197-
198- pub fn common_layer_path_prefix < ' a > ( & self , layers : impl Iterator < Item = & ' a [ LayerId ] > ) -> & ' a [ LayerId ] {
199- layers. reduce ( |a, b| & a[ ..a. iter ( ) . zip ( b. iter ( ) ) . take_while ( |& ( a, b) | a == b) . count ( ) ] ) . unwrap_or_default ( )
200- }
201-
202- /// Returns the shallowest folder given the selection, even if the selection doesn't contain any folders
203- pub fn shallowest_common_folder < ' a > ( & self , layers : impl Iterator < Item = & ' a [ LayerId ] > ) -> Result < & ' a [ LayerId ] , DocumentError > {
204- let common_prefix_of_path = self . common_layer_path_prefix ( layers) ;
205-
206- Ok ( match self . layer ( common_prefix_of_path) ?. data {
207- LegacyLayerType :: Folder ( _) => common_prefix_of_path,
208- _ => & common_prefix_of_path[ ..common_prefix_of_path. len ( ) - 1 ] ,
209- } )
210- }
211-
212- /// Returns all layers that are not contained in any other of the given folders
213- /// Takes and Iterator over &[LayerId] or &Vec<LayerId>.
214- pub fn shallowest_unique_layers < ' a , T > ( layers : impl Iterator < Item = T > ) -> Vec < T >
215- where
216- T : AsRef < [ LayerId ] > + std:: cmp:: Ord + ' a ,
217- {
218- let mut sorted_layers: Vec < _ > = layers. collect ( ) ;
219- sorted_layers. sort ( ) ;
220- // Sorting here creates groups of similar UUID paths
221- sorted_layers. dedup_by ( |a, b| a. as_ref ( ) . starts_with ( b. as_ref ( ) ) ) ;
222- sorted_layers
223- }
224-
225- /// Given a path to a layer, returns a vector of the indices in the layer tree
226- /// These indices can be used to order a list of layers
227- pub fn indices_for_path ( & self , path : & [ LayerId ] ) -> Result < Vec < usize > , DocumentError > {
228- let mut root = self . root . as_folder ( ) ?;
229- let mut indices = vec ! [ ] ;
230- let ( path, layer_id) = split_path ( path) ?;
231-
232- // TODO: appears to be n^2? should we maintain a lookup table?
233- for id in path {
234- let pos = root. layer_ids . iter ( ) . position ( |x| * x == * id) . ok_or_else ( || DocumentError :: LayerNotFound ( path. into ( ) ) ) ?;
235- indices. push ( pos) ;
236- root = match root. layer ( * id) {
237- Some ( LegacyLayer {
238- data : LegacyLayerType :: Folder ( folder) ,
239- ..
240- } ) => Some ( folder) ,
241- _ => None ,
242- }
243- . ok_or_else ( || DocumentError :: LayerNotFound ( path. into ( ) ) ) ?;
244- }
245-
246- indices. push ( root. layer_ids . iter ( ) . position ( |x| * x == layer_id) . ok_or_else ( || DocumentError :: LayerNotFound ( path. into ( ) ) ) ?) ;
247-
248- Ok ( indices)
249- }
250- }
251-
252- fn split_path ( path : & [ LayerId ] ) -> Result < ( & [ LayerId ] , LayerId ) , DocumentError > {
253- let ( id, path) = path. split_last ( ) . ok_or ( DocumentError :: InvalidPath ) ?;
254- Ok ( ( path, * id) )
255148}
0 commit comments