-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
attachment_gallery_filter.coffee
54 lines (41 loc) · 1.58 KB
/
attachment_gallery_filter.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
Trix.attachmentGalleryFilter = (snapshot) ->
filter = new Filter snapshot
filter.perform()
filter.getSnapshot()
class Filter
BLOCK_ATTRIBUTE_NAME = "attachmentGallery"
TEXT_ATTRIBUTE_NAME = "presentation"
TEXT_ATTRIBUTE_VALUE = "gallery"
constructor: (snapshot) ->
{@document, @selectedRange} = snapshot
perform: ->
@removeBlockAttribute()
@applyBlockAttribute()
getSnapshot: ->
{@document, @selectedRange}
# Private
removeBlockAttribute: ->
for range in @findRangesOfBlocks()
@document = @document.removeAttributeAtRange(BLOCK_ATTRIBUTE_NAME, range)
applyBlockAttribute: ->
offset = 0
for range in @findRangesOfPieces() when range[1] - range[0] > 1
range[0] += offset
range[1] += offset
unless @document.getCharacterAtPosition(range[1]) is "\n"
@document = @document.insertBlockBreakAtRange(range[1])
@moveSelectedRangeForward() if range[1] < @selectedRange[1]
offset++
unless range[0] is 0
unless @document.getCharacterAtPosition(range[0] - 1) is "\n"
@document = @document.insertBlockBreakAtRange(range[0])
@moveSelectedRangeForward() if range[0] < @selectedRange[0]
offset++
@document = @document.applyBlockAttributeAtRange(BLOCK_ATTRIBUTE_NAME, true, range)
findRangesOfBlocks: ->
@document.findRangesForBlockAttribute(BLOCK_ATTRIBUTE_NAME)
findRangesOfPieces: ->
@document.findRangesForTextAttribute(TEXT_ATTRIBUTE_NAME, withValue: TEXT_ATTRIBUTE_VALUE)
moveSelectedRangeForward: ->
@selectedRange[0] += 1
@selectedRange[1] += 1