Skip to content

Commit 5cc6bf1

Browse files
committed
DOC-3246: Update fetch_users option across other plugin examples
1 parent 767d81f commit 5cc6bf1

File tree

6 files changed

+110
-94
lines changed

6 files changed

+110
-94
lines changed

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

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then(({ faker }) => {
2+
/* This represents a database of users on the server */
23
const userDb = {
34
'michaelcook': {
45
id: 'michaelcook',
56
name: 'Michael Cook',
6-
fullName: 'Michael Cook',
7-
description: 'Product Owner',
8-
image: "{{imagesdir}}/avatars/michaelcook.png"
7+
avatar: "{{imagesdir}}/avatars/michaelcook.png",
8+
custom: {
9+
fullName: 'Michael Cook',
10+
description: 'Product Owner'
11+
}
912
},
1013
'kalebwilson': {
1114
id: 'kalebwilson',
1215
name: 'Kaleb Wilson',
13-
fullName: 'Kaleb Wilson',
14-
description: 'Marketing Director',
15-
image: "{{imagesdir}}/avatars/kalebwilson.png"
16+
avatar: "{{imagesdir}}/avatars/kalebwilson.png",
17+
custom: {
18+
fullName: 'Kaleb Wilson',
19+
description: 'Marketing Director',
20+
}
1621
}
1722
};
1823

@@ -35,7 +40,7 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
3540
const getAuthorInfo = (uid) => {
3641
const user = userDb[uid];
3742
if (user) {
38-
return fillAuthorInfo(user.id, user.fullName, user.image);
43+
return fillAuthorInfo(user.id, user.custom.fullName, user.avatar);
3944
}
4045
return {
4146
author: uid,
@@ -85,8 +90,8 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
8590
const resolvedConversationDb = {};
8691

8792
const setupFakeServer = () => {
88-
const images = [ adminUser.image, currentUser.image ];
89-
const userNames = [ adminUser.fullName, currentUser.fullName ];
93+
const images = [ adminUser.avatar, currentUser.avatar ];
94+
const userNames = [ adminUser.custom.fullName, currentUser.custom.fullName ];
9095

9196
for (let i = 0; i < numberOfUsers; i++) {
9297
images.push(faker.image.avatar());
@@ -99,14 +104,16 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
99104
[currentUser.id]: currentUser
100105
};
101106
userNames.map((fullName) => {
102-
if ((fullName !== currentUser.fullName) && (fullName !== adminUser.fullName)) {
107+
if ((fullName !== currentUser.custom.fullName) && (fullName !== adminUser.custom.fullName)) {
103108
const id = fullName.toLowerCase().replace(/ /g, '');
104109
userDb[id] = {
105110
id,
106111
name: fullName,
107-
fullName,
108-
description: faker.person.jobTitle(),
109-
image: images[Math.floor(images.length * Math.random())]
112+
avatar: images[Math.floor(images.length * Math.random())],
113+
custom: {
114+
fullName,
115+
description: faker.person.jobTitle(),
116+
}
110117
};
111118
}
112119
});
@@ -118,8 +125,8 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
118125
const users = Object.keys(userDb).map((id) => ({
119126
id,
120127
name: userDb[id].name,
121-
image: userDb[id].image,
122-
description: userDb[id].description
128+
image: userDb[id].avatar,
129+
description: userDb[id].custom.description
123130
}));
124131
resolve(users);
125132
}, fakeDelay);
@@ -349,15 +356,13 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
349356
tinycomments_mode: 'callback',
350357
user_id: currentUser.id,
351358
fetch_users: (userIds) => {
352-
const results = userIds.map((id) => {
353-
const user = Object.values(userDb).find((user) => user.id === id);
354-
if (user) {
355-
return user;
356-
} else {
357-
throw new Error(`User ${id} not found`);
358-
}
359-
});
360-
return Promise.resolve(results);
359+
return Promise.all(
360+
userIds.map(
361+
(userId) => new Promise(
362+
(resolve) => resolve(userDb[userId] || { id: userId })
363+
)
364+
)
365+
)
361366
},
362367
tinycomments_create,
363368
tinycomments_reply,

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
44
'johnsmith': {
55
id: 'johnsmith',
66
name: 'John Smith',
7-
fullName: 'John Smith',
8-
description: 'Company Founder',
9-
image: "https://i.pravatar.cc/150?img=11"
7+
avatar: 'https://i.pravatar.cc/150?img=11',
8+
custom: {
9+
fullName: 'John Smith',
10+
description: 'Company Founder',
11+
}
1012
},
1113
'jennynichols': {
1214
id: 'jennynichols',
1315
name: 'Jenny Nichols',
14-
fullName: 'Jenny Nichols',
15-
description: 'Marketing Director',
16-
image: "https://i.pravatar.cc/150?img=10"
16+
avatar: 'https://i.pravatar.cc/150?img=10',
17+
custom: {
18+
fullName: 'Jenny Nichols',
19+
description: 'Marketing Director',
20+
}
1721
}
1822
}
1923

@@ -29,23 +33,25 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
2933
const userRequest = {};
3034

3135
const setupFakeServer = () => {
32-
const images = [ adminUser.image, currentUser.image ];
33-
const userNames = [ adminUser.fullName, currentUser.fullName ];
36+
const images = [ adminUser.avatar, currentUser.avatar ];
37+
const userNames = [ adminUser.custom.fullName, currentUser.custom.fullName ];
3438

3539
for (let i = 0; i < numberOfUsers; i++) {
3640
images.push(faker.image.avatar());
3741
userNames.push(`${faker.person.firstName()} ${faker.person.lastName()}`);
3842
}
39-
43+
4044
userNames.map((fullName) => {
41-
if ((fullName !== currentUser.fullName) && (fullName !== adminUser.fullName)) {
45+
if ((fullName !== currentUser.custom.fullName) && (fullName !== adminUser.custom.fullName)) {
4246
const id = fullName.toLowerCase().replace(/ /g, '');
4347
userDb[id] = {
4448
id,
4549
name: fullName,
46-
fullName,
47-
description: faker.person.jobTitle(),
48-
image: images[Math.floor(images.length * Math.random())]
50+
avatar: images[Math.floor(images.length * Math.random())],
51+
custom: {
52+
fullName,
53+
description: faker.person.jobTitle(),
54+
}
4955
};
5056
}
5157
});
@@ -157,15 +163,13 @@ import ('https://cdn.jsdelivr.net/npm/@faker-js/faker@9/dist/index.min.js').then
157163
sidebar_show: 'showcomments',
158164
user_id: currentUser.id,
159165
fetch_users: (userIds) => {
160-
const results = userIds.map((id) => {
161-
const user = userDb[id];
162-
if (user) {
163-
return user;
164-
} else {
165-
throw new Error(`User ${id} not found`);
166-
}
167-
});
168-
return Promise.resolve(results);
166+
return Promise.all(
167+
userIds.map(
168+
(userId) => new Promise(
169+
(resolve) => resolve(userDb[userId] || { id: userId })
170+
)
171+
)
172+
)
169173
},
170174
tinycomments_can_resolve,
171175
});

modules/ROOT/examples/live-demos/revisionhistory/index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ const getRandomDelay = () => {
44
return Math.floor(Math.random() * (maxDelay - minDelay + 1)) + minDelay;
55
};
66

7-
const userDirectory = {
7+
/* This represents a database of users on the server */
8+
const userDb = {
89
'john.doe': {
910
id: 'john.doe',
1011
name: 'John Doe',
11-
avatar: 'https://example.com/avatar/john.png'
12+
avatar: 'https://i.pravatar.cc/150?img=11'
1213
}
1314
};
1415

@@ -211,16 +212,14 @@ tinymce.init({
211212
revisionhistory_fetch,
212213
revisionhistory_fetch_revision,
213214
user_id: 'john.doe',
214-
fetch_users: (userIds) => {
215-
const results = userIds.map((id) => {
216-
const user = userDirectory[id];
217-
if (user) {
218-
return user;
219-
} else {
220-
throw new Error(`User ${id} not found`);
221-
}
222-
});
223-
return Promise.resolve(results);
224-
},
215+
fetch_users: (userIds) => {
216+
return Promise.all(
217+
userIds.map(
218+
(userId) => new Promise(
219+
(resolve) => resolve(userDb[userId] || { id: userId })
220+
)
221+
)
222+
)
223+
},
225224
revisionhistory_display_author: true
226225
});

modules/ROOT/pages/comments-embedded-mode.adoc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To add the {pluginname} plugin in embedded mode to the {productname}, configure
1111

1212
[source,js]
1313
----
14-
const userDirectory = {
14+
const userDb = {
1515
'author': {
1616
id: 'author',
1717
name: 'Name of the commenter',
@@ -25,15 +25,13 @@ tinymce.init({
2525
toolbar: 'addcomment showcomments',
2626
user_id: 'author',
2727
fetch_users: (userIds) => {
28-
const results = userIds.map((id) => {
29-
const user = userDirectory[id];
30-
if (user) {
31-
return user;
32-
} else {
33-
throw new Error(`User ${id} not found`);
34-
}
35-
});
36-
return Promise.resolve(results);
28+
return Promise.all(
29+
userIds.map(
30+
(userId) => new Promise(
31+
(resolve) => resolve(userDb[userId] || { id: userId })
32+
)
33+
)
34+
)
3735
},
3836
tinycomments_mode: 'embedded'
3937
});

modules/ROOT/pages/userlookup.adoc

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,35 @@ include::partial$configuration/fetch_users.adoc[]
3535

3636
[source,js]
3737
----
38-
const userDirectory = {
39-
'janedoe': { id: 'janedoe', name: 'Jane Doe', avatar: 'https://example.com/avatar/jane.png' },
40-
'johnsmith': { id: 'johnsmith', name: 'John Smith' },
41-
'alextaylor': { id: 'alextaylor', name: 'Alex Taylor', avatar: 'https://example.com/avatar/alex.png' }
38+
const userDb = {
39+
'janedoe': {
40+
id: 'janedoe',
41+
name: 'Jane Doe',
42+
avatar: 'https://example.com/avatar/jane.png'
43+
},
44+
'johnsmith': {
45+
id: 'johnsmith',
46+
name: 'John Smith'
47+
},
48+
'alextaylor': {
49+
id: 'alextaylor',
50+
name: 'Alex Taylor',
51+
avatar: 'https://example.com/avatar/alex.png'
52+
}
4253
};
4354
4455
tinymce.init({
4556
selector: '#editor',
4657
user_id: 'alextaylor',
4758
fetch_users: (userIds) => {
48-
const results = userIds.map((id) => {
49-
const user = userDirectory[id];
50-
if (user) {
51-
return user;
52-
} else {
53-
throw new Error(`User ${id} not found`);
54-
}
55-
});
56-
return Promise.resolve(results);
57-
}
59+
return Promise.all(
60+
userIds.map(
61+
(userId) => new Promise(
62+
(resolve) => resolve(userDb[userId] || { id: userId })
63+
)
64+
)
65+
)
66+
},
5867
});
5968
----
6069

modules/ROOT/partials/configuration/tinycomments_mentions_enabled.adoc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ The {pluginname} plugin offers the `+tinycomments_mentions_enabled+` option to c
1313

1414
[source,js]
1515
----
16-
const userDirectory = {
17-
'johnsmith': { id: 'johnsmith', name: 'John Smith' },
16+
const userDb = {
17+
'johnsmith': {
18+
id: 'johnsmith',
19+
name: 'John Smith'
20+
},
1821
};
1922
2023
tinymce.init({
@@ -25,15 +28,13 @@ tinymce.init({
2528
tinycomments_mode: 'embedded',
2629
user_id: 'johnsmith',
2730
fetch_users: (userIds) => {
28-
const results = userIds.map((id) => {
29-
const user = userDirectory[id];
30-
if (user) {
31-
return user;
32-
} else {
33-
throw new Error(`User ${id} not found`);
34-
}
35-
});
36-
return Promise.resolve(results);
31+
return Promise.all(
32+
userIds.map(
33+
(userId) => new Promise(
34+
(resolve) => resolve(userDb[userId] || { id: userId })
35+
)
36+
)
37+
)
3738
},
3839
mentions_fetch,
3940
mentions_menu_complete,

0 commit comments

Comments
 (0)