|
1 | | -const getRandomDelay = () => { |
2 | | - const minDelay = 500; |
3 | | - const maxDelay = 2000; |
4 | | - return Math.floor(Math.random() * (maxDelay - minDelay + 1)) + minDelay; |
5 | | -}; |
| 1 | +const API_URL = 'https://demouserdirectory.tiny.cloud/users'; |
6 | 2 |
|
7 | | -/* This represents a database of users on the server */ |
8 | | -const userDb = { |
9 | | - 'john.doe': { |
10 | | - id: 'john.doe', |
11 | | - name: 'John Doe', |
12 | | - avatar: 'https://i.pravatar.cc/150?img=11' |
13 | | - } |
14 | | -}; |
15 | | - |
16 | | -const lightRevisions = [ |
17 | | - { |
18 | | - revisionId: '3', |
19 | | - author: { |
20 | | - id: 'tiny.husky', |
21 | | - name: 'A Tiny Husky', |
22 | | - avatar: '{{imagesdir}}/tiny-husky.jpg', |
23 | | - }, |
24 | | - createdAt: '2023-11-25T08:30:21.578Z' |
25 | | - }, |
26 | | - { |
27 | | - revisionId: '2', |
28 | | - author: { |
29 | | - id: 'tiny.user', |
30 | | - name: 'A Tiny User', |
31 | | - avatar: '{{imagesdir}}/logos/android-chrome-192x192.png', |
32 | | - }, |
33 | | - createdAt: '2023-11-24T22:26:21.578Z', |
34 | | - }, |
35 | | - { |
36 | | - revisionId: '1', |
37 | | - author: { |
38 | | - id: 'tiny.user', |
39 | | - name: 'A Tiny User', |
40 | | - avatar: '{{imagesdir}}/logos/android-chrome-192x192.png', |
41 | | - }, |
42 | | - createdAt: '2023-11-23T20:26:21.578Z', |
43 | | - }, |
44 | | -]; |
45 | | - |
46 | | -const revisionhistory_fetch = () => |
47 | | - new Promise((resolve) => { |
48 | | - setTimeout(() => { |
49 | | - resolve( |
50 | | - lightRevisions |
51 | | - .sort((a, b) => |
52 | | - new Date(a.createdAt) < new Date(b.createdAt) ? -1 : 1 |
53 | | - ) |
54 | | - .reverse() |
55 | | - ); |
56 | | - }, getRandomDelay()); |
57 | | - }); |
| 3 | +const fakeDelay = 200; |
58 | 4 |
|
59 | 5 | const revisions = [ |
60 | 6 | { |
61 | 7 | revisionId: '3', |
62 | 8 | createdAt: '2023-11-24T22:26:21.578Z', |
63 | 9 | author: { |
64 | | - id: 'tiny.husky', |
| 10 | + id: 'james-wilson', |
65 | 11 | name: 'A Tiny Husky', |
66 | | - avatar: '{{imagesdir}}/tiny-husky.jpg', |
| 12 | + avatar: 'https://sneak-preview.tiny.cloud/demouserdirectory/images/employee_james-wilson_128_52f19412.jpg', |
67 | 13 | }, |
68 | 14 | content: ` |
69 | 15 | <p><img style="display: block; margin-left: auto; margin-right: auto;" title="Tiny Logo" src="https://www.tiny.cloud/docs/images/logos/android-chrome-256x256.png" alt="TinyMCE Logo" width="128" height="128"></p> |
@@ -101,9 +47,9 @@ const revisions = [ |
101 | 47 | revisionId: '2', |
102 | 48 | createdAt: '2023-11-25T08:30:21.578Z', |
103 | 49 | author: { |
104 | | - id: 'tiny.user', |
105 | | - name: 'A Tiny User', |
106 | | - avatar: '{{imagesdir}}/logos/android-chrome-192x192.png', |
| 50 | + id: 'mia.andersson', |
| 51 | + name: 'Mia Andersson', |
| 52 | + avatar: 'https://sneak-preview.tiny.cloud/demouserdirectory/images/employee_mia-andersson_128_e6f9424b.jpg', |
107 | 53 | }, |
108 | 54 | content: ` |
109 | 55 | <p><img style="display: block; margin-left: auto; margin-right: auto;" title="Tiny Logo" src="https://www.tiny.cloud/docs/images/logos/android-chrome-256x256.png" alt="TinyMCE Logo" width="128" height="128"></p> |
@@ -147,9 +93,9 @@ const revisions = [ |
147 | 93 | revisionId: '1', |
148 | 94 | createdAt: '2023-11-29T10:11:21.578Z', |
149 | 95 | author: { |
150 | | - id: 'tiny.user', |
151 | | - name: 'A Tiny User', |
152 | | - avatar: '{{imagesdir}}/logos/android-chrome-192x192.png', |
| 96 | + id: 'mia.andersson', |
| 97 | + name: 'Mia Andersson', |
| 98 | + avatar: 'https://sneak-preview.tiny.cloud/demouserdirectory/images/employee_mia-andersson_128_e6f9424b.jpg', |
153 | 99 | }, |
154 | 100 | content: ` |
155 | 101 | <p><img style="display: block; margin-left: auto; margin-right: auto;" title="Tiny Logo" src="https://www.tiny.cloud/docs/images/logos/android-chrome-256x256.png" alt="TinyMCE Logo" width="128" height="128"></p> |
@@ -191,35 +137,44 @@ const revisions = [ |
191 | 137 | } |
192 | 138 | ]; |
193 | 139 |
|
194 | | -const revisionhistory_fetch_revision = (_editor, revision) => |
195 | | - new Promise((resolve, reject) => { |
196 | | - setTimeout(() => { |
197 | | - const newRevision = revisions.find((r) => r.revisionId === revision.revisionId); |
198 | | - if (newRevision === undefined) { |
199 | | - reject(`Revision ${revision.revisionId} is not found`); |
200 | | - } else { |
201 | | - resolve(newRevision); |
202 | | - } |
203 | | - }, getRandomDelay()); |
204 | | - }); |
| 140 | +const revisionhistory_fetch = () => new Promise((resolve) => { |
| 141 | + setTimeout(() => { |
| 142 | + const sortedRevisions = revisions |
| 143 | + .sort((a, b) => new Date(a.createdAt) < new Date(b.createdAt) ? 1 : -1); |
| 144 | + resolve(sortedRevisions); |
| 145 | + }, fakeDelay); |
| 146 | +}); |
| 147 | + |
| 148 | +const revisionhistory_fetch_revision = (_editor, revision) => new Promise((resolve, reject) => { |
| 149 | + setTimeout(() => { |
| 150 | + const revision = revisions.find((r) => r.revisionId === revision.revisionId); |
| 151 | + if (revision) { |
| 152 | + resolve(revision); |
| 153 | + } else { |
| 154 | + reject(`Revision ${revision.revisionId} is not found`); |
| 155 | + } |
| 156 | + }, fakeDelay); |
| 157 | +}); |
205 | 158 |
|
206 | 159 | tinymce.init({ |
207 | 160 | selector: 'textarea#revisionhistory', |
208 | 161 | height: 800, |
209 | | - plugins: 'revisionhistory', |
210 | | - toolbar: 'revisionhistory', |
| 162 | + plugins: 'revisionhistory help code link lists image', |
| 163 | + toolbar: 'undo redo | styles | bold italic underline | revisionhistory | link image | code', |
211 | 164 | content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:16px }', |
212 | 165 | revisionhistory_fetch, |
213 | 166 | revisionhistory_fetch_revision, |
214 | | - user_id: 'john.doe', |
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 | | - }, |
224 | | - revisionhistory_display_author: true |
| 167 | + revisionhistory_display_author: true, |
| 168 | + user_id: 'kai-nakamura', |
| 169 | + fetch_users: (userIds) => Promise.all(userIds |
| 170 | + .map((userId) => |
| 171 | + fetch(`${API_URL}/${userId}`) |
| 172 | + .then((response) => response.json()) |
| 173 | + .then((user) => ({ |
| 174 | + id: user.id, |
| 175 | + name: user.name, |
| 176 | + avatar: user.image, |
| 177 | + custom: user |
| 178 | + })) |
| 179 | + .catch(() => ({ id: userId })))), |
225 | 180 | }); |
0 commit comments