-
Notifications
You must be signed in to change notification settings - Fork 70
/
Scriptish_openInEditor.js
98 lines (83 loc) · 3 KB
/
Scriptish_openInEditor.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
92
93
94
95
96
97
98
"use strict";
var EXPORTED_SYMBOLS = ["Scriptish_openInEditor"];
const Cu = Components.utils;
Cu.import("resource://scriptish/constants.js");
lazyImport(this, "resource://scriptish/prefmanager.js", ["Scriptish_prefRoot"]);
lazyUtil(this, "getEditor");
lazyUtil(this, "launchApplicationWithDoc");
lazyUtil(this, "alert");
lazyUtil(this, "stringBundle");
function Scriptish_openInEditor(script, parentWindow) {
var file = script.editFile;
var editor = Scriptish_getEditor(parentWindow);
// The user did not choose an editor.
if (!editor) return;
try {
if ("Scratchpad" == editor) {
openScriptInScratchpad(script, parentWindow, file);
}
else {
Scriptish_launchApplicationWithDoc(editor, file);
}
}
catch (e) {
// Something may be wrong with the editor the user selected. Remove it.
Scriptish_alert(Scriptish_stringBundle("editor.couldNotLaunch") + "\n" + e);
Scriptish_prefRoot.remove("editor");
throw e;
}
}
function openScriptInScratchpad(script, parentWindow, file) {
let spWin = (parentWindow.Scratchpad
|| Services.wm.getMostRecentWindow("navigator:browser").Scratchpad)
.openScratchpad();
spWin.addEventListener("load", function spWinLoaded() {
spWin.removeEventListener("load", spWinLoaded, false);
let Scratchpad = spWin.Scratchpad;
setFilenameForScratchpadWindow(spWin, file);
if (Scratchpad.addObserver) {
Scratchpad.addObserver({"onReady": importFileForScratchpad.bind(null, spWin, file)});
}
else {
importFileForScratchpad(spWin, file);
}
}, false);
}
function setFilenameForScratchpadWindow(spWin, file) {
let Scratchpad = spWin.Scratchpad;
// Set the filename
if (Scratchpad.setFilename) {
Scratchpad.setFilename(file.path);
}
else {
spWin.document.title = Scratchpad.filename = file.path;
}
}
function importFileForScratchpad(spWin, file) {
let Scratchpad = spWin.Scratchpad;
// Open the user script in Scratchpad
// NOTE: Resetting the "undo/redo" state on our own until Scratchpad
// handles it. We want to ensure user scripts don't get screwed up.
// See: https://bug684546.bugzilla.mozilla.org/
Scratchpad.importFromFile(file, false, function() {
let spEditor = Scratchpad.editor;
// For the Orion editor...
if (spEditor && spEditor._undoStack && spEditor._undoStack.reset) {
spEditor._undoStack.reset();
return;
}
// If not using Orion, pick out the proper editor
// Scratchpad in FF6 still uses 'textbox'
if (spEditor && spEditor._editor)
spEditor = spEditor._editor;
else if (Scratchpad.textbox)
spEditor = Scratchpad.textbox.editor;
if (spEditor
&& spEditor.resetModificationCount
&& spEditor.transactionManager
&& spEditor.transactionManager.clear) {
spEditor.resetModificationCount();
spEditor.transactionManager.clear();
}
});
}