@fw-components/formula-editor 2.0.3-formula-editor.1 → 2.0.7-formula-editor.0
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/dist/formula-editor/src/cursor.js +123 -0
- package/dist/formula-editor/src/formula-builder.js +139 -0
- package/dist/formula-editor/src/formula-creator.js +83 -0
- package/dist/formula-editor/src/formula-editor.js +239 -0
- package/dist/formula-editor/src/helpers/types.js +16 -0
- package/dist/formula-editor/src/helpers.js +54 -0
- package/dist/formula-editor/src/parser.js +357 -0
- package/dist/formula-editor/src/recommendor.js +67 -0
- package/{src/styles/formula-editor-styles.ts → dist/formula-editor/src/styles/formula-editor-styles.js} +1 -2
- package/dist/formula-editor/src/sub-components/operator-input.js +24 -0
- package/dist/formula-editor/src/suggestion-menu.js +82 -0
- package/dist/styles/src/button-styles.js +419 -0
- package/package.json +8 -6
- package/tsconfig.json +20 -0
- package/src/cursor.ts +0 -134
- package/src/formula-builder.ts +0 -141
- package/src/formula-creator.ts +0 -99
- package/src/formula-editor.ts +0 -236
- package/src/helpers/types.ts +0 -20
- package/src/helpers.ts +0 -62
- package/src/parser.ts +0 -465
- package/src/recommendor.ts +0 -106
- package/src/sub-components/operator-input.ts +0 -13
- package/src/suggestion-menu.ts +0 -61
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
export class Cursor {
|
|
2
|
+
/**
|
|
3
|
+
* The functions `getCurrentCursorPosition`, `setCurrentCursorPosition` and their
|
|
4
|
+
* helpers `_createRange` and `_isChildOf` are not used for caret manipulation,
|
|
5
|
+
* but are still in the code for future reference, if the functionality breaks
|
|
6
|
+
* somehow in some obsolete browser.
|
|
7
|
+
*/
|
|
8
|
+
static getCurrentCursorPosition(parentElement) {
|
|
9
|
+
let selection = window.getSelection(), charCount = -1, node;
|
|
10
|
+
if (selection?.focusNode) {
|
|
11
|
+
if (Cursor._isChildOf(selection.focusNode, parentElement)) {
|
|
12
|
+
node = selection.focusNode;
|
|
13
|
+
charCount = selection.focusOffset;
|
|
14
|
+
while (node) {
|
|
15
|
+
if (node === parentElement) {
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
if (node.previousSibling) {
|
|
19
|
+
node = node.previousSibling;
|
|
20
|
+
charCount += node.textContent?.length ?? 0;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
node = node.parentNode;
|
|
24
|
+
if (node === null) {
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return charCount;
|
|
32
|
+
}
|
|
33
|
+
static setCurrentCursorPosition(chars, element) {
|
|
34
|
+
if (chars >= 0) {
|
|
35
|
+
var selection = window.getSelection();
|
|
36
|
+
let range = Cursor._createRange(element, { count: chars }, undefined);
|
|
37
|
+
if (range) {
|
|
38
|
+
range.collapse(false);
|
|
39
|
+
selection?.removeAllRanges();
|
|
40
|
+
selection?.addRange(range);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
static _createRange(node, chars, range) {
|
|
45
|
+
if (!range) {
|
|
46
|
+
range = document.createRange();
|
|
47
|
+
range.selectNode(node);
|
|
48
|
+
range.setStart(node, 0);
|
|
49
|
+
}
|
|
50
|
+
if (chars.count === 0) {
|
|
51
|
+
range.setEnd(node, chars.count);
|
|
52
|
+
}
|
|
53
|
+
else if (node && chars.count > 0) {
|
|
54
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
55
|
+
if (node.textContent.length < chars.count) {
|
|
56
|
+
chars.count -= node.textContent.length;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
range.setEnd(node, chars.count);
|
|
60
|
+
chars.count = 0;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
for (var lp = 0; lp < node.childNodes.length; lp++) {
|
|
65
|
+
range = Cursor._createRange(node.childNodes[lp], chars, range);
|
|
66
|
+
if (chars.count === 0) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return range;
|
|
73
|
+
}
|
|
74
|
+
static _isChildOf(node, parentElement) {
|
|
75
|
+
while (node !== null) {
|
|
76
|
+
if (node === parentElement) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
node = node.parentNode;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
static getCaretPosition(shadowRoot, element) {
|
|
84
|
+
// `getSelection` is not defined for the type ShadowRoot in TS,
|
|
85
|
+
// but it does exist.
|
|
86
|
+
const range = shadowRoot.getSelection().getRangeAt(0);
|
|
87
|
+
const prefix = range.cloneRange();
|
|
88
|
+
prefix.selectNodeContents(element);
|
|
89
|
+
prefix.setEnd(range.endContainer, range.endOffset);
|
|
90
|
+
return prefix.toString().length;
|
|
91
|
+
}
|
|
92
|
+
static { this.setCaretPosition = (pos, parent) => {
|
|
93
|
+
for (const node of parent.childNodes) {
|
|
94
|
+
if (node.nodeType == Node.TEXT_NODE) {
|
|
95
|
+
if (node.length >= pos) {
|
|
96
|
+
const range = document.createRange();
|
|
97
|
+
const sel = window.getSelection();
|
|
98
|
+
range.setStart(node, pos);
|
|
99
|
+
range.collapse(true);
|
|
100
|
+
sel.removeAllRanges();
|
|
101
|
+
sel.addRange(range);
|
|
102
|
+
return -1;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
pos = pos - node.length;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
pos = this.setCaretPosition(pos, node);
|
|
110
|
+
if (pos < 0) {
|
|
111
|
+
return pos;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return pos;
|
|
116
|
+
}; }
|
|
117
|
+
static getCursorRect(shadowRoot) {
|
|
118
|
+
return shadowRoot
|
|
119
|
+
.getSelection()
|
|
120
|
+
?.getRangeAt(0)
|
|
121
|
+
?.getClientRects()[0];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { LitElement, css, html } from "lit";
|
|
8
|
+
import { customElement, property, query } from "lit/decorators.js";
|
|
9
|
+
import { Formula } from "./helpers/types.js";
|
|
10
|
+
import { TextButtonStyles } from "../../styles/src/button-styles.js";
|
|
11
|
+
import "./formula-editor";
|
|
12
|
+
let FormulaBuilder = class FormulaBuilder extends LitElement {
|
|
13
|
+
constructor() {
|
|
14
|
+
super(...arguments);
|
|
15
|
+
this.variables = new Map([
|
|
16
|
+
["sales_expense", 2],
|
|
17
|
+
["sales_in_quarter", 3],
|
|
18
|
+
["sales_cummulative", 3],
|
|
19
|
+
["cummulative_sum", 4],
|
|
20
|
+
["mayank", 10],
|
|
21
|
+
]);
|
|
22
|
+
this.formula = new Formula("", "");
|
|
23
|
+
this.handleCalculate = () => {
|
|
24
|
+
this.formulaEditor?.requestCalculate();
|
|
25
|
+
};
|
|
26
|
+
this.handleFormat = () => {
|
|
27
|
+
this.formulaEditor?.requestFormat();
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
static { this.styles = css `
|
|
31
|
+
#wysiwyg-err {
|
|
32
|
+
width: 100%;
|
|
33
|
+
border-radius: 0px 0px var(--fe-err-border-radius, 4px)
|
|
34
|
+
var(--fe-err-border-radius, 4px);
|
|
35
|
+
color: var(--fe-err-text-color, #fc514f);
|
|
36
|
+
border: var(--fe-err-border-width, 2px) solid black;
|
|
37
|
+
/* border-top: 0px; */
|
|
38
|
+
background-color: var(--fe-background-color, #222222);
|
|
39
|
+
padding: 4px;
|
|
40
|
+
margin: 0px 0px 8px 0px;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.wysiwyg-no-err {
|
|
44
|
+
color: #098668 !important;
|
|
45
|
+
}
|
|
46
|
+
`; }
|
|
47
|
+
handleChange() {
|
|
48
|
+
this.dispatchEvent(new CustomEvent("fw-formula-changed", {
|
|
49
|
+
detail: {
|
|
50
|
+
name: this.nameInput?.value,
|
|
51
|
+
rawFormula: this.formula.formulaString,
|
|
52
|
+
error: this.formula.error,
|
|
53
|
+
precision: this.formula.precision,
|
|
54
|
+
},
|
|
55
|
+
bubbles: true,
|
|
56
|
+
}));
|
|
57
|
+
this.requestUpdate();
|
|
58
|
+
}
|
|
59
|
+
render() {
|
|
60
|
+
return html `
|
|
61
|
+
${TextButtonStyles}
|
|
62
|
+
|
|
63
|
+
<div>
|
|
64
|
+
<label for="metric-name-input">Metric Name</label>
|
|
65
|
+
<input
|
|
66
|
+
id="metric-name-input"
|
|
67
|
+
.value=${this.formula.name}
|
|
68
|
+
@input=${(e) => {
|
|
69
|
+
this.handleChange();
|
|
70
|
+
}}
|
|
71
|
+
/>
|
|
72
|
+
</div>
|
|
73
|
+
<label>Formula</label>
|
|
74
|
+
<formula-editor
|
|
75
|
+
class="fe"
|
|
76
|
+
minSuggestionLen="0"
|
|
77
|
+
@fw-formula-content-changed=${(e) => {
|
|
78
|
+
this.formula.formulaString = e.detail.formulaString;
|
|
79
|
+
this.formula.error = e.detail.error;
|
|
80
|
+
this.handleChange();
|
|
81
|
+
}}
|
|
82
|
+
.variables=${this.variables}
|
|
83
|
+
.content=${this.formula.formulaString}
|
|
84
|
+
.errorString=${this.formula.error}
|
|
85
|
+
>
|
|
86
|
+
</formula-editor>
|
|
87
|
+
<div id="wysiwyg-err" class="${this.formula.error ?? "wysiwyg-no-err"}">
|
|
88
|
+
${this.formula.error ??
|
|
89
|
+
`${this.formula.name} = ${this.formula.formulaString}`}
|
|
90
|
+
</div>
|
|
91
|
+
<button class="primary-text-button" @click=${this.handleCalculate}>
|
|
92
|
+
Calculate
|
|
93
|
+
</button>
|
|
94
|
+
<button class="primary-text-button" @click=${this.handleFormat}>
|
|
95
|
+
Format
|
|
96
|
+
</button>
|
|
97
|
+
`;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
__decorate([
|
|
101
|
+
property({
|
|
102
|
+
type: (Map),
|
|
103
|
+
converter: {
|
|
104
|
+
fromAttribute: (value) => {
|
|
105
|
+
if (value) {
|
|
106
|
+
return new Map(JSON.parse(value));
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
toAttribute: (value) => {
|
|
110
|
+
return JSON.stringify(Array.from(value.entries()));
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
], FormulaBuilder.prototype, "variables", void 0);
|
|
115
|
+
__decorate([
|
|
116
|
+
property({
|
|
117
|
+
type: Formula,
|
|
118
|
+
converter: {
|
|
119
|
+
fromAttribute: (value) => {
|
|
120
|
+
if (value) {
|
|
121
|
+
const formulaJSON = JSON.parse(value);
|
|
122
|
+
return new Formula(formulaJSON.name, formulaJSON.formulaString, formulaJSON.precision);
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
toAttribute: (value) => {
|
|
126
|
+
return JSON.stringify(value);
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
], FormulaBuilder.prototype, "formula", void 0);
|
|
131
|
+
__decorate([
|
|
132
|
+
query("#metric-name-input")
|
|
133
|
+
], FormulaBuilder.prototype, "nameInput", void 0);
|
|
134
|
+
__decorate([
|
|
135
|
+
query("formula-editor")
|
|
136
|
+
], FormulaBuilder.prototype, "formulaEditor", void 0);
|
|
137
|
+
FormulaBuilder = __decorate([
|
|
138
|
+
customElement("formula-builder")
|
|
139
|
+
], FormulaBuilder);
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { LitElement, html } from "lit";
|
|
8
|
+
import { customElement, state } from "lit/decorators.js";
|
|
9
|
+
import { repeat } from "lit/directives/repeat.js";
|
|
10
|
+
import { ifDefined } from "lit/directives/if-defined.js";
|
|
11
|
+
import { Operator } from "./helpers/types";
|
|
12
|
+
var FormulaEntityType;
|
|
13
|
+
(function (FormulaEntityType) {
|
|
14
|
+
FormulaEntityType[FormulaEntityType["Operator"] = 0] = "Operator";
|
|
15
|
+
FormulaEntityType[FormulaEntityType["Entity"] = 1] = "Entity";
|
|
16
|
+
})(FormulaEntityType || (FormulaEntityType = {}));
|
|
17
|
+
class FormulaEntity {
|
|
18
|
+
constructor(type, metric, operator = Operator.NONE) {
|
|
19
|
+
this.type = type;
|
|
20
|
+
this.metric = metric;
|
|
21
|
+
this.operator = operator;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
class FormulaRow {
|
|
25
|
+
constructor(type, metrices = null, operator = Operator.NONE) {
|
|
26
|
+
this.operator = Operator.NONE;
|
|
27
|
+
this.type = type;
|
|
28
|
+
this.metrices = metrices;
|
|
29
|
+
this.operator = operator;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
let FormulaCreator = class FormulaCreator extends LitElement {
|
|
33
|
+
constructor() {
|
|
34
|
+
super(...arguments);
|
|
35
|
+
this.formulaState = [
|
|
36
|
+
new FormulaRow(FormulaEntityType.Entity, [
|
|
37
|
+
new FormulaEntity(FormulaEntityType.Entity, "Sales Expense"),
|
|
38
|
+
new FormulaEntity(FormulaEntityType.Operator, null, Operator.MINUS),
|
|
39
|
+
new FormulaEntity(FormulaEntityType.Entity, "Marketing Expense"),
|
|
40
|
+
]),
|
|
41
|
+
new FormulaRow(FormulaEntityType.Operator, null, Operator.DIV),
|
|
42
|
+
new FormulaRow(FormulaEntityType.Entity, [
|
|
43
|
+
new FormulaEntity(FormulaEntityType.Entity, "Sales Expense"),
|
|
44
|
+
new FormulaEntity(FormulaEntityType.Operator, null, Operator.PLUS),
|
|
45
|
+
new FormulaEntity(FormulaEntityType.Entity, "Marketing Expense"),
|
|
46
|
+
]),
|
|
47
|
+
new FormulaRow(FormulaEntityType.Operator, null, Operator.DIV),
|
|
48
|
+
new FormulaRow(FormulaEntityType.Entity, [
|
|
49
|
+
new FormulaEntity(FormulaEntityType.Entity, "Sales Expense"),
|
|
50
|
+
new FormulaEntity(FormulaEntityType.Operator, null, Operator.DIV),
|
|
51
|
+
new FormulaEntity(FormulaEntityType.Entity, "Marketing Expense"),
|
|
52
|
+
]),
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
render() {
|
|
56
|
+
return html `
|
|
57
|
+
<label>Formula</label>
|
|
58
|
+
${repeat(this.formulaState, (_, rowIndex) => `row-${rowIndex}`, (formulaRow, rowIndex) => {
|
|
59
|
+
return formulaRow.type == FormulaEntityType.Entity
|
|
60
|
+
? html ` <div>
|
|
61
|
+
${repeat(formulaRow.metrices, (_, columnIndex) => `col-(${rowIndex},${columnIndex})`, (formulaEntity, columnIndex) => {
|
|
62
|
+
return formulaEntity.type == FormulaEntityType.Entity
|
|
63
|
+
? html `<input
|
|
64
|
+
value=${ifDefined(formulaEntity.metric === null
|
|
65
|
+
? undefined
|
|
66
|
+
: formulaEntity.metric)}
|
|
67
|
+
/>`
|
|
68
|
+
: html `<operator-input
|
|
69
|
+
.operator=${formulaEntity.operator}
|
|
70
|
+
></operator-input>`;
|
|
71
|
+
})}
|
|
72
|
+
</div>`
|
|
73
|
+
: html `<div>${formulaRow.operator}</div>`;
|
|
74
|
+
})}
|
|
75
|
+
`;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
__decorate([
|
|
79
|
+
state()
|
|
80
|
+
], FormulaCreator.prototype, "formulaState", void 0);
|
|
81
|
+
FormulaCreator = __decorate([
|
|
82
|
+
customElement("formula-creator")
|
|
83
|
+
], FormulaCreator);
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { html, LitElement } from "lit";
|
|
8
|
+
import { customElement, property, state, query } from "lit/decorators.js";
|
|
9
|
+
import { FormulaEditorStyles } from "./styles/formula-editor-styles.js";
|
|
10
|
+
import { Parser } from "./parser.js";
|
|
11
|
+
import { Cursor } from "./cursor.js";
|
|
12
|
+
import "./suggestion-menu.js";
|
|
13
|
+
let FormulaEditor = class FormulaEditor extends LitElement {
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
/**
|
|
17
|
+
* These `states` and `properties` can't be defined as `static get properties`,
|
|
18
|
+
* because TS doesn't support that.
|
|
19
|
+
* @see https://github.com/lit/lit-element/issues/414
|
|
20
|
+
*/
|
|
21
|
+
this._formattedContent = null;
|
|
22
|
+
this._recommendations = null;
|
|
23
|
+
this._calculatedResult = undefined;
|
|
24
|
+
/**
|
|
25
|
+
* If `parseInput` is called to add a recommendation, say by clicking,
|
|
26
|
+
* browser removes focus from the input box. In that case, we have no way
|
|
27
|
+
* of knowing where the cursor previously was, other than storing it somewhere.
|
|
28
|
+
*/
|
|
29
|
+
this.currentCursorPosition = null;
|
|
30
|
+
this.currentCursorRect = undefined;
|
|
31
|
+
this.lastInputType = "undef";
|
|
32
|
+
this.content = "";
|
|
33
|
+
this.variables = new Map();
|
|
34
|
+
this.minSuggestionLen = 2;
|
|
35
|
+
this.errorString = null;
|
|
36
|
+
this._parser = new Parser(this.variables, this.minSuggestionLen);
|
|
37
|
+
}
|
|
38
|
+
firstUpdated(_changedProperties) {
|
|
39
|
+
this._parser = new Parser(this.variables, this.minSuggestionLen);
|
|
40
|
+
this.parseInput(null, false);
|
|
41
|
+
}
|
|
42
|
+
handleChange(event) {
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
this.lastInputType = event.inputType;
|
|
45
|
+
this.content = event.target.innerText;
|
|
46
|
+
this.parseInput();
|
|
47
|
+
event.target.focus();
|
|
48
|
+
}
|
|
49
|
+
navigateRecommendations(direction) {
|
|
50
|
+
if (!this._recommendations)
|
|
51
|
+
return;
|
|
52
|
+
const currentIndex = this._recommendations.indexOf(this._selectedRecommendation);
|
|
53
|
+
const newIndex = direction === "ArrowDown"
|
|
54
|
+
? (currentIndex + 1) % this._recommendations.length
|
|
55
|
+
: direction === "ArrowUp"
|
|
56
|
+
? (currentIndex - 1 + this._recommendations.length) % this._recommendations.length
|
|
57
|
+
: currentIndex;
|
|
58
|
+
this._selectedRecommendation = this._recommendations[newIndex];
|
|
59
|
+
}
|
|
60
|
+
handleKeyboardEvents(event) {
|
|
61
|
+
if (event.code == "Tab" && this._recommendations?.length == 1) {
|
|
62
|
+
this._selectedRecommendation = null;
|
|
63
|
+
event.preventDefault();
|
|
64
|
+
this.parseInput(this._recommendations[0]);
|
|
65
|
+
}
|
|
66
|
+
else if (event.code === "ArrowDown" || event.code === "ArrowUp") {
|
|
67
|
+
event.preventDefault();
|
|
68
|
+
this.navigateRecommendations(event.code);
|
|
69
|
+
this.requestUpdate();
|
|
70
|
+
}
|
|
71
|
+
else if (event.code === "Enter" && this._selectedRecommendation) {
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
this.parseInput(this._selectedRecommendation);
|
|
74
|
+
this._selectedRecommendation = null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
onClickRecommendation(recommendation) {
|
|
78
|
+
let editor = this.shadowRoot?.getElementById("wysiwyg-editor");
|
|
79
|
+
if (!editor)
|
|
80
|
+
return;
|
|
81
|
+
this.parseInput(recommendation);
|
|
82
|
+
this.currentCursorPosition = null;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
*
|
|
86
|
+
* @param recommendation The recommendation which needs to be inserted
|
|
87
|
+
* at the current cursor position
|
|
88
|
+
* @param manageCursor Whether or not cursor management is needed. Not
|
|
89
|
+
* needed when manual insertion of text is required (eg: during initialization)
|
|
90
|
+
* @returns void
|
|
91
|
+
*/
|
|
92
|
+
parseInput(recommendation = null, manageCursor = true) {
|
|
93
|
+
let editor = this.shadowRoot?.getElementById("wysiwyg-editor");
|
|
94
|
+
if (!editor)
|
|
95
|
+
return;
|
|
96
|
+
/**
|
|
97
|
+
* @see https://github.com/WICG/webcomponents/issues/79
|
|
98
|
+
*/
|
|
99
|
+
if (manageCursor)
|
|
100
|
+
this.currentCursorPosition = recommendation
|
|
101
|
+
? this.currentCursorPosition
|
|
102
|
+
: Cursor.getCaretPosition(this.shadowRoot, editor);
|
|
103
|
+
const parseOutput = this._parser.parseInput(this.content, this.currentCursorPosition, recommendation);
|
|
104
|
+
this._recommendations = parseOutput.recommendations;
|
|
105
|
+
this._formattedContent = parseOutput.formattedContent;
|
|
106
|
+
this.errorString = parseOutput.errorString;
|
|
107
|
+
/**
|
|
108
|
+
* Don't modify the text stream manually if the text is being composed,
|
|
109
|
+
* unless the user manually chooses to do so by selecting a suggestion.
|
|
110
|
+
* @see https://github.com/w3c/input-events/issues/86
|
|
111
|
+
* @see https://github.com/w3c/input-events/pull/122
|
|
112
|
+
* @see https://bugs.chromium.org/p/chromium/issues/detail?id=689541
|
|
113
|
+
* */
|
|
114
|
+
if (this.lastInputType != "insertCompositionText" || recommendation) {
|
|
115
|
+
editor.innerHTML = parseOutput.formattedString;
|
|
116
|
+
}
|
|
117
|
+
this.content = editor.innerText;
|
|
118
|
+
if (recommendation) {
|
|
119
|
+
this._recommendations = null;
|
|
120
|
+
this.currentCursorPosition = parseOutput.newCursorPosition;
|
|
121
|
+
}
|
|
122
|
+
if (manageCursor)
|
|
123
|
+
Cursor.setCaretPosition(this.currentCursorPosition, editor);
|
|
124
|
+
editor?.focus();
|
|
125
|
+
if (manageCursor)
|
|
126
|
+
this.currentCursorRect = Cursor.getCursorRect(this.shadowRoot);
|
|
127
|
+
this.requestUpdate();
|
|
128
|
+
this.dispatchEvent(new CustomEvent("fw-formula-content-changed", {
|
|
129
|
+
detail: {
|
|
130
|
+
formulaString: this.content,
|
|
131
|
+
error: this.errorString,
|
|
132
|
+
},
|
|
133
|
+
bubbles: true,
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
requestCalculate() {
|
|
137
|
+
if (this._parser.parseInput(this.content).errorString) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const calculatedResult = this._parser.calculate(this.content);
|
|
141
|
+
this.content = this._parser.addParentheses(this.content) ?? this.content;
|
|
142
|
+
this.parseInput();
|
|
143
|
+
this._calculatedResult = calculatedResult.result;
|
|
144
|
+
this.errorString = calculatedResult.errorString;
|
|
145
|
+
this._recommendations = null;
|
|
146
|
+
this.requestUpdate();
|
|
147
|
+
}
|
|
148
|
+
requestFormat() {
|
|
149
|
+
this.content = this._parser.addParentheses(this.content) ?? this.content;
|
|
150
|
+
this.parseInput();
|
|
151
|
+
this._recommendations = null;
|
|
152
|
+
this.requestUpdate();
|
|
153
|
+
}
|
|
154
|
+
render() {
|
|
155
|
+
return html `
|
|
156
|
+
<style>
|
|
157
|
+
${FormulaEditorStyles}
|
|
158
|
+
</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>
|
|
167
|
+
${this._recommendations
|
|
168
|
+
? html ` <suggestion-menu
|
|
169
|
+
style="
|
|
170
|
+
position: absolute;
|
|
171
|
+
left: ${(this.currentCursorRect?.left ?? 0) -
|
|
172
|
+
(this.editor?.getClientRect()[0]?.left ?? 0) +
|
|
173
|
+
"px"};
|
|
174
|
+
top: ${(this.currentCursorRect?.top ??
|
|
175
|
+
0 - (this.editor?.getClientRect()[0]?.top ?? 0)) +
|
|
176
|
+
window.scrollY +
|
|
177
|
+
"px"};
|
|
178
|
+
"
|
|
179
|
+
.recommendations=${this._recommendations}
|
|
180
|
+
.currentSelection=${this._selectedRecommendation}
|
|
181
|
+
.onClickRecommendation=${(e) => this.onClickRecommendation(e)}
|
|
182
|
+
></suggestion-menu>`
|
|
183
|
+
: html ``}
|
|
184
|
+
<p>${this._calculatedResult}</p>
|
|
185
|
+
`;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
__decorate([
|
|
189
|
+
state()
|
|
190
|
+
], FormulaEditor.prototype, "_formattedContent", void 0);
|
|
191
|
+
__decorate([
|
|
192
|
+
state()
|
|
193
|
+
], FormulaEditor.prototype, "_recommendations", void 0);
|
|
194
|
+
__decorate([
|
|
195
|
+
state()
|
|
196
|
+
], FormulaEditor.prototype, "_calculatedResult", void 0);
|
|
197
|
+
__decorate([
|
|
198
|
+
state()
|
|
199
|
+
], FormulaEditor.prototype, "currentCursorPosition", void 0);
|
|
200
|
+
__decorate([
|
|
201
|
+
state()
|
|
202
|
+
], FormulaEditor.prototype, "currentCursorRect", void 0);
|
|
203
|
+
__decorate([
|
|
204
|
+
state()
|
|
205
|
+
], FormulaEditor.prototype, "lastInputType", void 0);
|
|
206
|
+
__decorate([
|
|
207
|
+
state()
|
|
208
|
+
], FormulaEditor.prototype, "_selectedRecommendation", void 0);
|
|
209
|
+
__decorate([
|
|
210
|
+
property()
|
|
211
|
+
], FormulaEditor.prototype, "content", void 0);
|
|
212
|
+
__decorate([
|
|
213
|
+
property({
|
|
214
|
+
type: (Map),
|
|
215
|
+
converter: {
|
|
216
|
+
fromAttribute: (value) => {
|
|
217
|
+
if (value) {
|
|
218
|
+
return new Map(JSON.parse(value));
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
toAttribute: (value) => {
|
|
222
|
+
return JSON.stringify(Array.from(value.entries()));
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
})
|
|
226
|
+
], FormulaEditor.prototype, "variables", void 0);
|
|
227
|
+
__decorate([
|
|
228
|
+
property()
|
|
229
|
+
], FormulaEditor.prototype, "minSuggestionLen", void 0);
|
|
230
|
+
__decorate([
|
|
231
|
+
property()
|
|
232
|
+
], FormulaEditor.prototype, "errorString", void 0);
|
|
233
|
+
__decorate([
|
|
234
|
+
query("wysiwyg-editor")
|
|
235
|
+
], FormulaEditor.prototype, "editor", void 0);
|
|
236
|
+
FormulaEditor = __decorate([
|
|
237
|
+
customElement("formula-editor")
|
|
238
|
+
], FormulaEditor);
|
|
239
|
+
export { FormulaEditor };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export var Operator;
|
|
2
|
+
(function (Operator) {
|
|
3
|
+
Operator["PLUS"] = "+";
|
|
4
|
+
Operator["MINUS"] = "-";
|
|
5
|
+
Operator["MUL"] = "*";
|
|
6
|
+
Operator["DIV"] = "/";
|
|
7
|
+
Operator["NONE"] = "";
|
|
8
|
+
})(Operator || (Operator = {}));
|
|
9
|
+
export class Formula {
|
|
10
|
+
constructor(name, formulaString, precision = -1) {
|
|
11
|
+
this.error = null;
|
|
12
|
+
this.name = name;
|
|
13
|
+
this.formulaString = formulaString;
|
|
14
|
+
this.precision = precision;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export class Stack {
|
|
2
|
+
constructor() {
|
|
3
|
+
this._elements = [];
|
|
4
|
+
}
|
|
5
|
+
push(item) {
|
|
6
|
+
this._elements.push(item);
|
|
7
|
+
}
|
|
8
|
+
pop() {
|
|
9
|
+
return this._elements.pop();
|
|
10
|
+
}
|
|
11
|
+
top() {
|
|
12
|
+
return this._elements.at(-1);
|
|
13
|
+
}
|
|
14
|
+
isEmpty() {
|
|
15
|
+
return this._elements.length == 0;
|
|
16
|
+
}
|
|
17
|
+
print() {
|
|
18
|
+
console.log(this._elements);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class Queue {
|
|
22
|
+
constructor() {
|
|
23
|
+
this._elements = {};
|
|
24
|
+
this._head = 0;
|
|
25
|
+
this._tail = 0;
|
|
26
|
+
}
|
|
27
|
+
enqueue(item) {
|
|
28
|
+
this._elements[this._tail] = item;
|
|
29
|
+
this._tail++;
|
|
30
|
+
}
|
|
31
|
+
dequeue() {
|
|
32
|
+
if (this._tail === this._head)
|
|
33
|
+
return undefined;
|
|
34
|
+
const element = this._elements[this._head];
|
|
35
|
+
delete this._elements[this._head];
|
|
36
|
+
this._head++;
|
|
37
|
+
return element;
|
|
38
|
+
}
|
|
39
|
+
peek() {
|
|
40
|
+
return this._elements[this._head];
|
|
41
|
+
}
|
|
42
|
+
isEmpty() {
|
|
43
|
+
return this._head == this._tail;
|
|
44
|
+
}
|
|
45
|
+
print() {
|
|
46
|
+
console.log(this._elements);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export var Expectation;
|
|
50
|
+
(function (Expectation) {
|
|
51
|
+
Expectation[Expectation["VARIABLE"] = 0] = "VARIABLE";
|
|
52
|
+
Expectation[Expectation["OPERATOR"] = 1] = "OPERATOR";
|
|
53
|
+
Expectation[Expectation["UNDEFINED"] = 2] = "UNDEFINED";
|
|
54
|
+
})(Expectation || (Expectation = {}));
|