Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(sql-editor): inline widget poc

Greg Richardsond9634d0dfe9f
diff --git a/package-lock.json b/package-lock.json
index 8f7cf61213f..f7ba214ff40 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -43430,6 +43430,7 @@
"markdown-table": "=2.0.0",
"mobx": "^6.3.2",
"mobx-react-lite": "^3.2.0",
+ "monaco-editor": "0.33.0",
"next": "^12.3.2",
"next-transpile-modules": "^9.0.0",
"openapi-types": "^12.0.2",
@@ -43977,6 +43978,11 @@
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug=="
},
+ "studio/node_modules/monaco-editor": {
+ "version": "0.33.0",
+ "resolved": "https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.33.0.tgz",
+ "integrity": "sha512-VcRWPSLIUEgQJQIE0pVT8FcGBIgFoxz7jtqctE+IiCxWugD0DwgyQBcZBhdSrdMC84eumoqMZsGl2GTreOzwqw=="
+ },
"studio/node_modules/proxy-compare": {
"version": "2.3.0",
"license": "MIT"
diff --git a/studio/components/interfaces/SQLEditor/MonacoEditor.tsx b/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
index e062680c8e3..f14676c6484 100644
--- a/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
@@ -1,6 +1,7 @@
import Editor, { Monaco, OnMount } from '@monaco-editor/react'
import { timeout } from 'lib/helpers'
-import { MutableRefObject, useEffect, useRef } from 'react'
+import { MutableRefObject, useEffect, useRef, useState } from 'react'
+import { createPortal } from 'react-dom'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
export type IStandaloneCodeEditor = Parameters<OnMount>[0]
@@ -20,6 +21,8 @@ const MonacoEditor = ({ id, editorRef, isExecuting, executeQuery }: MonacoEditor
const executeQueryRef = useRef(executeQuery)
executeQueryRef.current = executeQuery
+ const [domNode, setDomNode] = useState<HTMLElement>()
+
useEffect(() => {
if (!editorRef.current || !monacoRef.current) return
@@ -29,19 +32,6 @@ const MonacoEditor = ({ id, editorRef, isExecuting, executeQuery }: MonacoEditor
}
}, [])
- useEffect(() => {
- if (editorRef.current) {
- // add margin above first line
- editorRef.current?.changeViewZones((accessor: any) => {
- accessor.addZone({
- afterLineNumber: 0,
- heightInPx: 4,
- domNode: document.createElement('div'),
- })
- })
- }
- }, [])
-
const handleEditorOnMount: OnMount = async (editor, monaco) => {
editorRef.current = editor
monacoRef.current = monaco
@@ -57,13 +47,63 @@ const MonacoEditor = ({ id, editorRef, isExecuting, executeQuery }: MonacoEditor
},
})
- // add margin above first line
- editor.changeViewZones((accessor: any) => {
+ const domNode = document.createElement('div')
+
+ let viewZoneTop = 0
+ let viewZoneHeight = 0
+
+ setDomNode(domNode)
+
+ function layoutOverlayWidget() {
+ const layoutInfo = editorRef.current?.getLayoutInfo()
+
+ if (!layoutInfo) {
+ return
+ }
+
+ console.log({ layoutInfo })
+
+ domNode.style.left = `${layoutInfo.contentLeft}px`
+ domNode.style.top = `${viewZoneTop}px`
+ domNode.style.width = `${layoutInfo.width}px`
+ domNode.style.height = `${viewZoneHeight}px`
+ }
+
+ editorRef.current.changeViewZones((accessor) => {
+ // add margin above first line
accessor.addZone({
afterLineNumber: 0,
heightInPx: 4,
domNode: document.createElement('div'),
})
+
+ accessor.addZone({
+ afterLineNumber: 1,
+ heightInLines: 4,
+ domNode: document.createElement('div'),
+ onDomNodeTop: (top) => {
+ console.log({ top })
+ viewZoneTop = top
+ layoutOverlayWidget()
+ },
+ onComputedHeight: (height) => {
+ console.log({ height })
+ viewZoneHeight = height
+ layoutOverlayWidget()
+ },
+ })
+
+ editorRef.current?.addOverlayWidget({
+ getId: function () {
+ return 'my.inline.widget'
+ },
+ getDomNode: function () {
+ return domNode
+ },
+ getPosition: function () {
+ return null
+ },
+ })
})
// when editor did mount, it will need a delay before focus() works properly
@@ -76,24 +116,27 @@ const MonacoEditor = ({ id, editorRef, isExecuting, executeQuery }: MonacoEditor
}
return (
- <Editor
- className="monaco-editor"
- theme={'supabase'}
- onMount={handleEditorOnMount}
- onChange={handleEditorChange}
- defaultLanguage="pgsql"
- defaultValue={snippet?.snippet.content.sql}
- path={id}
- options={{
- tabSize: 2,
- fontSize: 13,
- minimap: {
- enabled: false,
- },
- wordWrap: 'on',
- fixedOverflowWidgets: true,
- }}
- />
+ <>
+ <Editor
+ className="monaco-editor"
+ theme={'supabase'}
+ onMount={handleEditorOnMount}
+ onChange={handleEditorChange}
+ defaultLanguage="pgsql"
+ defaultValue={snippet?.snippet.content.sql}
+ path={id}
+ options={{
+ tabSize: 2,
+ fontSize: 13,
+ minimap: {
+ enabled: false,
+ },
+ wordWrap: 'on',
+ fixedOverflowWidgets: true,
+ }}
+ />
+ {domNode && createPortal(<>My inline widget</>, domNode)}
+ </>
)
}
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
index 9525ed25b5a..eb60ceaa3d0 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
@@ -4,13 +4,17 @@ import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
import { useLocalStorage } from 'hooks'
import useLatest from 'hooks/misc/useLatest'
import { detectOS } from 'lib/helpers'
+import dynamic from 'next/dynamic'
import { useCallback, useRef } from 'react'
import Split from 'react-split'
import { getSqlEditorStateSnapshot, useSqlEditorStateSnapshot } from 'state/sql-editor'
import { AiIcon, IconCornerDownLeft, Input } from 'ui'
-import MonacoEditor, { IStandaloneCodeEditor } from './MonacoEditor'
+import type { IStandaloneCodeEditor } from './MonacoEditor'
import UtilityPanel from './UtilityPanel/UtilityPanel'
+// Load the monaco editor client-side only (does not behave well server-side)
+const MonacoEditor = dynamic(() => import('./MonacoEditor'), { ssr: false })
+
const SQLEditor = () => {
const { ref, id } = useParams()
const { project } = useProjectContext()
diff --git a/studio/next.config.js b/studio/next.config.js
index 482be47c169..7ed45a18591 100644
--- a/studio/next.config.js
+++ b/studio/next.config.js
@@ -207,6 +207,19 @@ const nextConfig = {
experimental: {
outputFileTracingRoot: path.join(__dirname, '../../'),
},
+ webpack(config) {
+ config.module?.rules
+ .find((rule) => rule.oneOf)
+ .oneOf.forEach((rule) => {
+ if (rule.issuer?.and?.[0]?.toString().includes('_app')) {
+ const and = rule.issuer.and
+ rule.issuer.or = [/[\\/]node_modules[\\/]monaco-editor[\\/]/, { and }]
+ delete rule.issuer.and
+ }
+ })
+
+ return config
+ },
}
// Export all config
diff --git a/studio/package.json b/studio/package.json
index a00f3be6e84..3239007bea6 100644
--- a/studio/package.json
+++ b/studio/package.json
@@ -62,6 +62,7 @@
"markdown-table": "=2.0.0",
"mobx": "^6.3.2",
"mobx-react-lite": "^3.2.0",
+ "monaco-editor": "0.33.0",
"next": "^12.3.2",
"next-transpile-modules": "^9.0.0",
"openapi-types": "^12.0.2",