Skip to content
Snippets Groups Projects
Commit 1b92e050 authored by Marius Dumitru Florea's avatar Marius Dumitru Florea
Browse files

XWIKI-13528: Add support for replacing the default WYSIWYG editor

* We cannot overwrite the existing bindings in the Velocity context when copying script context bindings because we have Velocity scripts that expect $doc instance to be shared among them.
parent 3adc4cef
No related branches found
No related tags found
No related merge requests found
......@@ -47,15 +47,23 @@ public abstract class AbstractEditor<D> implements Editor<D>
private ScriptContextManager scripts;
@Override
@SuppressWarnings("unchecked")
public String render(D data, Map<String, Object> parameters) throws EditException
{
ScriptContext scriptContext = this.scripts.getScriptContext();
scriptContext.setAttribute(EDIT_CONTEXT_KEY, getEditContext(data, parameters), ScriptContext.ENGINE_SCOPE);
try {
return render();
} finally {
scriptContext.removeAttribute(EDIT_CONTEXT_KEY, ScriptContext.ENGINE_SCOPE);
// DefaultVelocityManager doesn't overwrite the existing keys when copying the script context bindings so we
// have to reuse the existing binding for the edit context.
Object editContext = scriptContext.getAttribute(EDIT_CONTEXT_KEY, ScriptContext.ENGINE_SCOPE);
Map<String, Object> editContextMap;
if (editContext instanceof Map) {
editContextMap = (Map<String, Object>) editContext;
editContextMap.clear();
} else {
editContextMap = new HashMap<>();
scriptContext.setAttribute(EDIT_CONTEXT_KEY, editContextMap, ScriptContext.ENGINE_SCOPE);
}
editContextMap.putAll(getEditContext(data, parameters));
return render();
}
protected Map<String, Object> getEditContext(D data, Map<String, Object> parameters)
......
......@@ -168,7 +168,11 @@ private void copyScriptContext(VelocityContext vcontext, ScriptContext scriptCon
Bindings bindings = scriptContext.getBindings(scope);
if (bindings != null) {
for (Map.Entry<String, Object> entry : bindings.entrySet()) {
vcontext.put(entry.getKey(), entry.getValue());
// Not ideal since it doesn't allow us to modify a binding but it's too dangerous for existing velocity
// scripts otherwise (e.g. each velocity script would have a separate instance of $doc).
if (!vcontext.containsKey(entry.getKey())) {
vcontext.put(entry.getKey(), entry.getValue());
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment