Hi,
I need a functionality to allow users to add e a text that cannot be edited but can be deleted. It needs to have different style than ordinary text.
I Implemented it as.
private void AddHeading(string key)
{
if (HeadingsProvider == null)
return;
RtbEditor.BeginUndoGroup();
var box = RtbEditor.Document.GetLayoutBoxByPosition(RtbEditor.Document.CaretPosition.Location);
var element = box.AssociatedDocumentElement;
while (element is Inline && !(element is Span))
element = element.Parent;
while (element is Inline)
element = element.Parent;
var paragraph = element as Paragraph;
if (paragraph?.IsEmpty == false)
RtbEditor.InsertParagraph();
var headings = HeadingsProvider.GetHeadings(key).ToList();
for (int i = 0; i < headings.Count; i++)
{
RtbEditor.Document.Selection.Clear();
var lStart = RtbEditor.Document.CaretPosition.Location;
RtbEditor.InsertInline(new Span(" "));
RtbEditor.ChangeStyleName("HeadingChar", false);
RtbEditor.Insert(headings[i]);
RtbEditor.InsertInline(new Span(" "));
var lEnd = RtbEditor.Document.CaretPosition.Location;
var start = new DocumentPosition(RtbEditor.Document);
start.SetPosition(lStart);
var end = new DocumentPosition(RtbEditor.Document);
end.SetPosition(lEnd);
RtbEditor.Document.Selection.AddSelectionStart(start);
RtbEditor.Document.Selection.AddSelectionEnd(end);
RtbEditor.InsertReadOnlyRange();
RtbEditor.Document.Selection.Clear();
if (i < headings.Count - 1)
RtbEditor.InsertParagraph();
}
RtbEditor.EndUndoGroup();
}
private void RemoveHeading()
{
var box = RtbEditor.Document.GetLayoutBoxByPosition(RtbEditor.Document.CaretPosition.Location);
var span = box.AssociatedDocumentElement as Span;
if (span == null)
return;
var sibling = span.PreviousSibling;
while (sibling != null && !(sibling is ReadOnlyRangeStart) && !(sibling is ReadOnlyRangeEnd))
sibling = sibling.PreviousSibling;
var start = sibling as ReadOnlyRangeStart;
if (start == null)
return;
var end = start.End;
if (end == null)
return;
RtbEditor.Document.CaretPosition.MoveToInline(start);
var lStart = RtbEditor.Document.CaretPosition.Location;
RtbEditor.Document.CaretPosition.MoveToInline(end);
var lEnd = RtbEditor.Document.CaretPosition.Location;
var pStart = new DocumentPosition(RtbEditor.Document);
pStart.SetPosition(lStart);
var pEnd = new DocumentPosition(RtbEditor.Document);
pEnd.SetPosition(lEnd);
RtbEditor.BeginUndoGroup();
RtbEditor.DeleteReadOnlyRange(start);
RtbEditor.Document.Selection.Clear();
RtbEditor.Document.Selection.AddSelectionStart(pStart);
RtbEditor.Document.Selection.AddSelectionEnd(pEnd);
RtbEditor.Delete(false);
RtbEditor.EndUndoGroup();
}
The insert part kind of work. I need to insert an empty span before and after so that the special style not propagate when you type after the heading. I tried to insert Span(""), but it crashes the editor... I can live with " ".
The problem is that remove heading does not work. While the ReadOnlyRange is removed, the text is not. It remains in the RTB and it is selected. you need to click on another paragraph and then you can delete it manually, but then you ending with editable heading if you won't delete it manually.