-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
207 lines (172 loc) · 6.78 KB
/
main.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
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
203
204
205
206
207
define(function (require) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit");
var DocumentManager = brackets.getModule("document/DocumentManager");
var EditorManager = brackets.getModule("editor/EditorManager");
var FileUtils = brackets.getModule("file/FileUtils");
var MainViewManager = brackets.getModule("view/MainViewManager");
var PreferencesManager = brackets.getModule("preferences/PreferencesManager");
var packageJSON = require("text!package.json");
var COMMAND_ID = JSON.parse(packageJSON).name;
var prefs = PreferencesManager.getExtensionPrefs(COMMAND_ID);
var stateManager = PreferencesManager.stateManager.getPrefixedSystem(COMMAND_ID);
var CacheHandler = require("modules/CacheHandler");
var DiffHandler = require("modules/DiffHandler");
/*
* @private
* @method _loadDocumentHistory
* @description Loads the documents history from state if it exists.
* @params {CodeMirrorDocument} codeMirrorDoc Code Mirror document to set the history to
*/
function _loadDocumentHistory(codeMirrorDoc, wasModified, cachedDoc) {
var path = MainViewManager.getCurrentlyViewedPath();
var documentHistoriesInState = stateManager.get("documentHistories");
if (documentHistoriesInState) {
var history = documentHistoriesInState[path];
if (history) {
history = JSON.parse(history);
// Set editor value to cachedDoc before setting history and then set the value back to the modified file
// so that the history applies to the modified document too.
if (wasModified) {
var currentDoc = codeMirrorDoc.getValue();
codeMirrorDoc.setValue(cachedDoc);
codeMirrorDoc.setHistory(history);
codeMirrorDoc.setValue(currentDoc);
} else {
codeMirrorDoc.setHistory(history);
}
}
}
}
/*
* @private
* @method _onGainingEditorFocus
* @description Diff the current document against cached file, then load the history
* @param {Editor} editor Editor that gains focus
*/
function _onGainingEditorFocus(editor) {
if (!editor) {
return;
}
CacheHandler.loadCopyFromCache(editor.document.file._path).then(function (tmpCopy) {
var codeMirrorDoc = editor._codeMirror.doc;
if (tmpCopy) {
$.when(
FileUtils.readAsText(editor.document.file),
FileUtils.readAsText(tmpCopy)
).done(function (currentDoc, cachedDoc) {
DiffHandler.diffCharsAsync(currentDoc[0], cachedDoc[0]).then(
function (wasModified) {
_loadDocumentHistory(codeMirrorDoc, wasModified, cachedDoc[0]);
},
function (err) {
throw new Error(err);
}
);
});
} else {
_loadDocumentHistory(codeMirrorDoc, false);
}
});
}
/*
* @private
* @method _saveDocumentHistory
* @param {History} Code Mirror history object
* @path {string} path
*/
function _saveDocumentHistory(history, path) {
var histories = stateManager.get("documentHistories") || {};
histories[path] = JSON.stringify(history);
stateManager.set("documentHistories", histories);
}
/*
* @private
* @method _saveOnActiveEditorChange
* @description Calls _saveDocumentHistory relative to the editor that is currently losing focus
*/
function _onDocumentSaved(evt, document) {
var editor = document._masterEditor;
if (!editor) {
return;
}
var codeMirrorDoc = editor._codeMirror.doc;
var currentPath = editor.document.file._path;
var documentHistory = codeMirrorDoc.getHistory();
var docHistorySizes = codeMirrorDoc.historySize();
if (documentHistory && (docHistorySizes.undo || docHistorySizes.redo)) {
_saveDocumentHistory(documentHistory, currentPath);
CacheHandler.saveCopyToCache(currentPath);
}
}
/*
* @private
* @method _detachDocumentHistory()
* @description Detaches (deletes) and returns the document history for the deleted file on DocumentManager pathDeleted or fileNameChange
* @param {String} path
* @returns {String} history JSON.stringified history object, might be null
*/
function _detachDocumentHistory(path) {
var documentHistories = stateManager.get("documentHistories");
var history = documentHistories[path];
if (history) {
delete documentHistories[path];
CacheHandler.deleteCacheFile(path);
stateManager.set("documentHistories", documentHistories);
}
return history;
}
/*
* @private
* @method _renameDocumentHistoryOnFileRename()
* @description Renames the document history pref key for the changed file on DocumentManager.fileNameChange
* @param {Event} evt
* @param {String} oldName
* @param {String} newName
*/
function _renameDocumentHistoryPathOnFileRename(evt, oldPath, newPath) {
var detectedHistory = _detachDocumentHistory(null, oldPath);
if (detectedHistory) {
_saveDocumentHistory(detectedHistory, newPath);
}
}
/*
* @private
* @method _bindEventHandlers()
* @description Binds event handlers on load and on prefs change event if prefs.persistHistory is enabled
*/
function _bindEventHandlers() {
EditorManager.on("activeEditorChange.persistHistory", function (evt, editorGainingFocus) {
_onGainingEditorFocus(editorGainingFocus);
});
DocumentManager.on("documentSaved.persistHistory", _onDocumentSaved);
DocumentManager.on("pathDeleted.persistHistory", function (evt, path) {
if (path.indexOf(CacheHandler.getCacheDirPath()) === -1) {
_detachDocumentHistory(path);
}
});
DocumentManager.on("fileNameChange.persistHistory", _renameDocumentHistoryPathOnFileRename);
}
/*
* @private
* @method _unbindEventHandlers()
* @description binds event handlers on prefs change event if prefs.persistHistory is disabled
*/
function _unbindExtensionEventHandlers() {
EditorManager.off("activeEditorChange.persistHistory");
DocumentManager.off("pathDeleted.persistHistory fileNameChange.persistHistory pathDeleted.persistHistory");
}
/* Define preferences */
prefs.definePreference("persistHistory", "boolean", prefs.get("persistHistory") || true).on("change", function () {
if (prefs.get("persistHistory")) {
_bindEventHandlers();
} else {
_unbindExtensionEventHandlers();
}
});
prefs.definePreference("cacheTimeToLiveInDays", "number", prefs.get("cacheTimeToLiveInDays") || 14);
/* Init */
AppInit.extensionsLoaded(function () {
CacheHandler.cleanCacheFolder();
});
});