Check if in-place editing is active

Hi,
is it possible to check if in-place editing is active, like checking which mode is currently active via $xcontext.action (view, editing etc.), since in-place editing occurs in view mode?

What’s your use case exactly? Why do you need to know?

Hey there,
thank you for the reply.

I have several macros, that have different rendering outcomes based on the currently active mode.

For Example:
My custommacro “CommandBlock” shows 2 “show hide” buttons and a description in view mode. The macro also have a predefined content like a template.

In edit mode the macro content is inline editable. To be able to edit the content of the “show hide” fields it shows only the content not the buttons in edit mode.

Since the in-place editing takes place in view mode the content can not be edited with in-place editing, because my custom macros checks which mode is currently active to define the rendering outcome.

{{velocity}}
#set ($contents = $xcontext.macro.content )
#set ($test = $xwiki.getRelativeRequestURL())
$xcontext.put(“contents”, $contents)
{{/velocity}}

{{groovy}}
contents = xcontext.get(“contents”);
if (contents.isEmpty()){
contents = ‘’’

{{box cssClass=“mycontentbox”}}
Beschreibung

{{SynapticServiceShowHide showMes=“Tabelle der Kommandoeigenschaften anzeigen” hideMes=“Tabelle der Kommandoeigenschaften verbergen”}}
(% class=“configtable” %)
|=Parametername|=Beschreibung|=Werte|=Version
||||
{{/SynapticServiceShowHide}}

{{SynapticServiceShowHide showMes=“Kommandodatei anzeigen” hideMes=“Kommandodatei verbergen”}}
{{code language=“xml” layout=“PLAIN”}}
Mein CODE
{{/code}}
{{/SynapticServiceShowHide}}

{{/box}}

‘’’
}
xcontext.put(“contents”, contents);
{{/groovy}}
{{velocity}}

#if (($xcontext.action == ‘get’) || ($xcontext.action == ‘edit’) ))
#set ($editresult2 =’(% data-xwiki-non-generated-content=“java.util.List<org.xwiki.rendering.block.Block>” class=“xwiki-metadata-container cke_widget_editable” data-cke-widget-editable="$content" data-cke-enter-mode=“1” data-cke-display-name="$content" contenteditable=“true” %)(((’+ $contents +’)))’)
#set($wikimacro.result = $services.rendering.parse($editresult2, $xwiki.getCurrentContentSyntaxId()).getChildren())
#end
#if (($xcontext.action == ‘view’) || ($xcontext.action == ‘preview’))
#set ($viewresult = $contents)
#set($wikimacro.result = $services.rendering.parse($viewresult, $xwiki.getCurrentContentSyntaxId()).getChildren())
#end
#if ($xcontext.action == ‘export’ && $request.format == ‘pdf’)
#set ($viewresult = $contents)
#set($wikimacro.result = $services.rendering.parse($viewresult, $xwiki.getCurrentContentSyntaxId()).getChildren())
#end
{{/velocity}}

Maybe for better understanding also the code of the show hide macro:

{{velocity}}

#set ($mycontent = $xcontext.macro.content )
#set ($showMes = $xcontext.macro.params.showMes )
#set ($hideMes = $xcontext.macro.params.hideMes )

#if (($xcontext.action == ‘get’) || ($xcontext.action == ‘edit’) || ($test.toString().contains(“xpage=rendercontent&outputTitle=true&outputSyntax=annotatedxhtml”)))
#set ($editresult = ‘(% data-xwiki-non-generated-content=“java.util.List<org.xwiki.rendering.block.Block>” class=“xwiki-metadata-container cke_widget_editable” data-cke-widget-editable="$content" data-cke-enter-mode=“1” data-cke-display-name="$content" contenteditable=“true” %)(((’+ $mycontent +’)))’)
#set($wikimacro.result = $services.rendering.parse($editresult, $xwiki.getCurrentContentSyntaxId()).getChildren())
#end

#if (($xcontext.action == ‘view’) || ($xcontext.action == ‘preview’))
#set ($viewresult = ‘{{showhide showmessage=“○ ’ + $showMes + '” hidemessage=“● ’ + $hideMes + '” effect=“blind” effectduration=“0.1” visible=“true”}}’+
$mycontent+
‘{{/showhide}}’)
#set($wikimacro.result = $services.rendering.parse($viewresult, $xwiki.getCurrentContentSyntaxId()).getChildren())
#end

#if ($xcontext.action == ‘export’ && $request.format == ‘pdf’)
#set($wikimacro.result = $services.rendering.parse($mycontent, $xwiki.getCurrentContentSyntaxId()).getChildren())
#end
{{/velocity}}

I see. The in-place editor retrieves the rendered page content using an URL like this:

/xwiki/bin/view/Sandbox/?xpage=rendercontent&outputTitle=true&outputSyntax=annotatedxhtml&language=en&timestamp=1590141362584

So you’ll have to rely on one of these request parameters to determine if your macro is being edited in-place. I would check the outputSyntax because the annotatedxhtml syntax is specific to the WYSIWYG editor. So you could write:

#if ($xcontext.action == 'get' || $xcontext.action == 'edit' || $request.outputSyntax == 'annotatedxhtml')

Hope this helps,
Marius

Maybe we should offer a more official API to know if we’re in in-place editing.

@mflorea WWDYT?

We could, but the point is to have WYSIWYG, so normally you shouldn’t have to care: the macro should have the same output in view and in-place (WYSIWYG) edit. I wouldn’t want macro developers to abuse this.

Hey,
thanks for the reply. So far using $request.outputSyntax == ‘annotatedxhtml’ works, but saving in in-place editing is no longer possible. But the rendering is correctly transformed.

i have to check whats happening.

Browserlog:

ckeditor.js:1045 Uncaught TypeError: Cannot read property ‘getData’ of undefined
at d. (ckeditor.js:1045)
at d.c (ckeditor.js:10)
at d. (ckeditor.js:11)
at d.CKEDITOR.editor.CKEDITOR.editor.fire (ckeditor.js:13)
at CKEDITOR.htmlDataProcessor.toDataFormat (ckeditor.js:323)
at f.getData (ckeditor.js:366)
at f. (ckeditor.js:372)
at d.c (ckeditor.js:10)
at d. (ckeditor.js:11)
at d.CKEDITOR.editor.CKEDITOR.editor.fire (ckeditor.js:13)

Okay got it. Some macro-code messed up.

Thanks a lot for your help!