最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CodeMirror editor autocompletion for predefined words - Stack Overflow

programmeradmin2浏览0评论

I have website where I user CodeMirror editor to enter data. I want to add very simple autoplete feature: I have some words started with @ symbol (@cat, @dog, @bird etc) and I want editor to show dropdown with this words when user types @ or @c - how can I do this? I see autoplete plugins but they need to press ctrl+space and works with schemas... so, any help will be great!

Thanks!

I have website where I user CodeMirror editor to enter data. I want to add very simple autoplete feature: I have some words started with @ symbol (@cat, @dog, @bird etc) and I want editor to show dropdown with this words when user types @ or @c - how can I do this? I see autoplete plugins but they need to press ctrl+space and works with schemas... so, any help will be great!

Thanks!

Share Improve this question asked Feb 16, 2015 at 17:07 user1859243user1859243 3151 gold badge5 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Here's how (assuming you've loaded CodeMirror and the show-hint addon):

// Dummy mode that puts words into their own token
// (You probably have a mode already)
CodeMirror.defineMode("mylanguage", function() {
  return {token: function(stream, state) {
    if (stream.match(/[@\w+]/)) return "variable";
    stream.next();
    return null;
  }};
});
// Register an array of pletion words for this mode
CodeMirror.registerHelper("hintWords", "mylanguage",
                          ["@cat", "@dog", "@bird"]);

// Create an editor
var editor = CodeMirror(document.body, {mode: "mylanguage"});
// When an @ is typed, activate pletion
editor.on("inputRead", function(editor, change) {
  if (change.text[0] == "@")
    editor.showHint();
});
发布评论

评论列表(0)

  1. 暂无评论