-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathInlineTextEditor.js
More file actions
344 lines (288 loc) · 13 KB
/
InlineTextEditor.js
File metadata and controls
344 lines (288 loc) · 13 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// FUTURE: Merge part (or all) of this class with MultiRangeInlineEditor
define(function (require, exports, module) {
// Load dependent modules
var CodeMirror = require("thirdparty/CodeMirror/lib/codemirror"),
EventDispatcher = require("utils/EventDispatcher"),
DocumentManager = require("document/DocumentManager"),
EditorManager = require("editor/EditorManager"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
InlineWidget = require("editor/InlineWidget").InlineWidget,
KeyEvent = require("utils/KeyEvent");
/**
* Shows or hides the dirty indicator
* @private
*/
function _showDirtyIndicator($indicatorDiv, isDirty) {
// Show or hide the dirty indicator by adjusting
// the width of the div.
$indicatorDiv.css("width", isDirty ? 16 : 0);
}
/**
* Respond to dirty flag change event. If the dirty flag is associated with an inline editor,
* show (or hide) the dirty indicator.
* @private
*/
function _dirtyFlagChangeHandler(event, doc) {
var $dirtyIndicators = $(".inline-text-editor .dirty-indicator"),
$indicator;
$dirtyIndicators.each(function (index, indicator) {
$indicator = $(this);
if ($indicator.data("fullPath") === doc.file.fullPath) {
_showDirtyIndicator($indicator, doc.isDirty);
}
});
}
/**
* @constructor
* @extends {InlineWidget}
*/
function InlineTextEditor() {
InlineWidget.call(this);
this.editor = null;
// We need to set this as a capture handler so CodeMirror doesn't handle Esc before we see it.
this.handleKeyDown = this.handleKeyDown.bind(this);
this.htmlContent.addEventListener("keydown", this.handleKeyDown, true);
}
InlineTextEditor.prototype = Object.create(InlineWidget.prototype);
InlineTextEditor.prototype.constructor = InlineTextEditor;
InlineTextEditor.prototype.parentClass = InlineWidget.prototype;
InlineTextEditor.prototype.$wrapper = null;
/** @type {Editor} */
InlineTextEditor.prototype.editor = null;
InlineTextEditor.prototype.$editorHolder = null;
InlineTextEditor.prototype.$header = null;
InlineTextEditor.prototype.$filename = null;
/**
* Given a host editor and its inline editors, find the widest gutter and make all the others match
* @param {!Editor} hostEditor Host editor containing all the inline editors to sync
* @private
*/
function _syncGutterWidths(hostEditor) {
var allHostedEditors = EditorManager.getInlineEditors(hostEditor);
// add the host itself to the list too
allHostedEditors.push(hostEditor);
var maxWidth = 0;
allHostedEditors.forEach(function (editor) {
var $gutter = $(editor._codeMirror.getGutterElement()).find(".CodeMirror-linenumbers");
$gutter.css("min-width", "");
var curWidth = $gutter.width();
if (curWidth > maxWidth) {
maxWidth = curWidth;
}
});
if (allHostedEditors.length === 1) {
//There's only the host, just refresh the gutter
allHostedEditors[0]._codeMirror.setOption("gutters", allHostedEditors[0]._codeMirror.getOption("gutters"));
return;
}
maxWidth = maxWidth + "px";
allHostedEditors.forEach(function (editor) {
$(editor._codeMirror.getGutterElement()).find(".CodeMirror-linenumbers").css("min-width", maxWidth);
// Force CodeMirror to refresh the gutter
editor._codeMirror.setOption("gutters", editor._codeMirror.getOption("gutters"));
});
}
/**
* Called any time inline was closed, whether manually (via close()) or automatically
*/
InlineTextEditor.prototype.onClosed = function () {
InlineTextEditor.prototype.parentClass.onClosed.apply(this, arguments);
_syncGutterWidths(this.hostEditor);
// Destroy the inline editor.
this.setInlineContent(null);
this.htmlContent.removeEventListener("keydown", this.handleKeyDown, true);
};
/**
* Update the inline editor's height when the number of lines change. The
* base implementation of this method does nothing.
*/
InlineTextEditor.prototype.sizeInlineWidgetToContents = function () {
// brackets_codemirror_overrides.css adds height:auto to CodeMirror
// Inline editors themselves do not need to be sized, but layouts like
// the one used in CSSInlineEditor do need some manual layout.
};
/**
* Some tasks have to wait until we've been parented into the outer editor
* @param {string} the inline ID that is generated by CodeMirror after the widget that holds the inline
* editor is constructed and added to the DOM
*/
InlineTextEditor.prototype.onAdded = function () {
var self = this;
InlineTextEditor.prototype.parentClass.onAdded.apply(this, arguments);
if (this.editor) {
this.editor.refresh();
}
// Update display of inline editors when the hostEditor signals a redraw
CodeMirror.on(this.info, "redraw", function () {
// At the point where we get the redraw, CodeMirror might not yet have actually
// re-added the widget to the DOM. This is filed as https://github.com/codemirror/CodeMirror/issues/1226.
// For now, we can work around it by doing the refresh on a setTimeout().
window.setTimeout(function () {
if (self.editor) {
self.editor.refresh();
}
}, 0);
});
_syncGutterWidths(this.hostEditor);
if (this.editor) {
this.editor.focus();
}
};
/**
* @return {?Editor} If an Editor within this inline editor has focus, returns it. Otherwise returns null.
*/
InlineTextEditor.prototype.getFocusedEditor = function () {
if (this.editor && this.editor.hasFocus()) {
return this.editor;
}
return null;
};
/**
* @private
* Make sure that if we want to handle Esc to cancel a multiple selection, we don't let it bubble
* up to InlineWidget, which will close the edit.
*/
InlineTextEditor.prototype.handleKeyDown = function (e) {
if (e.keyCode === KeyEvent.DOM_VK_ESCAPE && this.editor && this.editor.getSelections().length > 1) {
CodeMirror.commands.singleSelection(this.editor._codeMirror);
e.stopImmediatePropagation();
}
};
/**
* Sets the document and range to show in the inline editor, or null to destroy the current editor and leave
* the content blank.
* @param {Document} doc The document to show, or null to show nothing
* @param {number} startLine The first line of text in `doc` to show in inline editor. Ignored if doc is null.
* @param {number} endLine The last line of text in `doc` to show in inline editor. Ignored if doc is null.
*/
InlineTextEditor.prototype.setInlineContent = function (doc, startLine, endLine) {
var self = this;
// Destroy the previous editor if we had one and clear out the filename info.
if (this.editor) {
this.editor.off(".InlineTextEditor");
this.editor.destroy(); // remove from DOM and release ref on Document
this.editor = null;
this.$filename.off(".InlineTextEditor")
.removeAttr("title");
this.$filename.html("");
}
if (!doc) {
return;
}
var range = {
startLine: startLine,
endLine: endLine
};
// dirty indicator, with file path stored on it
var $dirtyIndicatorDiv = $("<div/>")
.addClass("dirty-indicator")
.html("•")
.width(0); // initialize indicator as hidden
$dirtyIndicatorDiv.data("fullPath", doc.file.fullPath);
this.$lineNumber = $("<span class='line-number'/>");
// update contents of filename link
this.$filename.append($dirtyIndicatorDiv)
.append(doc.file.name + " : ")
.append(this.$lineNumber)
.attr("title", doc.file.fullPath);
// clicking filename jumps to full editor view
this.$filename.on("click.InlineTextEditor", function () {
CommandManager.execute(Commands.FILE_OPEN, { fullPath: doc.file.fullPath })
.done(function () {
EditorManager.getCurrentFullEditor().setCursorPos(startLine, 0, true);
});
});
var inlineInfo = EditorManager.createInlineEditorForDocument(doc, range, this.$editorHolder.get(0));
this.editor = inlineInfo.editor;
// Init line number display
this._updateLineRange(inlineInfo.editor);
// Always update the widget height when an inline editor completes a
// display update
this.editor.on("update.InlineTextEditor", function (event, editor) {
self.sizeInlineWidgetToContents();
});
// Size editor to content whenever text changes (via edits here or any
// other view of the doc: Editor fires "change" any time its text
// changes, regardless of origin)
this.editor.on("change.InlineTextEditor", function (event, editor) {
if (self.hostEditor.isFullyVisible()) {
self.sizeInlineWidgetToContents();
self._updateLineRange(editor);
}
});
// If Document's file is deleted, or Editor loses sync with Document, delegate to this._onLostContent()
this.editor.on("lostContent.InlineTextEditor", function () {
self._onLostContent.apply(self, arguments);
});
// set dirty indicator state
_showDirtyIndicator($dirtyIndicatorDiv, doc.isDirty);
};
/**
* Updates start line display.
* @param {Editor} editor
*/
InlineTextEditor.prototype._updateLineRange = function (editor) {
this._startLine = editor.getFirstVisibleLine();
this._endLine = editor.getLastVisibleLine();
this._lineCount = this._endLine - this._startLine;
this.$lineNumber.text(this._startLine + 1);
};
/**
* @param {Editor} hostEditor
*/
InlineTextEditor.prototype.load = function (hostEditor) {
InlineTextEditor.prototype.parentClass.load.apply(this, arguments);
// We don't create the actual editor here--that will happen the first time
// setInlineContent() is called.
this.$wrapper = $("<div/>").addClass("inline-text-editor").appendTo(this.$htmlContent);
this.$header = $("<div/>").addClass("inline-editor-header").appendTo(this.$wrapper);
this.$filename = $("<a/>").addClass("filename").appendTo(this.$header);
this.$editorHolder = $("<div/>").addClass("inline-editor-holder").appendTo(this.$wrapper);
};
/**
* Called when the editor containing the inline is made visible.
*/
InlineTextEditor.prototype.onParentShown = function () {
InlineTextEditor.prototype.parentClass.onParentShown.apply(this, arguments);
// Refresh line number display and codemirror line number gutter
if (this.editor) {
this._updateLineRange(this.editor);
this.editor.refresh();
}
// We need to call this explicitly whenever the host editor is reshown
this.sizeInlineWidgetToContents();
};
/**
* If Document's file is deleted, or Editor loses sync with Document, just close
*/
InlineTextEditor.prototype._onLostContent = function () {
// Note: this closes the entire inline widget if any one Editor loses sync. This seems
// better than leaving it open but suddenly removing one rule from the result list.
this.close();
};
// Consolidate all dirty document updates
// Due to circular dependencies, not safe to call on() directly
EventDispatcher.on_duringInit(DocumentManager, "dirtyFlagChange", _dirtyFlagChangeHandler);
exports.InlineTextEditor = InlineTextEditor;
});