Ejemplo n.º 1
0
        public void Execute()
        {
            using (var dialog = new EmojiDialog())
            {
                var result = dialog.ShowDialog(owner);

                if (result == DialogResult.OK)
                {
                    var codes = dialog.GetSelectedCodes();

                    using (var manager = new ApplicationManager())
                    {
                        var page = manager.CurrentPage();
                        var ns   = page.GetNamespaceOfPrefix("one");

                        var title = page.Element(ns + "Title")?
                                    .Elements(ns + "OE")?.Elements(ns + "T").FirstOrDefault();

                        if (title != null)
                        {
                            if (title.FirstNode?.NodeType == XmlNodeType.CDATA)
                            {
                                var wrapper = XElement.Parse("<x>" + title.FirstNode.Parent.Value + "</x>");
                                var wns     = wrapper.GetDefaultNamespace();

                                var emojii =
                                    (from e in wrapper.Elements(wns + "span")
                                     where e.Attributes("style").Any(a => a.Value.Contains("Segoe UI Emoji"))
                                     select e).FirstOrDefault();

                                if (emojii != null)
                                {
                                    emojii.Value = string.Join(string.Empty, codes) + emojii.Value;
                                }
                                else
                                {
                                    wrapper.AddFirst(new XElement("span",
                                                                  new XAttribute("style", "font-family:'Segoe UI Emoji';font-size:16pt;"),
                                                                  string.Join(string.Empty, codes)
                                                                  ));
                                }

                                var decoded = string.Concat(wrapper.Nodes()
                                                            .Select(x => x.ToString()).ToArray())
                                              .Replace("&amp;", "&");

                                title.FirstNode.ReplaceWith(new XCData(decoded));
                                manager.UpdatePageContent(page);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void AddFavorite()
        {
            XElement root;

            if (File.Exists(path))
            {
                root = XElement.Load(path, LoadOptions.None);
            }
            else
            {
                root = MakeMenuRoot();
            }

            using (var one = new OneNote())
            {
                var info = one.GetPageInfo();

                var name = EmojiDialog.RemoveEmojis(info.Name);
                if (name.Length > 50)
                {
                    name = name.Substring(0, 50) + "...";
                }

                // similar to mongo ObjectId, a random-enough identifier for our needs
                var id = ((DateTimeOffset.Now.ToUnixTimeSeconds() << 32)
                          + new Random().Next()).ToString("x");

                root.Add(new XElement(ns + "splitButton",
                                      new XAttribute("id", $"omFavorite{id}"),
                                      new XElement(ns + "button",
                                                   new XAttribute("id", $"omFavoriteLink{id}"),
                                                   new XAttribute("onAction", "NavigateToFavorite"),
                                                   new XAttribute("imageMso", "FileLinksToFiles"),
                                                   new XAttribute("label", name),
                                                   new XAttribute("tag", info.Link),
                                                   new XAttribute("screentip", info.Path)
                                                   ),
                                      new XElement(ns + "menu",
                                                   new XAttribute("id", $"omFavoriteMenu{id}"),
                                                   new XElement(ns + "button",
                                                                new XAttribute("id", $"omFavoriteRemoveButton{id}"),
                                                                new XAttribute("onAction", "RemoveFavorite"),
                                                                new XAttribute("label", "Remove this item"),
                                                                new XAttribute("imageMso", "HyperlinkRemove"),
                                                                new XAttribute("tag", $"omFavorite{id}")
                                                                )
                                                   )
                                      ));

                // sort by name/label
                var items =
                    from e in root.Elements(ns + "splitButton")
                    let key = e.Element(ns + "button").Attribute("label").Value
                              orderby key
                              select e;

                root = MakeMenuRoot();
                foreach (var item in items)
                {
                    root.Add(item);
                }

                logger.WriteLine($"Saving favorite '{info.Path}' ({info.Link})");
            }

            try
            {
                PathFactory.EnsurePathExists(PathFactory.GetAppDataPath());
                root.Save(path, SaveOptions.None);

                ribbon.InvalidateControl("ribFavoritesMenu");
            }
            catch (Exception exc)
            {
                logger.WriteLine($"Cannot save {path}");
                logger.WriteLine(exc);
            }
        }
Ejemplo n.º 3
0
        // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
        // Pages

        private void SortPages(SortDialog.Sortings sorting, SortDialog.Directions direction)
        {
            #region Notes

            /*
             * <one:Page ID="" name="Notes" pageLevel="1" />
             *
             * Pages within a section are stored as a flat list of elements with
             * indented pages indicated by pageLevel only - they are not recursive children.
             * So the code below must group child pages with their parent so all parents
             * can be sorted correctly. Children are not sorted.
             */
            #endregion Notes

            logger.StartClock();

            using (var one = new OneNote())
            {
                var root = one.GetSection();
                var ns   = one.GetNamespace(root);

                var pages = new List <PageNode>();

                foreach (var child in root.Elements(ns + "Page"))
                {
                    if (child.Attribute("pageLevel").Value == "1")
                    {
                        // found the next parent page
                        pages.Add(new PageNode(child));
                    }
                    else
                    {
                        // grouping child pages with the top parent
                        pages[pages.Count - 1].Children.Add(child);
                    }
                }

                if (direction == SortDialog.Directions.Descending)
                {
                    if (sorting == SortDialog.Sortings.ByName)
                    {
                        pages = pages.OrderByDescending(
                            p => EmojiDialog.RemoveEmojis(p.Page.Attribute("name").Value)).ToList();
                    }
                    else
                    {
                        var key = sorting == SortDialog.Sortings.ByCreated
                                                        ? "dateTime" : "lastModifiedTime";

                        pages = pages.OrderByDescending(
                            p => p.Page.Attribute(key).Value).ToList();
                    }
                }
                else
                {
                    if (sorting == SortDialog.Sortings.ByName)
                    {
                        pages = pages.OrderBy(
                            p => EmojiDialog.RemoveEmojis(p.Page.Attribute("name").Value)).ToList();
                    }
                    else
                    {
                        var key = sorting == SortDialog.Sortings.ByCreated
                                                        ? "dateTime" : "lastModifiedTime";

                        pages = pages.OrderBy(
                            p => p.Page.Attribute(key).Value).ToList();
                    }
                }

                root.RemoveNodes();

                // recreate flat list
                foreach (var page in pages)
                {
                    root.Add(page.Page);

                    foreach (var child in page.Children)
                    {
                        root.Add(child);
                    }
                }

                //logger.WriteLine(root.ToString());
                one.UpdateHierarchy(root);
            }

            logger.WriteTime(nameof(SortPages));
        }