@fw-components/formula-editor 2.0.7-formula-editor-enhancements.0 → 2.0.7-formula-editor-enhancements.2

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.
@@ -74,7 +74,8 @@ let FormulaEditor = class FormulaEditor extends LitElement {
74
74
  this.parseInput(this._recommendations[0]);
75
75
  }
76
76
  else if (event.code === "ArrowDown" || event.code === "ArrowUp") {
77
- event.preventDefault();
77
+ if (this._recommendations)
78
+ event.preventDefault();
78
79
  this.navigateRecommendations(event.code);
79
80
  this.requestUpdate();
80
81
  }
@@ -52,10 +52,15 @@ export class Parser {
52
52
  tokens?.forEach((token) => {
53
53
  // It is a number is either it's in the defined variables, or
54
54
  // it's a valid number literal.
55
- let isNumber = this.variables.has(token) || !Number.isNaN(Number(token));
55
+ let isNumber = token.trim() !== "" && (this.variables.has(token) || !Number.isNaN(Number(token)));
56
56
  let isOperator = this.mathematicalOperators.has(token);
57
57
  let isSpace = token.trim() == "";
58
58
  let isBracket = token == "(" || token == ")";
59
+ if (isSpace) {
60
+ formattedString = `${formattedString}${token}`;
61
+ currentPosition += token.length;
62
+ return;
63
+ }
59
64
  // If the cursor position is 'inside` the current token:
60
65
  //
61
66
  // 1. If we've got a recommendation to add, simply replace the
@@ -67,21 +72,26 @@ export class Parser {
67
72
  // Since we are sure that the recommendation will always correspond
68
73
  // to a variable.
69
74
  isNumber = true;
70
- if (this.mathematicalOperators.has(token))
71
- recommendation = token + recommendation;
75
+ if (this.mathematicalOperators.has(token)) {
76
+ // append recommendation at the end if token is an operator
77
+ const updatedTokenString = `${token} ${recommendation}`;
78
+ formattedString += updatedTokenString;
79
+ currentPosition += updatedTokenString.length;
80
+ parseOutput.newCursorPosition = currentPosition;
81
+ recommendation = null;
82
+ return;
83
+ }
84
+ ;
72
85
  // If the new cursor length somehow becomes larger than the
73
86
  // length of the formula string, setting the caret to that
74
87
  // length will move the caret to the start. Although this overflow
75
88
  // won't happen, but still, this check prevents that.
76
- const updatedTokenLength = recommendation.length -
77
- (isSpace ? 0 : token.length);
89
+ const updatedTokenLength = recommendation.length - token.length;
78
90
  parseOutput.newCursorPosition = Math.min(parseOutput.newCursorPosition, formula.length) + updatedTokenLength;
79
- token = isSpace ? ` ${recommendation}` : recommendation;
91
+ token = recommendation;
80
92
  recommendation = null;
81
93
  }
82
- if (!isSpace)
83
- parseOutput.recommendations = this._recommender.getRecommendation(token);
84
- previousToken = isSpace ? previousToken : token;
94
+ parseOutput.recommendations = this.mathematicalOperators.has(token) ? Array.from(this.variables.keys()) : this._recommender.getRecommendation(token);
85
95
  }
86
96
  let tokenClassName = "";
87
97
  // Don't check for errors if an error has already been encountered.
@@ -98,6 +108,7 @@ export class Parser {
98
108
  // be an error if the formula ends with them.
99
109
  else if (expectation == Expectation.VARIABLE &&
100
110
  !isNumber &&
111
+ !isSpace &&
101
112
  token != "(" &&
102
113
  !((token == "-" || token == "+") &&
103
114
  (!currentTokens.trim() || this.mathematicalOperators.has(previousToken)))) {
@@ -109,13 +120,14 @@ export class Parser {
109
120
  // Having a ) is fine. Eg: `23)` might be representing `(23 + 23)
110
121
  else if (expectation == Expectation.OPERATOR &&
111
122
  !isOperator &&
123
+ !isSpace &&
112
124
  token != ")") {
113
125
  parseOutput.errorString = `Expected mathematical operator at position ${currentPosition}`;
114
126
  tokenClassName += " error";
115
127
  expectation = Expectation.UNDEFINED;
116
128
  }
117
129
  // Unknown symbol/variable/word
118
- else if (!(isNumber || isOperator || isBracket)) {
130
+ else if (!(isNumber || isOperator || isBracket || isSpace)) {
119
131
  parseOutput.errorString = `Unknown word at position ${currentPosition}`;
120
132
  tokenClassName += " error";
121
133
  expectation = Expectation.UNDEFINED;
@@ -148,7 +160,12 @@ export class Parser {
148
160
  expectation = Expectation.OPERATOR;
149
161
  }
150
162
  }
163
+ if (token == "(")
164
+ parentheses.push(currentPosition);
165
+ else if (token == ")")
166
+ parentheses.pop();
151
167
  formattedString = `${formattedString}${token}`;
168
+ previousToken = token;
152
169
  currentPosition += token.length;
153
170
  currentTokens += token;
154
171
  });
@@ -8,9 +8,8 @@ export const FormulaEditorStyles = css `
8
8
  }
9
9
 
10
10
  #wysiwyg-editor {
11
- display: inline-block;
12
- resize: vertical;
13
- overflow-y: auto;
11
+ display: block;
12
+ resize: none;
14
13
  padding: var(--fe-padding, 4px);
15
14
  caret-color: var(--fe-caret-color, #fff);
16
15
  color: var(--fe-text-color, #f7f1ff);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fw-components/formula-editor",
3
- "version": "2.0.7-formula-editor-enhancements.0",
3
+ "version": "2.0.7-formula-editor-enhancements.2",
4
4
  "description": "A WYSIWYG type formula editor",
5
5
  "main": "dist/formula-editor/src/formula-builder.js",
6
6
  "publishConfig": {
@@ -25,5 +25,5 @@
25
25
  "@types/big.js": "^6.1.6",
26
26
  "es-dev-server": "^2.1.0"
27
27
  },
28
- "gitHead": "44b278fd46fea9fb963b5320e52e9c4d896a76da"
28
+ "gitHead": "d1d3c9cad9faf31d3ef0192c0c1d0f6604be41e2"
29
29
  }