Code source wiki de VelocityMacros

Version 1.1 par superadmin le 2022/06/20 08:49

Masquer les derniers auteurs
superadmin 1.1 1 {{velocity output="false"}}
2 #macro (ckeditor_convert $text $toHTML $fromHTML $stripHTMLEnvelope)
3 #set ($sourceSyntax = $request.sourceSyntax)
4 #if ("$!sourceSyntax" != '')
5 #set ($sourceSyntax = $services.rendering.resolveSyntax($sourceSyntax))
6 #end
7 #if (!$sourceSyntax)
8 #set ($sourceSyntax = $doc.syntax)
9 #end
10 #if ($toHTML)
11 #set ($source = {
12 'documentReference': $doc.documentReference,
13 'syntax': $sourceSyntax
14 })
15 #if ($fromHTML)
16 #set ($source.html = $text)
17 #else
18 #set ($source.content = $text)
19 #end
20 #if ($stripHTMLEnvelope)
21 ## Don't wrap the rendered content with the HTML and BODY tags. This is needed when the HTML fragment is injected
22 ## directly in the current page, e.g. when CKEditor is used in-line (no iframe). We still need to send back the
23 ## required style sheets and JavaScript files because some of them might not be available in the target page, e.g.
24 ## when a macro is inserted and it needs resource files not available on every page.
25 #ckeditor_initRequiredSkinExtensions()
26 #set ($output = "#ckeditor_getRenderedContent($source)$!html")
27 #set ($discard = $response.setHeader('X-XWIKI-HTML-HEAD', "#ckeditor_getRequiredSkinExtensions()"))
28 #else
29 ## Returns a full HTML page, including the HEAD tag with the style sheets and possibly the JavaScript includes.
30 ## This is needed when the edited content is loaded in an iframe, which is the case when CKEditor replaces a text
31 ## area (classical editor).
32 #set ($output = "#ckeditor_renderContentSheet($source)")
33 #end
34 #else
35 #set ($output = $services.wysiwyg.fromAnnotatedXHTML($text, $sourceSyntax.toIdString()))
36 #if ("$!output" == '' && $output != '')
37 ## $output is null, probably because $services.wysiwyg.fromAnnotatedXHTML() is not available (before 10.10RC1)
38 #set ($xdom = $services.rendering.parse($text, 'xhtml/1.0'))
39 #set ($output = $services.rendering.render($xdom, $sourceSyntax.toIdString()))
40 #end
41 #end
42 #if ("$!output" != '' || $output == '')
43 $output##
44 #else
45 ## The output is null so an exception must have been thrown.
46 $response.sendError(500)
47 #end
48 #end
49
50 #macro (ckeditor_renderContentSheet $source)
51 ## The $source variable is used inside the content sheet.
52 #set ($output = $services.wysiwyg.render('CKEditor.ContentSheet'))
53 #if ("$!output" == '' && $output != '')
54 ## $output is null, probably because $services.wysiwyg.render() is not available (before 7.4.1)
55 #set ($output = $xwiki.getDocument('CKEditor.ContentSheet').getRenderedContent())
56 #set ($skinExtensionHooks = {
57 'ssx': 'CssSkinExtensionPlugin',
58 'ssfx': 'CssSkinFileExtensionPlugin',
59 'ssrx': 'CssResourceSkinExtensionPlugin'
60 })
61 #foreach ($entry in $skinExtensionHooks.entrySet())
62 #set ($output = $output.replace("<!-- com.xpn.xwiki.plugin.skinx.$entry.value -->",
63 $xwiki.get($entry.key).importString))
64 #end
65 #end
66 ## Unescape {{ which happened in CKEditor.ContentSheet for protecting the {{html}} macro.
67 $!output.replace('&amp;#123;&amp;#123;', '{{')##
68 #end
69
70 #macro (ckeditor_getRenderedContent $source)
71 ## Make sure the edited content is rendered for view because this is a WYSIWYG editor.
72 #set ($macro.previousDisplayMode = $xcontext.displayMode)
73 #set ($discard = $xcontext.setDisplayMode('view'))
74 #if ($xcontext.displayMode != 'view')
75 ## Before XWiki 14.0-rc-1, 13.4.7, 13.10.3 and 12.10.12 there's no API to get the current display mode so we can't
76 ## restore the previous display mode properly. We assume the previous display mode is 'edit' because the WYSIWYG
77 ## editor is used.
78 #set ($macro.previousDisplayMode = 'edit')
79 #end
80 #if ($source.html)
81 #set ($html = $services.wysiwyg.parseAndRender($source.html, $source.syntax, $source.documentReference))
82 #if ("$!html" == '' && $html != '')
83 ## Before XWiki 11.9RC1 (see XWIKI-16737)
84 #set ($html = $services.wysiwyg.parseAndRender($source.html, $source.syntax.toIdString()))
85 #end
86 #else
87 #set ($html = $services.wysiwyg.toAnnotatedXHTML($source.content, $source.syntax, $source.documentReference))
88 #if ("$!html" == '' && $html != '')
89 ## Before XWiki 11.9RC1 (see XWIKI-16737)
90 #set ($html = $services.wysiwyg.toAnnotatedXHTML($source.content, $source.syntax.toIdString()))
91 #end
92 #end
93 #if ("$!html" == '')
94 ## Prevent Firefox from inserting bogus whitespace when the text area is empty.
95 ## CKEDITOR-185: Whitespace is generated in Firefox when creating a page without typing the content
96 #set ($html = '<p></p>')
97 #end
98 ## Restore the previous display mode.
99 #set ($discard = $xcontext.setDisplayMode($macro.previousDisplayMode))
100 #end
101
102 #macro (ckeditor_importCSS $groupId $artifactId $path $evaluate)
103 #set ($url = $services.webjars.url("$groupId:$artifactId", $path, {'evaluate': $evaluate}))
104 #set ($discard = $xwiki.linkx.use($url, {'type': 'text/css', 'rel': 'stylesheet'}))
105 #end
106
107 #macro (importCKEditorCSS $path $evaluate)
108 #ckeditor_importCSS('org.xwiki.contrib' 'application-ckeditor-webjar' $path $evaluate)
109 #end
110
111 #macro (importCKEditorResources)
112 #set ($discard = $xwiki.ssx.use('CKEditor.EditSheet'))
113 ## Add the version of each JavaScript dependency of CKEditor Integration to the URL used to load the CKEditor in order
114 ## to make sure the browser cache is invalidated when one of these versions changes (e.g. when we upgrade XWiki or one
115 ## of these dependencies but the CKEditor version remains the same).
116 #set ($discard = $xwiki.jsx.use('CKEditor.EditSheet', {
117 'v': $services.extension.installed.getInstalledExtension('org.xwiki.contrib:application-ckeditor-ui',
118 "wiki:$xcontext.database").version.value,
119 'xwiki-version': $services.extension.core.getCoreExtension(
120 'org.xwiki.platform:xwiki-platform-tree-webjar').version.value,
121 'fast-diff-version': $services.extension.installed.getInstalledExtension('org.webjars.npm:fast-diff',
122 "wiki:$xcontext.database").version.value,
123 'bs3typeahead-version': $services.extension.installed.getInstalledExtension('org.webjars.npm:bootstrap-3-typeahead',
124 "wiki:$xcontext.database").version.value
125 }))
126 #importCKEditorCSS('plugins/xwiki-resource/resourcePicker.min.css' true)
127 #importCKEditorCSS('plugins/xwiki-macro/macroWizard.min.css' true)
128 ## Tree styles needed for the document/attachment tree picker.
129 #set ($discard = $xwiki.linkx.use($services.webjars.url('org.xwiki.platform:xwiki-platform-tree-webjar', 'tree.min.css',
130 {'evaluate': true}), {'type': 'text/css', 'rel': 'stylesheet'}))
131 #set ($discard = $xwiki.linkx.use($services.webjars.url('org.xwiki.platform:xwiki-platform-tree-webjar',
132 'finder.min.css', {'evaluate': true}), {'type': 'text/css', 'rel': 'stylesheet'}))
133 #end
134
135 #macro (ckeditor $parameters)
136 #importCKEditorResources
137 #set ($sourceDocumentReference = $parameters.attributes.get('data-sourceDocumentReference'))
138 ## Use the current document as source document, if possible, because it may contain unsaved changes (e.g. changes
139 ## coming from a template specified in the URL or from other URL parameters). This is important for instance in order
140 ## to ensure that for new (unsaved) documents we use the syntax that has been copied from the template and not the
141 ## default wiki syntax.
142 #if (!$sourceDocumentReference || $sourceDocumentReference.equals($tdoc.documentReference))
143 #set ($sourceDocument = $tdoc)
144 #set ($sourceDocumentReference = $tdoc.documentReference)
145 #else
146 #set ($sourceDocument = $xwiki.getDocument($sourceDocumentReference))
147 #end
148 #set ($escapedName = $escapetool.xml($parameters.attributes.name))
149 <input value="$!escapedName" name="RequiresHTMLConversion" type="hidden"/>
150 <input value="$!escapetool.xml($sourceDocument.syntax.toIdString())" name="$!{escapedName}_syntax" type="hidden"/>
151 ## Chrome doesn't cache the enabled/disabled state of the form fields so we must store this state in the value of
152 ## another form field. For instance, the enabled/disabled state of the RequiresHTMLConversion hidden input
153 ## determines which editor mode (WYSIWYG vs. Source) is loaded initially.
154 ## See CKEDITOR-34: Wiki syntax gets escaped when you click "Back" in the browser
155 <input value="" name="$!{escapedName}_cache" type="hidden" class="cache"/>
156 #set ($discard = $parameters.attributes.putAll({
157 'class': "$!parameters.attributes.get('class') ckeditor-textarea loading",
158 'spellcheck': false,
159 'data-sourceDocumentSyntax': $sourceDocument.syntax.toIdString()
160 }))
161 <textarea
162 #foreach ($entry in $parameters.attributes.entrySet())
163 $entry.key="$!escapetool.xml($entry.value)"
164 #end
165 ## We render the content sheet at the end because the edited content can overwrite the variables used by this macro.
166 #set ($source = {
167 'documentReference': $sourceDocumentReference,
168 'syntax': $sourceDocument.syntax,
169 'content': $parameters.content
170 })
171 #set ($content = "#ckeditor_renderContentSheet($source)")
172 >$!escapetool.xml($content)</textarea>
173 #end
174
175 #macro (displayCKEditorConfigProperty $configDoc $propName $action)
176 <dt>
177 <label>$configDoc.displayPrettyName($propName)</label>
178 <span class="xHint">$escapetool.xml($services.localization.render("CKEditor.ConfigClass_${propName}.hint"))</span>
179 </dt>
180 <dd>#displayCKEditorConfigPropertyValue($configDoc $propName $action)</dd>
181 #end
182
183 #macro (displayCKEditorConfigBooleanProperty $configDoc $propName $action)
184 <dt>
185 <label>
186 #displayCKEditorConfigPropertyValue($configDoc $propName $action)
187 $configDoc.displayPrettyName($propName)
188 </label>
189 <span class="xHint">$escapetool.xml($services.localization.render("CKEditor.ConfigClass_${propName}.hint"))</span>
190 </dt>
191 <dd></dd>
192 #end
193
194 #macro (displayCKEditorConfigPropertyValue $configDoc $propName $action)
195 #set ($output = $configDoc.display($propName, $action))
196 #set ($output = $stringtool.removeStart($output, '{{html clean="false" wiki="false"}}'))
197 #set ($output = $stringtool.removeEnd($output, '{{/html}}'))
198 $output
199 #end
200
201 #macro (displayCKEditorConfig $configDoc $action)
202 #set ($discard = $xwiki.ssx.use('CKEditor.ConfigSheet'))
203 #set ($discard = $xwiki.jsx.use('CKEditor.ConfigSheet'))
204 #ckeditor_importCSS('org.webjars' 'bootstrap-select' 'css/bootstrap-select.min.css')
205 <dl class="ckeditor-config#if ($configDoc.isNew()) new#end">
206 #displayCKEditorConfigProperty($configDoc 'removePlugins' $action)
207 #displayCKEditorConfigProperty($configDoc 'removeButtons' $action)
208 #displayCKEditorConfigBooleanProperty($configDoc 'linkShowAdvancedTab' $action)
209 #displayCKEditorConfigBooleanProperty($configDoc 'linkShowTargetTab' $action)
210 #displayCKEditorConfigBooleanProperty($configDoc 'loadJavaScriptSkinExtensions' $action)
211 <dt>
212 <label>$configDoc.displayPrettyName('advanced')</label>
213 #set ($advancedHint = $escapetool.xml(
214 $services.localization.render('CKEditor.ConfigClass_advanced.hint', ['__linkStart__', '__linkEnd__'])
215 ).replace('__linkStart__', '<a href="http://docs.ckeditor.com/#!/api/CKEDITOR.config">'
216 ).replace('__linkEnd__', '</a>'))
217 <span class="xHint">$advancedHint</span>
218 </dt>
219 <dd>#displayCKEditorConfigPropertyValue($configDoc 'advanced' $action)</dd>
220 </dl>
221 #end
222
223 #macro (ckeditor_initRequiredSkinExtensions)
224 #set ($requiredSkinExtensions = $collectiontool.orderedMap)
225 #if ("$!requiredSkinExtensions" == '')
226 ## For XWiki versions older than 12.1RC1 fallback on the old $collectionstool (available since 4.0M1).
227 #set ($requiredSkinExtensions = $collectionstool.orderedMap)
228 #end
229 ## Save the import string for each skin extension plugin in order to be able to remove the always used extensions
230 ## (they are aready available on the edit page so there's no need to load them).
231 #foreach ($pluginName in ['ssrx', 'ssfx', 'ssx', 'linkx', 'jsrx', 'jsfx', 'jsx'])
232 #set ($discard = $requiredSkinExtensions.put($pluginName, $xwiki.get($pluginName).importString))
233 #end
234 #end
235
236 #macro (ckeditor_getRequiredSkinExtensions)
237 #foreach ($entry in $requiredSkinExtensions.entrySet())
238 #set ($importString = $xwiki.get($entry.key).importString)
239 ## Remove the always used skin extensions, keep only those that have been requested by the macro parameter pickers.
240 $!stringtool.removeStart($importString, $entry.value).trim()##
241 #end
242 #end
243
244 #macro (ckeditor_defineSortTool $sortToolVariable)
245 #set ($checkedExtension = "org.xwiki.commons:xwiki-commons-velocity")
246 ## Because of an API collision, $collectionstool has been replaced by $collectiontool in XCOMMONS-1861
247 #if ($services.extension.core.getCoreExtension($checkedExtension).id.version.compareTo("12.1-SNAPSHOT") >= 0)
248 #setVariable ($sortToolVariable, $collectiontool)
249 ## With the upgrade of Velocity $sorttool has been replaced by $collectionstool (XCOMMONS-1296)
250 #elseif ($services.extension.core.getCoreExtension($checkedExtension).id.version.compareTo("12.0-SNAPSHOT") >= 0)
251 #setVariable ($sortToolVariable, $collectionstool)
252 ## Old API for sorting was $sorttool
253 #else
254 #setVariable ($sortToolVariable, $sorttool)
255 #end
256 #end
257
258 #ckeditor_defineSortTool("$resolvedSortTool")
259 {{/velocity}}