Changes for page MentionsMacro

Last modified by Drunk Monkey on 2023-11-02 09:50

From version 3.1
edited by Drunk Monkey
on 2020-08-01 04:53
Change comment: Install extension [org.xwiki.platform:xwiki-platform-mentions-ui/12.6]
To version 17.1
edited by Drunk Monkey
on 2023-04-26 18:21
Change comment: Migrated property [type] from class [XWiki.WikiMacroParameterClass]

Summary

Details

XWiki.JavaScriptExtension[0]
Code
... ... @@ -1,16 +1,15 @@
1 1  require.config({
2 2   paths: {
3 - 'xwiki-suggestUsers': "$xwiki.getSkinFile('uicomponents/suggest/suggestUsersAndGroups.js', true))" +
4 - "?v=$escapetool.url($xwiki.version)"
3 + 'xwiki-suggestUsers': $jsontool.serialize($xwiki.getSkinFile('uicomponents/suggest/suggestUsersAndGroups.js'))
5 5   }
6 6  });
7 7  require(['deferred!ckeditor', 'xwiki-suggestUsers', 'jquery', 'xwiki-meta'], function (ckeditorPromise, suggestUsers, $, xm) {
8 -
7 +
9 9   /**
10 10   * Get the current wiki scope for displaying global, local or global and local users
11 11   */
12 12   const userScope = "$!services.wiki.user.userScope";
13 -
12 +
14 14   // see https://stackoverflow.com/a/6248722/657524
15 15   function random6chars() {
16 16   // I generate the UID from two parts here
... ... @@ -21,7 +21,7 @@
21 21   secondPart = ("000" + secondPart.toString(36)).slice(-3);
22 22   return firstPart + secondPart;
23 23   }
24 -
23 +
25 25   /**
26 26   * Compute a new unique anchor for the given reference.
27 27   * The unique anchor is based on the mentionned user id, concatenaed with a random string of 6 alphanumeric
... ... @@ -40,8 +40,8 @@
40 40   'input': text,
41 41   'limit': 6,
42 42   };
43 - $.when(suggestUsers.loadUsers(userScope, params)).then(function (user) {
44 - const cct = user.map(function (x) {
42 + suggestUsers.loadUsers(userScope, params).then(users => {
43 + const cct = users.map(function (x) {
45 45   // insert an id because that's required by the mentions plugins.
46 46   x.id = x.value;
47 47   // Make sure to display the icon avatar or the image one.
... ... @@ -57,11 +57,11 @@
57 57   return x;
58 58   });
59 59   callback(cct);
60 - })
59 + });
61 61   }
62 62  
63 - ckeditorPromise.done(function (ckeditor) {
64 - function confMentions(name) {
62 + ckeditorPromise.then(ckeditor => {
63 + function getUserMentionsConfig(editor) {
65 65   return {
66 66   feed: function (opts, callback) {
67 67   search(opts.query, callback);
... ... @@ -69,33 +69,25 @@
69 69   marker: '@',
70 70   minChars: 0,
71 71   itemsLimit: 6,
72 - itemTemplate: '<li data-id="{id}" class="ckeditor-autocomplete-item">'+
73 - '<div>'+
74 - '<span class="ckeditor-autocomplete-item-icon-wrapper">'+
75 - '<span class="{cssClass}"></span>'+
76 - '<img src="{imgUrl}" class="{imgClass}"/>'+
77 - '</span>'+
78 - '<span class="ckeditor-autocomplete-item-label">{label}</span>'+
79 - '</div>'+
80 - '</li>',
71 + itemTemplate:
72 + `<li data-id="{id}" class="ckeditor-autocomplete-item">
73 + <div>
74 + <span class="ckeditor-autocomplete-item-icon-wrapper">
75 + <span class="{cssClass}"></span>
76 + <img src="{imgUrl}" class="{imgClass}"/>
77 + </span>
78 + <span class="ckeditor-autocomplete-item-label">{label}</span>
79 + </div>
80 + </li>`,
81 81   outputTemplate: function (param) {
82 - var editor = ckeditor.instances[name];
83 - var currentWikiReference = xm.documentReference.extractReference(XWiki.EntityType.WIKI);
84 -
85 - // Compute an absolute reference containing the wiki reference even if the user is local.
86 - var documentReference = XWiki.Model.resolve(param.id, XWiki.EntityType.DOCUMENT);
87 - if (!documentReference.extractReference(XWiki.EntityType.WIKI)) {
88 - documentReference = documentReference.appendParent(currentWikiReference);
89 - }
90 - var serializedReference = XWiki.Model.serialize(documentReference);
91 - editor.once('afterInsertHtml', function () {
82 + editor.once('afterInsertHtml', function() {
92 92   editor.execCommand('xwiki-macro-insert', {
93 93   name: 'mention',
94 - inline: true,
85 + inline: 'enforce',
95 95   parameters: {
96 - reference: serializedReference,
87 + reference: param.id,
97 97   style: 'FULL_NAME',
98 - anchor: getAnchor(serializedReference)
89 + anchor: getAnchor(param.id)
99 99   }
100 100   });
101 101   });
... ... @@ -106,22 +106,19 @@
106 106   };
107 107   }
108 108  
109 - function updateConf(config, name) {
110 - const newConf = config;
111 - newConf.mentions = newConf.mentions || [];
112 - newConf.mentions.push(confMentions(name));
113 - return newConf;
100 + function updateConfig(editor) {
101 + editor.config.mentions = editor.config.mentions || [];
102 + editor.config.mentions.push(getUserMentionsConfig(editor));
114 114   }
115 115  
116 - var oldReplace = ckeditor.replace;
117 - ckeditor.replace = function (element, config) {
118 - return oldReplace.call(this, element, updateConf(config, element.id));
119 - };
120 -
121 - var oldInline = ckeditor.inline;
122 - ckeditor.inline = function (element, config) {
123 - return oldInline.call(this, element, updateConf(config, element.id));
124 - };
105 + ckeditor.on('instanceCreated', function(event) {
106 + // The editor instance was created but it not yet initialized. Unfortunately the configuration object passed when
107 + // the instance was created has not been merged with the global configuration yet.
108 + event.editor.once('configLoaded', function(event) {
109 + // The editor configuration has been loaded (the instance configuration has been merged with the global
110 + // configuration) but the editor has not been fully initialized yet so we can modify the configuration.
111 + updateConfig(event.editor);
112 + });
113 + });
125 125   });
126 126  });
127 -
XWiki.WikiMacroClass[0]
Macro code
... ... @@ -1,9 +1,10 @@
1 1  {{velocity}}
2 2  #set ($reference = $wikimacro.parameters.reference)
3 3  #set ($style = $wikimacro.parameters.style)
4 -#set ($content = $services.mentions.format($reference.reference, $style))
4 +#set ($type = "$!wikimacro.parameters.type")
5 +#set ($content = $services.mentions.format($reference.reference, $style, $type))
5 5  #set ($anchor = $wikimacro.parameters.anchor)
6 -#set ($isCurrentUser = $xcontext.userReference == $reference.reference)
7 +#set ($isCurrentUser = $xcontext.userReference == $reference.reference && ($type == '' || $type == 'user'))
7 7  #set ($cssClasses = ['xwiki-mention', 'user'])
8 8  #if ($isCurrentUser)
9 9   #set ($discard = $cssClasses.add('self'))
... ... @@ -10,6 +10,8 @@
10 10  #end
11 11  #set ($link = $xwiki.getURL($reference.reference, 'view'))
12 12  {{html}}
13 -<a id="$anchor" class="$stringtool.join($cssClasses, ' ')" data-reference="$services.model.serialize($reference.reference, 'default')" href="$link">$content</a>
14 +<a id="$escapetool.xml($anchor)" class="$stringtool.join($cssClasses, ' ')" data-reference="$escapetool.xml($services.model.serialize($reference.reference, 'default'))" href="$escapetool.xml($link)">##
15 + $escapetool.xml($content)## Do not remove this comment as it ensures that the spacing after mention is not broken.
16 +</a>
14 14  {{/html}}
15 15  {{/velocity}}
Default categories
... ... @@ -1,0 +1,1 @@
1 +Notifications
Macro description
... ... @@ -1,1 +1,1 @@
1 -Insert a user mention.
1 +Inserts a user mention.
XWiki.WikiMacroParameterClass[3]
Parameter description
... ... @@ -1,0 +1,1 @@
1 +The type of mentioned actor.
Parameter name
... ... @@ -1,0 +1,1 @@
1 +type
Parameter type
... ... @@ -1,0 +1,1 @@
1 +java.lang.String