-
Notifications
You must be signed in to change notification settings - Fork 8
/
QuickLinksPlugin.ts
94 lines (85 loc) · 2.61 KB
/
QuickLinksPlugin.ts
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
92
93
94
interface Hint {
text: string;
hint: Function;
displayText?: string;
render?: Function;
}
module.exports = {
default: function(context: any) {
const buildHints = async (prefix: string) =>{
const response = await context.postMessage({ command: 'getNotes', prefix: prefix });
let hints: Hint[] = [];
const notes = response.notes;
for (let i = 0; i < notes.length; i++) {
const note = notes[i];
const hint: Hint = {
text: note.title,
hint: async (cm, data, completion) => {
const from = completion.from || data.from;
from.ch -= 2;
cm.replaceRange(`[${note.title}](:/${note.id})`, from, cm.getCursor(), "complete");
},
};
if (response.showFolders) {
const folder = !!note.folder ? note.folder : "unknown";
hint.render = (elem, _data, _completion) => {
const p = elem.ownerDocument.createElement('div');
p.setAttribute('style', 'width: 100%; display:table;');
elem.appendChild(p);
p.innerHTML = `
<div style="display:table-cell; padding-right: 5px">${note.title}</div>
<div style="display:table-cell; text-align: right;"><small><em>In ${note.folder}</em></small></div>
`
};
} else {
hint.displayText = note.title;
}
hints.push(hint);
}
return hints;
}
const plugin = function(CodeMirror) {
CodeMirror.defineOption('quickLinks', false, function(cm, value, prev) {
if (!value) return;
cm.on('inputRead', async function (cm1, change) {
if (!cm1.state.completionActive && cm.getTokenAt(cm.getCursor()).string === '@@') {
const start = {line: change.from.line, ch: change.from.ch + 1};
const hint = function(cm, callback) {
const cursor = cm.getCursor();
let prefix = cm.getRange(start, cursor) || '';
buildHints(prefix).then(hints => {
callback({
list: hints,
from: {line: change.from.line, ch: change.from.ch + 1},
to: {line: change.to.line, ch: change.to.ch + 1},
});
});
};
setTimeout(function () {
CodeMirror.showHint(cm, hint, {
completeSingle: false,
closeOnUnfocus: true,
async: true,
closeCharacters: /[()\[\]{};:>,]/
});
}, 10);
}
});
});
};
return {
plugin: plugin,
codeMirrorResources: [
'addon/hint/show-hint',
],
codeMirrorOptions: {
'quickLinks': true,
},
assets: function() {
return [
{ name: './show-hint.css'},
]
}
}
}
}