Coder Social home page Coder Social logo

monaco-sqlpad's Introduction

monaco-sqlpad

基于微软vscode开源的monaco-editor的sql编辑器,支持SQL语法高亮、提示,支持表名、字段提示

示例

preview.gif

安装

npm i monaco-sqlpad --save

webpack配置

const CopyWebpackPlugin = require('copy-webpack-plugin')

module.exports = {
  configureWebpack: {
    plugins: [new CopyWebpackPlugin(
      {
        patterns: [
          {
            context: 'node_modules/monaco-sqlpad/dist/',
            from: '*.umd.min.*.js',
            to: 'js/',
            toType: 'dir'
          },
          {
            from: 'node_modules/monaco-sqlpad/dist/editor.worker.js',
            to: 'editor.worker.js',
            toType: 'file'
          },
          {
            context: 'node_modules/monaco-sqlpad/dist/',
            from: 'fonts',
            to: 'js/fonts',
            toType: 'dir'
          }
        ]
      }
    )]
  }
}

为何需要配置webpack?

使用

<template>
  <div style="height:500px">
    <monaco-sqlpad v-model="content" :on-input-field="onInputField" :dbs="dbs" :width="500" :height="500" />
  </div>
</template>
<script>
import MonacoSqlpad from 'monaco-sqlpad'
export default {
  data() {
    return {
      content: null,
      dbs: []
    }
  },
  components: {
    MonacoSqlpad
  },
  async mounted() {
    // 初始化数据库及表信息用于编辑器提示数据库和表
    this.dbs = await this.getDbList()
  },
  methods: {
    /
     * 当用户需要输入表字段时回调方法
     * @return { Array } 表格字段数组
     */
    async onInputField() {
      const fields = await this.getTableCloumn()
      return fields
    },
    /**
     * 模拟异步获取数据库名&&表名
     */
    getDbList() {
      return new Promise((resolve, reject) => {
        resolve([
        {
          dbName: 'db_bar',
          tables: [
            {
              tblName: 'user',
              tableColumns: [
                {
                  fieldName: 'username'
                }
              ]
            },
            {
              tblName: 'log',
              tableColumns: []
            },
            {
              tblName: 'goods',
              tableColumns: []
            }
          ]
        },
        {
          dbName: 'db_foo',
          tables: [
            {
              tblName: 'price',
              tableColumns: []
            },
            {
              tblName: 'time',
              tableColumns: []
            },
            {
              tblName: 'updata_user',
              tableColumns: []
            }
          ]
        }
      ])
      })
    },
    /**
     * 模拟异步获取表字段
     */
    getTableCloumn() {
      return new Promise((resolve, reject) => {
        resolve(['username', 'password'])
      })
    }
  }
}
</script>

future

future
单元测试
Event配置
Option配置
SQL解析优化 目前使用正则匹配表名和字段

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.