@fw-components/formula-editor 2.0.3-formula-editor.2 → 2.0.7-cline-formulaeditor.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 +142 -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 +367 -0
- package/dist/formula-editor/src/helpers/types.js +16 -0
- package/dist/formula-editor/src/helpers.js +72 -0
- package/dist/formula-editor/src/parser.js +461 -0
- package/dist/formula-editor/src/recommendor.js +18 -0
- package/dist/formula-editor/src/styles/formula-editor-styles.js +149 -0
- package/dist/formula-editor/src/sub-components/operator-input.js +24 -0
- package/dist/formula-editor/src/suggestion-menu.js +198 -0
- package/dist/styles/src/button-styles.js +419 -0
- package/package.json +5 -5
- package/src/cursor.js +3 -3
- package/src/cursor.js.map +1 -1
- package/src/formula-builder.js.map +1 -1
- package/src/formula-editor.js.map +1 -1
- package/src/parser.js.map +1 -1
- package/src/recommendor.js.map +1 -1
- package/src/suggestion-menu.js.map +1 -1
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,142 @@
|
|
|
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 _getComposedRange(shadowRoot) {
|
|
84
|
+
// `getSelection` is not defined for the type ShadowRoot in TS,
|
|
85
|
+
// but it does exist.
|
|
86
|
+
const sr = shadowRoot;
|
|
87
|
+
if (sr.getSelection && typeof sr.getSelection === 'function') {
|
|
88
|
+
const selection = sr.getSelection();
|
|
89
|
+
if (selection && selection.rangeCount > 0) {
|
|
90
|
+
return selection.getRangeAt(0);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Fallback for browsers like Firefox/Safari.
|
|
94
|
+
const selection = window.getSelection();
|
|
95
|
+
if (selection && selection.rangeCount > 0) {
|
|
96
|
+
const range = selection.getRangeAt(0);
|
|
97
|
+
if (shadowRoot.contains(range.startContainer)) {
|
|
98
|
+
return range;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
static getCaretPosition(shadowRoot, element) {
|
|
104
|
+
const range = Cursor._getComposedRange(shadowRoot);
|
|
105
|
+
if (!range) {
|
|
106
|
+
return 0;
|
|
107
|
+
}
|
|
108
|
+
const prefix = range.cloneRange();
|
|
109
|
+
prefix.selectNodeContents(element);
|
|
110
|
+
prefix.setEnd(range.endContainer, range.endOffset);
|
|
111
|
+
return prefix.toString().length;
|
|
112
|
+
}
|
|
113
|
+
static { this.setCaretPosition = (pos, parent) => {
|
|
114
|
+
for (const node of parent.childNodes) {
|
|
115
|
+
if (node.nodeType == Node.TEXT_NODE) {
|
|
116
|
+
if (node.length >= pos) {
|
|
117
|
+
const range = document.createRange();
|
|
118
|
+
const sel = window.getSelection();
|
|
119
|
+
range.setStart(node, pos);
|
|
120
|
+
range.collapse(true);
|
|
121
|
+
sel.removeAllRanges();
|
|
122
|
+
sel.addRange(range);
|
|
123
|
+
return -1;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
pos = pos - node.length;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
pos = this.setCaretPosition(pos, node);
|
|
131
|
+
if (pos < 0) {
|
|
132
|
+
return pos;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return pos;
|
|
137
|
+
}; }
|
|
138
|
+
static getCursorRect(shadowRoot) {
|
|
139
|
+
const range = Cursor._getComposedRange(shadowRoot);
|
|
140
|
+
return range ? range.getClientRects()[0] : undefined;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -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);
|