private void CreateItemCloze() { try { var parentEl = Svc.SM.UI.ElementWdw.CurrentElement; if (parentEl == null || parentEl.Type == ElementType.Item) { return; } var selObj = ContentUtils.GetSelectionObject(); string selText = selObj?.htmlText; if (selObj == null || string.IsNullOrEmpty(selText)) { return; } var htmlCtrl = ContentUtils.GetFocusedHtmlCtrl(); var htmlDoc = htmlCtrl?.GetDocument(); if (htmlDoc == null) { return; } selObj.pasteHTML("[...]"); string questionChild = htmlDoc.body.innerHTML.Replace("[...]", "<SPAN class=cloze>[...]</SPAN>"); int MaxTextLength = 2000000000; selObj.moveEnd("character", MaxTextLength); selObj.moveStart("character", -MaxTextLength); selObj.findText("[...]"); selObj.select(); selObj.pasteHTML("<SPAN class=clozed>" + selText + "</SPAN>"); string parentContent = htmlDoc.body.innerHTML; htmlCtrl.Text = parentContent; var references = ReferenceParser.GetReferences(parentContent); CreateSMElement(RemoveReferences(questionChild), selText, references); } catch (RemotingException) { } }
/// <summary> /// Create the cloze hint. /// TODO: Add proper UI locking when methods are available /// </summary> private void CreateClozeHint() { /// It is not currently possible to modify an SM element without it being /// the currently displayed SM Element. Therefore to add the cloze hint /// this first generates the cloze, then navigates to /// the new cloze element to modify it, then returns to the original element // Cancel if there is no currently selected text if (ContentUtils.GetSelectedText().IsNullOrEmpty()) { return; } var wdw = OpenClozeHintWdw(); if (wdw.IsNull()) { return; } ClozeLocation Location = wdw.Location; string Hint = wdw.Hint; if (Hint.IsNullOrEmpty()) { return; } // The current element we are executing GenerateCloze on int restoreElementId = Svc.SM.UI.ElementWdw.CurrentElementId; // The newly created element int newClozeId = Svc.SM.UI.ElementWdw.GenerateCloze(); if (newClozeId < 0) { string msg = "Failed to create cloze: GenerateCloze method failed."; MessageBox.Show(msg); LogTo.Debug(msg); return; } // Move to the new cloze, add the cloze hint Svc.SM.UI.ElementWdw.GoToElement(newClozeId); // TODO: Loop over htmlCtrls for the first control with a cloze symbol var htmlCtrl = ContentUtils.GetClozeHtmlControl(); var text = htmlCtrl?.Text; var htmlDoc = htmlCtrl?.GetDocument(); var body = htmlDoc?.body as IHTMLBodyElement; if (text.IsNullOrEmpty() || htmlCtrl.IsNull() || body.IsNull()) { string msg = "Failed to create cloze: Failed to get text from the generated item"; MessageBox.Show(msg); LogTo.Error(msg); return; } string replacement = Location == ClozeLocation.Inside ? $"[{Hint}]" : $"[...]({Hint})"; htmlCtrl.Text = htmlCtrl.Text.Replace("[...]", replacement); // Return to the parent element Svc.SM.UI.ElementWdw.GoToElement(restoreElementId); }
private void ResurrectParent() { var currentElement = Svc.SM.UI.ElementWdw.CurrentElement; var parentOfCurrent = currentElement.Parent; var htmls = ContentUtils.GetHtmlCtrls(); if (htmls == null || htmls.Count < 2) { LogTo.Debug("Failed to resurrect. There are less than 2 html ctrls"); return; } var fst = htmls.FirstOrDefault(); var lst = htmls.LastOrDefault(); if (!fst.Text.Contains("[...]")) { LogTo.Debug("Failed to resurrect. There was no cloze marker found in the first html ctrl"); return; } var answer = lst.Text; // Replace cloze marker with answer text. var doc = new HtmlDocument(); doc.LoadHtml(fst.Text); var cloze = doc.DocumentNode.SelectSingleNode("//span[@class='cloze']"); if (cloze == null) { LogTo.Debug("Failed to resurrect. Failed to find the cloze span."); return; } cloze.Attributes["class"].Remove(); cloze.InnerHtml = answer; var question = doc.DocumentNode.OuterHtml; var refs = ReferenceParser.GetReferences(question); question = RemoveReferences(question); if (parentOfCurrent == null) { LogTo.Debug("Failed to resurrect parent. Parent element of current was null."); return; } bool ret = Svc.SM.Registry.Element.Add( out var value, ElemCreationFlags.ForceCreate, new ElementBuilder(ElementType.Topic, new ContentBase[] { new TextContent(true, question) }) .WithParent(parentOfCurrent) .WithPriority(Config.ResurrectedParentPriority) .WithReference(_ => refs) .DoNotDisplay()); if (ret) { LogTo.Debug("Successfully resurrected parent of cloze"); } else { LogTo.Error("Failed to resurrect parent of cloze"); } }