瀏覽代碼

Merge pull request #1168 from PixiEditor/fix/clipboard-cut-fix

Fixed cuttting selected area
Krzysztof Krysiński 3 天之前
父節點
當前提交
004e5a9efc

+ 1 - 0
src/PixiEditor/Helpers/Constants/ClipboardDataFormats.cs

@@ -10,4 +10,5 @@ public static class ClipboardDataFormats
     public const string CelIdList = "PixiEditor.CelIdList";
     public const string PixiVectorData = "PixiEditor.VectorData";
     public const string UriList = "text/uri-list";
+    public const string HadSelectionFormat = "PixiEditor.HadSelection";
 }

+ 14 - 3
src/PixiEditor/Models/Controllers/ClipboardController.cs

@@ -68,6 +68,7 @@ internal static class ClipboardController
 
         Surface surfaceToCopy = null;
         RectD copyArea = RectD.Empty;
+        bool hadSelection = false;
 
         if (!document.SelectionPathBindable.IsEmpty)
         {
@@ -83,6 +84,7 @@ internal static class ClipboardController
 
             surfaceToCopy = surface.AsT2.Item1;
             copyArea = (RectD)surface.AsT2.Item2;
+            hadSelection = true;
         }
         else if (document.TransformViewModel.TransformActive || lastTransform != null)
         {
@@ -122,6 +124,11 @@ internal static class ClipboardController
             data.SetVecD(ClipboardDataFormats.PositionFormat, copyArea.Pos);
         }
 
+        if (hadSelection)
+        {
+            data.Set(ClipboardDataFormats.HadSelectionFormat, true);
+        }
+
         string[] layerIds = document.GetSelectedMembers().Select(x => x.ToString()).ToArray();
         string layerIdsString = string.Join(";", layerIds);
 
@@ -235,14 +242,18 @@ internal static class ClipboardController
         Guid[] layerIds = await GetLayerIds(data);
 
         bool hasPos = data.Any(x => x.Contains(ClipboardDataFormats.PositionFormat));
+        bool hadSelection = data.Any(x => x.Contains(ClipboardDataFormats.HadSelectionFormat));
 
         IDocument? targetDoc = manager.Documents.FirstOrDefault(x => x.Id == sourceDocument);
 
-        if (targetDoc != null && pasteAsNew && layerIds is { Length: > 0 } &&
+        if (targetDoc != null && !hadSelection && pasteAsNew && layerIds is { Length: > 0 } &&
             (!hasPos || await AllMatchesPos(layerIds, data, targetDoc)))
         {
             foreach (var layerId in layerIds)
             {
+                if (targetDoc.StructureHelper.Find(layerId) == null)
+                    continue;
+
                 if (sourceDocument == document.Id)
                 {
                     document.Operations.DuplicateMember(layerId);
@@ -495,7 +506,7 @@ internal static class ClipboardController
         {
             text = await importObj.GetDataAsync(DataFormats.Text) as string;
         }
-        catch(InvalidCastException ex) // bug on x11
+        catch (InvalidCastException ex) // bug on x11
         {
         }
 
@@ -848,7 +859,7 @@ internal static class ClipboardController
         data.Set(ClipboardDataFormats.DocumentFormat, Encoding.UTF8.GetBytes(docId.ToString()));
 
         byte[] idsBytes = Encoding.UTF8.GetBytes(string.Join(";", ids.Select(x => x.ToString())));
-        
+
         data.Set(format, idsBytes);
 
         await Clipboard.SetDataObjectAsync(data);