示例#1
0
        public void CreateTableFromClipboardHtml(string html = null)
        {
            if (string.IsNullOrEmpty(html))
            {
                html = ClipboardHelper.GetHtmlFromClipboard();
                if (string.IsNullOrEmpty(html))
                {
                    html = ClipboardHelper.GetText();
                }
            }

            var parser = new TableParser();

            ObservableCollection <ObservableCollection <CellContent> > data = null;

            if (html.Contains("<tr>"))
            {
                data = parser.ParseHtmlToData(html);
            }
            else if (html.Contains("-|-") || html.Contains("- | -") || html.Contains(""))
            {
                data = parser.ParseMarkdownToData(html);
            }
            else if (html.Contains("-|-") || html.Contains("- | -") || html.Contains(""))
            {
                data = parser.ParseMarkdownToData(html);
            }
            else if (html.Contains("-+-"))
            {
                data = parser.ParseMarkdownGridTableToData(html);
            }

            if (data == null || data.Count < 1)
            {
                AppModel.Window.ShowStatusError("No HTML Table to process found...");
                return;
            }

            TableData = data;
            DataGridTableEditor.TableSource = TableData;
        }
示例#2
0
        public TableEditor(string tableHtml = null)
        {
            InitializeComponent();

            AppModel = mmApp.Model;

            mmApp.SetThemeWindowOverride(this);
            Owner = AppModel.Window;

            var data = new List <string[]>();

            if (tableHtml == null)
            {
                CreateInitialTableData();
            }
            else
            {
                var parser = new TableParser();
                TableData = parser.ParseMarkdownToData(tableHtml);
                if (tableHtml.StartsWith("+"))
                {
                    TableMode = "Grid Table";
                }
            }

            DataGridTableEditor.ParentWindow = this;
            DataGridTableEditor.AppModel     = mmApp.Model;
            DataGridTableEditor.TableSource  = TableData;

            ColumnKeyCommand = new CommandBase((parameter, command) =>
            {
            }, (p, c) => true);


            DataContext = this;
        }
        /// <summary>
        /// Re-Formats a Markdown table to nicely formatted output (size permitting)
        /// </summary>
        /// <param name="tableMarkdown"></param>
        /// <returns>formatted markdown, if it can't be formatted original is returned</returns>
        public string FormatMarkdownTable(string tableMarkdown)
        {
            var parser = new TableParser();
            var type   = parser.DetectTableType(tableMarkdown);

            if (type == MarkdownTableType.None)
            {
                return(null);
            }

            var tableData = ParseMarkdownToData(tableMarkdown);

            if (tableData == null)
            {
                return(tableMarkdown);
            }

            string output = null;

            switch (type)
            {
            case MarkdownTableType.Pipe:
                output = parser.ToPipeTableMarkdown(tableData);
                break;

            case MarkdownTableType.Grid:
                output = parser.ToGridTableMarkdown(tableData);
                break;

            case MarkdownTableType.Html:
                output = parser.ToTableHtml(tableData);
                break;
            }

            return(output);
        }
示例#4
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var name = (sender as Control).Name;

            if (sender == ButtonOk)
            {
                var parser = new TableParser();

                if (TableMode == "Grid Table")
                {
                    TableHtml = parser.ToGridTableMarkdown(TableData);
                }
                else if (TableMode == "HTML Table")
                {
                    TableHtml = parser.ToTableHtml(TableData);
                }
                else
                {
                    TableHtml = parser.ToPipeTableMarkdown(TableData);
                }

                Cancelled    = false;
                DialogResult = true;
                Close();
            }
            else if (sender == ButtonCancel)
            {
                Cancelled = true;
                Close();
            }
            else if (sender == ButtonPasteHtml)
            {
                CreateTableFromClipboardHtml();
            }
            else if (sender == ButtonImportCsv)
            {
                var form = new TableEditorCsvImport();
                form.Owner = this;
                form.ShowDialog();

                if (form.IsCancelled)
                {
                    return;
                }

                var parser = new TableParser();

                bool   deleteCsvFile = false;
                string csvFile       = form.CsvFilename;

                if (form.ImportFromClipboard)
                {
                    string csvText = ClipboardHelper.GetText();
                    csvFile = Path.GetTempFileName();
                    csvFile = Path.ChangeExtension(csvFile, "csv");
                    File.WriteAllText(csvFile, csvText);
                    deleteCsvFile = true;
                }


                var data = parser.ParseCsvFileToData(csvFile, form.CsvSeparator);
                if (data == null || data.Count < 1)
                {
                    AppModel.Window.ShowStatusError($"Couldn\'t open file {csvFile} or the file is empty.");
                    return;
                }

                TableData = data;
                DataGridTableEditor.TableSource = TableData;

                if (deleteCsvFile)
                {
                    File.Delete(csvFile);
                }
            }

            var focusedTextBox = FocusManager.GetFocusedElement(this) as TextBox;

            if (focusedTextBox == null)
            {
                return;
            }

            if ("MenuInsertColumnRight" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Right);
            }
            else if ("MenuInsertColumnLeft" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Left);
            }
            else if ("MenuDeleteColumn" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteColumn(pos.Row, pos.Column);
            }
            else if ("MenuInsertRowBelow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Below);
            }
            else if ("MenuInsertRowAbove" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Above);
            }
            else if ("MenuDeleteRow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteRow(pos.Row, pos.Column);
            }
        }
示例#5
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var name = (sender as Control).Name;

            if (sender == ButtonOk)
            {
                var parser = new TableParser();

                if (TableMode == "Grid Table")
                {
                    TableHtml = parser.ToGridTableMarkdown(TableData);
                }
                else if (TableMode == "HTML Table")
                {
                    TableHtml = parser.ToTableHtml(TableData);
                }
                else
                {
                    TableHtml = parser.ToPipeTableMarkdown(TableData);
                }

                Cancelled    = false;
                DialogResult = true;
                Close();
            }
            else if (sender == ButtonCancel)
            {
                Cancelled = true;
                Close();
            }
            else if (sender == ButtonPasteHtml)
            {
                CreateTableFromClipboardHtml();
            }
            else if (sender == ButtonImportCsv)
            {
                var fd = new OpenFileDialog
                {
                    DefaultExt = ".csv",
                    Filter     = "Csv files (*.csv)|*.csv|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = true,
                    RestoreDirectory = true,
                    Multiselect      = false,
                    Title            = "Open CsvFile"
                };
                if (!string.IsNullOrEmpty(mmApp.Configuration.LastFolder))
                {
                    fd.InitialDirectory = mmApp.Configuration.LastFolder;
                }

                var res = fd.ShowDialog();
                if (res == null || !res.Value)
                {
                    return;
                }

                var parser = new TableParser();
                var data   = parser.ParseCsvFileToData(fd.FileName);
                if (data == null || data.Count < 1)
                {
                    AppModel.Window.ShowStatusError($"Couldn\'t open file {fd.FileName} or the file is empty.");
                    return;
                }

                TableData = data;
                DataGridTableEditor.TableSource = TableData;
            }

            var focusedTextBox = FocusManager.GetFocusedElement(this) as TextBox;

            if (focusedTextBox == null)
            {
                return;
            }

            if ("MenuInsertColumnRight" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Right);
            }
            else if ("MenuInsertColumnLeft" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Left);
            }
            else if ("MenuDeleteColumn" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteColumn(pos.Row, pos.Column);
            }
            else if ("MenuInsertRowBelow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Below);
            }
            else if ("MenuInsertRowAbove" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Above);
            }
            else if ("MenuDeleteRow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteRow(pos.Row, pos.Column);
            }
        }
示例#6
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var name = (sender as Control).Name;

            if (sender == ButtonOk)
            {
                var parser = new TableParser();

                if (TableMode == "Grid Table")
                {
                    TableHtml = parser.ToGridTableMarkdown(TableData);
                }
                else if (TableMode == "HTML Table")
                {
                    TableHtml = parser.ToTableHtml(TableData);
                }
                else
                {
                    TableHtml = parser.ToPipeTableMarkdown(TableData);
                }

                Cancelled    = false;
                DialogResult = true;
                Close();
            }
            else if (sender == ButtonCancel)
            {
                Cancelled = true;
                Close();
            }
            else if (sender == ButtonPasteHtml)
            {
                CreateTableFromClipboardHtml();
            }



            var focusedTextBox = FocusManager.GetFocusedElement(this) as TextBox;

            if (focusedTextBox == null)
            {
                return;
            }

            if (sender == ButtonInsertColumnRight || "MenuInsertColumnRight" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Right);
            }
            else if (sender == ButtonInsertColumnLeft || "MenuInsertColumnLeft" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Left);
            }
            else if (sender == ButtonDeleteColumn || "MenuDeleteColumn" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteColumn(pos.Row, pos.Column);
            }
            else if (sender == ButtonInsertRowBelow || "MenuInsertRowBelow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Below);
            }
            else if (sender == ButtonInsertRowAbove || "MenuInsertRowAbove" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Above);
            }
            else if (sender == ButtonDeleteRow || "MenuDeleteRow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteRow(pos.Row, pos.Column);
            }
        }