-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtrix_editor_element.coffee
More file actions
158 lines (124 loc) · 4.32 KB
/
Copy pathtrix_editor_element.coffee
File metadata and controls
158 lines (124 loc) · 4.32 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
#= require trix/elements/trix_toolbar_element
#= require trix/controllers/editor_controller
{makeElement, triggerEvent, handleEvent, handleEventOnce, defer} = Trix
{attachmentSelector} = Trix.AttachmentView
Trix.registerElement "trix-editor", do ->
id = 0
# Contenteditable support helpers
autofocus = (element) ->
unless document.querySelector(":focus")
if element.hasAttribute("autofocus") and document.querySelector("[autofocus]") is element
element.focus()
makeEditable = (element) ->
return if element.hasAttribute("contenteditable")
element.setAttribute("contenteditable", "")
handleEventOnce("focus", onElement: element, withCallback: -> configureContentEditable(element))
configureContentEditable = (element) ->
disableObjectResizing(element)
setDefaultParagraphSeparator(element)
disableObjectResizing = (element) ->
if document.queryCommandSupported?("enableObjectResizing")
document.execCommand("enableObjectResizing", false, false)
handleEvent("mscontrolselect", onElement: element, preventDefault: true)
setDefaultParagraphSeparator = (element) ->
if document.queryCommandSupported?("DefaultParagraphSeparator")
{tagName} = Trix.config.blockAttributes.default
if tagName in ["div", "p"]
document.execCommand("DefaultParagraphSeparator", false, tagName)
# Style
defaultCSS: """
%t:empty:not(:focus)::before {
content: attr(placeholder);
color: graytext;
}
%t a[contenteditable=false] {
cursor: text;
}
%t img {
max-width: 100%;
height: auto;
}
%t #{attachmentSelector} figcaption textarea {
resize: none;
}
%t #{attachmentSelector} figcaption textarea.trix-autoresize-clone {
position: absolute;
left: -9999px;
max-height: 0px;
}
"""
# Properties
trixId:
get: ->
if @hasAttribute("trix-id")
@getAttribute("trix-id")
else
@setAttribute("trix-id", ++id)
@trixId
toolbarElement:
get: ->
if @hasAttribute("toolbar")
@ownerDocument?.getElementById(@getAttribute("toolbar"))
else if @parentElement
toolbarId = "trix-toolbar-#{@trixId}"
@setAttribute("toolbar", toolbarId)
element = makeElement("trix-toolbar", id: toolbarId)
@parentElement.insertBefore(element, this)
element
inputElement:
get: ->
if @hasAttribute("input")
@ownerDocument?.getElementById(@getAttribute("input"))
else if @parentElement
inputId = "trix-input-#{@trixId}"
@setAttribute("input", inputId)
element = makeElement("input", type: "hidden", id: inputId)
@parentElement.insertBefore(element, @nextElementSibling)
element
editor:
get: ->
@editorController?.editor
name:
get: ->
@inputElement?.name
value:
get: ->
@inputElement?.value
set: (@defaultValue) ->
@editor?.loadHTML(@defaultValue)
# Controller delegate methods
notify: (message, data) ->
switch message
when "document-change"
@documentChangedSinceLastRender = true
when "render"
if @documentChangedSinceLastRender
@documentChangedSinceLastRender = false
@notify("change")
when "change", "attachment-add", "attachment-edit", "attachment-remove"
@inputElement?.value = Trix.serializeToContentType(this, "text/html")
if @editorController
triggerEvent("trix-#{message}", onElement: this, attributes: data)
# Element lifecycle
createdCallback: ->
makeEditable(this)
attachedCallback: ->
autofocus(this)
@editorController ?= new Trix.EditorController(editorElement: this, html: @defaultValue = @value)
@editorController.registerSelectionManager()
@registerResetListener()
requestAnimationFrame => @notify("initialize")
detachedCallback: ->
@editorController?.unregisterSelectionManager()
@unregisterResetListener()
# Form reset support
registerResetListener: ->
@resetListener = @resetBubbled.bind(this)
window.addEventListener("reset", @resetListener, false)
unregisterResetListener: ->
window.removeEventListener("reset", @resetListener, false)
resetBubbled: (event) ->
if event.target is @inputElement?.form
@reset() unless event.defaultPrevented
reset: ->
@value = @defaultValue