-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from tidbcloud/react-component
feat: implement react component
- Loading branch information
Showing
11 changed files
with
276 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { SQLEditorInstance } from './editor-instance' | ||
|
||
//------------- | ||
|
||
export class EditorCache { | ||
cache: Map<string, SQLEditorInstance> = new Map() | ||
|
||
addEditor = (editorId: string, editor: SQLEditorInstance) => { | ||
this.cache.set(editorId, editor) | ||
} | ||
|
||
getEditor = (editorId: string) => { | ||
return this.cache.get(editorId) | ||
} | ||
|
||
deleteEditor = (editorId: string) => { | ||
this.cache.delete(editorId) | ||
} | ||
|
||
clearEditors = () => { | ||
this.cache.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
|
||
export * from './editor-instance' | ||
export * from './editor-cache' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "@tidbcloud/tisqleditor-react", | ||
"version": "0.0.1", | ||
"description": "", | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist/*.js", | ||
"dist/*.ts", | ||
"package.json", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"tsc:watch": "tsc --watch", | ||
"rollup:watch": "rollup -c --watch", | ||
"dev": "concurrently --kill-others \"pnpm tsc:watch\" \"pnpm rollup:watch\"", | ||
"build": "tsc && rollup -c" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@tidbcloud/tisqleditor": "workspace:^" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"@types/react": "^18.3.3", | ||
"react": "^18.3.1", | ||
"rollup": "^4.18.0", | ||
"tslib": "^2.6.3", | ||
"typescript": "^5.4.5" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18.3.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import typescript from '@rollup/plugin-typescript' | ||
|
||
export default { | ||
input: 'src/index.ts', | ||
output: { | ||
dir: 'dist', | ||
format: 'es' | ||
}, | ||
plugins: [typescript()] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createContext, useContext, useEffect, useMemo, useState } from 'react' | ||
import { EditorCache } from '@tidbcloud/tisqleditor' | ||
|
||
type EditorCacheCtxValue = { | ||
cache: EditorCache | ||
|
||
activeEditorId: string | ||
setActiveEditorId: (editorId: string) => void | ||
} | ||
|
||
const EditorCacheContext = createContext<EditorCacheCtxValue | null>(null) | ||
|
||
export const useEditorCacheContext = () => { | ||
const context = useContext(EditorCacheContext) | ||
|
||
if (!context) { | ||
throw new Error('useEditorCacheContext must be used within a provider') | ||
} | ||
|
||
return context | ||
} | ||
|
||
export function EditorCacheProvider(props: { children: React.ReactNode }) { | ||
const [activeEditorId, setActiveEditorId] = useState('') | ||
const cache = useMemo(() => new EditorCache(), []) | ||
const ctxValue = useMemo( | ||
() => ({ | ||
cache, | ||
activeEditorId, | ||
setActiveEditorId | ||
}), | ||
[activeEditorId] | ||
) | ||
|
||
useEffect(() => { | ||
return () => { | ||
cache.clearEditors() | ||
} | ||
}, []) | ||
|
||
return ( | ||
<EditorCacheContext.Provider value={ctxValue}> | ||
{props.children} | ||
</EditorCacheContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './editor-cache-context' | ||
export * from './sql-editor' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useEffect, useLayoutEffect, useRef } from 'react' | ||
|
||
import { useEditorCacheContext } from './editor-cache-context' | ||
import { | ||
CreateSQLEditorOptions, | ||
createSQLEditorInstance | ||
} from '@tidbcloud/tisqleditor' | ||
|
||
type SQLEditorProps = CreateSQLEditorOptions & { | ||
editorId: string | ||
className?: string | ||
} | ||
|
||
export default function SQLEditor({ | ||
className, | ||
editorId, | ||
theme, | ||
sqlConfig, | ||
...rest | ||
}: SQLEditorProps) { | ||
const editorContainerRef = useRef<HTMLDivElement>(null) | ||
const cacheCtx = useEditorCacheContext() | ||
|
||
useLayoutEffect(() => { | ||
if (!editorContainerRef.current) return | ||
|
||
let editorInst = cacheCtx.cache.getEditor(editorId) | ||
if (!editorInst) { | ||
editorInst = createSQLEditorInstance({ | ||
editorId, | ||
theme, | ||
sqlConfig, | ||
...rest | ||
}) | ||
cacheCtx.cache.addEditor(editorId, editorInst) | ||
} | ||
|
||
editorContainerRef.current.appendChild(editorInst.editorView.dom) | ||
editorInst.editorView.focus() | ||
|
||
return () => { | ||
if (editorContainerRef.current && editorInst) { | ||
editorContainerRef.current.removeChild(editorInst.editorView.dom) | ||
} | ||
} | ||
}, [editorId]) | ||
|
||
// use `useLayoutEffect` to avoid flicker | ||
useLayoutEffect(() => { | ||
cacheCtx.cache.getEditor(editorId)?.changeTheme(theme ?? []) | ||
}, [editorId, theme]) | ||
|
||
useEffect(() => { | ||
cacheCtx.cache.getEditor(editorId)?.changeSQLConfig(sqlConfig ?? {}) | ||
}, [editorId, sqlConfig]) | ||
|
||
return <div className={className} ref={editorContainerRef} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"useDefineForClassFields": true, | ||
"lib": ["ES2020", "DOM", "DOM.Iterable"], | ||
"module": "ESNext", | ||
"skipLibCheck": true, | ||
|
||
/* declaration */ | ||
"declaration": true, | ||
"emitDeclarationOnly": true, | ||
"outDir": "./dist", | ||
|
||
/* Bundler mode */ | ||
"moduleResolution": "bundler", | ||
"allowImportingTsExtensions": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
// "noEmit": true, | ||
"jsx": "react-jsx", | ||
|
||
/* Linting */ | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"include": ["src"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.