Explorar o código

内容编辑-文本选中快捷键

weixiangjie hai 9 meses
pai
achega
e68350d232

+ 202 - 133
Demo/Examples/Compdfkit_Tools/Edit/ContentEditCOntrol/ContentEditControl.xaml.cs

@@ -22,8 +22,10 @@ using System.Windows.Input;
 using System.Windows.Media;
 using static ComPDFKit.Tool.CPDFToolManager;
 using System.Windows.Media.Imaging;
+using ComPDFKit.Import;
 using ComPDFKit.Tool.Help;
 using ComPDFKit.Tool.UndoManger;
+using ComPDFKit.Viewer.Helper;
 
 namespace Compdfkit_Tools.PDFControl
 {
@@ -42,6 +44,9 @@ namespace Compdfkit_Tools.PDFControl
         public event PropertyChangedEventHandler PropertyChanged;
         public ICommand CloseTabCommand;
         public ICommand ExpandPropertyPanelCommand;
+        private CPoint startPoint;
+        private CPoint endPoint;
+        private bool isUpdateStartPoint = true;
 
         public bool CanUndo
         {
@@ -363,18 +368,18 @@ namespace Compdfkit_Tools.PDFControl
             FloatPageTool.InitWithPDFViewer(view);
             pdfContentEditControl.InitWithPDFViewer(view);
             DataContext = this;
-            //if(pdfViewer!=null)
-            //{
-            //    pdfViewer.EnableMultiSelectEdit = true;
-            //    pdfViewer.PDFEditMultiActiveHandler -= PDFEditMultiActiveHandler;
-            //    pdfViewer.PDFEditMultiActiveHandler += PDFEditMultiActiveHandler;
-            //    if (KeyDownHandler != null)
-            //    {
-            //        pdfViewer.RemoveHandler(KeyDownEvent, KeyDownHandler);
-            //    }
-            //    KeyDownHandler = new KeyEventHandler(PDFView_KeyDown);
-            //    pdfViewer.AddHandler(KeyDownEvent, KeyDownHandler, false, true);
-            //}
+            if(PdfViewControl != null)
+            {
+                PdfViewControl.PDFViewTool.IsManipulationEnabled = true;
+                //pdfViewer.PDFEditMultiActiveHandler -= PDFEditMultiActiveHandler;
+                //pdfViewer.PDFEditMultiActiveHandler += PDFEditMultiActiveHandler;
+                if (KeyDownHandler != null)
+                {
+                    PdfViewControl.PDFViewTool.RemoveHandler(KeyDownEvent, KeyDownHandler);
+                }
+                KeyDownHandler = new KeyEventHandler(PDFView_KeyDown);
+                PdfViewControl.PDFViewTool.AddHandler(KeyDownEvent, KeyDownHandler, false);
+            }
         }
 
         private void PDFEditEmptyPanel()
@@ -437,136 +442,200 @@ namespace Compdfkit_Tools.PDFControl
         //    }
         //}
 
-        //public void PDFView_KeyDown(object sender, KeyEventArgs e)
-        //{
-        //    if (PdfViewControl.PDFView.MouseMode != MouseModes.PDFEdit)
-        //    {
-        //        return;
-        //    }
-
-        //    if (Keyboard.Modifiers == ModifierKeys.Control)
-        //    {
-        //        if (e.Key == Key.Left)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, false);
-        //            e.Handled = true;
-        //        }
-
-        //        if (e.Key == Key.Right)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, false);
-        //            e.Handled = true;
-        //        }
-
-        //        if (e.Key == Key.Up)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, false);
-        //            e.Handled = true;
-        //        }
-
-        //        if (e.Key == Key.Down)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, false);
-        //            e.Handled = true;
-        //        }
-        //    }
-
-        //    if (Keyboard.Modifiers == ModifierKeys.Shift)
-        //    {
-        //        if (e.Key == Key.Left)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreCharPlace, true);
-        //            e.Handled = true;
-        //        }
-
-        //        if (e.Key == Key.Right)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextCharPlace, true);
-        //            e.Handled = true;
-        //        }
-
-        //        if (e.Key == Key.Up)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeUpCharPlace, true);
-        //            e.Handled = true;
-        //        }
-
-        //        if (e.Key == Key.Down)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeDownCharPlace, true);
-        //            e.Handled = true;
-        //        }
-        //    }
+        private CPoint GetPoint(CPDFEditTextArea textArea)
+        {
+            CPoint caretPoint = new CPoint();
+            CPoint caretPointHigh = new CPoint();
+            textArea.GetTextCursorPoints(ref caretPoint, ref caretPointHigh);
+            var lineHeight = caretPoint.y - caretPointHigh.y;
+            CRect caretRect = new CRect(caretPoint.x, caretPoint.y, caretPointHigh.x, caretPointHigh.y);
+            caretPoint = new CPoint(caretRect.left, caretRect.top);
+            return new CPoint(caretRect.left, (caretRect.top + caretRect.bottom) / 2);
+        }
 
-        //    if (Keyboard.Modifiers == ModifierKeys.Alt)
-        //    {
-        //        if (e.SystemKey == Key.Up)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLocationLineBegin, false);
-        //            e.Handled = true;
-        //        }
+        public void PDFView_KeyDown(object sender, KeyEventArgs e)
+        {
+            if (PdfViewControl.PDFViewTool.GetViewerModel() != MouseModes.kContentEdit)
+            {
+                return;
+            }
+            int pageIndex = -1;
+            CPDFEditTextArea textArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref pageIndex) as CPDFEditTextArea;
+            if(textArea == null)
+            {
+                return;
+            }
 
-        //        if (e.SystemKey == Key.Down)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeLineEnd, false);
-        //            e.Handled = true;
-        //        }
-        //    }
+            if (Keyboard.Modifiers == ModifierKeys.Control)
+            {
+                if (e.Key == Key.Left)
+                {
+                    textArea.GetPreWordCharPlace();
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
 
-        //    if (Keyboard.Modifiers == ModifierKeys.None)
-        //    {
-        //        if (e.Key == Key.Left)
-        //        {
-        //            PdfViewControl.PDFView.MoveEditArea(new Point(-5, 0));
-        //            e.Handled = true;
-        //        }
+                if (e.Key == Key.Right)
+                {
+                    textArea.GetNextWordCharPlace();
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
 
-        //        if (e.Key == Key.Right)
-        //        {
-        //            PdfViewControl.PDFView.MoveEditArea(new Point(5, 0));
-        //            e.Handled = true;
-        //        }
+                if (e.Key == Key.Up)
+                {
+                    textArea.GetSectionBeginCharPlace();
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
 
-        //        if (e.Key == Key.Up)
-        //        {
-        //            PdfViewControl.PDFView.MoveEditArea(new Point(0, -5));
-        //            e.Handled = true;
-        //        }
+                if (e.Key == Key.Down)
+                {
+                    textArea.GetSectionEndCharPlace();
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
+            }
 
-        //        if (e.Key == Key.Down)
-        //        {
-        //            PdfViewControl.PDFView.MoveEditArea(new Point(0, 5));
-        //            e.Handled = true;
-        //        }
-        //    }
+            if (Keyboard.Modifiers == ModifierKeys.Shift)
+            {
+                if (isUpdateStartPoint)
+                {
+                    startPoint = GetPoint(textArea);
+                    isUpdateStartPoint = false;
+                }
+                if (e.Key == Key.Left)
+                {
+                    textArea.GetPrevCharPlace();
+                    endPoint = GetPoint(textArea);
+                    textArea.GetSelectChars(startPoint, endPoint);
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
+                
+                if (e.Key == Key.Right)
+                {
+                    textArea.GetNextCharPlace();
+                    endPoint = GetPoint(textArea);
+                    textArea.GetSelectChars(startPoint, endPoint);
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
+                
+                if (e.Key == Key.Up)
+                {
+                    textArea.GetUpCharPlace();
+                    endPoint = GetPoint(textArea);
+                    textArea.GetSelectChars(startPoint, endPoint);
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
+                
+                if (e.Key == Key.Down)
+                {
+                    textArea.GetDownCharPlace();
+                    endPoint = GetPoint(textArea);
+                    textArea.GetSelectChars(startPoint, endPoint);
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
+            }
+                
+            if (Keyboard.Modifiers == ModifierKeys.Alt)
+            {
+                if (e.SystemKey == Key.Up)
+                {
+                    textArea.GetLineBeginCharPlace();
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
+                
+                if (e.SystemKey == Key.Down)
+                {
+                    textArea.GetLineEndCharPlace();
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
+            }
+                
+            // if (Keyboard.Modifiers == ModifierKeys.None)
+            // {
+            //     if (e.Key == Key.Left)
+            //     {
+            //         PdfViewControl.PDFView.MoveEditArea(new Point(-5, 0));
+            //         e.Handled = true;
+            //     }
+            //     
+            //     if (e.Key == Key.Right)
+            //     {
+            //         PdfViewControl.PDFView.MoveEditArea(new Point(5, 0));
+            //         e.Handled = true;
+            //     }
+            //     
+            //     if (e.Key == Key.Up)
+            //     {
+            //         PdfViewControl.PDFView.MoveEditArea(new Point(0, -5));
+            //         e.Handled = true;
+            //     }
+            //     
+            //     if (e.Key == Key.Down)
+            //     {
+            //         PdfViewControl.PDFView.MoveEditArea(new Point(0, 5));
+            //         e.Handled = true;
+            //     }
+            // }
+                
+            if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
+            {
+                if (isUpdateStartPoint)
+                {
+                    startPoint = GetPoint(textArea);
+                    isUpdateStartPoint = false;
+                }
+                if (e.Key == Key.Left)
+                {
+                    textArea.GetPreWordCharPlace();
+                    endPoint = GetPoint(textArea);
+                    textArea.GetSelectChars(startPoint, endPoint);
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
 
-        //    if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
-        //    {
-        //        if (e.Key == Key.Left)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, true);
-        //            e.Handled = true;
-        //        }
+                if (e.Key == Key.Right)
+                {
+                    textArea.GetNextWordCharPlace();
+                    endPoint = GetPoint(textArea);
+                    textArea.GetSelectChars(startPoint, endPoint);
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
 
-        //        if (e.Key == Key.Right)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, true);
-        //            e.Handled = true;
-        //        }
+                if (e.Key == Key.Up)
+                {
+                    textArea.GetSectionBeginCharPlace();
+                    endPoint = GetPoint(textArea);
+                    textArea.GetSelectChars(startPoint, endPoint);
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
 
-        //        if (e.Key == Key.Up)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, true);
-        //            e.Handled = true;
-        //        }
+                if (e.Key == Key.Down)
+                {
+                    textArea.GetSectionEndCharPlace();
+                    endPoint = GetPoint(textArea);
+                    textArea.GetSelectChars(startPoint, endPoint);
+                    UpdateCursor(textArea);
+                    e.Handled = true;
+                }
+            }
+        }
 
-        //        if (e.Key == Key.Down)
-        //        {
-        //            PdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, true);
-        //            e.Handled = true;
-        //        }
-        //    }
-        //}
+        private void UpdateCursor(CPDFEditTextArea textArea)
+        {
+            Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
+            PdfViewControl.PDFViewTool.UpDateRender(oldRect,textArea);
+        }
+        
         private void PdfViewControl_MouseRightButtonDownHandler(object sender, MouseEventObject e)
         {
             ContextMenu ContextMenu = PdfViewControl.GetRightMenu();
@@ -603,7 +672,7 @@ namespace Compdfkit_Tools.PDFControl
 
         private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
         {
-
+            isUpdateStartPoint = true;
             if (PdfViewControl.PDFToolManager.GetToolType() != ToolType.ContentEdit)
             {
                 PropertyContainer.Child = pdfContentEditControl;