@fw-components/formula-editor 2.0.7-formula-editor.38 → 2.0.7-formula-editor.40
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.
|
@@ -19,19 +19,17 @@ let FormulaEditor = class FormulaEditor extends LitElement {
|
|
|
19
19
|
/**
|
|
20
20
|
* Text area input value
|
|
21
21
|
*/
|
|
22
|
-
this.
|
|
22
|
+
this.formulaString = "";
|
|
23
23
|
this.placeholder = "Type your formula...";
|
|
24
24
|
this.recommendationLabels = new Map();
|
|
25
25
|
this.variables = new Map();
|
|
26
26
|
this.minSuggestionLen = 2;
|
|
27
27
|
this.errorString = "";
|
|
28
|
-
this.formulaRegex = /'[^']*'|[A-Za-z0-9_#@]+|[-+(),*^/\s]/g;
|
|
29
28
|
this.allowedNumbers = true;
|
|
30
|
-
this.allowedOperators = new Set(["^", "+", "-", "*", "/"]);
|
|
31
29
|
}
|
|
32
30
|
updated(_changedProperties) {
|
|
33
|
-
if (_changedProperties.has("
|
|
34
|
-
if (!this.
|
|
31
|
+
if (_changedProperties.has("formulaString")) {
|
|
32
|
+
if (!this.formulaString?.trim()) {
|
|
35
33
|
this.recommendations = Array.from(this.variables.keys());
|
|
36
34
|
}
|
|
37
35
|
this._adjustTextAreaHeight();
|
|
@@ -47,11 +45,11 @@ let FormulaEditor = class FormulaEditor extends LitElement {
|
|
|
47
45
|
handleContentUpdate(event) {
|
|
48
46
|
event.preventDefault();
|
|
49
47
|
this.lastInputType = event.inputType;
|
|
50
|
-
this.
|
|
48
|
+
this.formulaString = event.target.value;
|
|
51
49
|
this.parseInput();
|
|
52
50
|
}
|
|
53
51
|
_adjustTextAreaHeight() {
|
|
54
|
-
if (!this.
|
|
52
|
+
if (!this.formulaString)
|
|
55
53
|
this.editor.style.height = "var(--fe-height, 30px)";
|
|
56
54
|
if (this.editor.scrollHeight > this.editor.clientHeight)
|
|
57
55
|
this.editor.style.height = String(this.editor.scrollHeight + 5).concat("px");
|
|
@@ -63,7 +61,7 @@ let FormulaEditor = class FormulaEditor extends LitElement {
|
|
|
63
61
|
*/
|
|
64
62
|
parseInput(recommendation = "") {
|
|
65
63
|
this.currentCursorPosition = this.editor.selectionStart;
|
|
66
|
-
const { recommendations, errorString, formattedString, newCursorPosition } = this._parser.parseInput(this.
|
|
64
|
+
const { recommendations, errorString, formattedString, newCursorPosition } = this._parser.parseInput(this.formulaString, this.currentCursorPosition, recommendation);
|
|
67
65
|
this.recommendations = recommendations;
|
|
68
66
|
this.errorString = errorString;
|
|
69
67
|
/**
|
|
@@ -74,7 +72,7 @@ let FormulaEditor = class FormulaEditor extends LitElement {
|
|
|
74
72
|
* @see https://bugs.chromium.org/p/chromium/issues/detail?id=689541
|
|
75
73
|
*/
|
|
76
74
|
if (this.lastInputType !== "insertCompositionText" || recommendation) {
|
|
77
|
-
this.
|
|
75
|
+
this.formulaString = formattedString;
|
|
78
76
|
}
|
|
79
77
|
if (Boolean(recommendation)) {
|
|
80
78
|
this.recommendations = [];
|
|
@@ -86,19 +84,19 @@ let FormulaEditor = class FormulaEditor extends LitElement {
|
|
|
86
84
|
}
|
|
87
85
|
this.dispatchEvent(new CustomEvent("fw-formula-content-changed", {
|
|
88
86
|
detail: {
|
|
89
|
-
formulaString: this.
|
|
87
|
+
formulaString: this.formulaString,
|
|
90
88
|
error: this.errorString,
|
|
91
89
|
recommendations: this.recommendations,
|
|
92
|
-
formulaTokens: getFormulaTokens(this.
|
|
90
|
+
formulaTokens: getFormulaTokens(this.formulaString || "", this.formulaRegex)
|
|
93
91
|
},
|
|
94
92
|
bubbles: true,
|
|
95
93
|
}));
|
|
96
94
|
}
|
|
97
95
|
formatFormula() {
|
|
98
|
-
if (!this.
|
|
96
|
+
if (!this.formulaString)
|
|
99
97
|
return;
|
|
100
|
-
const newContent = this._parser.addParentheses(this.
|
|
101
|
-
this.
|
|
98
|
+
const newContent = this._parser.addParentheses(this.formulaString);
|
|
99
|
+
this.formulaString = newContent && newContent.length ? newContent : this.formulaString;
|
|
102
100
|
this.parseInput();
|
|
103
101
|
this.recommendations = [];
|
|
104
102
|
}
|
|
@@ -137,7 +135,7 @@ let FormulaEditor = class FormulaEditor extends LitElement {
|
|
|
137
135
|
<textarea
|
|
138
136
|
id="fw-formula-editor"
|
|
139
137
|
class=${this.errorString?.length ? "error" : ""}
|
|
140
|
-
.value=${this.
|
|
138
|
+
.value=${this.formulaString}
|
|
141
139
|
.placeholder=${this.placeholder}
|
|
142
140
|
spellcheck="false"
|
|
143
141
|
autocomplete="off"
|
|
@@ -175,7 +173,7 @@ __decorate([
|
|
|
175
173
|
], FormulaEditor.prototype, "isFocused", void 0);
|
|
176
174
|
__decorate([
|
|
177
175
|
property()
|
|
178
|
-
], FormulaEditor.prototype, "
|
|
176
|
+
], FormulaEditor.prototype, "formulaString", void 0);
|
|
179
177
|
__decorate([
|
|
180
178
|
property()
|
|
181
179
|
], FormulaEditor.prototype, "placeholder", void 0);
|
|
@@ -6,12 +6,12 @@ import { Expectation } from "../types";
|
|
|
6
6
|
import { operatorPrecedence, unaryOperators, mathematicalOperators } from "./constants.js";
|
|
7
7
|
import { getFormulaTokens } from "./get-formula-tokens.js";
|
|
8
8
|
export class Parser {
|
|
9
|
-
constructor(variables, minSuggestionLen, formulaRegex, allowedNumbers, allowedOperators, variableType) {
|
|
9
|
+
constructor(variables, minSuggestionLen, formulaRegex = /[A-Za-z0-9_#@]+|[-+(),*^/\s]/g, allowedNumbers = true, allowedOperators = mathematicalOperators, variableType = "") {
|
|
10
10
|
this.variables = variables;
|
|
11
11
|
this.formulaRegex = formulaRegex;
|
|
12
12
|
this._recommender = new Recommender(Array.from(this.variables.keys()), minSuggestionLen);
|
|
13
|
-
this.allowedNumbers = allowedNumbers
|
|
14
|
-
this.allowedOperators = allowedOperators
|
|
13
|
+
this.allowedNumbers = allowedNumbers;
|
|
14
|
+
this.allowedOperators = allowedOperators;
|
|
15
15
|
this.variableType = variableType;
|
|
16
16
|
}
|
|
17
17
|
isNumber(value) {
|
|
@@ -19,6 +19,14 @@ export class Parser {
|
|
|
19
19
|
return false;
|
|
20
20
|
return !Number.isNaN(Number(value));
|
|
21
21
|
}
|
|
22
|
+
formatFormulaToken(token) {
|
|
23
|
+
for (let existingKey of this.variables.keys()) {
|
|
24
|
+
if (existingKey.toLowerCase() === token.toLowerCase()) {
|
|
25
|
+
return existingKey;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return token;
|
|
29
|
+
}
|
|
22
30
|
parseInput(formula, prevCurPos = null, recommendation = null) {
|
|
23
31
|
const tokens = getFormulaTokens(formula, this.formulaRegex);
|
|
24
32
|
const parentheses = new Stack();
|
|
@@ -38,6 +46,7 @@ export class Parser {
|
|
|
38
46
|
return parseOutput;
|
|
39
47
|
}
|
|
40
48
|
tokens?.forEach((token) => {
|
|
49
|
+
token = this.formatFormulaToken(token);
|
|
41
50
|
let isNumber = this.variables.has(token) || this.isNumber(token);
|
|
42
51
|
const isOperator = this.allowedOperators.has(token);
|
|
43
52
|
const isSpace = token.trim() === "";
|
|
@@ -81,11 +90,11 @@ export class Parser {
|
|
|
81
90
|
* Unknown symbol/variable/word
|
|
82
91
|
*/
|
|
83
92
|
if (!(isNumber || isOperator || isBracket || isSpace)) {
|
|
84
|
-
parseOutput.errorString = `${this.variableType} : ${token} does not exist`;
|
|
93
|
+
parseOutput.errorString = `${this.variableType} : '${token}' does not exist`;
|
|
85
94
|
expectation = Expectation.UNDEFINED;
|
|
86
95
|
}
|
|
87
96
|
else if (this.allowedOperators.has(previousToken) && isOperator) {
|
|
88
|
-
parseOutput.errorString = `
|
|
97
|
+
parseOutput.errorString = `Please use ${this.variableType}${this.allowedNumbers ? " or numbers" : ""} after '${previousToken}'. Pls do not use consecutive two mathametical operators (+ ,- ,* ,/ ,^)`;
|
|
89
98
|
expectation = Expectation.UNDEFINED;
|
|
90
99
|
}
|
|
91
100
|
else if (parentheses.isEmpty() && token === ")") {
|
|
@@ -98,14 +107,14 @@ export class Parser {
|
|
|
98
107
|
*/
|
|
99
108
|
else if (expectation === Expectation.VARIABLE && !isNumber && !isSpace && token != "("
|
|
100
109
|
&& !((unaryOperators.includes(token)) && (!parsedString.trim() || previousToken === "(" || this.allowedOperators.has(previousToken)))) {
|
|
101
|
-
parseOutput.errorString = `
|
|
110
|
+
parseOutput.errorString = `Please use ${this.variableType}${this.allowedNumbers ? " or numbers" : ""} after '${previousToken}'.`;
|
|
102
111
|
expectation = Expectation.UNDEFINED;
|
|
103
112
|
}
|
|
104
113
|
/**
|
|
105
114
|
* Multiple number/variable together without operator
|
|
106
115
|
*/
|
|
107
116
|
else if (expectation === Expectation.OPERATOR && !isOperator && !isSpace && token != ")") {
|
|
108
|
-
parseOutput.errorString = `
|
|
117
|
+
parseOutput.errorString = `Please use mathametical operators (${Array.from(this.allowedOperators).join(", ")}) after ${previousToken}.`;
|
|
109
118
|
expectation = Expectation.UNDEFINED;
|
|
110
119
|
}
|
|
111
120
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fw-components/formula-editor",
|
|
3
|
-
"version": "2.0.7-formula-editor.
|
|
3
|
+
"version": "2.0.7-formula-editor.40",
|
|
4
4
|
"description": "A WYSIWYG type formula editor",
|
|
5
5
|
"main": "dist/formula-editor/src/formula-editor.js",
|
|
6
6
|
"exports": {
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"@types/big.js": "^6.1.6",
|
|
32
32
|
"es-dev-server": "^2.1.0"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "5bf490d4aae797852b522a53bec12fcbf3d524bd"
|
|
35
35
|
}
|