0 Stimmen

Änderungen von Dokument MentionsMacro

Zuletzt geändert von xwikiadmin am 2024/01/25 22:10

Von Version 2.1
bearbeitet von xwikiadmin
am 2020/11/01 23:47
Änderungskommentar: Install extension [org.xwiki.platform:xwiki-platform-mentions-ui/12.9]
Auf Version 1.1
bearbeitet von xwikiadmin
am 2020/07/08 22:24
Änderungskommentar: Install extension [org.xwiki.platform:xwiki-platform-mentions-ui/12.5.1]

Zusammenfassung

Details

XWiki.JavaScriptExtension[0]
Code
... ... @@ -5,6 +5,15 @@
5 5   }
6 6  });
7 7  require(['deferred!ckeditor', 'xwiki-suggestUsers', 'jquery', 'xwiki-meta'], function (ckeditorPromise, suggestUsers, $, xm) {
8 + /*
9 + * Keep records of the added anchors during the current edit session.
10 + * Note that the anchors saved here are not only the ones added on the current session,
11 + * but also those already present in the document.
12 + * For more information, see #getAnchor.
13 + * The information are stored on the form:
14 + * { reference: Array<String> }
15 + */
16 + const anchorIds = {};
8 8  
9 9   /**
10 10   * Get the current wiki scope for displaying global, local or global and local users
... ... @@ -11,28 +11,38 @@
11 11   */
12 12   const userScope = "$!services.wiki.user.userScope";
13 13  
14 - // see https://stackoverflow.com/a/6248722/657524
15 - function random6chars() {
16 - // I generate the UID from two parts here
17 - // to ensure the random number provide enough bits.
18 - var firstPart = (Math.random() * 46656) | 0;
19 - var secondPart = (Math.random() * 46656) | 0;
20 - firstPart = ("000" + firstPart.toString(36)).slice(-3);
21 - secondPart = ("000" + secondPart.toString(36)).slice(-3);
22 - return firstPart + secondPart;
23 - }
24 -
25 25   /**
26 26   * Compute a new unique anchor for the given reference.
27 - * The unique anchor is based on the mentionned user id, concatenaed with a random string of 6 alphanumeric
28 - * characters.
29 - * The chances of collision are quite low, about 46k mentions for a given mentioned user on a given page (assuming
30 - * that no mentions are ever deleted).
25 + * The uniqueness of the anchor is given by two mechanisms:
26 + * - retrieve on first call all mentions available on current document and store them
27 + * - then use that information to compute a next anchor not overlapping an existing one on the current document.
28 + * The mechanism is obviously not perfect and might be improved later but should be enough for most usage.
31 31   */
32 32   const getAnchor = function (reference) {
33 - const refId = reference.replace(/[.:]/g, '-');
34 - const randomId = random6chars();
35 - return refId + '-' + randomId;
31 + var existingIds;
32 +
33 + if (anchorIds.hasOwnProperty(reference)) {
34 + existingIds = anchorIds[reference];
35 + } else {
36 + existingIds = [];
37 + $('.xwiki-mention').each(function() {
38 + var mention = $(this);
39 + if (mention.attr('data-reference') === reference) {
40 + existingIds.push(mention.attr('id'));
41 + }
42 + });
43 + anchorIds[reference] = existingIds;
44 + }
45 +
46 + var counter = existingIds.length + 1;
47 + var refId = reference.replace(/[.:]/g, '-');
48 + var proposedAnchor = refId + "-" + counter;
49 + while (existingIds.indexOf(proposedAnchor) != -1) {
50 + counter++;
51 + proposedAnchor = refId + "-" + counter;
52 + }
53 + anchorIds[reference].push(proposedAnchor);
54 + return proposedAnchor;
36 36   };
37 37  
38 38   const search = function (text, callback) {
... ... @@ -80,14 +80,22 @@
80 80   '</li>',
81 81   outputTemplate: function (param) {
82 82   var editor = ckeditor.instances[name];
102 + var currentWikiReference = xm.documentReference.extractReference(XWiki.EntityType.WIKI);
103 +
104 + // Compute an absolute reference containing the wiki reference even if the user is local.
105 + var documentReference = XWiki.Model.resolve(param.id, XWiki.EntityType.DOCUMENT);
106 + if (!documentReference.extractReference(XWiki.EntityType.WIKI)) {
107 + documentReference = documentReference.appendParent(currentWikiReference);
108 + }
109 + var serializedReference = XWiki.Model.serialize(documentReference);
83 83   editor.once('afterInsertHtml', function () {
84 84   editor.execCommand('xwiki-macro-insert', {
85 85   name: 'mention',
86 86   inline: true,
87 87   parameters: {
88 - reference: param.id,
115 + reference: serializedReference,
89 89   style: 'FULL_NAME',
90 - anchor: getAnchor(param.id)
117 + anchor: getAnchor(serializedReference)
91 91   }
92 92   });
93 93   });
... ... @@ -116,3 +116,4 @@
116 116   };
117 117   });
118 118  });
146 +
XWiki.StyleSheetExtension[0]
Code
... ... @@ -11,7 +11,3 @@
11 11  .xwiki-mention.removed {
12 12   text-decoration: line-through;
13 13  }
14 -
15 -blockquote.mention-quote {
16 - font-size: inherit;
17 -}
XWiki.WikiMacroClass[0]
Makro-Code
... ... @@ -1,13 +1,24 @@
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))
5 5  #set ($anchor = $wikimacro.parameters.anchor)
5 +#set ($userProperties = $services.user.getProperties($reference))
6 +#set ($firstName = $userProperties.getFirstName())
7 +#set ($lastName = $userProperties.getLastName())
6 6  #set ($isCurrentUser = $xcontext.userReference == $reference.reference)
7 7  #set ($cssClasses = ['xwiki-mention', 'user'])
8 8  #if ($isCurrentUser)
9 9   #set ($discard = $cssClasses.add('self'))
10 10  #end
13 +#if ("$!firstName" == "")
14 + #set($content = "@$reference.reference.name")
15 +#elseif ($style == 'FIRST_NAME')
16 + #set($content = "@$firstName")
17 +#elseif ($style == 'LOGIN')
18 + #set($content = "@$reference.reference.name")
19 +#else
20 + #set($content = "@$firstName $!lastName")
21 +#end
11 11  #set ($link = $xwiki.getURL($reference.reference, 'view'))
12 12  {{html}}
13 13  <a id="$anchor" class="$stringtool.join($cssClasses, ' ')" data-reference="$services.model.serialize($reference.reference, 'default')" href="$link">$content</a>
Powered by MakerspaceBonn e.V.