forked from yabwe/medium-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
placeholder.js
128 lines (103 loc) · 4.57 KB
/
placeholder.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
(function () {
'use strict';
var Placeholder = MediumEditor.Extension.extend({
name: 'placeholder',
/* Placeholder Options */
/* text: [string]
* Text to display in the placeholder
*/
text: 'Type your text',
/* hideOnClick: [boolean]
* Should we hide the placeholder on click (true) or when user starts typing (false)
*/
hideOnClick: true,
init: function () {
MediumEditor.Extension.prototype.init.apply(this, arguments);
this.initPlaceholders();
this.attachEventHandlers();
},
initPlaceholders: function () {
this.getEditorElements().forEach(this.initElement, this);
},
handleAddElement: function (event, editable) {
this.initElement(editable);
},
initElement: function (el) {
if (!el.getAttribute('data-placeholder')) {
el.setAttribute('data-placeholder', this.text);
}
this.updatePlaceholder(el);
},
destroy: function () {
this.getEditorElements().forEach(this.cleanupElement, this);
},
handleRemoveElement: function (event, editable) {
this.cleanupElement(editable);
},
cleanupElement: function (el) {
if (el.getAttribute('data-placeholder') === this.text) {
el.removeAttribute('data-placeholder');
}
},
showPlaceholder: function (el) {
if (el) {
// https://github.com/yabwe/medium-editor/issues/234
// In firefox, styling the placeholder with an absolutely positioned
// pseudo element causes the cursor to appear in a bad location
// when the element is completely empty, so apply a different class to
// style it with a relatively positioned pseudo element
if (MediumEditor.util.isFF && el.childNodes.length === 0) {
el.classList.add('medium-editor-placeholder-relative');
el.classList.remove('medium-editor-placeholder');
} else {
el.classList.add('medium-editor-placeholder');
el.classList.remove('medium-editor-placeholder-relative');
}
}
},
hidePlaceholder: function (el) {
if (el) {
el.classList.remove('medium-editor-placeholder');
el.classList.remove('medium-editor-placeholder-relative');
}
},
updatePlaceholder: function (el, dontShow) {
// If the element has content, hide the placeholder
if (el.querySelector('img, blockquote, ul, ol, table') || (el.textContent.replace(/^\s+|\s+$/g, '') !== '')) {
return this.hidePlaceholder(el);
}
if (!dontShow) {
this.showPlaceholder(el);
}
},
attachEventHandlers: function () {
if (this.hideOnClick) {
// For the 'hideOnClick' option, the placeholder should always be hidden on focus
this.subscribe('focus', this.handleFocus.bind(this));
}
// If the editor has content, it should always hide the placeholder
this.subscribe('editableInput', this.handleInput.bind(this));
// When the editor loses focus, check if the placeholder should be visible
this.subscribe('blur', this.handleBlur.bind(this));
// Need to know when elements are added/removed from the editor
this.subscribe('addElement', this.handleAddElement.bind(this));
this.subscribe('removeElement', this.handleRemoveElement.bind(this));
},
handleInput: function (event, element) {
// If the placeholder should be hidden on focus and the
// element has focus, don't show the placeholder
var dontShow = this.hideOnClick && (element === this.base.getFocusedElement());
// Editor's content has changed, check if the placeholder should be hidden
this.updatePlaceholder(element, dontShow);
},
handleFocus: function (event, element) {
// Editor has focus, hide the placeholder
this.hidePlaceholder(element);
},
handleBlur: function (event, element) {
// Editor has lost focus, check if the placeholder should be shown
this.updatePlaceholder(element);
}
});
MediumEditor.extensions.placeholder = Placeholder;
}());