public MainWindow()
        {
            if (!App.ok)
            {
                this.Close();
                return;
            }

            caption = Title;
            try
            {
                Item obj = JsonConvert.DeserializeObject <Item>(File.ReadAllText(App.filename));
                data = obj;
            }
            catch (Exception)
            {
                data = new Item()
                {
                    title = "Neu"
                };
            }

            //// debug reading
            //JsonSerializerSettings settings = new JsonSerializerSettings();
            //settings.NullValueHandling = NullValueHandling.Ignore;
            //string json = JsonConvert.SerializeObject(data, Formatting.Indented, settings);
            //File.WriteAllText(App.filename + "_old.json", json, Encoding.UTF8);

            InitializeComponent();
            tree.ContextMenu = MainWindow._createCM(true, tree);

            //if (showRoot)
            //{
            TVI tvi1 = new TVI(data.title, data.info, data.expanded, data.expandedContent, data.height, data.presentation);

            tree.Items.Add(tvi1);
            _addItems(tvi1, data.items);
            tvi1.IsExpanded = data.expanded;
            //}
            //else
            //{
            //    foreach (Item it in data.items)
            //    {
            //        TVI tvi1 = new TVI(it.title, it.info, it.expanded, it.expandedContent, it.height, it.presentation);
            //        tree.Items.Add(tvi1);
            //        _addItems(tvi1, it.items);
            //        tvi1.IsExpanded = it.expanded;
            //    }
            //}

            this.Closing += MainWindow_Closing;
        }
 private static void _addItems(TVI tviP, List <Item> items)
 {
     if (items != null)
     {
         foreach (Item it in items)
         {
             TVI tvi = new TVI(it.title, it.info, it.expanded, it.expandedContent, it.height, it.presentation);
             tviP.Items.Add(tvi);
             _addItems(tvi, it.items);
             tvi.IsExpanded = it.expanded;
         }
     }
 }
Beispiel #3
0
        internal ItemExport AsExportItem()
        {
            List <ItemExport> items = null;

            foreach (object obj in this.Items)
            {
                if (obj is TVI tvi)
                {
                    if (items == null)
                    {
                        items = new List <ItemExport>();
                    }
                    items.Add(tvi.AsExportItem());
                }
            }

            // detect type: empyty, presentation, presentation item
            string type           = null;
            bool   inPresentation = false;
            TVI    tviP           = this.Parent as TVI;

            while (tviP != null)
            {
                if (tviP.presentation)
                {
                    inPresentation = true;
                    break;
                }
                tviP = tviP.Parent as TVI;
            }
            if (presentation)
            {
                type = "presentation";
            }
            else if (inPresentation)
            {
                type = "presentation_item";
            }

            ItemExport item = new ItemExport()
            {
                type  = type,
                title = MainWindow._title(title),
                info  = presentation ? null : MainWindow._md2html(info),
                items = items
            };

            return(item);
        }
        private static void Cm_Opened(object sender, RoutedEventArgs e)
        {
            if (sender is ContextMenu cm)
            {
                foreach (Control mi0 in cm.Items)
                {
                    if (mi0 is MenuItem mi)
                    {
                        TVI      tvi  = mi.Tag as TVI;
                        TreeView tvP  = tvi?.Parent as TreeView;
                        TVI      tviP = tvi?.Parent as TVI;
                        switch (mi.Name)
                        {
                        case "move_up":
                            bool           enabled = false;
                            ItemCollection items   = tvP != null ? tvP.Items : tviP?.Items;
                            if (items != null)
                            {
                                // index 0 is container of editor, but not for level 1
                                int limit = tvP != null ? 0 : 1;
                                enabled = items.IndexOf(tvi) > limit;
                            }
                            mi.IsEnabled = enabled;
                            break;

                        case "move_down":
                            enabled = false;
                            items   = tvP != null ? tvP.Items : tviP?.Items;
                            if (items != null)
                            {
                                enabled = items.IndexOf(tvi) < items.Count - 1;
                            }
                            mi.IsEnabled = enabled;
                            break;

                        //case "add":
                        //    mi.IsEnabled = tviP == null || !tviP.presentation;
                        //    break;
                        case "add_presentation":
                            mi.IsEnabled = !tvi.presentation && (tviP == null || !tviP.presentation);
                            break;
                        }
                    }
                }
            }
        }
        private static void _save(TreeView tv, bool save)
        {
            try
            {
                string res, fn, msg;
                JsonSerializerSettings settings = new JsonSerializerSettings()
                {
                    Formatting        = Formatting.Indented,
                    NullValueHandling = NullValueHandling.Ignore
                };

                if (save)
                {
                    res = JsonConvert.SerializeObject(((TVI)tv.Items[0]).AsItem(), settings);
                    fn  = App.filename;
                    msg = "Gespeichert:" + Environment.NewLine + fn;
                }
                else
                {
                    res = JsonConvert.SerializeObject(((TVI)tv.Items[0]).AsExportItem(), settings);
                    fn  = App.filename.Replace(".work.", ".");
                    msg = "Exportiert:" + Environment.NewLine + fn;
                }

                File.WriteAllText(fn, res, Encoding.UTF8);

                if (save)
                {
                    TVI tvi = (TVI)tv.Items[0];
                    tvi.ResetDirty();
                }

                MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace,
                                caption + " - Fehler", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
        private static void Mi_Click(object sender, RoutedEventArgs e)
        {
            if (sender is MenuItem mi)
            {
                TreeView tv  = mi.Tag as TreeView;
                TVI      tvi = mi.Tag as TVI;

                switch (mi.Name)
                {
                case "add":
                    if (tv != null)
                    {
                        tv.Items.Add(new TVI("Neu", "", true, true, 0, false));
                        return;
                    }
                    if (tvi != null)
                    {
                        tvi.Items.Add(new TVI("Neu", "", false, false, 0, false));
                        return;
                    }
                    break;

                case "add_presentation":
                    if (tvi != null)
                    {
                        tvi.Items.Add(new TVI("Neue Präsentation", "", false, false, 0, true));
                        return;
                    }
                    break;

                case "del":
                    if (tvi != null)
                    {
                        if (MessageBoxResult.Yes == MessageBox.Show("Wollen Sie " + tvi.title + " wirklich löschen?", caption,
                                                                    MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No))
                        {
                            object parent = tvi.Parent;
                            if (parent is TreeView tvParent)
                            {
                                tvParent.Items.Remove(tvi);
                                return;
                            }
                            if (parent is TVI tviParent)
                            {
                                tviParent.Items.Remove(tvi);
                                return;
                            }
                        }
                        return;
                    }
                    break;

                case "move_up":
                    TreeView       tvP   = tvi?.Parent as TreeView;
                    TVI            tviP  = tvi?.Parent as TVI;
                    ItemCollection items = tvP != null ? tvP.Items : tviP?.Items;

                    if (items != null)
                    {
                        // index 0 is the container of the editors, but not for level 1
                        int limit = tvP != null ? 0 : 1,
                            pos   = items.IndexOf(tvi);
                        if (pos > limit)
                        {
                            items.RemoveAt(pos);
                            items.Insert(pos - 1, tvi);
                        }
                    }
                    break;

                case "move_down":
                    tvP   = tvi?.Parent as TreeView;
                    tviP  = tvi?.Parent as TVI;
                    items = tvP != null ? tvP.Items : tviP?.Items;

                    if (items != null)
                    {
                        int pos = items.IndexOf(tvi);
                        if (pos < items.Count - 1)
                        {
                            items.RemoveAt(pos);
                            items.Insert(pos + 1, tvi);
                        }
                    }
                    break;

                case "save":
                    _save(tv, true);
                    break;

                case "export":
                    _save(tv, false);
                    break;
                }
            }
        }
Beispiel #7
0
        private string _md2html(string text)
        {
            string txt = text;

            string str;

            if (string.IsNullOrEmpty(txt))
            {
                str = "&lt;&lt;empty&gt;&gt;";
            }
            else
            {
                string container = "{{content}}";

                // look for presentation item as ancestor
                bool inPresentation = false;
                TVI  tviP           = this.Parent as TVI;
                while (tviP != null)
                {
                    if (tviP.presentation)
                    {
                        inPresentation = true;
                        break;
                    }
                    tviP = tviP.Parent as TVI;
                }

                if (inPresentation)
                {
                    Regex           regexImgTxt = new Regex(@"^(!\[[^\]]+\]\([^\n]+\))(\r?\n)(.+)$", RegexOptions.Singleline);
                    Match           matchImgTxt = regexImgTxt.Match(txt);
                    GroupCollection grps        = matchImgTxt.Groups;

                    // check for presentation item with image and text
                    if (grps.Count == 4)
                    {
                        // group 0: everything
                        // group 1: image
                        // group 2: new line between image and text
                        // group 3: text
                        container = "<div class=\"presentation\">{{content}}</div>";
                        txt       = grps[1] + Environment.NewLine
                                    + "<div class=\"presentation_text\">" + grps[3] + "</div>";
                    }
                    else
                    {
                        container = "<div class=\"presentation full\">{{content}}</div>";
                    }
                }

                // inject markdown
                str = container.Replace("{{content}}", MainWindow._md2html(txt));

                // inject complete file name for image preview
                string basedir = @"file:///" + App.images;
                int    start   = 0;

                do
                {
                    start = str.IndexOf("<img ", start);
                    if (start >= 0)
                    {
                        int srcPos = str.IndexOf(" src=\"", start);
                        if (srcPos > 0)
                        {
                            int endPos = str.IndexOf("\"", srcPos + 6);
                            if (endPos > 0)
                            {
                                string src = basedir + str.Substring(srcPos + 6, endPos - srcPos - 6);
                                str = str.Substring(0, srcPos + 6) + src + str.Substring(endPos);
                            }
                        }
                        start += 10;
                    }
                } while (start > 0);
            }

            return(App.template.Replace("{{content}}", str));
        }