Browse Source

feat(gui-v2): include getWordUntilCaret & insertAtCursor in formula utils

pull/2998/head
Wing-Kam Wong 2 years ago
parent
commit
2bcfd7c7e5
  1. 373
      packages/nc-gui-v2/utils/formulaUtils.ts

373
packages/nc-gui-v2/utils/formulaUtils.ts

@ -1,9 +1,11 @@
import type { Input as AntInput } from 'ant-design-vue'
const formulaTypes = { const formulaTypes = {
NUMERIC: "numeric", NUMERIC: 'numeric',
STRING: "string", STRING: 'string',
DATE: "date", DATE: 'date',
LOGICAL: "logical", LOGICAL: 'logical',
COND_EXP: "conditional_expression" COND_EXP: 'conditional_expression',
} }
const formulas = { const formulas = {
@ -11,38 +13,30 @@ const formulas = {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1 min: 1,
} },
}, },
description: 'Average of input parameters', description: 'Average of input parameters',
syntax: 'AVG(value1, [value2, ...])', syntax: 'AVG(value1, [value2, ...])',
examples: [ examples: ['AVG(10, 5) => 7.5', 'AVG({column1}, {column2})', 'AVG({column1}, {column2}, {column3})'],
'AVG(10, 5) => 7.5',
'AVG({column1}, {column2})',
'AVG({column1}, {column2}, {column3})'
]
}, },
ADD: { ADD: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1 min: 1,
} },
}, },
description: 'Sum of input parameters', description: 'Sum of input parameters',
syntax: 'ADD(value1, [value2, ...])', syntax: 'ADD(value1, [value2, ...])',
examples: [ examples: ['ADD(5, 5) => 10', 'ADD({column1}, {column2})', 'ADD({column1}, {column2}, {column3})'],
'ADD(5, 5) => 10',
'ADD({column1}, {column2})',
'ADD({column1}, {column2}, {column3})'
]
}, },
DATEADD: { DATEADD: {
type: formulaTypes.DATE, type: formulaTypes.DATE,
validation: { validation: {
args: { args: {
rqd: 3 rqd: 3,
} },
}, },
description: 'Adds a "count" units to Datetime.', description: 'Adds a "count" units to Datetime.',
syntax: 'DATEADD(date | datetime, value, ["day" | "week" | "month" | "year"])', syntax: 'DATEADD(date | datetime, value, ["day" | "week" | "month" | "year"])',
@ -54,400 +48,317 @@ const formulas = {
'DATEADD({column1}, 2, "month")', 'DATEADD({column1}, 2, "month")',
'DATEADD({column1}, -2, "month")', 'DATEADD({column1}, -2, "month")',
'DATEADD({column1}, 2, "year")', 'DATEADD({column1}, 2, "year")',
'DATEADD({column1}, -2, "year")' 'DATEADD({column1}, -2, "year")',
] ],
}, },
AND: { AND: {
type: formulaTypes.COND_EXP, type: formulaTypes.COND_EXP,
validation: { validation: {
args: { args: {
min: 1 min: 1,
} },
}, },
description: 'TRUE if all expr evaluate to TRUE', description: 'TRUE if all expr evaluate to TRUE',
syntax: 'AND(expr1, [expr2, ...])', syntax: 'AND(expr1, [expr2, ...])',
examples: [ examples: ['AND(5 > 2, 5 < 10) => 1', 'AND({column1} > 2, {column2} < 10)'],
'AND(5 > 2, 5 < 10) => 1',
'AND({column1} > 2, {column2} < 10)'
]
}, },
OR: { OR: {
type: formulaTypes.COND_EXP, type: formulaTypes.COND_EXP,
validation: { validation: {
args: { args: {
min: 1 min: 1,
} },
}, },
description: 'TRUE if at least one expr evaluates to TRUE', description: 'TRUE if at least one expr evaluates to TRUE',
syntax: 'OR(expr1, [expr2, ...])', syntax: 'OR(expr1, [expr2, ...])',
examples: [ examples: ['OR(5 > 2, 5 < 10) => 1', 'OR({column1} > 2, {column2} < 10)'],
'OR(5 > 2, 5 < 10) => 1',
'OR({column1} > 2, {column2} < 10)'
]
}, },
CONCAT: { CONCAT: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
min: 1 min: 1,
} },
}, },
description: 'Concatenated string of input parameters', description: 'Concatenated string of input parameters',
syntax: 'CONCAT(str1, [str2, ...])', syntax: 'CONCAT(str1, [str2, ...])',
examples: [ examples: ['CONCAT("AA", "BB", "CC") => "AABBCC"', 'CONCAT({column1}, {column2}, {column3})'],
'CONCAT("AA", "BB", "CC") => "AABBCC"',
'CONCAT({column1}, {column2}, {column3})'
]
}, },
TRIM: { TRIM: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Remove trailing and leading whitespaces from input parameter', description: 'Remove trailing and leading whitespaces from input parameter',
syntax: 'TRIM(str)', syntax: 'TRIM(str)',
examples: [ examples: ['TRIM(" HELLO WORLD ") => "HELLO WORLD"', 'TRIM({column1})'],
'TRIM(" HELLO WORLD ") => "HELLO WORLD"',
'TRIM({column1})'
]
}, },
UPPER: { UPPER: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Upper case converted string of input parameter', description: 'Upper case converted string of input parameter',
syntax: 'UPPER(str)', syntax: 'UPPER(str)',
examples: [ examples: ['UPPER("nocodb") => "NOCODB"', 'UPPER({column1})'],
'UPPER("nocodb") => "NOCODB"',
'UPPER({column1})'
]
}, },
LOWER: { LOWER: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Lower case converted string of input parameter', description: 'Lower case converted string of input parameter',
syntax: 'LOWER(str)', syntax: 'LOWER(str)',
examples: [ examples: ['LOWER("NOCODB") => "nocodb"', 'LOWER({column1})'],
'LOWER("NOCODB") => "nocodb"',
'LOWER({column1})'
]
}, },
LEN: { LEN: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Input parameter character length', description: 'Input parameter character length',
syntax: 'LEN(value)', syntax: 'LEN(value)',
examples: [ examples: ['LEN("NocoDB") => 6', 'LEN({column1})'],
'LEN("NocoDB") => 6',
'LEN({column1})'
]
}, },
MIN: { MIN: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1 min: 1,
} },
}, },
description: 'Minimum value amongst input parameters', description: 'Minimum value amongst input parameters',
syntax: 'MIN(value1, [value2, ...])', syntax: 'MIN(value1, [value2, ...])',
examples: [ examples: ['MIN(1000, 2000) => 1000', 'MIN({column1}, {column2})'],
'MIN(1000, 2000) => 1000',
'MIN({column1}, {column2})'
]
}, },
MAX: { MAX: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1 min: 1,
} },
}, },
description: 'Maximum value amongst input parameters', description: 'Maximum value amongst input parameters',
syntax: 'MAX(value1, [value2, ...])', syntax: 'MAX(value1, [value2, ...])',
examples: [ examples: ['MAX(1000, 2000) => 2000', 'MAX({column1}, {column2})'],
'MAX(1000, 2000) => 2000',
'MAX({column1}, {column2})'
]
}, },
CEILING: { CEILING: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Rounded next largest integer value of input parameter', description: 'Rounded next largest integer value of input parameter',
syntax: 'CEILING(value)', syntax: 'CEILING(value)',
examples: [ examples: ['CEILING(1.01) => 2', 'CEILING({column1})'],
'CEILING(1.01) => 2',
'CEILING({column1})'
]
}, },
FLOOR: { FLOOR: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Rounded largest integer less than or equal to input parameter', description: 'Rounded largest integer less than or equal to input parameter',
syntax: 'FLOOR(value)', syntax: 'FLOOR(value)',
examples: [ examples: ['FLOOR(3.1415) => 3', 'FLOOR({column1})'],
'FLOOR(3.1415) => 3',
'FLOOR({column1})'
]
}, },
ROUND: { ROUND: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Nearest integer to the input parameter', description: 'Nearest integer to the input parameter',
syntax: 'ROUND(value)', syntax: 'ROUND(value)',
examples: [ examples: ['ROUND(3.1415) => 3', 'ROUND({column1})'],
'ROUND(3.1415) => 3',
'ROUND({column1})'
]
}, },
MOD: { MOD: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 2 rqd: 2,
} },
}, },
description: 'Remainder after integer division of input parameters', description: 'Remainder after integer division of input parameters',
syntax: 'MOD(value1, value2)', syntax: 'MOD(value1, value2)',
examples: [ examples: ['MOD(1024, 1000) => 24', 'MOD({column}, 2)'],
'MOD(1024, 1000) => 24',
'MOD({column}, 2)'
]
}, },
REPEAT: { REPEAT: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2 rqd: 2,
} },
}, },
description: 'Specified copies of the input parameter string concatenated together', description: 'Specified copies of the input parameter string concatenated together',
syntax: 'REPEAT(str, count)', syntax: 'REPEAT(str, count)',
examples: [ examples: ['REPEAT("A", 5) => "AAAAA"', 'REPEAT({column}, 5)'],
'REPEAT("A", 5) => "AAAAA"',
'REPEAT({column}, 5)'
]
}, },
LOG: { LOG: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: {}, validation: {},
description: 'Logarithm of input parameter to the base (default = e) specified', description: 'Logarithm of input parameter to the base (default = e) specified',
syntax: 'LOG([base], value)', syntax: 'LOG([base], value)',
examples: [ examples: ['LOG(2, 1024) => 10', 'LOG(2, {column1})'],
'LOG(2, 1024) => 10',
'LOG(2, {column1})'
]
}, },
EXP: { EXP: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: {}, validation: {},
description: 'Exponential value of input parameter (e ^ power)', description: 'Exponential value of input parameter (e ^ power)',
syntax: 'EXP(power)', syntax: 'EXP(power)',
examples: [ examples: ['EXP(1) => 2.718281828459045', 'EXP({column1})'],
'EXP(1) => 2.718281828459045',
'EXP({column1})'
]
}, },
POWER: { POWER: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 2 rqd: 2,
} },
}, },
description: 'base to the exponent power, as in base ^ exponent', description: 'base to the exponent power, as in base ^ exponent',
syntax: 'POWER(base, exponent)', syntax: 'POWER(base, exponent)',
examples: [ examples: ['POWER(2, 10) => 1024', 'POWER({column1}, 10)'],
'POWER(2, 10) => 1024',
'POWER({column1}, 10)'
]
}, },
SQRT: { SQRT: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Square root of the input parameter', description: 'Square root of the input parameter',
syntax: 'SQRT(value)', syntax: 'SQRT(value)',
examples: [ examples: ['SQRT(100) => 10', 'SQRT({column1})'],
'SQRT(100) => 10',
'SQRT({column1})'
]
}, },
ABS: { ABS: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Absolute value of the input parameter', description: 'Absolute value of the input parameter',
syntax: 'ABS(value)', syntax: 'ABS(value)',
examples: [ examples: ['ABS({column1})'],
'ABS({column1})'
]
}, },
NOW: { NOW: {
type: formulaTypes.DATE, type: formulaTypes.DATE,
validation: { validation: {
args: { args: {
rqd: 0 rqd: 0,
} },
}, },
description: 'Returns the current time and day', description: 'Returns the current time and day',
syntax: 'NOW()', syntax: 'NOW()',
examples: [ examples: ['NOW() => 2022-05-19 17:20:43'],
'NOW() => 2022-05-19 17:20:43'
]
}, },
REPLACE: { REPLACE: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 3 rqd: 3,
} },
}, },
description: 'String, after replacing all occurrences of srchStr with rplcStr', description: 'String, after replacing all occurrences of srchStr with rplcStr',
syntax: 'REPLACE(str, srchStr, rplcStr)', syntax: 'REPLACE(str, srchStr, rplcStr)',
examples: [ examples: ['REPLACE("AABBCC", "AA", "BB") => "BBBBCC"', 'REPLACE({column1}, {column2}, {column3})'],
'REPLACE("AABBCC", "AA", "BB") => "BBBBCC"',
'REPLACE({column1}, {column2}, {column3})'
]
}, },
SEARCH: { SEARCH: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2 rqd: 2,
} },
}, },
description: 'Index of srchStr specified if found, 0 otherwise', description: 'Index of srchStr specified if found, 0 otherwise',
syntax: 'SEARCH(str, srchStr)', syntax: 'SEARCH(str, srchStr)',
examples: [ examples: ['SEARCH("HELLO WORLD", "WORLD") => 7', 'SEARCH({column1}, "abc")'],
'SEARCH("HELLO WORLD", "WORLD") => 7',
'SEARCH({column1}, "abc")'
]
}, },
INT: { INT: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Integer value of input parameter', description: 'Integer value of input parameter',
syntax: 'INT(value)', syntax: 'INT(value)',
examples: [ examples: ['INT(3.1415) => 3', 'INT({column1})'],
'INT(3.1415) => 3',
'INT({column1})'
]
}, },
RIGHT: { RIGHT: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2 rqd: 2,
} },
}, },
description: 'n characters from the end of input parameter', description: 'n characters from the end of input parameter',
syntax: 'RIGHT(str, n)', syntax: 'RIGHT(str, n)',
examples: [ examples: ['RIGHT("HELLO WORLD", 5) => WORLD', 'RIGHT({column1}, 3)'],
'RIGHT("HELLO WORLD", 5) => WORLD',
'RIGHT({column1}, 3)'
]
}, },
LEFT: { LEFT: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2 rqd: 2,
} },
}, },
description: 'n characters from the beginning of input parameter', description: 'n characters from the beginning of input parameter',
syntax: 'LEFT(str, n)', syntax: 'LEFT(str, n)',
examples: [ examples: ['LEFT({column1}, 2)', 'LEFT("ABCD", 2) => "AB"'],
'LEFT({column1}, 2)',
'LEFT("ABCD", 2) => "AB"'
]
}, },
SUBSTR: { SUBSTR: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
min: 2, min: 2,
max: 3 max: 3,
} },
}, },
description: 'Substring of length n of input string from the postition specified', description: 'Substring of length n of input string from the postition specified',
syntax: ' SUBTR(str, position, [n])', syntax: ' SUBTR(str, position, [n])',
examples: [ examples: ['SUBSTR("HELLO WORLD", 7) => WORLD', 'SUBSTR("HELLO WORLD", 7, 3) => WOR', 'SUBSTR({column1}, 7, 5)'],
'SUBSTR("HELLO WORLD", 7) => WORLD',
'SUBSTR("HELLO WORLD", 7, 3) => WOR',
'SUBSTR({column1}, 7, 5)'
]
}, },
MID: { MID: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 3 rqd: 3,
} },
}, },
description: 'Alias for SUBSTR', description: 'Alias for SUBSTR',
syntax: 'MID(str, position, [count])', syntax: 'MID(str, position, [count])',
examples: [ examples: ['MID("NocoDB", 3, 2) => "co"', 'MID({column1}, 3, 2)'],
'MID("NocoDB", 3, 2) => "co"',
'MID({column1}, 3, 2)'
]
}, },
IF: { IF: {
type: formulaTypes.COND_EXP, type: formulaTypes.COND_EXP,
validation: { validation: {
args: { args: {
min: 2, min: 2,
max: 3 max: 3,
} },
}, },
description: 'SuccessCase if expr evaluates to TRUE, elseCase otherwise', description: 'SuccessCase if expr evaluates to TRUE, elseCase otherwise',
syntax: 'IF(expr, successCase, elseCase)', syntax: 'IF(expr, successCase, elseCase)',
examples: [ examples: ['IF(5 > 1, "YES", "NO") => "YES"', 'IF({column} > 1, "YES", "NO")'],
'IF(5 > 1, "YES", "NO") => "YES"',
'IF({column} > 1, "YES", "NO")'
]
}, },
SWITCH: { SWITCH: {
type: formulaTypes.COND_EXP, type: formulaTypes.COND_EXP,
validation: { validation: {
args: { args: {
min: 3 min: 3,
} },
}, },
description: 'Switch case value based on expr output', description: 'Switch case value based on expr output',
syntax: 'SWITCH(expr, [pattern, value, ..., default])', syntax: 'SWITCH(expr, [pattern, value, ..., default])',
@ -455,22 +366,19 @@ const formulas = {
'SWITCH(1, 1, "One", 2, "Two", "N/A") => "One""', 'SWITCH(1, 1, "One", 2, "Two", "N/A") => "One""',
'SWITCH(2, 1, "One", 2, "Two", "N/A") => "Two"', 'SWITCH(2, 1, "One", 2, "Two", "N/A") => "Two"',
'SWITCH(3, 1, "One", 2, "Two", "N/A") => "N/A"', 'SWITCH(3, 1, "One", 2, "Two", "N/A") => "N/A"',
'SWITCH({column1}, 1, "One", 2, "Two", "N/A")' 'SWITCH({column1}, 1, "One", 2, "Two", "N/A")',
] ],
}, },
URL: { URL: {
type: formulaTypes.STRING, type: formulaTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1 rqd: 1,
} },
}, },
description: 'Convert to a hyperlink if it is a valid URL', description: 'Convert to a hyperlink if it is a valid URL',
syntax: 'URL(str)', syntax: 'URL(str)',
examples: [ examples: ['URL("https://github.com/nocodb/nocodb")', 'URL({column1})'],
'URL("https://github.com/nocodb/nocodb")',
'URL({column1})'
]
}, },
WEEKDAY: { WEEKDAY: {
type: formulaTypes.NUMERIC, type: formulaTypes.NUMERIC,
@ -478,17 +386,76 @@ const formulas = {
args: { args: {
min: 1, min: 1,
max: 2, max: 2,
} },
}, },
description: 'Returns the day of the week as an integer between 0 and 6 inclusive starting from Monday by default', description: 'Returns the day of the week as an integer between 0 and 6 inclusive starting from Monday by default',
syntax: 'WEEKDAY(date, [startDayOfWeek])', syntax: 'WEEKDAY(date, [startDayOfWeek])',
examples: [ examples: ['WEEKDAY("2021-06-09")', 'WEEKDAY(NOW(), "sunday")'],
'WEEKDAY("2021-06-09")', },
'WEEKDAY(NOW(), "sunday")'
]
}
} }
const formulaList = Object.keys(formulas) const formulaList = Object.keys(formulas)
export { formulaList, formulas, formulaTypes } // ref : https://stackoverflow.com/a/11077016
function insertAtCursor(myField: typeof AntInput, myValue: string, len = 0, b = 0) {
// IE support
if (document) {
myField.focus()
const sel = document.createRange() as any
sel.text = myValue
} // MOZILLA and others
else if (myField.selectionStart || myField.selectionStart == '0') {
const startPos = myField.selectionStart
const endPos = myField.selectionEnd
myField.value = myField.value.substring(0, startPos - len) + myValue + myField.value.substring(endPos, myField.value.length)
const pos = +startPos - len + myValue.length - b
// https://stackoverflow.com/a/4302688
if (myField.setSelectionRange) {
myField.focus()
myField.setSelectionRange(pos, pos)
} else if (myField.createTextRange) {
const range = myField.createTextRange()
range.collapse(true)
range.moveEnd('character', pos)
range.moveStart('character', pos)
range.select()
}
} else {
myField.value += myValue
}
return myField.value
}
function ReturnWord(text: string, caretPos: number) {
const preText = text.substring(0, caretPos)
if (preText.indexOf(' ') > 0) {
const words = preText.split(' ')
return words[words.length - 1] // return last word
} else {
return preText
}
}
function getWordUntilCaret(ctrl: typeof AntInput) {
const caretPos = GetCaretPosition(ctrl)
const word = ReturnWord(ctrl.value, caretPos)
return word || ''
}
function GetCaretPosition(ctrl: typeof AntInput) {
let CaretPos = 0 // IE Support
if (document) {
ctrl.focus()
const Sel = document.createRange() as any
Sel.moveStart('character', -ctrl.value.length)
CaretPos = Sel.text.length
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
CaretPos = ctrl.selectionStart
}
return CaretPos
}
export { formulaList, formulas, formulaTypes, getWordUntilCaret, insertAtCursor }

Loading…
Cancel
Save