forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
202 lines (188 loc) · 7.62 KB
/
Copy patheditor.js
File metadata and controls
202 lines (188 loc) · 7.62 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var ___userscripts = {
cursor: "auto",
editor: null,
savedCode: null,
sessionCode: null,
toggleOff: null,
discardButton: document.getElementById("discard"),
downloadButton: document.getElementById("download"),
editorElement: document.getElementById("code"),
infoButton: document.getElementById("info"),
saveButton: document.getElementById("save"),
statusElement: document.getElementById("status"),
toggleButton: document.getElementById("toggle"),
getLanguageCode: function(langCode) {
langCode = langCode.includes("-") ? langCode.split("-")[0] : langCode;
langCode = !(langCode in ___strings) ? "en" : langCode;
return "en";
},
toLocalizedString: function(str) {
var langCode = document.body.getAttribute("lang");
return ___strings[langCode][str];
},
setStatusText: function(str, isModificationDate = false) {
if (isModificationDate) {
str = this.toLocalizedString("modification_date") + " " + str;
}
this.statusElement.innerHTML = str;
},
getInfo: function() {
window.webkit.messageHandlers.getInfo.postMessage("");
},
downloadScript: function() {
window.webkit.messageHandlers.downloadScript.postMessage("");
},
toggle: function() {
if (___userscripts.toggleOff === true) {
___userscripts.toggleOff = false;
document.body.classList.remove("disabled");
window.webkit.messageHandlers.setStatus.postMessage(false);
if (___userscripts.savedCode != null) {
window.webkit.messageHandlers.setModificationDate.postMessage("");
} else {
___userscripts.setStatusText(___userscripts.toLocalizedString("status_ready"))
}
} else {
___userscripts.toggleOff = true;
document.body.classList.add("disabled");
___userscripts.setStatusText(___userscripts.toLocalizedString("status_disabled"));
window.webkit.messageHandlers.setStatus.postMessage(true);
}
},
enableButtons: function() {
this.saveButton.removeAttribute("disabled");
this.discardButton.removeAttribute("disabled");
},
disableButtons: function() {
this.saveButton.setAttribute("disabled", true);
this.discardButton.setAttribute("disabled", true);
},
discard: function() {
___userscripts.editor.setValue(___userscripts.savedCode);
___userscripts.disableButtons();
___userscripts.editor.focus();
},
saveTry: function() {
if (!___userscripts.saveButton.disabled) {
var code = ___userscripts.editor.getValue();
window.webkit.messageHandlers.saveCode.postMessage(code);
___userscripts.disableButtons();
___userscripts.setStatusText(___userscripts.toLocalizedString("status_saving"));
}
},
saveSucceed: function(newlySavedCode) {
this.savedCode = unescape(newlySavedCode);
this.sessionCode = "";
this.setStatusText(this.toLocalizedString("status_save_succeed"));
},
saveFail: function() {
this.setStatusText(this.toLocalizedString("status_save_fail"));
this.enableButtons();
},
mouseOverListen: function(e) {
var sourceElement = e.srcElement;
var cursorValue = getComputedStyle(sourceElement).cursor;
if (cursorValue != this.currentCursor) {
this.currentCursor = cursorValue;
window.webkit.messageHandlers.setCursor.postMessage(cursorValue);
}
},
editorOnChange: function(e, c) {
var inputAction = c.origin;
if (inputAction !== "setValue") {
___userscripts.sessionCode = e.getValue();
___userscripts.enableButtons();
}
if (inputAction === "undo" || inputAction === "redo") {
if (___userscripts.sessionCode.localeCompare(___userscripts.savedCode) === 0) {
___userscripts.disableButtons();
}
}
},
editorOnKeydown: function(cm, e) {
var currentLinePosition = cm.getCursor()["ch"];
if (!cm.state.completionActive && e.metaKey === false && e.keyCode >= 65 && e.keyCode <= 90 && currentLinePosition != 0) {
cm.showHint({completeSingle: false});
}
},
startEditor: function() {
this.editor = CodeMirror.fromTextArea(this.editorElement, {
autoCloseBrackets: true,
extraKeys: {
"Ctrl-Space": "autocomplete",
"Cmd-S": function() {
___userscripts.saveTry();
},
"Cmd-/": "toggleComment",
"Ctrl-Q": function(cm) {
cm.foldCode(cm.getCursor());
},
Tab: function(cm) {
var s = Array(cm.getOption("indentUnit") + 1).join(" ");
cm.replaceSelection(s);
}
},
hintOptions: {
useGlobalScope: true
},
indentUnit: 2,
lineNumbers: true,
lineWrapping: true,
matchBrackets: true,
mode: "javascript",
smartIndent: true,
styleActiveLine: true,
tabSize: 2,
foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
});
//add event listeners for the editor
this.editor.on("change", this.editorOnChange);
this.editor.on("keydown", this.editorOnKeydown);
if (this.savedCode != null) {
this.editor.setValue(this.savedCode);
}
//focus the editor
this.editor.focus();
//tell VC that editor is ready
window.webkit.messageHandlers.webViewReady.postMessage("");
},
init: function(langCode, version, toggle, code) {
//set the language code on the body element
document.body.setAttribute("lang", this.getLanguageCode(langCode));
//set the version number in the version element
document.getElementById("version").innerHTML = version;
//set toggle status
if (JSON.parse(toggle) === true) {
this.toggleOff = true;
document.body.classList.add("disabled");
this.setStatusText(___userscripts.toLocalizedString("status_disabled"))
} else {
this.toggleOff = false;
this.setStatusText(___userscripts.toLocalizedString("status_ready"))
}
//set the saved code
if (code != null) {
this.savedCode = unescape(code);
}
//load the init text for the various DOM elements
this.editorElement.placeholder = this.toLocalizedString("editor_placeholder");
this.saveButton.innerHTML = this.toLocalizedString("button_save");
this.discardButton.innerHTML = this.toLocalizedString("button_discard");
//add event listeners
this.downloadButton.addEventListener("click", this.downloadScript);
this.infoButton.addEventListener("click", this.getInfo);
this.toggleButton.addEventListener("click", this.toggle);
this.discardButton.addEventListener("click", this.discard);
this.saveButton.addEventListener("click", this.saveTry);
document.addEventListener("mouseover", this.mouseOverListen);
//start the editor
this.startEditor();
},
windowLoad: function() {
var code = `console.log('foo');`;
var tog = true;
this.init("en-GB", "v1.4.0", tog, code);
}
};
//window.onload = ___userscripts.windowLoad();