js-spread-grid 0.0.50 → 0.0.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-spread-grid",
3
- "version": "0.0.50",
3
+ "version": "0.0.53",
4
4
  "description": "Fast grid for js applications",
5
5
  "author": "Tomasz Rewak <tomasz.rewak@gmail.com>",
6
6
  "license": "MIT",
package/src/core/state.js CHANGED
@@ -120,7 +120,7 @@ function updateStateInternal(context) {
120
120
  const selection = cache('selection', getSelection, [selectedCells]);
121
121
  const highlight = cache('highlight', getSelection, [highlightedCells]);
122
122
  // TODO: addDataFormattingRules and addRenderFormattingRules should remove unnecessary rules
123
- const renderFormatting = cache('renderFormatting', getRenderFormatting, [dataFormatting, hoveredCell, focusedCell, selection, highlight, edition, resizableColumn, resizableRow, options.borderWidth]);
123
+ const renderFormatting = cache('renderFormatting', getRenderFormatting, [dataFormatting, hoveredCell, focusedCell, selection, highlight, edition, sortBy, resizableColumn, resizableRow, options.borderWidth]);
124
124
  const renderFormattingRules = cache('renderFormattingRules', getFormattingRules, [renderFormatting]);
125
125
  const renderFormatResolver = cache('renderFormatResolver', getFormatResolver, [renderFormattingRules, data, rows, columns, edition]);
126
126
  const inputFormatting = cache('inputFormatting', getInputFormatting, [dataFormatting]);
@@ -10,13 +10,23 @@ export default function getNewSortBy(sortBy, column, row, ctrlKey) {
10
10
  return column.key === stringifyId(columnId) && row.key === stringifyId(rowId);
11
11
  }
12
12
 
13
+ function isConflictingWithCurrentRule(rule) {
14
+ const columnId = 'columnId' in rule ? rule.columnId : 'HEADER';
15
+ const rowId = 'rowId' in rule ? rule.rowId : 'HEADER';
16
+
17
+ return column.type === 'HEADER' && column.key === stringifyId(columnId)
18
+ || row.type === 'HEADER' && row.key === stringifyId(rowId);
19
+ }
20
+
13
21
  const directionLoop = ['ASC', 'DESC', undefined];
14
22
  const currentRule = sortBy.find(isCurrentRule);
15
23
  const directionIndex = directionLoop.indexOf(currentRule?.direction);
16
24
  const newDirection = directionLoop[(directionIndex + 1) % directionLoop.length];
17
25
  const isLastRule = sortBy.indexOf(currentRule) === sortBy.length - 1;
18
26
  const shouldKeepOld = ctrlKey && (isLastRule || !currentRule);
19
- const rulesToKeep = shouldKeepOld ? sortBy.filter(rule => !isCurrentRule(rule)) : [];
27
+ const rulesToKeep = shouldKeepOld
28
+ ? sortBy.filter(rule => !isCurrentRule(rule))
29
+ : sortBy.filter(rule => !isConflictingWithCurrentRule(rule));
20
30
  const newRules = newDirection ? [{ columnId: column.id, rowId: row.id, direction: newDirection }] : [];
21
31
 
22
32
  return [...rulesToKeep, ...newRules];
@@ -7,7 +7,7 @@ function getHighlightColor(baseColor, isStrong) {
7
7
  return baseColor + '33';
8
8
  }
9
9
 
10
- export default function getRenderFormatting(formatting, hoveredCell, focusedCell, selection, highlight, edition, resizableColumn, resizableRow, borderWidth) {
10
+ export default function getRenderFormatting(formatting, hoveredCell, focusedCell, selection, highlight, edition, sortBy, resizableColumn, resizableRow, borderWidth) {
11
11
  const focusedColumnKey = focusedCell ? stringifyId(focusedCell.columnId) : null;
12
12
  const focusedRowKey = focusedCell ? stringifyId(focusedCell.rowId) : null;
13
13
  const isResizingColumn = resizableColumn !== null;
@@ -103,6 +103,11 @@ export default function getRenderFormatting(formatting, hoveredCell, focusedCell
103
103
  row: { id: focusedCell?.rowId },
104
104
  style: { highlight: '#ffffffaa' }
105
105
  }),
106
+ ...sortBy.map(({ columnId, rowId, direction }, index) => ({
107
+ column: { id: columnId },
108
+ row: { id: rowId },
109
+ style: { highlight: '#0377fc44' }
110
+ })),
106
111
  {
107
112
  column: { type: 'ANY' },
108
113
  row: { type: 'ANY' },
@@ -32,6 +32,16 @@ function getText(context) {
32
32
  return '';
33
33
  }
34
34
 
35
+ function getEdit(rule, context) {
36
+ if (rule.edit === false)
37
+ return undefined;
38
+ if (rule.edit === true)
39
+ return 'edit' in context ? context.edit : defaultEdit;
40
+ if ('edit' in context)
41
+ return { ...context.edit, ...rule.edit };
42
+ return { ...defaultEdit, ...rule.edit };
43
+ }
44
+
35
45
  // TODO: Rename to FormatResolver
36
46
  // TODO: Optimize by not searching using keys that don't have correlated type rules
37
47
  // TODO: Accept both a function and an object as a style (where the object is a resolved style)
@@ -96,7 +106,7 @@ export default class FormattingRules {
96
106
  if ('padding' in rule)
97
107
  padding = { ...padding, ...rule.padding(context) };
98
108
  if ('edit' in rule)
99
- context = { ...context, edit: rule.edit && 'edit' in context ? { ...context.edit, ...rule.edit } : { ...defaultEdit, ...rule.edit } };
109
+ context = { ...context, edit: getEdit(rule, context) };
100
110
  if ('draw' in rule) {
101
111
  const currentContext = context;
102
112
  draw = (ctx) => rule.draw({ ...currentContext, ctx });
@@ -116,7 +126,7 @@ export default class FormattingRules {
116
126
 
117
127
  if ('value' in context)
118
128
  result.value = context.value;
119
- if ('edit' in context)
129
+ if ('edit' in context && context.edit !== undefined)
120
130
  result.edit = context.edit;
121
131
  if (draw !== undefined)
122
132
  result.draw = draw;
@@ -1,7 +1,5 @@
1
1
  import RulesLookup from "./RulesLookup.js";
2
-
3
- // const defaultComparatorAsc = (lhs, rhs) => lhs.value < rhs.value;
4
- // const defaultComparatorDesc = (lhs, rhs) => lhs.value > rhs.value;
2
+ import stringifyId from "../core-utils/stringifyId.js";
5
3
 
6
4
  function defaultComparatorAsc(lhs, rhs) {
7
5
  if (lhs.value == undefined)
@@ -24,10 +22,17 @@ export default class SortingRules {
24
22
  this.rulesLookup = new RulesLookup();
25
23
 
26
24
  for (const rule of rules) {
25
+ const comparatorAsc = rule.comparator
26
+ ? (lhs, rhs) => rule.comparator(lhs, rhs)
27
+ : (lhs, rhs) => defaultComparatorAsc(lhs, rhs);
28
+ const comparatorDesc = rule.comparator
29
+ ? (lhs, rhs) => !rule.comparator(lhs, rhs)
30
+ : (lhs, rhs) => defaultComparatorDesc(lhs, rhs);
31
+
27
32
  const entry = {
28
33
  by: stringifyId('by' in rule ? rule.by : 'HEADER'),
29
- comparatorAsc: rule.comparator || defaultComparatorAsc,
30
- comparatorDesc: (lhs, rhs) => -rule.comparator(lhs, rhs) || defaultComparatorDesc
34
+ comparatorAsc: comparatorAsc,
35
+ comparatorDesc: comparatorDesc
31
36
  };
32
37
 
33
38
  this.rulesLookup.addRule(rule.column, rule.row, entry);
@@ -55,6 +60,9 @@ export default class SortingRules {
55
60
 
56
61
  const rule = rules[0];
57
62
 
63
+ if (!sortByLookup.has(rule.by))
64
+ return null;
65
+
58
66
  return sortByLookup.get(rule.by) === 'ASC'
59
67
  ? rule.comparatorAsc
60
68
  : rule.comparatorDesc;