forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-editor-element.coffee
328 lines (258 loc) · 10.5 KB
/
text-editor-element.coffee
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
{Emitter, CompositeDisposable} = require 'event-kit'
TextBuffer = require 'text-buffer'
TextEditorComponent = require './text-editor-component'
StylesElement = require './styles-element'
ShadowStyleSheet = null
class TextEditorElement extends HTMLElement
model: null
componentDescriptor: null
component: null
attached: false
tileSize: null
focusOnAttach: false
hasTiledRendering: true
logicalDisplayBuffer: true
createdCallback: ->
# Use globals when the following instance variables aren't set.
@themes = atom.themes
@workspace = atom.workspace
@assert = atom.assert
@views = atom.views
@styles = atom.styles
@emitter = new Emitter
@subscriptions = new CompositeDisposable
@addEventListener 'focus', @focused.bind(this)
@addEventListener 'blur', @blurred.bind(this)
@classList.add('editor')
@setAttribute('tabindex', -1)
initializeContent: (attributes) ->
unless ShadowStyleSheet?
ShadowStyleSheet = document.createElement('style')
ShadowStyleSheet.textContent = @themes.loadLessStylesheet(require.resolve('../static/text-editor-shadow.less'))
@createShadowRoot()
@shadowRoot.appendChild(ShadowStyleSheet.cloneNode(true))
@stylesElement = new StylesElement
@stylesElement.initialize(@styles)
@stylesElement.setAttribute('context', 'atom-text-editor')
@rootElement = document.createElement('div')
@rootElement.classList.add('editor--private')
@shadowRoot.appendChild(@stylesElement)
@shadowRoot.appendChild(@rootElement)
attachedCallback: ->
@buildModel() unless @getModel()?
@assert(@model.isAlive(), "Attaching a view for a destroyed editor")
@mountComponent() unless @component?
@listenForComponentEvents()
@component.checkForVisibilityChange()
if this is document.activeElement
@focused()
@emitter.emit("did-attach")
detachedCallback: ->
@unmountComponent()
@subscriptions.dispose()
@subscriptions = new CompositeDisposable
@emitter.emit("did-detach")
listenForComponentEvents: ->
@subscriptions.add @component.onDidChangeScrollTop =>
@emitter.emit("did-change-scroll-top", arguments...)
@subscriptions.add @component.onDidChangeScrollLeft =>
@emitter.emit("did-change-scroll-left", arguments...)
initialize: (model, {@views, @themes, @workspace, @assert, @styles}) ->
throw new Error("Must pass a views parameter when initializing TextEditorElements") unless @views?
throw new Error("Must pass a themes parameter when initializing TextEditorElements") unless @themes?
throw new Error("Must pass a workspace parameter when initializing TextEditorElements") unless @workspace?
throw new Error("Must pass an assert parameter when initializing TextEditorElements") unless @assert?
throw new Error("Must pass a styles parameter when initializing TextEditorElements") unless @styles?
@setModel(model)
this
setModel: (model) ->
throw new Error("Model already assigned on TextEditorElement") if @model?
return if model.isDestroyed()
@model = model
@model.setUpdatedSynchronously(@isUpdatedSynchronously())
@initializeContent()
@mountComponent()
@addGrammarScopeAttribute()
@addMiniAttribute() if @model.isMini()
@addEncodingAttribute()
@model.onDidChangeGrammar => @addGrammarScopeAttribute()
@model.onDidChangeEncoding => @addEncodingAttribute()
@model.onDidDestroy => @unmountComponent()
@model.onDidChangeMini (mini) => if mini then @addMiniAttribute() else @removeMiniAttribute()
@model
getModel: ->
@model ? @buildModel()
buildModel: ->
@setModel(@workspace.buildTextEditor(
buffer: new TextBuffer(@textContent)
softWrapped: false
tabLength: 2
softTabs: true
mini: @hasAttribute('mini')
lineNumberGutterVisible: not @hasAttribute('gutter-hidden')
placeholderText: @getAttribute('placeholder-text')
))
mountComponent: ->
@component = new TextEditorComponent(
hostElement: this
rootElement: @rootElement
stylesElement: @stylesElement
editor: @model
tileSize: @tileSize
views: @views
themes: @themes
workspace: @workspace
assert: @assert
)
@rootElement.appendChild(@component.getDomNode())
@shadowRoot.addEventListener('blur', @shadowRootBlurred.bind(this), true)
unmountComponent: ->
if @component?
@component.destroy()
@component.getDomNode().remove()
@component = null
focused: ->
@component?.focused()
blurred: (event) ->
@component?.blurred()
# Work around what seems to be a bug in Chromium. Focus can be stolen from the
# hidden input when clicking on the gutter and transferred to the
# already-focused host element. The host element never gets a 'focus' event
# however, which leaves us in a limbo state where the text editor element is
# focused but the hidden input isn't focused. This always refocuses the hidden
# input if a blur event occurs in the shadow DOM that is transferring focus
# back to the host element.
shadowRootBlurred: (event) ->
@component.focused() if event.relatedTarget is this
addGrammarScopeAttribute: ->
@dataset.grammar = @model.getGrammar()?.scopeName?.replace(/\./g, ' ')
addMiniAttribute: ->
@setAttributeNode(document.createAttribute("mini"))
removeMiniAttribute: ->
@removeAttribute("mini")
addEncodingAttribute: ->
@dataset.encoding = @model.getEncoding()
hasFocus: ->
this is document.activeElement or @contains(document.activeElement)
setUpdatedSynchronously: (@updatedSynchronously) ->
@model?.setUpdatedSynchronously(@updatedSynchronously)
@updatedSynchronously
isUpdatedSynchronously: -> @updatedSynchronously
# Extended: Continuously reflows lines and line numbers. (Has performance overhead)
#
# * `continuousReflow` A {Boolean} indicating whether to keep reflowing or not.
setContinuousReflow: (continuousReflow) ->
@component?.setContinuousReflow(continuousReflow)
# Extended: get the width of a character of text displayed in this element.
#
# Returns a {Number} of pixels.
getDefaultCharacterWidth: ->
@getModel().getDefaultCharWidth()
# Extended: Get the maximum scroll top that can be applied to this element.
#
# Returns a {Number} of pixels.
getMaxScrollTop: ->
@component?.getMaxScrollTop()
# Extended: Converts a buffer position to a pixel position.
#
# * `bufferPosition` An object that represents a buffer position. It can be either
# an {Object} (`{row, column}`), {Array} (`[row, column]`), or {Point}
#
# Returns an {Object} with two values: `top` and `left`, representing the pixel position.
pixelPositionForBufferPosition: (bufferPosition) ->
@component.pixelPositionForBufferPosition(bufferPosition)
# Extended: Converts a screen position to a pixel position.
#
# * `screenPosition` An object that represents a screen position. It can be either
# an {Object} (`{row, column}`), {Array} (`[row, column]`), or {Point}
#
# Returns an {Object} with two values: `top` and `left`, representing the pixel positions.
pixelPositionForScreenPosition: (screenPosition) ->
@component.pixelPositionForScreenPosition(screenPosition)
# Extended: Retrieves the number of the row that is visible and currently at the
# top of the editor.
#
# Returns a {Number}.
getFirstVisibleScreenRow: ->
@getVisibleRowRange()[0]
# Extended: Retrieves the number of the row that is visible and currently at the
# bottom of the editor.
#
# Returns a {Number}.
getLastVisibleScreenRow: ->
@getVisibleRowRange()[1]
# Extended: call the given `callback` when the editor is attached to the DOM.
#
# * `callback` {Function}
onDidAttach: (callback) ->
@emitter.on("did-attach", callback)
# Extended: call the given `callback` when the editor is detached from the DOM.
#
# * `callback` {Function}
onDidDetach: (callback) ->
@emitter.on("did-detach", callback)
onDidChangeScrollTop: (callback) ->
@emitter.on("did-change-scroll-top", callback)
onDidChangeScrollLeft: (callback) ->
@emitter.on("did-change-scroll-left", callback)
setScrollLeft: (scrollLeft) ->
@component.setScrollLeft(scrollLeft)
setScrollRight: (scrollRight) ->
@component.setScrollRight(scrollRight)
setScrollTop: (scrollTop) ->
@component.setScrollTop(scrollTop)
setScrollBottom: (scrollBottom) ->
@component.setScrollBottom(scrollBottom)
# Essential: Scrolls the editor to the top
scrollToTop: ->
@setScrollTop(0)
# Essential: Scrolls the editor to the bottom
scrollToBottom: ->
@setScrollBottom(Infinity)
getScrollTop: ->
@component?.getScrollTop() or 0
getScrollLeft: ->
@component?.getScrollLeft() or 0
getScrollRight: ->
@component?.getScrollRight() or 0
getScrollBottom: ->
@component?.getScrollBottom() or 0
getScrollHeight: ->
@component?.getScrollHeight() or 0
getScrollWidth: ->
@component?.getScrollWidth() or 0
getVerticalScrollbarWidth: ->
@component?.getVerticalScrollbarWidth() or 0
getHorizontalScrollbarHeight: ->
@component?.getHorizontalScrollbarHeight() or 0
getVisibleRowRange: ->
@component?.getVisibleRowRange() or [0, 0]
intersectsVisibleRowRange: (startRow, endRow) ->
[visibleStart, visibleEnd] = @getVisibleRowRange()
not (endRow <= visibleStart or visibleEnd <= startRow)
selectionIntersectsVisibleRowRange: (selection) ->
{start, end} = selection.getScreenRange()
@intersectsVisibleRowRange(start.row, end.row + 1)
screenPositionForPixelPosition: (pixelPosition) ->
@component.screenPositionForPixelPosition(pixelPosition)
pixelRectForScreenRange: (screenRange) ->
@component.pixelRectForScreenRange(screenRange)
pixelRangeForScreenRange: (screenRange) ->
@component.pixelRangeForScreenRange(screenRange)
setWidth: (width) ->
@style.width = (@component.getGutterWidth() + width) + "px"
getWidth: ->
@offsetWidth - @component.getGutterWidth()
setHeight: (height) ->
@style.height = height + "px"
getHeight: ->
@offsetHeight
# Experimental: Invalidate the passed block {Decoration} 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: ->
@component.invalidateBlockDecorationDimensions(arguments...)
module.exports = TextEditorElement = document.registerElement 'atom-text-editor', prototype: TextEditorElement.prototype