Änderungen von Dokument MacroService

Zuletzt geändert von Daniel Herrmann am 2026/02/04 20:23

Von Version Icon 2.1 Icon
bearbeitet von Daniel Herrmann
am 2025/07/19 16:48
Änderungskommentar: Install extension [org.xwiki.platform:xwiki-platform-ckeditor-ui/17.5.0]
Auf Version 1.1 Icon
bearbeitet von admin
am 2025/02/23 19:33
Änderungskommentar: Imported from XAR

Zusammenfassung

Details

Icon Seiteneigenschaften
Dokument-Autor
... ... @@ -1,1 +1,1 @@
1 -XWiki.dherrman
1 +XWiki.admin
Inhalt
... ... @@ -150,16 +150,191 @@
150 150  #end
151 151  
152 152  #macro (maybeGetMacroDescriptor $macroIdAsString)
153 + #set ($macroId = $services.rendering.resolveMacroId($macroIdAsString))
154 + #if ($macroId)
155 + #set ($macroDescriptor = $services.rendering.getMacroDescriptor($macroId))
156 + #if (!$macroDescriptor && $macroId.syntax)
157 + ## Try the macro id without the syntax.
158 + #set ($macroId = $services.rendering.resolveMacroId($macroId.id))
159 + #set ($macroDescriptor = $services.rendering.getMacroDescriptor($macroId))
160 + #end
161 + #if ($macroDescriptor)
162 + #getMacroDescriptor($macroDescriptor)
163 + #end
164 + #end
165 +#end
166 +
167 +#macro (getMacroDescriptor $macroDescriptor)
168 + ## Translate the macro name and description.
169 + #set ($macroTranslationKey = "rendering.macro.$macroDescriptor.id")
153 153   #ckeditor_initRequiredSkinExtensions()
154 - #set ($macroDescriptor = $services.wysiwyg.getMacroDescriptorUI($macroIdAsString))
155 - #if ($macroDescriptor)
156 - #set ($data = {
157 - 'descriptor': $macroDescriptor,
158 - 'requiredSkinExtensions': "#ckeditor_getRequiredSkinExtensions()"
171 + #set ($data = {
172 + 'id': $macroDescriptor.id,
173 + 'name': "#maybeTranslate(""${macroTranslationKey}.name"" $macroDescriptor.name)",
174 + 'description': "#maybeTranslate(""${macroTranslationKey}.description"" $macroDescriptor.description)",
175 + 'defaultCategory': $macroDescriptor.defaultCategory,
176 + 'supportsInlineMode': $macroDescriptor.supportsInlineMode(),
177 + 'parameterDescriptorMap': {}
178 + })
179 + #if ($macroDescriptor.contentDescriptor)
180 + ## Translate the content label and description.
181 + ## Treat the macro content as if it is the last macro parameter.
182 + #set ($data.contentDescriptor = {
183 + 'name': "#maybeTranslate('rendering.macroContent' 'Content')",
184 + 'description': "#maybeTranslate(""${macroTranslationKey}.content.description""
185 + $macroDescriptor.contentDescriptor.description)",
186 + 'mandatory': $macroDescriptor.contentDescriptor.mandatory,
187 + 'deprecated': $macroDescriptor.contentDescriptor.deprecated,
188 + 'advanced': $macroDescriptor.contentDescriptor.advanced,
189 + 'defaultValue': $macroDescriptor.contentDescriptor.defaultValue,
190 + 'type': $macroDescriptor.contentDescriptor.type,
191 + 'editTemplate': '<textarea name="$content" rows="7"></textarea>',
192 + 'index': $macroDescriptor.parameterDescriptorMap.size()
159 159   })
194 + #fixDescriptorType($data.contentDescriptor)
160 160   #end
196 + #set ($groupDescriptorTree = {})
197 + #foreach ($entry in $macroDescriptor.parameterDescriptorMap.entrySet())
198 + #set ($parameterDescriptor = $entry.value)
199 + ## Translate the parameter name and description.
200 + #set ($parameterTranslationKey = "${macroTranslationKey}.parameter.$parameterDescriptor.id")
201 + #set ($translatedParameterDescriptor = {
202 + 'id': $parameterDescriptor.id,
203 + 'name': "#maybeTranslate(""${parameterTranslationKey}.name"" $parameterDescriptor.name)",
204 + 'description': "#maybeTranslate(""${parameterTranslationKey}.description"" $parameterDescriptor.description)",
205 + 'mandatory': $parameterDescriptor.mandatory,
206 + 'deprecated': $parameterDescriptor.deprecated,
207 + 'advanced': $parameterDescriptor.advanced,
208 + 'defaultValue': $parameterDescriptor.defaultValue,
209 + 'type': $parameterDescriptor.displayType,
210 + 'hidden' : $parameterDescriptor.displayHidden,
211 + 'index': $foreach.index
212 + })
213 + #set ($translatedParameterDescriptor.caseInsensitive = $translatedParameterDescriptor.type.isEnum())
214 + #set ($groupDescriptor = $parameterDescriptor.groupDescriptor)
215 + #if ($groupDescriptor)
216 + #handleMacroParameterGroup($groupDescriptor $groupDescriptorTree $translatedParameterDescriptor)
217 + #end
218 + #if ($translatedParameterDescriptor.type.getName() == 'java.lang.String'
219 + && ($parameterDescriptor.defaultValue == 'false' || $parameterDescriptor.defaultValue == 'true')
220 + && $macroDescriptor.parametersBeanClass.getSimpleName() == 'WikiMacroParameters')
221 + #set ($translatedParameterDescriptor.defaultValue = $parameterDescriptor.defaultValue == 'true')
222 + #set ($translatedParameterDescriptor.type = $translatedParameterDescriptor.defaultValue.getClass())
223 + #end
224 + #set ($htmlDisplayerParameters = {'name': $parameterDescriptor.id})
225 + #if ($translatedParameterDescriptor.group)
226 + #set ($discard = $htmlDisplayerParameters.put('data-property-group',
227 + $stringtool.join($translatedParameterDescriptor.group, '/')))
228 + #end
229 + #set ($translatedParameterDescriptor.editTemplate = $services.display.html.display(
230 + $translatedParameterDescriptor.type, $translatedParameterDescriptor.defaultValue, $htmlDisplayerParameters, 'edit'
231 + ))
232 + #if ("$!translatedParameterDescriptor.editTemplate" == '')
233 + #set ($translatedParameterDescriptor.editTemplate = "#getMacroParameterEditTemplate(
234 + $translatedParameterDescriptor)")
235 + #end
236 + #set ($translatedParameterDescriptor.editTemplate = $translatedParameterDescriptor.editTemplate.trim())
237 + #fixDescriptorType($translatedParameterDescriptor)
238 + #set ($discard = $data.parameterDescriptorMap.put($entry.key, $translatedParameterDescriptor))
239 + #end
240 + #if ($groupDescriptorTree.groups)
241 + #set ($data.groupDescriptorTree = $groupDescriptorTree.groups)
242 + #end
243 + #set ($data.requiredSkinExtensions = "#ckeditor_getRequiredSkinExtensions()")
161 161  #end
162 162  
246 +#macro (fixDescriptorType $descriptor)
247 + ## The goal of this code is to obtain a normalized string representation of the type specified in the descriptor.
248 + ## See XCOMMONS-1583: Define a stable way to serialize types
249 + ##
250 + ## The type specified in the descriptor can be any implementation of java.lang.reflect.Type, not necessarily a
251 + ## java.lang.Class. We can't use toString() because the return of Class#toString() is different than Class#getName().
252 + ## We can't use Type#getTypeName() either because the access to this method is restricted from Velocity. The only
253 + ## option for now is to try #getName() first and fall back on #toString() for types that are not instances of
254 + ## java.lang.Class.
255 + #set ($typeName = $descriptor.type.getName())
256 + #if ("$!typeName" == '')
257 + ## Probably not a java.lang.Class. Fall back on #toString().
258 + #set ($typeName = "$!descriptor.type")
259 + #end
260 + ## Remove whitespace from the type name in order to have a single string representation.
261 + #set ($descriptor.type = $typeName.replaceAll('\s+', ''))
262 +#end
263 +
264 +## Builds the group tree with the following structure:
265 +##
266 +## {
267 +## 'parentGroupId': {
268 +## 'id': 'parentGroupId',
269 +## 'name': 'Parent Group',
270 +## 'feature': 'someFeature',
271 +## 'groups': {
272 +## 'childGroupId': {...},
273 +## ...
274 +## }
275 +## },
276 +## ...
277 +## }
278 +#macro (handleMacroParameterGroup $groupDescriptor $groupDescriptorTree $translatedParameterDescriptor)
279 + #if ($groupDescriptor.group && $groupDescriptor.group.size() > 0)
280 + #set ($translatedParameterDescriptor.group = $groupDescriptor.group)
281 + #set ($parentGroup = $groupDescriptorTree)
282 + #foreach ($groupId in $groupDescriptor.group)
283 + #if (!$parentGroup.groups)
284 + #set ($parentGroup.groups = {})
285 + #end
286 + #set ($childGroup = $parentGroup.groups.get($groupId))
287 + #if (!$childGroup)
288 + #if ($groupId == $translatedParameterDescriptor.id)
289 + #set ($groupName = $translatedParameterDescriptor.name)
290 + #else
291 + #set ($groupTranslationKey = "${macroTranslationKey}.group.$groupId")
292 + #set ($groupName = "#maybeTranslate(""${groupTranslationKey}.name"" $groupId)")
293 + #end
294 + #set ($childGroup = {
295 + 'id': $groupId,
296 + 'name': $groupName
297 + })
298 + #set ($discard = $parentGroup.groups.put($groupId, $childGroup))
299 + #end
300 + #set ($parentGroup = $childGroup)
301 + #end
302 + #if ("$!groupDescriptor.feature" != '')
303 + #set ($parentGroup.feature = $groupDescriptor.feature)
304 + #end
305 + #elseif ($groupDescriptor.feature)
306 + ## This group is made of a single parameter. The feature then refers to this parameter.
307 + #set ($translatedParameterDescriptor.feature = $groupDescriptor.feature)
308 + #end
309 +#end
310 +
311 +#macro (getMacroParameterEditTemplate $translatedParameterDescriptor)
312 + #if ($translatedParameterDescriptor.type.getName() == 'boolean'
313 + || $translatedParameterDescriptor.type.getName() == 'java.lang.Boolean')
314 + <input type="checkbox" name="$escapetool.xml($translatedParameterDescriptor.id)" value="true"/>##
315 + ## We need to submit something in case the checkbox is not checked.
316 + <input type="hidden" name="$escapetool.xml($translatedParameterDescriptor.id)" value="false"/>
317 + #elseif ($translatedParameterDescriptor.type.isEnum())
318 + #if ($translatedParameterDescriptor.defaultValue)
319 + #set ($enumValues = $translatedParameterDescriptor.defaultValue.values())
320 + #else
321 + ## A parameter of type enum that doesn't have a default value is very unlikely. We attempt to read the list of
322 + ## possible values from the enum type in this case, which is currently forbidden, but at least it will generate
323 + ## a warning in the logs that will help us investigate the problem.
324 + #set ($enumValues = $translatedParameterDescriptor.type.getEnumConstants())
325 + #end
326 + <select name="$escapetool.xml($translatedParameterDescriptor.id)">##
327 + #foreach ($enumValue in $enumValues)
328 + #set ($value = $enumValue.name())
329 + #set ($label = "#maybeTranslate(""${parameterTranslationKey}.value.$value"" $enumValue)")
330 + <option value="$escapetool.xml($value)">$escapetool.xml($label)</option>##
331 + #end
332 + </select>
333 + #else
334 + <input type="text" name="$escapetool.xml($translatedParameterDescriptor.id)"/>
335 + #end
336 +#end
337 +
163 163  #macro (maybeTranslate $key $defaultValue)
164 164   #if ($services.localization.get($key))
165 165   $services.localization.render($key)##
... ... @@ -209,19 +209,9 @@
209 209   $response.sendError(404, $exceptiontool.getRootCauseMessage($job.status.error))
210 210   #end
211 211  #end
387 +{{/velocity}}
212 212  
213 -#macro (getMacroParameters $macroId $macroParameters)
214 - #set ($macroId = $services.rendering.resolveMacroId($macroId))
215 - #set ($macroParameters = $jsontool.fromString($macroParameters))
216 - #foreach($macroParameter in $macroParameters.entrySet())
217 - ## TODO: Do we need to take into account the macro parameter type? We could have a macro parameter that is editable
218 - ## inline but whose value is not a List<Block> but rather a String (plain text).
219 - #set ($macroParameter.value = $services.wysiwyg.fromAnnotatedXHTML($macroParameter.value, $macroId.syntax))
220 - #end
221 - #set ($data = $macroParameters)
222 -#end
223 -
224 -#set ($data = $NULL)
389 +{{velocity wiki="false"}}
225 225  #if ("$!request.action" == 'install')
226 226   #if ($services.csrf.isTokenValid($request.form_token))
227 227   #installMacroExtension($request.extensionId, $request.extensionVersion)
... ... @@ -228,18 +228,18 @@
228 228   #else
229 229   $response.sendError(403)
230 230   #end
231 -#elseif ($request.data == 'list')
232 - #getMacroList($request.syntaxId)
233 -#elseif ($request.data == 'descriptor')
234 - #maybeGetMacroDescriptor($request.macroId)
235 -#elseif ($request.data == 'macroParameters')
236 - #getMacroParameters($request.macroId, $request.macroParameters)
396 +#elseif ("$!request.data" != '')
397 + #set ($data = $NULL)
398 + #if ($request.data == 'list')
399 + #getMacroList($request.syntaxId)
400 + #elseif ($request.data == 'descriptor')
401 + #maybeGetMacroDescriptor($request.macroId)
402 + #end
403 + #if ($data)
404 + #set ($discard = $response.setContentType('application/json'))
405 + $jsontool.serialize($data)
406 + #else
407 + $response.sendError(404)
408 + #end
237 237  #end
238 -#if ($response.isCommitted())
239 - ## Do nothing.
240 -#elseif ($data)
241 - #jsonResponse($data)
242 -#elseif ("$!request.action" != '' || "$!request.data" != '')
243 - $response.sendError(404)
244 -#end
245 245  {{/velocity}}