Wiki-Quellcode von VelocityMacros

Version 1.1 von admin am 2025/02/23 19:33

Verstecke letzte Bearbeiter
admin 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 'restricted' : $request.wysiwygRestricted
15 })
16 #if ($fromHTML)
17 #set ($source.html = $text)
18 #else
19 #set ($source.content = $text)
20 #end
21 #if ($stripHTMLEnvelope)
22 ## Don't wrap the rendered content with the HTML and BODY tags. This is needed when the HTML fragment is injected
23 ## directly in the current page, e.g. when CKEditor is used in-line (no iframe). We still need to send back the
24 ## required style sheets and JavaScript files because some of them might not be available in the target page, e.g.
25 ## when a macro is inserted and it needs resource files not available on every page.
26 #ckeditor_initRequiredSkinExtensions()
27 #set ($output = "#ckeditor_getRenderedContent($source)$!html")
28 #set ($discard = $response.setHeader('X-XWIKI-HTML-HEAD', "#ckeditor_getRequiredSkinExtensions()"))
29 #else
30 ## Returns a full HTML page, including the HEAD tag with the style sheets and possibly the JavaScript includes.
31 ## This is needed when the edited content is loaded in an iframe, which is the case when CKEditor replaces a text
32 ## area (classical editor).
33 #set ($output = "#ckeditor_renderContentSheet($source)")
34 #end
35 #else
36 #set ($output = $services.wysiwyg.fromAnnotatedXHTML($text, $sourceSyntax.toIdString()))
37 #end
38 #if ("$!output" != '' || $output == '')
39 $output##
40 #else
41 ## The output is null so an exception must have been thrown.
42 $response.sendError(500)
43 #end
44 #end
45
46 #macro (ckeditor_renderContentSheet $source)
47 ## The $source variable is used inside the content sheet.
48 #set ($output = $services.wysiwyg.render('CKEditor.ContentSheet'))
49 ## Unescape {{ which happened in CKEditor.ContentSheet for protecting the {{html}} macro.
50 $!output.replace('{{', '{{')##
51 #end
52
53 #macro (ckeditor_getRenderedContent $source)
54 ## Make sure the edited content is rendered for view because this is a WYSIWYG editor.
55 #set ($macro.previousDisplayMode = $xcontext.displayMode)
56 #set ($discard = $xcontext.setDisplayMode('view'))
57 #set ($restricted = "$!source.restricted" == 'true')
58 #if ($source.html)
59 #set ($html = $services.wysiwyg.parseAndRender($source.html, $source.syntax, $source.documentReference, $restricted))
60 #else
61 #set ($html = $services.wysiwyg.toAnnotatedXHTML($source.content, $source.syntax, $source.documentReference, $restricted))
62 #end
63 #if ("$!html" == '')
64 ## Prevent Firefox from inserting bogus whitespace when the text area is empty.
65 ## CKEDITOR-185: Whitespace is generated in Firefox when creating a page without typing the content
66 #set ($html = '<p></p>')
67 #end
68 ## Restore the previous display mode.
69 #set ($discard = $xcontext.setDisplayMode($macro.previousDisplayMode))
70 #end
71
72 #macro (ckeditor_importCSS $groupId $artifactId $path $evaluate)
73 #set ($url = $services.webjars.url("$groupId:$artifactId", $path, {'evaluate': $evaluate}))
74 #set ($discard = $xwiki.linkx.use($url, {'type': 'text/css', 'rel': 'stylesheet'}))
75 #end
76
77 #macro (importCKEditorResources)
78 #set ($discard = $xwiki.ssx.use('CKEditor.EditSheet'))
79 ## Add the version of each JavaScript dependency of CKEditor Integration to the URL used to load the CKEditor in order
80 ## to make sure the browser cache is invalidated when one of these versions changes (e.g. when we upgrade XWiki or one
81 ## of these dependencies but the CKEditor version remains the same).
82 #set ($discard = $xwiki.jsx.use('CKEditor.EditSheet', {
83 'v': $services.extension.installed.getInstalledExtension('org.xwiki.platform:xwiki-platform-ckeditor-ui',
84 "wiki:$xcontext.database").version.value,
85 'xwiki-version': $services.extension.core.getCoreExtension(
86 'org.xwiki.platform:xwiki-platform-tree-webjar').version.value,
87 'fast-diff-version': $services.extension.installed.getInstalledExtension('org.webjars.npm:fast-diff',
88 "wiki:$xcontext.database").version.value,
89 'bs3typeahead-version': $services.extension.installed.getInstalledExtension('org.webjars.npm:bootstrap-3-typeahead',
90 "wiki:$xcontext.database").version.value
91 }))
92 #ckeditor_importCSS('org.xwiki.platform' 'xwiki-platform-ckeditor-plugins' 'webjar.bundle.min.css' true)
93 ## Tree styles needed for the document/attachment tree picker.
94 #set ($discard = $xwiki.linkx.use($services.webjars.url('org.xwiki.platform:xwiki-platform-tree-webjar', 'tree.min.css',
95 {'evaluate': true}), {'type': 'text/css', 'rel': 'stylesheet'}))
96 #set ($discard = $xwiki.linkx.use($services.webjars.url('org.xwiki.platform:xwiki-platform-tree-webjar',
97 'finder.min.css', {'evaluate': true}), {'type': 'text/css', 'rel': 'stylesheet'}))
98 #end
99
100 #macro (ckeditor $parameters)
101 #importCKEditorResources
102 ##
103 #set ($escapedName = $escapetool.xml($parameters.attributes.name))
104 <input value="$!escapedName" name="RequiresHTMLConversion" type="hidden"/>
105 ##
106 #set ($syntaxId = $parameters.attributes.get('data-syntax'))
107 <input value="$!escapetool.xml($syntaxId)" name="$!{escapedName}_syntax" type="hidden"/>
108 ##
109 ## Chrome doesn't cache the enabled/disabled state of the form fields so we must store this state in the value of
110 ## another form field. For instance, the enabled/disabled state of the RequiresHTMLConversion hidden input
111 ## determines which editor mode (WYSIWYG vs. Source) is loaded initially.
112 ## See CKEDITOR-34: Wiki syntax gets escaped when you click "Back" in the browser
113 <input value="" name="$!{escapedName}_cache" type="hidden" class="cache"/>
114 ##
115 #set ($discard = $parameters.attributes.putAll({
116 'class': "$!parameters.attributes.get('class') ckeditor-textarea loading",
117 'spellcheck': false
118 }))
119 ##
120 #set ($sourceDocumentReference = $parameters.attributes.get('data-sourceDocumentReference'))
121 #if (!$sourceDocumentReference)
122 #set ($sourceDocumentReference = $tdoc.documentReference)
123 #end
124 #set ($source = {
125 'documentReference': $sourceDocumentReference,
126 'syntax': $syntaxId,
127 'content': $parameters.content,
128 'restricted': $parameters.attributes.data-restricted
129 })
130 ##
131 ## Disable the file upload and drag & drop when the current user doesn't have edit right on the source document, if
132 ## the source is restricted. This is the case for instance when an user with comment right adds a comment to a page
133 ## they don't have edit right on. They are allowed to add the comment but they can't upload files because that
134 ## requires edit right. We check if the source is in restricted mode because in some cases (such as for Change Request
135 ## Application) we want to allow file upload even if the current user doesn't have edit right on the source document.
136 #if ($source.restricted == 'true' && !$services.security.authorization.hasAccess('edit', $source.documentReference))
137 #set ($parameters.attributes.data-upload-disabled = 'true')
138 #end
139 ## We use the velocity template context to convey the startupFocus parameter to CKEditor.
140 #if ($wysiwygEditorConfig.startupFocus)
141 #set ($parameters.attributes.data-startup-focus = 'true')
142 ## We make sure to clear this context so that it doesn't interfere with CKEditors opened later.
143 #set ($wysiwygEditorConfig.startupFocus = false)
144 #end
145 ##
146 <textarea
147 #foreach ($entry in $parameters.attributes.entrySet())
148 $entry.key="$!escapetool.xml($entry.value)"
149 #end
150 ## We render the content sheet at the end because the edited content can overwrite the variables used by this macro.
151 #set ($content = "#ckeditor_renderContentSheet($source)")
152 >$!escapetool.xml($content)</textarea>
153 #end
154
155 #macro (displayCKEditorConfigProperty $configDoc $propName $action)
156 <dt>
157 <label for="CKEditor.ConfigClass_$escapetool.xml($configDoc.getObject("CKEditor.ConfigClass").getNumber())_${propName}">$configDoc.displayPrettyName($propName)</label>
158 <span class="xHint">$escapetool.xml($services.localization.render("CKEditor.ConfigClass_${propName}.hint"))</span>
159 </dt>
160 <dd>#displayCKEditorConfigPropertyValue($configDoc $propName $action)</dd>
161 #end
162
163 #macro (displayCKEditorConfigBooleanProperty $configDoc $propName $action)
164 <dt>
165 <label>
166 #displayCKEditorConfigPropertyValue($configDoc $propName $action)
167 $configDoc.displayPrettyName($propName)
168 </label>
169 <span class="xHint">$escapetool.xml($services.localization.render("CKEditor.ConfigClass_${propName}.hint"))</span>
170 </dt>
171 <dd></dd>
172 #end
173
174 #macro (displayCKEditorConfigPropertyValue $configDoc $propName $action)
175 #unwrapXPropertyDisplay($configDoc.display($propName, $action))
176 #end
177
178 #macro (displayCKEditorConfig $configDoc $action)
179 #set ($discard = $xwiki.ssx.use('CKEditor.ConfigSheet'))
180 #set ($discard = $xwiki.jsx.use('CKEditor.ConfigSheet'))
181 #ckeditor_importCSS('org.webjars' 'bootstrap-select' 'css/bootstrap-select.min.css')
182 <dl class="ckeditor-config#if ($configDoc.isNew()) new#end">
183 #displayCKEditorConfigProperty($configDoc 'removePlugins' $action)
184 #displayCKEditorConfigProperty($configDoc 'removeButtons' $action)
185 #displayCKEditorConfigBooleanProperty($configDoc 'linkShowAdvancedTab' $action)
186 #displayCKEditorConfigBooleanProperty($configDoc 'linkShowTargetTab' $action)
187 #displayCKEditorConfigBooleanProperty($configDoc 'loadJavaScriptSkinExtensions' $action)
188 <dt>
189 <label>$configDoc.displayPrettyName('advanced')</label>
190 #set ($advancedHint = $escapetool.xml(
191 $services.localization.render('CKEditor.ConfigClass_advanced.hint', ['__linkStart__', '__linkEnd__'])
192 ).replace('__linkStart__', '<a href="http://docs.ckeditor.com/#!/api/CKEDITOR.config">'
193 ).replace('__linkEnd__', '</a>'))
194 <span class="xHint">$advancedHint</span>
195 </dt>
196 <dd>#displayCKEditorConfigPropertyValue($configDoc 'advanced' $action)</dd>
197 </dl>
198 #end
199
200 #macro (ckeditor_initRequiredSkinExtensions)
201 #set ($requiredSkinExtensions = $collectiontool.orderedMap)
202 ## Save the import string for each skin extension plugin in order to be able to remove the always used extensions
203 ## (they are aready available on the edit page so there's no need to load them).
204 #foreach ($pluginName in ['ssrx', 'ssfx', 'ssx', 'linkx', 'jsrx', 'jsfx', 'jsx'])
205 #set ($discard = $requiredSkinExtensions.put($pluginName, $xwiki.get($pluginName).importString))
206 #end
207 #end
208
209 #macro (ckeditor_getRequiredSkinExtensions)
210 #foreach ($entry in $requiredSkinExtensions.entrySet())
211 #set ($importString = $xwiki.get($entry.key).importString)
212 ## Remove the always used skin extensions, keep only those that have been requested by the macro parameter pickers.
213 $!stringtool.removeStart($importString, $entry.value).trim()##
214 #end
215 #end
216 {{/velocity}}