Support 'compare' mode in a template plugin

Template plugins have built-in support for a "compare" mode. In this mode two versions of a document are compared, a "base" one and a "current" one. The current version is typically the "unpublished" variant of a document, whereas the "base" one is the "published" variant or a historic version from the version storage.

The template engine provides a backwards-compatible implementation of the mode by instantiating templates twice. Plugins that do not come with a template, e.g. because they are explicitly added to a document template, or plugins that want to provide a better user experience than what is provided (i.e. being instantiated twice), need to support this new mode.

The template has access to two models. The first is named wicket.model and provides access to the "current" version, the second is named model.compareTo and provides access to the "base" version of the document.

A template can be made compatible with compare mode by adding the model.compareTo reference to the template configuration.

+ _default_ [frontend:plugincluster]
  - frontend:properties: [mode]
  - frontend:references: [engine, wicket.model, model.compareTo, validator.id]
  - frontend:services: [wicket.id, validator.id]
  + root [frontend:plugin]
    ...

The plugin code for supporting the compare mode would look similar to the following (simplified) snippet from the TextTemplatePlugin in the CMS:

plugin code:

final IModel<String> valueModel = getModel();
final IEditor.Mode mode = IEditor.Mode.fromString(config.getString("mode", "view"));

if (IEditor.Mode.EDIT == mode) {
    add(new TextAreaWidget("value", valueModel));
} else if (IEditor.Mode.COMPARE == mode) {
    final DiffService diffService = getDiffService(context);
    final IModelReference baseModelReference = context.getService(config.getString("model.compareTo"), IModelReference.class);

    final IModel baseModel = baseModelReference.getModel();
    final IModel compareModel = new HtmlDiffModel(new NewLinesToBrModel(baseModel), new NewLinesToBrModel(valueModel), diffService);

    add(new Label("value", compareModel).setEscapeModelStrings(false));
} else {
    add(new Label("value", valueModel));
}

There are some utilities available in the org.hippoecm.frontend.editor.compare package that can help determine whether there are actually any differences plus an implementation of the Longest Common Subsequence algorithm to generate a minimal diff (see org.hippoecm.frontend.plugins.standards.diff).

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?