Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back-porting Parsing updates to v1 branch #34

Open
wants to merge 2 commits into
base: v13/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/.vs/
src/Limbo.Umbraco.Tables/bin/
src/Limbo.Umbraco.Tables/obj/
src/Limbo.Umbraco.Tables/obj/
/Custom.targets
24 changes: 17 additions & 7 deletions src/Limbo.Umbraco.Tables/Models/TableCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ namespace Limbo.Umbraco.Tables.Models;
/// </summary>
public class TableCell : TableObject {

/// <summary>
/// Gets a reference to the parent table.
/// </summary>
[JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public TableModel Table { get; }

/// <summary>
/// Gets the row index.
/// </summary>
Expand All @@ -25,7 +32,7 @@ public class TableCell : TableObject {
/// </summary>
[JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public TableRow Row { get; }
public TableRow Row => Table.Rows[RowIndex];

/// <summary>
/// Gets the column index.
Expand All @@ -39,7 +46,7 @@ public class TableCell : TableObject {
/// </summary>
[JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public TableColumn Column { get; }
public TableColumn Column => Table.Columns[ColumnIndex];

/// <summary>
/// Gets a reference to the column value.
Expand All @@ -65,15 +72,18 @@ public class TableCell : TableObject {
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public TableCellScope Scope { get; }

internal TableCell(JObject json, int rowIndex, TableRow row, int columnIndex, TableColumn column, TableModel model, TablesHtmlParser htmlParser, bool preview) : base(json) {
internal TableCell(JObject json, int rowIndex, int rowCount, int columnIndex, TableModel model, TablesHtmlParser htmlParser, bool preview) : base(json) {

Table = model;
RowIndex = rowIndex;
Row = row;
ColumnIndex = columnIndex;
Column = column;
Value = new HtmlString(json.GetString("value", x => htmlParser.Parse(x, preview))!);
Type = row.IsHeader || column.IsHeader ? TableCellType.Th : TableCellType.Td;

if (RowIndex == 0 && model.UseFirstRowAsHeader) {
if (rowIndex == 0 && model.UseFirstRowAsHeader) Type = TableCellType.Th;
else if (rowIndex == rowCount - 1 && model.UseLastRowAsFooter) Type = TableCellType.Th;
else if (columnIndex == 0 && model.UseFirstColumnAsHeader) Type = TableCellType.Th;

if (rowIndex == 0 && model.UseFirstRowAsHeader) {
Scope = TableCellScope.Col;
} else if (ColumnIndex == 0 && model.UseFirstColumnAsHeader) {
Scope = TableCellScope.Row;
Expand Down
5 changes: 2 additions & 3 deletions src/Limbo.Umbraco.Tables/Models/TableColumn.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Limbo.Umbraco.Tables.Models;

/// <summary>
/// Class representing a column in a <see cref="TableModel"/> value.
/// </summary>
public class TableColumn : TableObject {
public class TableColumn {

/// <summary>
/// Gets a reference to the parent <see cref="TableModel"/>.
Expand All @@ -29,7 +28,7 @@ public class TableColumn : TableObject {
[System.Text.Json.Serialization.JsonIgnore]
public bool IsHeader { get; }

internal TableColumn(int index, JObject json, TableModel table) : base(json) {
internal TableColumn(int index, TableModel table) {
Index = index;
Table = table;
IsHeader = index == 0 && table.UseFirstColumnAsHeader;
Expand Down
65 changes: 42 additions & 23 deletions src/Limbo.Umbraco.Tables/Models/TableModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -73,46 +74,64 @@ private TableModel(JObject json, TableConfiguration config, TablesHtmlParser htm
UseFirstColumnAsHeader = json.GetBoolean("useFirstColumnAsHeader") && config.AllowUseFirstColumnAsHeader;
UseLastRowAsFooter = json.GetBoolean("useLastRowAsFooter") && config.AllowUseLastRowAsFooter;

JArray rows = json.GetArrayOrNew("rows");
JArray? jsonCells = json.GetArray("cells");

Rows = json.GetArrayOrNew("rows")
.ForEach((i, x) => new TableRow(i, x, rows.Count, this))
.ToList();
TableRow[] rows = Array.Empty<TableRow>();
TableColumn[]? columns = null;
TableCell[][] cells = Array.Empty<TableCell[]>();

Columns = json.GetArrayOrNew("columns")
.ForEach((i, x) => new TableColumn(i, x, this))
.ToList();
if (jsonCells is not null) {

Cells = json
.GetArrayOrNew("cells")
.ForEach((i, x) => ParseCellRow(i, x, htmlParser, preview))
.ToList();
int rowCount = jsonCells.Count;

}
rows = new TableRow[rowCount];

#endregion
cells = new TableCell[rowCount][];

#region Member methods
int rowIndex = 0;

// ReSharper disable PossibleInvalidCastExceptionInForeachLoop

foreach (JArray array in jsonCells) {

int columnCount = array.Count;

rows[rowIndex] = new TableRow(rowIndex, rowCount, this);

cells[rowIndex] = new TableCell[columnCount];

private List<TableCell> ParseCellRow(int index, JArray array, TablesHtmlParser htmlParser, bool preview) {
int columnIndex = 0;

TableRow row = Rows[index];
foreach (JObject jsonCell in array) {

List<TableCell> temp = new();
columns ??= new TableColumn[columnCount];

for (int c = 0; c < array.Count; c++) {
columns[columnIndex] = new TableColumn(columnIndex, this);

int columnIndex = c;
TableColumn column = Columns[columnIndex];
cells[rowIndex][columnIndex] = new TableCell(jsonCell, rowIndex, rowCount, columnIndex, this, htmlParser, preview);

temp.Add(array.GetObject(c, x => new TableCell(x, index, row, columnIndex, column, this, htmlParser, preview))!);
columnIndex++;

}

rowIndex++;

}

// ReSharper restore PossibleInvalidCastExceptionInForeachLoop

}

return temp;
Rows = rows;
Columns = columns ?? Array.Empty<TableColumn>();
Cells = cells;

}

#endregion

#region Member methods

/// <inheritdoc />
public void WriteTo(TextWriter writer, HtmlEncoder encoder) {

Expand Down
5 changes: 2 additions & 3 deletions src/Limbo.Umbraco.Tables/Models/TableRow.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Limbo.Umbraco.Tables.Models;

/// <summary>
/// Class representing a row in a <see cref="TableModel"/> value.
/// </summary>
public class TableRow : TableObject {
public class TableRow {

/// <summary>
/// gets a reference to the parent <see cref="TableModel"/>.
Expand Down Expand Up @@ -44,7 +43,7 @@ public class TableRow : TableObject {
[System.Text.Json.Serialization.JsonIgnore]
public IReadOnlyList<TableCell> Cells => Table.Cells[Index];

internal TableRow(int index, JObject json, int rowsCount, TableModel table) : base(json) {
internal TableRow(int index, int rowsCount, TableModel table) {
Table = table;
Index = index;
IsHeader = index == 0 && table.UseFirstRowAsHeader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,55 @@


function loadTable() {

if (!$scope.model.value || !($scope.model.value instanceof Object)) return;
vm.table = $scope.model.value;

const t = $scope.model.value;

// If the current model doesn't contain a "cells" array or the array is empty, it means
// that we have an invalid model, which means we can stop further parsing of the model
if (!Array.isArray(t.cells) || t.cells.length === 0) {
vm.table = null;
return;
}

// If the "rows" array is missing, we can create it from the "cells" array. The "rows"
// array would for instance be missing if the value originates from the "Imulus.TableEditor",
// in which the data format is slightly different than this package.
let rows;
if (Array.isArray(t.rows)) {
rows = t.rows;
} else {
rows = [];
for (let i = 0; i < t.cells.length; i++) {
rows.push({});
}
}

// Similar to the "rows" array, we need to create the "columns" array is missing in the
// current table model
let columns;
if (Array.isArray(t.columns)) {
columns = t.columns;
} else {
columns = [];
if (rows.length > 0) {
for (let i = 0; i < t.cells[0].length; i++) {
columns.push({});
}
}
}

// Initialize the overall table object used by the property editor
vm.table = {
useFirstRowAsHeader: t.useFirstRowAsHeader === true,
useFirstColumnAsHeader: t.useFirstColumnAsHeader === true,
useLastRowAsFooter: t.useLastRowAsFooter === true,
columns,
rows,
cells: t.cells
};

}

function save() {
Expand Down