public static void LoadInto(XElement fullXml, DocumentEditorContext editorContext) { try { var contentXml = fullXml; var node = XmlNodeSerializer.Deserialize(contentXml); var serializationContext = new SerializationContext( new DescriptorsLookup(ParagraphBlock.Descriptor, (BlockDescriptor)HeadingBlock.Descriptor ) ); var mode = new CaretMovementMode(); mode.SetModeToEnd(); editorContext.Document.Root.Deserialize(serializationContext, node.Children.First()); editorContext.Selection.Replace(editorContext.Document.Root.GetView <IBlockView>().GetCaretFromBottom(mode)); } catch (Exception) { // ignored } }
/// <inheritdoc /> public override bool Activate(DocumentSelection cursor, CaretMovementMode movementMode, SelectionMode mode) { // first we try to move the cursor directly var caret = cursor.Start; var next = caret.MoveForward(); // try a simply to move the cursor forwards if (next.IsValid) { cursor.MoveTo(next, mode); return(true); } // try get the block backward to get a cursor that looks at the beginning of that block var block = caret.Block?.Parent.GetBlockTo(BlockDirection.Forward, caret.Block) as ContentBlock; if (block != null) { cursor.MoveTo(block.GetCaretAtStart(), mode); return(true); } // we couldn't do it return(false); }
/// <summary> Default constructor. </summary> public DocumentEditorContext(DocumentOwner owner) { Document = owner; var cursor = ((ContentBlock)Document.Root.FirstBlock).GetCaretAtStart(); Selection = new DocumentSelection(Document, cursor); CaretMovementMode = new CaretMovementMode(); UndoStack = new ActionStack(this, new StandardMergePolicy()); }
/// <inheritdoc /> public override bool Activate(DocumentSelection cursor, CaretMovementMode movementMode, SelectionMode mode) { var textCaret = cursor.Start.As <TextCaret>(); double desiredPosition = UpdateMovementMode(movementMode, textCaret); var contentView = textCaret.Block.GetViewOrNull <ITextBlockView>(); if (contentView == null) { return(false); } var currentLine = contentView.GetLineFor(textCaret); var nextLine = currentLine.Next; if (nextLine != null) { // TODO what if it's invalid (maybe only if the line couldn't be found?) var caret = nextLine.FindClosestTo(desiredPosition); if (!caret.IsValid) { return(false); } cursor.MoveTo(caret, mode); return(true); } var currentBlock = textCaret.Block; var nextBlock = currentBlock.Parent.GetBlockTo(BlockDirection.Bottom, currentBlock); if (nextBlock != null) { if (nextBlock.GetViewOrNull <IBlockView>() is IBlockView nextBlockView) { var newCursor = nextBlockView.GetCaretFromTop(movementMode); cursor.MoveTo(newCursor, mode); return(true); } else { return(false); } } // TODO work the way up the document // we couldn't do it return(false); }
/// <inheritdoc/> public override bool Activate(DocumentSelection cursor, CaretMovementMode movementMode, SelectionMode mode) { var textCaret = cursor.Start.As <TextCaret>(); movementMode.SetModeToHome(); var contentView = textCaret.Block.GetViewOrNull <ITextBlockView>(); if (contentView == null) { return(false); } textCaret = contentView.GetLineFor(textCaret).FindClosestTo(0); cursor.MoveTo(textCaret, mode); return(true); }
private double UpdateMovementMode(CaretMovementMode movementMode, TextCaret caret) { switch (movementMode.CurrentMode) { case CaretMovementMode.Mode.None: var position = TextCaretMeasurerHelper.Measure(caret).Left; movementMode.SetModeToPosition(position); return(position); case CaretMovementMode.Mode.Position: return(movementMode.Position); case CaretMovementMode.Mode.Home: return(0); case CaretMovementMode.Mode.End: return(double.MaxValue); default: throw new ArgumentOutOfRangeException(); } }
/// <inheritdoc /> public BlockCaret GetCaretFromTop(CaretMovementMode movementMode) => CollectionViewHelper.GetCaretFromTop(Children, movementMode);
/// <summary> Activates the given command, acting on the given caret. </summary> /// <param name="cursor"> The caret on which to act. </param> /// <param name="movementMode"> The current caret mode. </param> /// <param name="mode"></param> /// <param name="shouldExtend"> How the caret should be moved. </returns> public abstract bool Activate(DocumentSelection cursor, CaretMovementMode movementMode, SelectionMode mode);
public static BlockCaret GetCaretFromTop(UIElementCollection collection, CaretMovementMode movementMode) => ((IBlockView)collection.First()).GetCaretFromTop(movementMode);
/// <inheritdoc /> public BlockCaret GetCaretFromBottom(CaretMovementMode movementMode) => CollectionViewHelper.GetCaretFromBottom(_childContents.Children, movementMode);