From b3910d30da1a9822cb674e3f77876f09ae57c58c Mon Sep 17 00:00:00 2001 From: Pranav C <61551451+pranavxc@users.noreply.github.com> Date: Wed, 4 Aug 2021 18:42:59 +0530 Subject: [PATCH] feat: formula autocomplete Signed-off-by: Pranav C <61551451+pranavxc@users.noreply.github.com> --- .../components/editColumn/formulaOptions.vue | 139 ++++++++++++++++-- packages/nc-gui/helpers/index.js | 35 +++++ 2 files changed, 158 insertions(+), 16 deletions(-) diff --git a/packages/nc-gui/components/project/spreadsheet/components/editColumn/formulaOptions.vue b/packages/nc-gui/components/project/spreadsheet/components/editColumn/formulaOptions.vue index 3eb5015e9e..a80570993b 100644 --- a/packages/nc-gui/components/project/spreadsheet/components/editColumn/formulaOptions.vue +++ b/packages/nc-gui/components/project/spreadsheet/components/editColumn/formulaOptions.vue @@ -1,22 +1,67 @@ - diff --git a/packages/nc-gui/helpers/index.js b/packages/nc-gui/helpers/index.js index 6f452743a1..30a8928350 100644 --- a/packages/nc-gui/helpers/index.js +++ b/packages/nc-gui/helpers/index.js @@ -12,6 +12,41 @@ export const isValidURL = (str) => { } export const parseIfInteger = v => /^\d+$/.test(v) ? +v : v + +// ref : https://stackoverflow.com/a/11077016 +export function insertAtCursor(myField, myValue, len) { + // IE support + if (document.selection) { + myField.focus() + const sel = document.selection.createRange() + 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) + + myValue + + myField.value.substring(endPos, myField.value.length) + + const pos = +startPos + (len ?? myValue.length) + // 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 + } +} + /** * @copyright Copyright (c) 2021, Xgene Cloud Ltd *