Repository navigation
Expand file tree
/
Copy pathcomments.js
More file actions
154 lines (93 loc) · 4.09 KB
/
Copy pathcomments.js
File metadata and controls
154 lines (93 loc) · 4.09 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
let editor = codex.editor;
module.exports = function (comments) {
var draw = {
commentsField: function (blockId) {
var field = editor.draw.node('DIV', 'ce-comments-field');
field.dataset.blockId = blockId;
return field;
},
input: function (text) {
var wrapper = editor.draw.node('DIV', 'ce-comment'),
input = editor.draw.node('DIV', 'ce-comment__input', {'contentEditable': 'true', 'textContent':text||''}),
deleteBtn = editor.draw.node('DIV', 'ce-comment__delete', {'textContent': 'Delete'}),
postBtn = editor.draw.node('DIV', 'ce-comment__post', {'textContent': text?'Save':'Comment'});
postBtn.addEventListener('click', callbacks.commentClicked);
deleteBtn.addEventListener('click', callbacks.deleteClicked);
wrapper.appendChild(input);
wrapper.appendChild(postBtn);
wrapper.appendChild(deleteBtn);
wrapper.dataset.edited = text?'edited ':'';
return wrapper;
},
comment: function (data) {
if (!data.text) return;
var wrapper = editor.draw.node('DIV', 'ce-comment'),
text = editor.draw.node('DIV', 'ce-comment__text', {'textContent': data.text}),
date = new Date().toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
hour12: false
}),
time = editor.draw.node('DIV', 'ce-comment__time', {'textContent': date}),
deleteBtn = editor.draw.node('DIV', 'ce-comment__delete', {'textContent': 'Delete'}),
editBtn = editor.draw.node('DIV', 'ce-comment__edit', {'textContent': 'Edit'});
editBtn.addEventListener('click', callbacks.editClicked);
deleteBtn.addEventListener('click', callbacks.deleteClicked);
wrapper.dataset.edited = data.edited;
time.dataset.edited = data.edited;
wrapper.appendChild(text);
wrapper.appendChild(time);
wrapper.appendChild(editBtn);
wrapper.appendChild(deleteBtn);
return wrapper;
}
};
var callbacks = {
commentClicked: function (e) {
var field = e.path[2],
wrapper = e.path[1],
input = wrapper.querySelector('.ce-comment__input');
if (input.textContent.trim() == '') return;
var comment = draw.comment({
text: input.textContent,
edited: wrapper.dataset.edited
});
field.replaceChild(comment, wrapper);
},
editClicked: function (e) {
var field = e.path[2],
wrapper = e.path[1],
text = wrapper.querySelector('.ce-comment__text');
var comment = draw.input(text.textContent);
field.replaceChild(comment, wrapper);
},
deleteClicked: function (e) {
var field = e.path[2],
wrapper = e.path[1];
field.removeChild(wrapper);
}
};
var methods = {
getCoords: function (block) {
var rect = block.getBoundingClientRect();
return {
x: window.pageXOffset + rect.left,
y: window.pageYOffset + rect.top
};
}
};
comments.add = function (block) {
var blockId = block.dataset.id;
var field = editor.nodes.commentsSidebar.querySelector('.ce-comments-field[data-block-id="'+blockId+'"]') ||
draw.commentsField(blockId);
var comment = draw.input();
comment.querySelector('.ce-comment__input').focus();
field.appendChild(comment);
var blockCoords = methods.getCoords(block);
field.style.top = blockCoords.y + 'px';
editor.nodes.commentsSidebar.appendChild(field);
};
return comments;
}({});