forked from hieuthi/joplin-plugin-slash-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeMirror5.js
91 lines (83 loc) · 3.01 KB
/
codeMirror5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const COMMAND_PREFIX = '/';
module.exports = {
default: function(_context) {
function plugin(CodeMirror) {
// Handled by ./codeMirror6.ts
if (CodeMirror.cm6) return;
// Requiring Command at the top level of the file can cause an error
// when loading in CodeMirror 6 (requires are handled differently).
const Command = require('../command.js');
function createCommands(definitions) {
const commands = [];
for (let i=0; i<definitions.length; i++) {
var command = Command.create(definitions[i]);
if (command) {commands.push(command)};
}
return commands;
}
async function showSlashHints(cm, change){
if (!cm.state.completionActive && cm.state.slashCommands && change.text[0] == COMMAND_PREFIX) {
const hintFunc = async function(cm, callback) {
const cursor = cm.getCursor();
const token = cm.getRange(change.from, cursor);
let hints = [];
let supps = [];
for (let i=0; i<cm.state.slashCommands.length; i++){
var command = cm.state.slashCommands[i];
let keyword = command.keyword_;
let icon = command.icon_;
hints = hints.concat(command.getHints(token))
if (token.length> 0 && token == keyword.substring(0,token.length)){
supps.push({
text: keyword,
displayText: icon + '\t' + keyword,
hint: async (cm, data, completion) => {
const from = completion.from || data.from;
cm.replaceRange(keyword, from, cm.getCursor(), "complete");
},
})
}
}
hints = hints.concat(supps);
callback({
list: hints,
from: change.from,
to : change.to,
});
};
CodeMirror.showHint(cm, hintFunc, {
completeSingle: false,
closeOnUnfocus: true,
completeSingle: false,
async: true,
closeCharacters: /[()\[\]{};>,.`'" ]/
});
}
}
CodeMirror.defineOption("slashCommands", false, async function(cm, val, old) {
if (old && old != CodeMirror.Init) {
cm.state.slashCommands = null;
cm.off('inputRead', showSlashHints );
}
if (val){
cm.on('inputRead', showSlashHints);
await _context.postMessage("request-slash-definitions");
}
});
CodeMirror.defineExtension('updateSlashDefinitions', function(message) {
var definitions = message;
if (definitions && definitions.length > 0){
this.state.slashCommands = createCommands(definitions);
}
});
};
return {
plugin: plugin,
codeMirrorResources: [ 'addon/hint/show-hint' ],
codeMirrorOptions: {'slashCommands': true},
assets: function() {
return [ { name: './hints.css'} ];
}
}
},
}