private async Task <Page> CreateTemplatePage()
        {
            // use a temporary scratch page to convert the HTML into OneNote XML by pasting the
            // HTML contents of the clipboard and let OneNote do its magic...

            var currentPageId = one.CurrentPageId;

            one.CreatePage(one.CurrentSectionId, out var pageId);
            await one.NavigateTo(pageId);

            // since the Hotkey message loop is watching all input, explicitly setting
            // focus on the OneNote main window provides a direct path for SendKeys
            Native.SetForegroundWindow(one.WindowHandle);

            new InputSimulator().Keyboard
            .ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V);

            // yeah this is dumb but have to wait for paste to complete
            await Task.Delay(200);

            var page = one.GetPage(pageId);

            one.DeleteHierarchy(pageId);

            await one.NavigateTo(currentPageId);

            return(page);
        }
Exemple #2
0
        public override async Task Execute(params object[] args)
        {
            using (one = new OneNote())
            {
                var section = one.GetSection();
                ns = one.GetNamespace(section);

                // find first selected - active page

                var active =
                    section.Elements(ns + "Page")
                    .FirstOrDefault(e => e.Attributes("isCurrentlyViewed").Any(a => a.Value.Equals("true")));

                if (active == null)
                {
                    UIHelper.ShowInfo(one.Window, "At least two pages must be selected to merge");
                    return;
                }

                var selections =
                    section.Elements(ns + "Page")
                    .Where(e =>
                           !e.Attributes("isCurrentlyViewed").Any() &&
                           e.Attributes("selected").Any(a => a.Value.Equals("all")))
                    .ToList();

                // get first selected (active) page and reference its quick styles, outline, size

                page = one.GetPage(active.Attribute("ID").Value);

                if (page.Root.Elements(ns + "Outline").Count() == 1 && SimplePages(selections))
                {
                    // result in a single outline with all content
                    ConcatenateContent(page, selections);
                }
                else
                {
                    // result in multiple outlines preserving relative positioning
                    AppendOutlines(page, selections);
                }

                // update page and section hierarchy

                await one.Update(page);

                foreach (var selection in selections)
                {
                    one.DeleteHierarchy(selection.Attribute("ID").Value);
                }
            }
        }
Exemple #3
0
        public override async Task Execute(params object[] args)
        {
            using (var one = new OneNote())
            {
                var section = one.GetSection();
                ns = one.GetNamespace(section);

                // find first selected - active page

                var active =
                    section.Elements(ns + "Page")
                    .FirstOrDefault(e => e.Attributes("isCurrentlyViewed").Any(a => a.Value.Equals("true")));

                if (active == null)
                {
                    UIHelper.ShowInfo(one.Window, "At least two pages must be selected to merge");
                    return;
                }

                var selections =
                    section.Elements(ns + "Page")
                    .Where(e =>
                           !e.Attributes("isCurrentlyViewed").Any() &&
                           e.Attributes("selected").Any(a => a.Value.Equals("all")));

                if (active == null)
                {
                    UIHelper.ShowInfo(one.Window, "At least two pages must be selected to merge");
                    return;
                }


                // get first selected (active) page and reference its quick styles, outline, size

                page = one.GetPage(active.Attribute("ID").Value);

                quickmap = page.GetQuickStyleMap();

                var offset = GetPageBottomOffset();

                // track running bottom as we add new outlines
                var maxOffset = offset;

                // find maximum z-offset
                var z = page.Root.Elements(ns + "Outline").Elements(ns + "Position")
                        .Attributes("z").Max(a => int.Parse(a.Value)) + 1;

                // merge each of the subsequently selected pages into the active page

                foreach (var selection in selections.ToList())
                {
                    var childPage = one.GetPage(selection.Attribute("ID").Value);

                    var map = page.MergeQuickStyles(childPage);

                    var childOutlines = childPage.Root.Elements(ns + "Outline");
                    if (childOutlines == null || !childOutlines.Any())
                    {
                        break;
                    }

                    var topOffset = childOutlines.Elements(ns + "Position")
                                    .Min(p => double.Parse(p.Attribute("y").Value, CultureInfo.InvariantCulture));

                    foreach (var childOutline in childOutlines)
                    {
                        // adjust position relative to new parent page outlines
                        var position = childOutline.Elements(ns + "Position").FirstOrDefault();
                        var y        = double.Parse(position.Attribute("y").Value, CultureInfo.InvariantCulture)
                                       - topOffset + offset + OutlineMargin;

                        position.Attribute("y").Value = y.ToString("#0.0", CultureInfo.InvariantCulture);

                        // keep track of lowest bottom
                        var size   = childOutline.Elements(ns + "Size").FirstOrDefault();
                        var bottom = y + double.Parse(size.Attribute("height").Value, CultureInfo.InvariantCulture);
                        if (bottom > maxOffset)
                        {
                            maxOffset = bottom;
                        }

                        position.Attribute("z").Value = z.ToString();
                        z++;

                        // remove its IDs so the page can apply its own
                        childOutline.Attributes("objectID").Remove();
                        childOutline.Descendants().Attributes("objectID").Remove();

                        page.ApplyStyleMapping(map, childOutline);

                        page.Root.Add(childOutline);
                    }

                    if (maxOffset > offset)
                    {
                        offset = maxOffset;
                    }
                }

                // update page and section hierarchy

                await one.Update(page);

                foreach (var selection in selections)
                {
                    one.DeleteHierarchy(selection.Attribute("ID").Value);
                }
            }
        }