Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app">
    <codemirror
        v-model="code"
        ref="cm"
        :options="cmOption"
                @input="inputChange"
    ></codemirror>
</div>
              
            
!

CSS

              
                .autocomplete-div {
    display: inline-block;
    width: 100%;
  }
  .autocomplete-name {
    display: inline-block;
  }
  .autocomplete-hint {
    display: inline-block;
    float: right;
    color: #0088ff;
    margin-left: 1em;
  }
              
            
!

JS

              
                function handleShowHint(cmInstance, hintOptions) {
  let cursor = cmInstance.getCursor();
  let cursorLine = cmInstance.getLine(cursor.line);
  let end = cursor.ch;
  let start = end;

  let token = cmInstance.getTokenAt(cursor);
  console.log(cmInstance, cursor, cursorLine, end, token);
  // console.log(hintOptions.tables)
  // return hintOptions.tables;
  return {
    list: ["hello", "world"],
    from: { ch: token.start, line: cursor.line },
    to: { ch: token.end, line: cursor.line }
  };
}

function handleShowHint2(cmInstance, hintOptions) {
  let cursor = cmInstance.getCursor();
  let cursorLine = cmInstance.getLine(cursor.line);
  let end = cursor.ch;
  let start = end;

  let token = cmInstance.getTokenAt(cursor);
  console.log(cmInstance, cursor, cursorLine, end, token);
  return {
    list: [
      {
        text: "hello",
        displayText: "你好呀",
        displayInfo: "提示信息1",
        render: this.hintRender
      },
      {
        text: "world",
        displayText: "世界",
        displayInfo: "提示信息2",
        render: this.hintRender
      }
    ],
    from: {
      ch: token.start,
      line: cursor.line
    },
    to: {
      ch: token.end,
      line: cursor.line
    }
  };
}
function hintRender(element, self, data) {
  let div = document.createElement("div");
  div.setAttribute("class", "autocomplete-div");

  let divText = document.createElement("div");
  divText.setAttribute("class", "autocomplete-name");
  divText.innerText = data.displayText;

  let divInfo = document.createElement("div");
  divInfo.setAttribute("class", "autocomplete-hint");
  divInfo.innerText = data.displayInfo;

  div.appendChild(divText);
  div.appendChild(divInfo);
  element.appendChild(div);
}

Vue.use(VueCodemirror);

new Vue({
  el: "#app",
  data: {
    code: `select a from table1 where b = 1`,
    cmOption: {
      // 语言及语法模式
      mode: "text/x-sql",
      // 主题
      theme: "darcula",
      // 显示函数
      line: true,
      lineNumbers: true,
      // 软换行
      lineWrapping: true,
      // tab宽度
      tabSize: 4,
      // 代码提示功能
      hintOptions: {
        // 避免由于提示列表只有一个提示信息时,自动填充
        completeSingle: false,
        hint: handleShowHint2
        // 不同的语言支持从配置中读取自定义配置 sql语言允许配置表和字段信息,用于代码提示
        // tables: {
        //   table1: ["c1", "c2"]
        // }
      },
      // 高亮行功能
      styleActiveLine: true
    }
  },
  methods: {
    inputChange(content) {
      this.$nextTick(() => {
        // console.log("code:" + this.code);
        // console.log("content:" + content);
      });
    }
  },
  computed: {
    cm() {
      return this.$refs.cm.codemirror;
    }
  },
  mounted() {
    // 代码提示功能 当用户有输入时,显示提示信息
    this.$refs.cm.codemirror.on("inputRead", (cm) => {
      cm.showHint();
    });

    this.$refs.cm.codemirror.setSize(
      "auto",
      Math.max(document.documentElement.clientHeight - 20, 200) + "px"
    );
    this.$nextTick(() => {
      window.addEventListener("resize", () => {
        //监听浏览器窗口大小改变
        //浏览器变化执行动作
        this.$refs.cm.codemirror.setSize(
          "auto",
          Math.max(document.documentElement.clientHeight - 20, 200) + "px"
        );
      });
    });
  }
});

              
            
!
999px

Console