@fw-components/formula-editor 2.0.7-formula-editor.0 → 2.0.7-formula-editor.1

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.
@@ -146,6 +146,9 @@ let FormulaEditor = class FormulaEditor extends LitElement {
146
146
  this.requestUpdate();
147
147
  }
148
148
  requestFormat() {
149
+ if (!Boolean(this.content)) {
150
+ return;
151
+ }
149
152
  this.content = this._parser.addParentheses(this.content) ?? this.content;
150
153
  this.parseInput();
151
154
  this._recommendations = null;
@@ -156,14 +159,14 @@ let FormulaEditor = class FormulaEditor extends LitElement {
156
159
  <style>
157
160
  ${FormulaEditorStyles}
158
161
  </style>
159
- <div
160
- contenteditable
161
- id="wysiwyg-editor"
162
- spellcheck="false"
163
- autocomplete="off"
164
- @input=${this.handleChange}
165
- @keydown=${this.handleKeyboardEvents}
166
- ></div>
162
+ <div
163
+ contenteditable
164
+ id="wysiwyg-editor"
165
+ spellcheck="false"
166
+ autocomplete="off"
167
+ @input=${this.handleChange}
168
+ @keydown=${this.handleKeyboardEvents}
169
+ ></div>
167
170
  ${this._recommendations
168
171
  ? html ` <suggestion-menu
169
172
  style="
@@ -176,10 +179,10 @@ let FormulaEditor = class FormulaEditor extends LitElement {
176
179
  window.scrollY +
177
180
  "px"};
178
181
  "
179
- .recommendations=${this._recommendations}
180
- .currentSelection=${this._selectedRecommendation}
181
- .onClickRecommendation=${(e) => this.onClickRecommendation(e)}
182
- ></suggestion-menu>`
182
+ .recommendations=${this._recommendations}
183
+ .currentSelection=${this._selectedRecommendation}
184
+ .onClickRecommendation=${(e) => this.onClickRecommendation(e)}
185
+ ></suggestion-menu>`
183
186
  : html ``}
184
187
  <p>${this._calculatedResult}</p>
185
188
  `;
@@ -75,7 +75,6 @@ export class Parser {
75
75
  // Fetch recommendations nonetheless.
76
76
  parseOutput.recommendations =
77
77
  this._recommender.getRecommendation(token);
78
- console.log(parseOutput.recommendations);
79
78
  }
80
79
  let tokenClassName = "";
81
80
  // Don't check for errors if an error has already been encountered.
@@ -1,67 +1,20 @@
1
+ import { Fzf } from 'fzf';
1
2
  export class Recommender {
2
3
  constructor(variables, minSuggestionLen) {
3
- this._mininumSuggestionLength = minSuggestionLen > 0 ? minSuggestionLen : 1;
4
- this._trie = new TrieNode();
5
- for (let variable of variables) {
6
- this.insert(variable[0]);
7
- }
8
- }
9
- insert(word, position = -1, node = undefined) {
10
- if (position == -1) {
11
- this.insert(word, 0, this._trie);
12
- return;
13
- }
14
- if (position == word.length) {
15
- node?.addChild("\0");
16
- return;
17
- }
18
- if (!node.getChild(word[position])) {
19
- node?.addChild(word[position]);
20
- }
21
- this.insert(word, position + 1, node.getChild(word[position]));
4
+ this._minimumSuggestionLength = minSuggestionLen > 0 ? minSuggestionLen : 1;
5
+ const variableList = Array.from(variables.keys());
6
+ this._fzf = new Fzf(variableList);
22
7
  }
23
8
  getRecommendation(word) {
24
- if (word.length < this._mininumSuggestionLength) {
25
- return null;
26
- }
27
- let recommendations = [];
28
- let currentPosition = 0;
29
- let currentNode = this._trie;
30
- while (currentNode && currentPosition < word.length) {
31
- currentNode = currentNode.getChild(word[currentPosition]);
32
- currentPosition++;
33
- }
34
- if (!currentNode) {
9
+ if (word.length < this._minimumSuggestionLength) {
35
10
  return null;
36
11
  }
37
- this._traverseAndGet(recommendations, currentNode, word);
38
- if (recommendations.length == 0 ||
39
- (recommendations.length == 1 && recommendations[0] == word)) {
12
+ const entries = this._fzf.find(word);
13
+ const recommendations = entries.map(entry => entry.item);
14
+ if (recommendations.length === 0 ||
15
+ (recommendations.length === 1 && recommendations[0] === word)) {
40
16
  return null;
41
17
  }
42
18
  return recommendations;
43
19
  }
44
- _traverseAndGet(recommendations, node, word, currentString = "") {
45
- for (let child of node.children) {
46
- if (child[0] == "\0") {
47
- recommendations.push(word + currentString);
48
- // return;
49
- }
50
- this._traverseAndGet(recommendations, child[1], word, currentString + child[0]);
51
- }
52
- }
53
- }
54
- class TrieNode {
55
- constructor() {
56
- this._children = new Map();
57
- }
58
- get children() {
59
- return this._children;
60
- }
61
- getChild(char) {
62
- return this._children.get(char);
63
- }
64
- addChild(char) {
65
- this._children.set(char, new TrieNode());
66
- }
67
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fw-components/formula-editor",
3
- "version": "2.0.7-formula-editor.0",
3
+ "version": "2.0.7-formula-editor.1",
4
4
  "description": "A WYSIWYG type formula editor",
5
5
  "main": "dist/formula-editor/src/formula-builder.js",
6
6
  "publishConfig": {
@@ -15,6 +15,7 @@
15
15
  "dependencies": {
16
16
  "@fw-components/styles": "^2.0.7-formula-editor.0",
17
17
  "big.js": "^6.2.1",
18
+ "fzf": "^0.5.2",
18
19
  "lit": "^2.1.2",
19
20
  "typescript": "^5.0.4"
20
21
  },
@@ -24,5 +25,5 @@
24
25
  "@types/big.js": "^6.1.6",
25
26
  "es-dev-server": "^2.1.0"
26
27
  },
27
- "gitHead": "05f8378db753ed6edfd56b945162795de33ae0fe"
28
+ "gitHead": "ac3d8a9eb757e3b2b5001bd46e9e7003a323f46e"
28
29
  }