forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-editor-element.js
368 lines (303 loc) · 9.92 KB
/
text-editor-element.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
const { Emitter, Range } = require('atom');
const Grim = require('grim');
const TextEditorComponent = require('./text-editor-component');
const dedent = require('dedent');
class TextEditorElement extends HTMLElement {
initialize(component) {
this.component = component;
return this;
}
get shadowRoot() {
Grim.deprecate(dedent`
The contents of \`atom-text-editor\` elements are no longer encapsulated
within a shadow DOM boundary. Please, stop using \`shadowRoot\` and access
the editor contents directly instead.
`);
return this;
}
get rootElement() {
Grim.deprecate(dedent`
The contents of \`atom-text-editor\` elements are no longer encapsulated
within a shadow DOM boundary. Please, stop using \`rootElement\` and access
the editor contents directly instead.
`);
return this;
}
constructor() {
super();
this.emitter = new Emitter();
this.initialText = this.textContent;
if (this.tabIndex == null) this.tabIndex = -1;
this.addEventListener('focus', event =>
this.getComponent().didFocus(event)
);
this.addEventListener('blur', event => this.getComponent().didBlur(event));
}
connectedCallback() {
this.getComponent().didAttach();
this.emitter.emit('did-attach');
}
disconnectedCallback() {
this.emitter.emit('did-detach');
this.getComponent().didDetach();
}
static get observedAttributes() {
return ['mini', 'placeholder-text', 'gutter-hidden', 'readonly'];
}
attributeChangedCallback(name, oldValue, newValue) {
if (this.component) {
switch (name) {
case 'mini':
this.getModel().update({ mini: newValue != null });
break;
case 'placeholder-text':
this.getModel().update({ placeholderText: newValue });
break;
case 'gutter-hidden':
this.getModel().update({ lineNumberGutterVisible: newValue == null });
break;
case 'readonly':
this.getModel().update({ readOnly: newValue != null });
break;
}
}
}
// Extended: Get a promise that resolves the next time the element's DOM
// is updated in any way.
//
// This can be useful when you've made a change to the model and need to
// be sure this change has been flushed to the DOM.
//
// Returns a {Promise}.
getNextUpdatePromise() {
return this.getComponent().getNextUpdatePromise();
}
getModel() {
return this.getComponent().props.model;
}
setModel(model) {
this.getComponent().update({ model });
this.updateModelFromAttributes();
}
updateModelFromAttributes() {
const props = { mini: this.hasAttribute('mini') };
if (this.hasAttribute('placeholder-text'))
props.placeholderText = this.getAttribute('placeholder-text');
if (this.hasAttribute('gutter-hidden'))
props.lineNumberGutterVisible = false;
this.getModel().update(props);
if (this.initialText) this.getModel().setText(this.initialText);
}
onDidAttach(callback) {
return this.emitter.on('did-attach', callback);
}
onDidDetach(callback) {
return this.emitter.on('did-detach', callback);
}
measureDimensions() {
this.getComponent().measureDimensions();
}
setWidth(width) {
this.style.width =
this.getComponent().getGutterContainerWidth() + width + 'px';
}
getWidth() {
return this.getComponent().getScrollContainerWidth();
}
setHeight(height) {
this.style.height = height + 'px';
}
getHeight() {
return this.getComponent().getScrollContainerHeight();
}
onDidChangeScrollLeft(callback) {
return this.emitter.on('did-change-scroll-left', callback);
}
onDidChangeScrollTop(callback) {
return this.emitter.on('did-change-scroll-top', callback);
}
// Deprecated: get the width of an `x` character displayed in this element.
//
// Returns a {Number} of pixels.
getDefaultCharacterWidth() {
return this.getComponent().getBaseCharacterWidth();
}
// Extended: get the width of an `x` character displayed in this element.
//
// Returns a {Number} of pixels.
getBaseCharacterWidth() {
return this.getComponent().getBaseCharacterWidth();
}
getMaxScrollTop() {
return this.getComponent().getMaxScrollTop();
}
getScrollHeight() {
return this.getComponent().getScrollHeight();
}
getScrollWidth() {
return this.getComponent().getScrollWidth();
}
getVerticalScrollbarWidth() {
return this.getComponent().getVerticalScrollbarWidth();
}
getHorizontalScrollbarHeight() {
return this.getComponent().getHorizontalScrollbarHeight();
}
getScrollTop() {
return this.getComponent().getScrollTop();
}
setScrollTop(scrollTop) {
const component = this.getComponent();
component.setScrollTop(scrollTop);
component.scheduleUpdate();
}
getScrollBottom() {
return this.getComponent().getScrollBottom();
}
setScrollBottom(scrollBottom) {
return this.getComponent().setScrollBottom(scrollBottom);
}
getScrollLeft() {
return this.getComponent().getScrollLeft();
}
setScrollLeft(scrollLeft) {
const component = this.getComponent();
component.setScrollLeft(scrollLeft);
component.scheduleUpdate();
}
getScrollRight() {
return this.getComponent().getScrollRight();
}
setScrollRight(scrollRight) {
return this.getComponent().setScrollRight(scrollRight);
}
// Essential: Scrolls the editor to the top.
scrollToTop() {
this.setScrollTop(0);
}
// Essential: Scrolls the editor to the bottom.
scrollToBottom() {
this.setScrollTop(Infinity);
}
hasFocus() {
return this.getComponent().focused;
}
// Extended: Converts a buffer position to a pixel position.
//
// * `bufferPosition` A {Point}-like object that represents a buffer position.
//
// Be aware that calling this method with a column that does not translate
// to column 0 on screen could cause a synchronous DOM update in order to
// measure the requested horizontal pixel position if it isn't already
// cached.
//
// Returns an {Object} with two values: `top` and `left`, representing the
// pixel position.
pixelPositionForBufferPosition(bufferPosition) {
const screenPosition = this.getModel().screenPositionForBufferPosition(
bufferPosition
);
return this.getComponent().pixelPositionForScreenPosition(screenPosition);
}
// Extended: Converts a screen position to a pixel position.
//
// * `screenPosition` A {Point}-like object that represents a buffer position.
//
// Be aware that calling this method with a non-zero column value could
// cause a synchronous DOM update in order to measure the requested
// horizontal pixel position if it isn't already cached.
//
// Returns an {Object} with two values: `top` and `left`, representing the
// pixel position.
pixelPositionForScreenPosition(screenPosition) {
screenPosition = this.getModel().clipScreenPosition(screenPosition);
return this.getComponent().pixelPositionForScreenPosition(screenPosition);
}
screenPositionForPixelPosition(pixelPosition) {
return this.getComponent().screenPositionForPixelPosition(pixelPosition);
}
pixelRectForScreenRange(range) {
range = Range.fromObject(range);
const start = this.pixelPositionForScreenPosition(range.start);
const end = this.pixelPositionForScreenPosition(range.end);
const lineHeight = this.getComponent().getLineHeight();
return {
top: start.top,
left: start.left,
height: end.top + lineHeight - start.top,
width: end.left - start.left
};
}
pixelRangeForScreenRange(range) {
range = Range.fromObject(range);
return {
start: this.pixelPositionForScreenPosition(range.start),
end: this.pixelPositionForScreenPosition(range.end)
};
}
getComponent() {
if (!this.component) {
this.component = new TextEditorComponent({
element: this,
mini: this.hasAttribute('mini'),
updatedSynchronously: this.updatedSynchronously,
readOnly: this.hasAttribute('readonly')
});
this.updateModelFromAttributes();
}
return this.component;
}
setUpdatedSynchronously(updatedSynchronously) {
this.updatedSynchronously = updatedSynchronously;
if (this.component)
this.component.updatedSynchronously = updatedSynchronously;
return updatedSynchronously;
}
isUpdatedSynchronously() {
return this.component
? this.component.updatedSynchronously
: this.updatedSynchronously;
}
// Experimental: Invalidate the passed block {Decoration}'s dimensions,
// forcing them to be recalculated and the surrounding content to be adjusted
// on the next animation frame.
//
// * {blockDecoration} A {Decoration} representing the block decoration you
// want to update the dimensions of.
invalidateBlockDecorationDimensions() {
this.getComponent().invalidateBlockDecorationDimensions(...arguments);
}
setFirstVisibleScreenRow(row) {
this.getModel().setFirstVisibleScreenRow(row);
}
getFirstVisibleScreenRow() {
return this.getModel().getFirstVisibleScreenRow();
}
getLastVisibleScreenRow() {
return this.getModel().getLastVisibleScreenRow();
}
getVisibleRowRange() {
return this.getModel().getVisibleRowRange();
}
intersectsVisibleRowRange(startRow, endRow) {
return !(
endRow <= this.getFirstVisibleScreenRow() ||
this.getLastVisibleScreenRow() <= startRow
);
}
selectionIntersectsVisibleRowRange(selection) {
const { start, end } = selection.getScreenRange();
return this.intersectsVisibleRowRange(start.row, end.row + 1);
}
setFirstVisibleScreenColumn(column) {
return this.getModel().setFirstVisibleScreenColumn(column);
}
getFirstVisibleScreenColumn() {
return this.getModel().getFirstVisibleScreenColumn();
}
static createTextEditorElement() {
return document.createElement('atom-text-editor');
}
}
window.customElements.define('atom-text-editor', TextEditorElement);
module.exports = TextEditorElement;