|
| 1 | += Using the User Lookup API |
| 2 | +:description: TinyMCE User Lookup provides an API to identify the current user and retrieve user details via integration-defined methods. |
| 3 | +:keywords: userLookup, fetchUsers, userId, avatar, user metadata |
| 4 | + |
| 5 | +The {productname} User Lookup API enables integrations to retrieve and cache user details (such as names and avatars) and identify the current user within the editor. |
| 6 | + |
| 7 | +This is useful when building features that rely on user context, such as commenting, or displaying a list of elements container user information, and lets you manage user data efficiently with built-in caching. |
| 8 | + |
| 9 | +== Using the User Lookup API |
| 10 | + |
| 11 | +Follow the steps below to configure and use the `userLookup` API within your integration. |
| 12 | + |
| 13 | +[[step-1-set-the-current-user]] |
| 14 | +=== 1. Set the current user ID |
| 15 | + |
| 16 | +The first step is to tell the editor which user is currently active. This is done using the `+user_id+` initialization option. |
| 17 | + |
| 18 | +[source,js] |
| 19 | +---- |
| 20 | +tinymce.init({ |
| 21 | + selector: '#editor', |
| 22 | + user_id: 'user-42', // <1> |
| 23 | + // ... |
| 24 | +}); |
| 25 | +---- |
| 26 | +**This ID will be used as the value of `+editor.userLookup.userId+`.** |
| 27 | + |
| 28 | +To access it: |
| 29 | + |
| 30 | +[source,js] |
| 31 | +---- |
| 32 | +editor.on('init', () => { |
| 33 | + console.log('Current user:', editor.userLookup.userId); |
| 34 | +}); |
| 35 | +---- |
| 36 | + |
| 37 | +[[step-2-provide-a-user-fetcher]] |
| 38 | +=== 2. Provide a function to fetch user details |
| 39 | + |
| 40 | +To simulate a backend lookup, define a local object that maps user IDs to their corresponding data. |
| 41 | + |
| 42 | +[source,js] |
| 43 | +---- |
| 44 | +const userDirectory = { |
| 45 | + 'user-1': { id: 'user-1', name: 'Jane Doe', avatar: 'https://example.com/avatar/jane.png' }, |
| 46 | + 'user-2': { id: 'user-2', name: 'John Smith' }, |
| 47 | + 'user-42': { id: 'user-42', name: 'Alex Taylor', avatar: 'https://example.com/avatar/alex.png' } |
| 48 | +}; |
| 49 | +
|
| 50 | +tinymce.init({ |
| 51 | + selector: '#editor', |
| 52 | + user_id: 'user-42', |
| 53 | + fetch_users: (userIds) => { |
| 54 | + const results = userIds.map((id) => { |
| 55 | + const user = Object.values(userDirectory).find((user) => user.id === id); |
| 56 | + if (user) { |
| 57 | + return user; |
| 58 | + } else { |
| 59 | + throw new Error(`User ${id} not found`); |
| 60 | + } |
| 61 | + }); |
| 62 | + return Promise.resolve(results); |
| 63 | + } |
| 64 | +}); |
| 65 | +---- |
| 66 | + |
| 67 | +This example demonstrates how to simulate user lookup in-browser. In a production environment, this function could be swapped out for a network request. |
| 68 | + |
| 69 | +The returned array should include objects with: |
| 70 | + |
| 71 | +* `id` (string) - Required |
| 72 | +* `name` (string) - Optional (defaults to `id`) |
| 73 | +* `avatar` (string, URL or data URI) - Optional (auto-generated SVG fallback if missing) |
| 74 | +* `custom` (object) - Optional metadata |
| 75 | + |
| 76 | +[[step-3-fetch-user-information]] |
| 77 | +=== 3. Fetch user information |
| 78 | + |
| 79 | +Once the editor is initialized, use the API to look up multiple users: |
| 80 | + |
| 81 | +[source,js] |
| 82 | +---- |
| 83 | +editor.on('init', () => { |
| 84 | + const users = editor.userLookup.fetchUsers(['user-1', 'user-2']); |
| 85 | +
|
| 86 | + users['user-1'].then((user) => { |
| 87 | + console.log('User 1 name:', user.name); |
| 88 | + }); |
| 89 | +
|
| 90 | + Promise.all(Object.values(users)).then((allUsers) => { |
| 91 | + allUsers.forEach((user) => { |
| 92 | + console.log(`Fetched: ${user.name} (${user.id})`); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
| 96 | +---- |
| 97 | + |
| 98 | +NOTE: All lookups are cached. If a user has already been fetched, the same promise will be returned without calling `fetch_users` again. |
| 99 | + |
| 100 | +[[step-4-handle-missing-or-default-users]] |
| 101 | +=== 4. Handle default users if `fetch_users` is not provided |
| 102 | + |
| 103 | +If no `fetch_users` function is configured, TinyMCE will return fallback users: |
| 104 | + |
| 105 | +* `name` will default to the ID |
| 106 | +* `avatar` will be a generated SVG using the first letter of the name |
| 107 | + |
| 108 | +This is useful for simpler setups or prototyping. |
| 109 | + |
| 110 | +[source,js] |
| 111 | +---- |
| 112 | +tinymce.init({ |
| 113 | + selector: '#editor', |
| 114 | + user_id: 'anonymous-guest' |
| 115 | +}); |
| 116 | +
|
| 117 | +editor.on('init', () => { |
| 118 | + const users = editor.userLookup.fetchUsers(['anonymous-guest']); |
| 119 | + users['anonymous-guest'].then((user) => { |
| 120 | + console.log('Generated fallback user:', user); |
| 121 | + }); |
| 122 | +}); |
| 123 | +---- |
| 124 | + |
| 125 | +[[step-5-integrate-user-lookup-in-a-ui]] |
| 126 | +=== 5. Integrate User Lookup in a UI Button (Optional) |
| 127 | + |
| 128 | +Here's how you might use the API to show the current user's name via a toolbar button. |
| 129 | + |
| 130 | +[source,js] |
| 131 | +---- |
| 132 | +setup: (editor) => { |
| 133 | + editor.ui.registry.addButton('show-user', { |
| 134 | + text: 'Show User', |
| 135 | + onAction: () => { |
| 136 | + const currentUser = editor.userLookup.userId; |
| 137 | + const users = editor.userLookup.fetchUsers([currentUser]); |
| 138 | +
|
| 139 | + users[currentUser].then((user) => { |
| 140 | + editor.notificationManager.open({ |
| 141 | + text: `Logged in as: ${user.name}`, |
| 142 | + type: 'info' |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + }); |
| 147 | +} |
| 148 | +---- |
| 149 | + |
| 150 | +Then add this to your toolbar: |
| 151 | + |
| 152 | +[source,js] |
| 153 | +---- |
| 154 | +toolbar: 'show-user' |
| 155 | +---- |
| 156 | + |
| 157 | +[[summary]] |
| 158 | +== Summary |
| 159 | + |
| 160 | +The `userLookup` API provides two main functions: |
| 161 | + |
| 162 | +[cols="2,4",options="header"] |
| 163 | +|=== |
| 164 | +|Function|Description |
| 165 | + |
| 166 | +|`editor.userLookup.userId`|Returns the current user’s ID, set via the `user_id` option. |
| 167 | +|`editor.userLookup.fetchUsers(userIds)`|Fetches and caches user information from a backend. |
| 168 | +|=== |
| 169 | + |
| 170 | +== Example User Object |
| 171 | + |
| 172 | +Each returned user object from `fetchUsers()` has the following structure: |
| 173 | + |
| 174 | +[source,js] |
| 175 | +---- |
| 176 | +{ |
| 177 | + id: 'user-1', |
| 178 | + name: 'Jane Doe', |
| 179 | + avatar: 'https://example.com/jane.png', |
| 180 | + custom: { |
| 181 | + role: 'admin', |
| 182 | + department: 'engineering' |
| 183 | + } |
| 184 | +} |
| 185 | +---- |
0 commit comments