Ejemplo n.º 1
0
        public void OnInsertCodeBox(IRibbonControl control)
        {
            OneNotePageHandler page = new OneNotePageHandler(app);
            var tElement            = page.GetCursorElement("T");
            var oeElement           = tElement.Parent;

            if (oeElement.Name.LocalName != "OE")
            {
                MessageBox.Show("Cannot Insert Code Box here");
                return;
            }

            var tableElement = XDocument.Parse(@"
<one:OE selected='partial' xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'>
    <one:Table bordersVisible='false' hasHeaderRow='false'>
        <one:Columns>
		    <one:Column index='0' width='37.11000061035156'/>
	    </one:Columns>
	    <one:Row>
		    <one:Cell shadingColor='#F2F2F2'>
			    <one:OEChildren selected='partial'>
				    <one:OE alignment='left' quickStyleIndex='0' selected='partial' style='font-family:Microsoft YaHei Mono;font-size:11.0pt'>
					    <one:T selected='all'><![CDATA[]]></one:T>
                    </one:OE>
			    </one:OEChildren>
		    </one:Cell>
	    </one:Row>
    </one:Table>
</one:OE>
            ").Root;

            int tElementsCount = oeElement.Elements().Count();

            if (tElementsCount == 1)
            {
                oeElement.ReplaceWith(tableElement);
            }
            else
            {
                int i = 0;
                while (oeElement.Elements().ElementAt(i) != tElement)
                {
                    i++;
                }
                tElement.Remove();
                page.SplitOEElement(oeElement, i);
                oeElement.AddAfterSelf(tableElement);
            }
            page.Save();
        }
Ejemplo n.º 2
0
        public void OnOpenInVSCode(IRibbonControl control)
        {
            OneNotePageHandler page = new OneNotePageHandler(app);
            string             text = page.GetSelectedText();
            var isCell = false;

            if (string.IsNullOrEmpty(text))
            {
                isCell = true;
                var cell = page.GetCursorElement("Cell");
                if (cell == null)
                {
                    MessageBox.Show("Please select text or set input cursor into a table.");
                    return;
                }
                else
                {
                    text = page.GetInnerText(cell);
                }
            }

            CopyToClipboard(text);
            VSCodeHandler codeHandler = new VSCodeHandler(setting.VSCode);

            if (codeHandler.EditCode(GetDefaultValue("cmbStyle"), out string newText))
            {
                CopyToClipboard(newText);
                FormatByVSCode(false);
                wordHandler.PasteAndCopy(true);
                if (isCell)
                {
                    // 单元格替换
                    SendKeys.SendWait("^(aav)");
                }
                else
                {
                    SendKeys.SendWait("^(v)");
                }
            }
        }
Ejemplo n.º 3
0
        public void OnSetFontClick(IRibbonControl control)
        {
            OneNotePageHandler page                     = new OneNotePageHandler(app);
            string             id                       = GetControlId(control).ToUpper();
            string             fontName                 = GetDefaultValue("cmbFont" + Regex.Match(id, @"\d+").Value);
            string             oneNoteFontValue         = fontName.Contains(" ") ? ("'" + fontName + "'") : fontName;
            string             htmlFontValue            = fontName.Contains(" ") ? ("\"" + fontName + "\"") : fontName;
            bool addFontFamilyIfNotExist                = false;
            Func <string, string, string> setFontFamily = (attrValue, fontFamilyName) =>
            {
                attrValue = attrValue ?? "";
                var match = Regex.Match(attrValue, @"font-family:(?:\r?\n)?(?:['""][^;]*['""]|[^;]*)(?:;|$)");
                if (match.Success)
                {
                    attrValue = attrValue.Remove(match.Groups[0].Index, match.Groups[0].Length);
                }

                if (match.Success || addFontFamilyIfNotExist)
                {
                    if (attrValue.Length == 0)
                    {
                        attrValue = $"font-family:{fontFamilyName}";
                    }
                    else
                    {
                        attrValue = $"font-family:{fontFamilyName};" + attrValue;
                    }
                }
                return(attrValue);
            };

            Action <HtmlAgilityPack.HtmlDocument> changeTextHtml = doc =>
            {
                foreach (var node in doc.DocumentNode.ChildNodes)
                {
                    foreach (var attr in node.Attributes)
                    {
                        if (attr.Name == "style")
                        {
                            string newValue = setFontFamily.Invoke(attr.Value, htmlFontValue);
                            attr.Value = newValue;
                            break;
                        }
                    }
                }
            };

            if (id.Contains("SELECTION"))
            {
                // set selection
                var selections = page.GetSelectedTextElement();
                if (selections.Count() == 1 && selections.First().Value == "")
                {
                    selections = selections.First().Parent.Elements();
                }
                addFontFamilyIfNotExist = true;
                page.SetAttributeValue(
                    selections,
                    "style",
                    (ref bool exist, ref string value) =>
                {
                    exist = true;
                    value = setFontFamily(value, oneNoteFontValue);
                });
                addFontFamilyIfNotExist = false;
                page.EnumTextHtml(selections, changeTextHtml);
            }
            else
            {
                // set page
                addFontFamilyIfNotExist = false;
                page.SetPageAttributes("style", (ref bool exist, ref string value) =>
                {
                    if (exist)
                    {
                        value = setFontFamily(value, oneNoteFontValue);
                    }
                });
                page.EnumPageTextHtml(changeTextHtml);
                page.SetQuickStyleDef("font", fontName);
            }

            page.Save();
        }