From 970e9310999779cbcf6ab50ba492dc1495aed946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Gessler?= <36667834+gessfred@users.noreply.github.com> Date: Mon, 15 Jan 2024 23:48:37 +0000 Subject: [PATCH] implement basic worksheet without parsing --- app/src/App.jsx | 4 ++- app/src/components/Code.jsx | 52 +++++++++++++++++---------- app/src/components/DataSourceGrid.jsx | 2 +- app/src/components/Table.jsx | 47 ++++++++++++++++++++++++ app/src/pages/Home.jsx | 4 +-- app/src/pages/Worksheet.jsx | 31 ++++++++++++++-- 6 files changed, 116 insertions(+), 24 deletions(-) diff --git a/app/src/App.jsx b/app/src/App.jsx index f075800..c699cab 100644 --- a/app/src/App.jsx +++ b/app/src/App.jsx @@ -221,6 +221,7 @@ function App() { setStateProperty({user: JSON.parse(cachedUser), pageid: 'main'}) } }, []) + console.log('state') return ( <> setStateProperty({pageid: 'worksheet'})} + createWorksheet={(ds) => setStateProperty({pageid: 'worksheet', datasource: ds})} /> ) diff --git a/app/src/components/Code.jsx b/app/src/components/Code.jsx index dc7a6e1..2663121 100644 --- a/app/src/components/Code.jsx +++ b/app/src/components/Code.jsx @@ -2,29 +2,43 @@ import { useRef, useEffect } from 'react' import CodeMirror from '@uiw/react-codemirror' import { sql } from '@codemirror/lang-sql' +function extractTextBetweenSemicolons(text, caretPos) { + // Find the position of the semicolon before the caret + let startPos = text.lastIndexOf(';', caretPos - 1); + + // If there is no semicolon before the caret, start from the beginning + startPos = startPos === -1 ? 0 : startPos + 1; + + // Find the position of the semicolon after the caret + let endPos = text.indexOf(';', caretPos); + + // If there is no semicolon after the caret, go to the end of the text + endPos = endPos === -1 ? text.length : endPos; + + // Extract and return the text between the semicolons + return text.substring(startPos, endPos); +} + export function CodeEditor({code, setCode, onCtrlEnter}) { - /*return ( -
console.log(e.target.value)}> - {code} -
- )*/ - //if(!onCtrlEnter) onCtrlEnter = () => {} - const sqlEditorRef = useRef(null) - useEffect(() => { - if(sqlEditorRef.current) { - console.log("attaching") - const cm = sqlEditorRef.current - cm.on('keydown', (editor, event) => { - if (event.ctrlKey && event.keyCode === 13) { // 13 is the keyCode for Enter - console.log("ctrl+key") - } - }) + const handleCmdEnter = (event) => { + console.log('cut @', window.getSelection()) + const {keyCode, ctrlKey} = event + if(ctrlKey && keyCode == 13) { + console.log(window.getSelection().toString()) + const execode = extractTextBetweenSemicolons(code, window.getSelection().baseOffset) + console.log("EXEC", execode) + onCtrlEnter() + } else { + console.log(keyCode) } - }, []) + } return ( setCode(code)} + onChange={(code, view) => { + console.log(code, view) + setCode(code) + }} height='500px' basicSetup={{lineNumbers: false}} extensions={[sql()]} @@ -32,6 +46,8 @@ export function CodeEditor({code, setCode, onCtrlEnter}) { extraKeys: { 'Ctrl-Enter': (cm) => console.log("ctrl+enter") }}} + onKeyDown={handleCmdEnter} + /> ) } \ No newline at end of file diff --git a/app/src/components/DataSourceGrid.jsx b/app/src/components/DataSourceGrid.jsx index 2aacbf2..eed3140 100644 --- a/app/src/components/DataSourceGrid.jsx +++ b/app/src/components/DataSourceGrid.jsx @@ -35,7 +35,7 @@ export default function DataSourceGrid({datasources, createWorksheet, editDataSo
{}} /> -

Files

- + {false &&

Files

} + {false && } diff --git a/app/src/pages/Worksheet.jsx b/app/src/pages/Worksheet.jsx index a3c5f41..f0f71a8 100644 --- a/app/src/pages/Worksheet.jsx +++ b/app/src/pages/Worksheet.jsx @@ -14,6 +14,7 @@ import { XMarkIcon, } from '@heroicons/react/24/outline' import { ChevronDownIcon, MagnifyingGlassIcon } from '@heroicons/react/20/solid' +import Table from "../components/Table" const navigation = [ { name: 'Dashboard', href: '#', icon: HomeIcon, current: true }, @@ -346,11 +347,37 @@ function Sidebar({children}) { ) } -export default function Worksheet({show}) { +function execSql(api, query, datasource, onSuccess, onError) { + console.log(query, datasource, {"query": query, datasource_id: datasource}) + api.post('/query', {"query": query, datasource_id: datasource.datasource_id}, { + json: (j) => { + console.log(j) + onSuccess(j) + + }, + error: err => { + console.log(err) + onError && onError(err) + } + }) +} + + +export default function Worksheet({api, show, datasource}) { + const [state, setState] = useState({code: "select * from hello_world"}) + const setStateProperty = property => value => setState(prev => Object.assign({}, prev, {[property]: value})) if(!show) return return ( - + { + console.log("invoking exec sql with", datasource) + execSql(api, state.code, datasource, setStateProperty('result'), console.log) + }} /> + {state.result && } ) } \ No newline at end of file