Skip to content

Commit 27e22c3

Browse files
committed
DOC-3161: Updated mentions demo
1 parent f172347 commit 27e22c3

File tree

2 files changed

+155
-127
lines changed
  • modules/ROOT/examples/live-demos

2 files changed

+155
-127
lines changed

modules/ROOT/examples/live-demos/comments-callback-with-mentions/index.js

Lines changed: 155 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,70 @@
11
import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then(({ faker }) => {
2-
const adminUser = {
3-
id: 'johnsmith',
4-
name: 'John Smith',
5-
fullName: 'John Smith',
6-
description: 'Company Founder',
7-
image: "https://i.pravatar.cc/150?img=11"
8-
};
9-
10-
const currentUser = {
11-
id: 'jennynichols',
12-
name: 'Jenny Nichols',
13-
fullName: 'Jenny Nichols',
14-
description: 'Marketing Director',
15-
image: "https://i.pravatar.cc/150?img=10"
2+
const userDb = {
3+
'michaelcook': {
4+
id: 'michaelcook',
5+
name: 'Michael Cook',
6+
fullName: 'Michael Cook',
7+
description: 'Product Owner',
8+
image: "{{imagesdir}}/avatars/michaelcook.png"
9+
},
10+
'kalebwilson': {
11+
id: 'kalebwilson',
12+
name: 'Kaleb Wilson',
13+
fullName: 'Kaleb Wilson',
14+
description: 'Marketing Director',
15+
image: "{{imagesdir}}/avatars/kalebwilson.png"
16+
}
1617
};
1718

19+
const currentUid = 'kalebwilson';
20+
const adminUid = 'michaelcook';
21+
22+
const now = new Date();
23+
const yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString();
24+
const anhourago = new Date(now.getTime() - 60 * 60 * 1000).toISOString();
25+
26+
const fillAuthorInfo = (id, fullName, image) => ({
27+
author: id,
28+
authorName: fullName,
29+
authorAvatar: image,
30+
});
31+
32+
const getAuthorInfo = (uid) => {
33+
const user = userDb[uid];
34+
if (user) {
35+
return fillAuthorInfo(user.id, user.fullName, user.image);
36+
}
37+
return {
38+
author: uid,
39+
authorName: uid,
40+
};
41+
}
42+
1843
const conversationDb = {
1944
'mce-conversation_19679600221621399703915': {
2045
uid: 'mce-conversation_19679600221621399703915',
2146
comments: [{
2247
uid: 'mce-conversation_19679600221621399703915',
23-
author: currentUser.id,
24-
authorName: currentUser.fullName,
25-
authorAvatar: currentUser.image,
26-
content: `What do you think about this @${adminUser.id}?`,
27-
createdAt: '2021-05-19T04:48:23.914Z',
28-
modifiedAt: '2021-05-19T04:48:23.914Z'
29-
},
30-
{
48+
...getAuthorInfo(currentUid),
49+
content: `What do you think about this? @${adminUser.id}?`,
50+
createdAt: yesterday,
51+
modifiedAt: yesterday
52+
}, {
3153
uid: 'mce-conversation_19679600221621399703917',
32-
author: adminUser.id,
33-
authorName: adminUser.fullName,
34-
authorAvatar: adminUser.image,
54+
...getAuthorInfo(adminUid),
3555
content: `I think this is a great idea @${currentUser.id}!`,
36-
createdAt: "2024-05-28T12:54:24.126Z",
37-
modifiedAt: "2024-05-28T12:54:24.126Z",
56+
createdAt: anhourago,
57+
modifiedAt: anhourago,
3858
}]
3959
},
4060
'mce-conversation_420304606321716900864126': {
4161
uid: 'mce-conversation_420304606321716900864126',
4262
comments: [{
4363
uid: 'mce-conversation_420304606321716900864126',
44-
author: adminUser.id,
45-
authorName: adminUser.fullName,
46-
authorAvatar: adminUser.image,
64+
...getAuthorInfo(adminUid),
4765
content: `@${currentUser.id} Please revise this sentence, exclamation points are unprofessional!`,
48-
createdAt: '2024-05-28T12:54:24.126Z',
49-
modifiedAt: '2024-05-28T12:54:24.126Z'
66+
createdAt: yesterday,
67+
modifiedAt: anhourago
5068
}]
5169
}
5270
};
@@ -122,6 +140,10 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
122140

123141
const fakeServer = setupFakeServer();
124142

143+
/********************************
144+
* Mentions functions *
145+
********************************/
146+
125147
const mentions_fetch = (query, success) => {
126148
if (!fetchedUsers) {
127149
fetchedUsers = true;
@@ -162,6 +184,11 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
162184
}
163185
};
164186

187+
/********************************
188+
* Tiny Comments functions *
189+
* (must call "done" or "fail") *
190+
********************************/
191+
165192
const tinycomments_create = (req, done, fail) => {
166193
if (req.content === 'fail') {
167194
fail(new Error('Something has gone wrong...'));
@@ -170,128 +197,129 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
170197
conversationDb[uid] = {
171198
uid,
172199
comments: [{
173-
uid,
174-
authorName: currentUser.fullName,
175-
authorAvatar: currentUser.image,
176-
author: currentUser.name,
177-
content: req.content,
178-
createdAt: req.createdAt,
179-
modifiedAt: req.createdAt
200+
uid,
201+
...getAuthorInfo(currentUid),
202+
content: req.content,
203+
createdAt: req.createdAt,
204+
modifiedAt: req.createdAt
180205
}]
181206
};
182-
183-
setTimeout(() => {
184-
done({
185-
conversationUid: uid
186-
});
187-
}, fakeDelay);
207+
setTimeout(() => done({ conversationUid: uid }), fakeDelay);
188208
}
189209
};
190-
210+
191211
const tinycomments_reply = (req, done) => {
192212
const replyUid = 'annotation-' + randomString();
193-
const current = conversationDb[req.conversationUid];
194-
current.comments.push(
195-
{
196-
uid: replyUid,
197-
authorName: currentUser.fullName,
198-
authorAvatar: currentUser.image,
199-
author: currentUser.name,
200-
content: req.content,
201-
createdAt: req.createdAt,
202-
modifiedAt: req.createdAt
203-
}
204-
);
205-
206-
setTimeout(() => {
207-
done({
208-
commentUid: replyUid
209-
});
210-
}, fakeDelay);
213+
conversationDb[req.conversationUid].comments.push({
214+
uid: replyUid,
215+
...getAuthorInfo(currentUid),
216+
content: req.content,
217+
createdAt: req.createdAt,
218+
modifiedAt: req.createdAt
219+
});
220+
setTimeout(() => done({ commentUid: replyUid }), fakeDelay);
211221
};
212-
222+
213223
const tinycomments_delete = (req, done) => {
214-
delete conversationDb[req.conversationUid];
215-
216-
setTimeout(() => {
217-
done({
218-
canDelete: true
219-
});
220-
}, fakeDelay);
224+
if (currentUid === adminUid) { // Replace wth your own logic, e.g. check if user created the conversation
225+
delete conversationDb[req.conversationUid];
226+
setTimeout(() => done({ canDelete: true }), fakeDelay);
227+
} else {
228+
setTimeout(() => done({ canDelete: false, reason: 'Must be admin user' }), fakeDelay);
229+
}
221230
};
222-
231+
223232
const tinycomments_resolve = (req, done) => {
224-
resolvedConversationDb[req.conversationUid] = conversationDb[req.conversationUid];
225-
delete conversationDb[req.conversationUid];
226-
227-
setTimeout(() => {
228-
done({
229-
canResolve: true
230-
});
231-
}, fakeDelay);
233+
const conversation = conversationDb[req.conversationUid];
234+
if (currentUid === conversation.comments[0].author) { // Replace wth your own logic, e.g. check if user has admin priveleges
235+
delete conversationDb[req.conversationUid];
236+
setTimeout(() => done({ canResolve: true }), fakeDelay);
237+
} else {
238+
setTimeout(() => done({ canResolve: false, reason: 'Must be conversation author' }), fakeDelay);
239+
}
232240
};
233-
241+
234242
const tinycomments_delete_comment = (req, done) => {
235-
const current = conversationDb[req.conversationUid];
236-
// Should be supported on browsers ...
237-
current.comments = current.comments.filter((f) => {
238-
return f.uid !== req.commentUid;
243+
const oldcomments = conversationDb[req.conversationUid].comments;
244+
let reason = 'Comment not found';
245+
246+
const newcomments = oldcomments.filter((comment) => {
247+
if (comment.uid === req.commentUid) { // Found the comment to delete
248+
if (currentUid === comment.author) { // Replace with your own logic, e.g. check if user has admin privileges
249+
return false; // Remove the comment
250+
} else {
251+
reason = 'Not authorised to delete this comment'; // Update reason
252+
}
253+
}
254+
return true; // Keep the comment
239255
});
240-
241-
setTimeout(() => {
242-
done({
243-
canDelete: true
244-
});
245-
}, fakeDelay);
256+
if (newcomments.length === oldcomments.length) {
257+
setTimeout(() => done({ canDelete: false, reason }), fakeDelay);
258+
} else {
259+
conversationDb[req.conversationUid].comments = newcomments;
260+
setTimeout(() => done({ canDelete: true }), fakeDelay);
261+
}
246262
};
247-
263+
248264
const tinycomments_edit_comment = (req, done) => {
249-
const current = conversationDb[req.conversationUid];
250-
// Should be supported on browsers ...
251-
current.comments = current.comments.map((f) => {
252-
return f.uid === req.commentUid ? {
253-
...f,
254-
content: req.content,
255-
modifiedAt: new Date().toISOString()
256-
} : f;
265+
const oldcomments = conversationDb[req.conversationUid].comments;
266+
let reason = 'Comment not found';
267+
let canEdit = false;
268+
269+
const newcomments = oldcomments.map((comment) => {
270+
if (comment.uid === req.commentUid) { // Found the comment to delete
271+
if (currentUid === comment.author) { // Replace with your own logic, e.g. check if user has admin privileges
272+
canEdit = true; // User can edit the comment
273+
return { ...comment, content: req.content, modifiedAt: new Date().toISOString() }; // Update the comment
274+
} else {
275+
reason = 'Not authorised to edit this comment'; // Update reason
276+
}
277+
}
278+
return comment; // Keep the comment
257279
});
258-
259-
setTimeout(() => {
260-
done({
261-
canEdit: true
262-
});
263-
}, fakeDelay);
280+
281+
if (canEdit) {
282+
conversationDb[req.conversationUid].comments = newcomments;
283+
setTimeout(() => done({ canEdit }), fakeDelay);
284+
} else {
285+
setTimeout(() => done({ canEdit, reason }), fakeDelay);
286+
}
264287
};
265-
288+
266289
const tinycomments_delete_all = (req, done) => {
267-
Object.keys(conversationDb).forEach((k) => {
268-
delete conversationDb[k];
269-
});
270-
271-
setTimeout(() => {
272-
done({
273-
canDelete: true
274-
});
275-
}, fakeDelay);
290+
const conversation = conversationDb[req.conversationUid];
291+
if (currentUid === conversation.comments[0].author) { // Replace wth your own logic, e.g. check if user has admin priveleges
292+
delete conversationDb[req.conversationUid];
293+
setTimeout(() => done({ canDelete: true }), fakeDelay);
294+
} else {
295+
setTimeout(() => done({ canDelete: false, reason: 'Must be conversation author' }), fakeDelay);
296+
}
276297
};
277-
298+
278299
const tinycomments_lookup = (req, done) => {
279300
setTimeout(() => {
280301
done({
281302
conversation: {
282-
uid: conversationDb[req.conversationUid].uid,
283-
comments: conversationDb[req.conversationUid].comments.slice(0)
303+
uid: conversationDb[req.conversationUid].uid,
304+
comments: [...conversationDb[req.conversationUid].comments]
284305
}
285306
});
286307
}, fakeDelay);
287308
};
288-
289-
const tinycomments_fetch = (_, done) => {
290-
setTimeout(() => done({
291-
conversations: conversationDb
292-
}), fakeDelay);
309+
310+
const tinycomments_fetch = (conversationUids, done) => {
311+
const fetchedConversations = {};
312+
conversationUids.forEach((uid) => {
313+
const conversation = conversationDb[uid];
314+
if (conversation) {
315+
fetchedConversations[uid] = {...conversation};
316+
}
317+
});
318+
setTimeout(() => done({ conversations: fetchedConversations }), fakeDelay);
293319
};
294-
320+
321+
// Read the above `getAuthorInfo` function to see how this could be implemented
322+
const tinycomments_fetch_author_info = (_, done) => done(getAuthorInfo(currentUid));
295323

296324
tinymce.init({
297325
selector: 'textarea#comments-callback-with-mentions',
@@ -331,5 +359,6 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
331359
tinycomments_delete_comment,
332360
tinycomments_edit_comment,
333361
tinycomments_fetch,
362+
tinycomments_fetch_author_info
334363
});
335364
});

modules/ROOT/examples/live-demos/comments-callback/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const currentUid = 'kalebwilson';
2323
const adminUid = 'michaelcook';
2424

2525
const now = new Date();
26-
const currentTime = now.toISOString();
2726
const yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString();
2827
const anhourago = new Date(now.getTime() - 60 * 60 * 1000).toISOString();
2928

0 commit comments

Comments
 (0)