-
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 #64 from tidbcloud/feat/try-vue-component
feat: implement vue component
- Loading branch information
Showing
9 changed files
with
486 additions
and
4 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,48 @@ | ||
# @tidbcloud/tisqleditor-vue | ||
|
||
This package provides a vue component wrap for `SQLEditorInstance` from `@tidbcloud/tisqleditor`. | ||
|
||
## Installation | ||
|
||
```shell | ||
npm install @tidbcloud/tisqleditor-vue | ||
``` | ||
|
||
You need to install all its peer dependencies manually as well, such as `@codemirror/view`, `@codemirror/state`. | ||
|
||
## Usage | ||
|
||
`@tidbcloud/tisqleditor-vue` supports multiple CodeMirror instances, it uses `EditorCacheProvide` to cache the instances, then you can access all the cached editor instances anywhere inside the provide. You can put the provide in a proper place. | ||
|
||
```vue | ||
<script setup> | ||
import { EditorCacheProvide, SQLEditor } from '@tidbcloud/tisqleditor-vue' | ||
</script> | ||
<template> | ||
<EditorCacheProvide> | ||
<SQLEditor editorId="test" doc="select * from test;" /> | ||
</EditorCacheProvide> | ||
</template> | ||
``` | ||
|
||
When `EditorCacheProvide` unmounts, it will clear all the cached editor instances. | ||
|
||
## API | ||
|
||
### EditorCacheProvide | ||
|
||
- `const cache=inject('editor-cache')`: get the cache | ||
- `cache.addEditor(editorId, instance)`: add new editor instance | ||
- `cache.getEditor(editorId)`: get the cached editor instance by editorId | ||
- `cache.deleteEditor(editorId)`: remove the editor instance from cache | ||
- `cache.clearEditors()`: clear all cached editor instances | ||
|
||
### SQLEditor Props | ||
|
||
- `class`: CodeMirror root container css class | ||
- `editorId`: editor id, used to differ multiple editor instances | ||
- `doc`: editor initial content | ||
- `sqlConfig`: config for SQL dialect, schemas, tables | ||
- `theme`: editor theme, `@tidbcloud/codemirror-extensions-themes` provides 2 simple themes, `bbedit` for light mode, `oneDark` for dark mode, you can choose any other themes from third-party libraries or customize it by self | ||
- `extraExts`: any other extra CodeMirror extensions you want to use, `@tidbcloud/tisqleditor-react` install some builtin extensions, likes `@codemirror/lang-sql`, `@tidbcloud/codemirror-extension-sql-parser`, `@tidbcloud/codemirror-extension-cur-sql` |
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,40 @@ | ||
{ | ||
"name": "@tidbcloud/tisqleditor-vue", | ||
"version": "0.0.1", | ||
"description": "tisqleditor vue component", | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"files": [ | ||
"dist/*.js", | ||
"dist/*.ts", | ||
"package.json", | ||
"README.md", | ||
"CHANGELOG.md" | ||
], | ||
"scripts": { | ||
"build": "vue-tsc -b && vite build" | ||
}, | ||
"keywords": [ | ||
"tidbcloud", | ||
"sql", | ||
"editor", | ||
"codemirror", | ||
"vue component" | ||
], | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@tidbcloud/tisqleditor": "workspace:^" | ||
}, | ||
"devDependencies": { | ||
"@vitejs/plugin-vue": "^5.0.5", | ||
"typescript": "^5.2.2", | ||
"vite": "^5.3.4", | ||
"vue": "^3.4.31", | ||
"vue-tsc": "^2.0.24" | ||
}, | ||
"peerDependencies": { | ||
"vue": "^3.4.31" | ||
} | ||
} |
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,16 @@ | ||
<script setup lang="ts"> | ||
import { provide, onUnmounted } from 'vue' | ||
import { EditorCache } from '@tidbcloud/tisqleditor' | ||
const editorCache = new EditorCache() | ||
provide('editor-cache', editorCache) | ||
onUnmounted(() => { | ||
editorCache.clearEditors() | ||
}) | ||
</script> | ||
|
||
<template> | ||
<slot /> | ||
</template> |
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,4 @@ | ||
import EditorCacheProvide from './editor-cache-provide.vue' | ||
import SQLEditor from './sql-editor.vue' | ||
|
||
export { EditorCacheProvide, SQLEditor } |
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,61 @@ | ||
<script setup lang="ts"> | ||
import { ref, watch, inject, onMounted } from 'vue' | ||
import { | ||
CreateSQLEditorOptions, | ||
createSQLEditorInstance, | ||
EditorCache | ||
} from '@tidbcloud/tisqleditor' | ||
type SQLEditorProps = CreateSQLEditorOptions & { | ||
class?: string | ||
} | ||
const props = defineProps<SQLEditorProps>() | ||
const editorContainerRef = ref<HTMLInputElement | null>(null) | ||
const editorCache = inject('editor-cache', new EditorCache()) | ||
function editorIdChange(newId: string, oldId: string) { | ||
if (!editorContainerRef.value) { | ||
return | ||
} | ||
const oldInst = editorCache.getEditor(oldId) | ||
if (oldInst) { | ||
editorContainerRef.value.removeChild(oldInst.editorView.dom) | ||
} | ||
let newInst = editorCache.getEditor(newId) | ||
if (!newInst) { | ||
const { theme, sqlConfig, ...rest } = props | ||
newInst = createSQLEditorInstance({ | ||
theme, | ||
sqlConfig, | ||
...rest, | ||
editorId: newId | ||
}) | ||
editorCache.addEditor(newId, newInst) | ||
} else { | ||
newInst.changeTheme(props.theme ?? []) | ||
newInst.changeSQLConfig(props.sqlConfig ?? {}) | ||
} | ||
editorContainerRef.value.appendChild(newInst.editorView.dom) | ||
newInst.editorView.focus() | ||
} | ||
onMounted(() => { | ||
editorIdChange(props.editorId, '') | ||
}) | ||
watch(() => props.editorId, editorIdChange) | ||
watch( | ||
() => props.theme, | ||
(newVal) => editorCache.getEditor(props.editorId)?.changeTheme(newVal ?? []) | ||
) | ||
watch( | ||
() => props.sqlConfig, | ||
(newVal) => | ||
editorCache.getEditor(props.editorId)?.changeSQLConfig(newVal ?? {}) | ||
) | ||
</script> | ||
|
||
<template> | ||
<div :class="class" ref="editorContainerRef"></div> | ||
</template> |
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,8 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"jsx": "preserve", | ||
"outDir": "./dist" | ||
}, | ||
"include": ["./src"] | ||
} |
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,21 @@ | ||
import { defineConfig } from 'vite' | ||
import vue from '@vitejs/plugin-vue' | ||
|
||
export default defineConfig({ | ||
plugins: [vue()], | ||
build: { | ||
lib: { | ||
entry: 'src/index.ts', | ||
fileName: 'index', | ||
formats: ['es'] | ||
}, | ||
rollupOptions: { | ||
external: ['vue'], | ||
output: { | ||
globals: { | ||
vue: 'Vue' | ||
} | ||
} | ||
} | ||
} | ||
}) |
Oops, something went wrong.