-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack Snippets Intellisense.user.js
More file actions
181 lines (156 loc) · 6.15 KB
/
Stack Snippets Intellisense.user.js
File metadata and controls
181 lines (156 loc) · 6.15 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// ==UserScript==
// @name Stack Snippets Intellisense
// @namespace http://stackoverflow.com/
// @version 0.1
// @description Adds Intellisense using the VS Monaco JS libraries to Stack Snippets
// @author Dean Ward
// @match http://stackoverflow.com/questions/ask
// @grant none
// ==/UserScript==
window.require = {
baseUrl: "http://p.sfx.ms/Monaco/V1.6/"
};
$(function() {
$(document).on("popupLoad", function(e) {
if (!e.popup || !e.popup.hasClass || !e.popup.hasClass('popup-snippet')) return;
window.MonacoEnvironment = window.MonacoEnvironment || {
getWorkerUrl: function (workerId, label)
{
var blob = new Blob([
'self.MonacoEnvironment = self.MonacoEnvironment || {};',
'self.MonacoEnvironment.baseUrl = "https\x3a\x2f\x2fp.sfx.ms\x2fMonaco\x2fV1.6\x2f";',
'self.MonacoEnvironment.label = "' + label + '";',
'importScripts("https://p.sfx.ms/Monaco/V1.6/vs/base/worker/workerMain.js");'
], {
type: 'text/javascript'
});
var urlBuilder = window.URL || window.webkitURL;
var blobUrl = urlBuilder.createObjectURL(blob);
return blobUrl;
},
baseUrl: "http://p.sfx.ms/Monaco/V1.6/"
};
addStyle("http://p.sfx.ms/Monaco/V1.6/vs/editor/editor.main.css", "vs/editor/editor.main");
addStyle("http://p.sfx.ms/Monaco/V1.6/vs/editor/css/vs-theme.css");
$.getScript("http://p.sfx.ms/Monaco/V1.6/vs/loader.js")
.then(function() {
$.when(
$.getScript("http://p.sfx.ms/Monaco/V1.6/vs/editor/editor.main.nls.js"),
$.getScript("http://p.sfx.ms/Monaco/V1.6/vs/editor/editor.main.js")
).done(initialiseEditor);
});
});
});
function addStyle(url, name) {
var link = $('<link rel="stylesheet" type="text/css" />').attr('href', url);
if (name) {
link.attr('data-name', name);
}
$('head').append(link);
}
function initialiseEditor() {
function updateTextArea() {
var model = this.newEditor.getModel();
var version = model.getVersionId();
if (version !== this.version) {
this.textArea.text(model.getValue());
this.version = version;
}
}
var js = {
domNode: $(".snippet-box-bottom.snippet-box-left .snippet-box-container")[0],
originalEditor: $(".snippet-box-bottom.snippet-box-left .CodeMirror"),
textArea: $(".snippet-box-bottom.snippet-box-left .snippet-box-edit"),
newEditor: null,
mode: null,
version: null,
updateTextArea: updateTextArea
};
var html = {
domNode: $(".snippet-box-top.snippet-box-left .snippet-box-container")[0],
originalEditor: $(".snippet-box-top.snippet-box-left .CodeMirror"),
textArea: $(".snippet-box-top.snippet-box-left .snippet-box-edit"),
newEditor: null,
mode: null,
version: null,
updateTextArea: updateTextArea
};
var css = {
domNode: $(".snippet-box-top.snippet-box-right .snippet-box-container")[0],
originalEditor: $(".snippet-box-top.snippet-box-right .CodeMirror"),
textArea: $(".snippet-box-top.snippet-box-right .snippet-box-edit"),
newEditor: null,
mode: null,
version: null,
updateTextArea: updateTextArea
};
var IDLE_STATE = 0,
EDITORS_RENDERED = 1,
FINISHED = 2,
state = IDLE_STATE;
(function() {
var editorLoaded = false;
require(['vs/editor/editor.main', 'vs/nls!vs/editor/editor.main', 'vs/css!vs/editor/editor.main'], function () {
Monaco.Editor.getOrCreateMode('text/javascript').then(function (mode) {
js.mode = mode;
onEditorLoaded();
});
Monaco.Editor.getOrCreateMode('text/css').then(function (mode) {
css.mode = mode;
onEditorLoaded();
});
Monaco.Editor.getOrCreateMode('text/html').then(function (mode) {
html.mode = mode;
onEditorLoaded();
});
});
function onEditorLoaded() {
if (state === IDLE_STATE) {
js.newEditor = Monaco.Editor.create(js.domNode, {
value: '',
mode: 'text/plain',
fontIsMonospace: true,
suggestOnTriggerCharacters: true
});
css.newEditor = Monaco.Editor.create(css.domNode, {
value: '',
mode: 'text/plain',
fontIsMonospace: true,
suggestOnTriggerCharacters: true
});
html.newEditor = Monaco.Editor.create(html.domNode, {
value: '',
mode: 'text/plain',
fontIsMonospace: true,
suggestOnTriggerCharacters: true
});
state = EDITORS_RENDERED;
}
if (state === EDITORS_RENDERED) {
if (js.mode) {
js.newEditor.getModel().setValue('', js.mode);
js.originalEditor.remove();
}
if (css.mode) {
css.newEditor.getModel().setValue('', css.mode);
css.originalEditor.remove();
}
if (html.mode) {
html.newEditor.getModel().setValue('', html.mode);
html.originalEditor.remove();
}
if (js.mode && css.mode && html.mode) {
state = FINISHED;
}
}
}
setInterval(function () {
if (state < FINISHED) {
return;
}
js.updateTextArea();
css.updateTextArea();
html.updateTextArea();
}, 1000);
})();
}