📜 ⬆️ ⬇️

Compare pages. Simple plugin for Atlassian Confluence

Atlassian Confluence has a wonderful page version comparison functionality. It is convenient to use them, but it will not be possible to go beyond the change history. How to be? The search for a ready-made plug-in in the Marketplace did not give any results and it was decided to write my own.

Instruments


We will need:


Create a project


Install the SDK and run the atlas-create-confluence-plugin script.

Enter:
')

To build, you can use maven and dependencies from the SDK, but it's easier to connect additional repositories in your settings.xml.

 <repository> <id>atlassian-public</id> <url>https://packages.atlassian.com/maven/repository/public</url> <snapshots> <enabled>true</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>warn</checksumPolicy> </snapshots> <releases> <enabled>true</enabled> <checksumPolicy>warn</checksumPolicy> </releases> </repository> <pluginRepository> <id>atlassian-public</id> <url>https://maven.atlassian.com/repository/public</url> <releases> <enabled>true</enabled> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <updatePolicy>never</updatePolicy> <checksumPolicy>warn</checksumPolicy> </snapshots> </pluginRepository> 

Open the resulting project in the IDE, run Confluence in debug mode confluence:debug



and check the local installation at http: // localhost: 1990 / confluence / . Login admin, password admin. The database has already created a demonstration space and several articles.

Little functionality


Let's start with the menu. Add the web-item module to the atlassian-plugin.xml plugin descriptor.

 <web-item key="diff-page-menu" name="Diff Page" section="system.content.action/secondary" weight="300"> <description>Add diff item to drop-down menu</description> <label key="diff.page.menu.name"/> <link linkId="diff-page">/plugins/diffPage/diffPagePopup.action?spaceKey=${space.key}&sourcePageId=${page.id}</link> <condition class="com.atlassian.confluence.plugin.descriptor.web.conditions.HasPageCondition"/> </web-item> 

Where:


Add a line to diff-page.properties:
diff.page.menu.name=Diff page

If you need translation support, we will create additional properties, for example, for Russian localization the file will be called diff-page_ru_RU.properties.

Perform the maven package . The plugin will automatically restart and our menu item will appear.



Modal window


To select a page it is convenient to use a modal window Continue to edit atlassian-plugin.xml

Add some resources:

 <web-resource key="diff-page-popup-resources" name="diff-page Popup Web Resources"> <dependency>com.atlassian.auiplugin:ajs</dependency> <dependency>com.atlassian.auiplugin:dialog2</dependency> <resource type="download" name="diff-page-popup.js" location="/js/diff-page-popup.js"/> <context>page</context> </web-resource> 

Where:


We define the handlers of our urls:

 <xwork name="Diff Page Action" key="diff-page-action"> <description>Diff page action</description> <package name="diff-page-package" extends="default" namespace="/plugins/diffPage"> <default-interceptor-ref name="defaultStack"/> <action name="diffPage" class="com.kshch.confluence.plugins.diff.page.action.DiffPageAction"> <result name="success" type="velocity">/templates/diff-page.vm</result> </action> <action name="diffPagePopup" class="com.kshch.confluence.plugins.diff.page.action.DiffPagePopupAction"> <result name="success" type="velocity">/templates/diff-page-popup.vm</result> </action> </package> </xwork> 

Where:


Let's create a new com.kshch.confluence.plugins.diff.page.action package and add the DiffPagePopupAction class to it, which extends ConfluenceActionSupport with 2 fields.

 private String spaceKey; private Long sourcePageId; 

We will generate getters and setters for them. The spaceKey field will contain the key of the current space, and the sourcePageId will contain the id of the current page.

Let's take the front. To create a popup window, use the dialog2 element from the AUI framework.

Add a form, submit button and input with css classes autocomplete-space and autocomplete-page to the dialog, which will allow you to organize the choice of space and page without additional efforts. Save the result in /templates/diff-page-popup.vm.

 <section role="dialog" id="diff-page-popup" class="aui-layer aui-dialog2 aui-dialog2-medium" aria-hidden="true" data-aui-remove-on-hide="true"> <form action="$action.getBootstrapManager().getWebAppContextPath()/plugins/diffPage/diffPage.action" method="get" class="aui"> ... <input type="hidden" name="sourcePageId" value="$action.getSourcePageId()"> <input class="text autocomplete-space" type="text" id="diff-page-space" name="spaceKey" data-max="10" data-none-message="$action.getText("diff.page.popup.no.result")" placeholder="$action.getText("diff.page.popup.select.space")" value="$action.getSpaceKey()" data-template="{key}"> <input type="text" class="text autocomplete-page" name="destinationPageName" data-max="10" placeholder="$action.getText("diff.page.popup.select.page")" data-none-message="$action.getText("diff.page.popup.no.result")"> <button id="dialog-diff-button" class="aui-button aui-button-primary">$action.getText("diff.page.popup.diff")</button> ... </form> </section> 

To display the window, create the /js/diff-page-popup.js resources with the following contents:

 (function ($) { $(function () { AJS.$('#diff-page').unbind('click'); AJS.$('#diff-page').bind("click", function (e) { e.preventDefault(); var link = AJS.$(this); AJS.$.get(link.attr('href'), function (response) { AJS.$('.aui-page-panel').after(response); AJS.dialog2("#diff-page-popup").show(); Confluence.Binder.autocompletePage(AJS.$("#diff-page-popup-binder")); }); return false; }); }); })(AJS.$); 

His task is to override the standard click event and show us a pop-up window, and not to follow the link.

Perform a maven package and try to select a page and space. The prompt is activated after typing 2 characters.



Main functionality


Let's start implementation of the main functionality. Add to the com.kshch.confluence.plugins.diff.page.action package another DiffPageAction class that extends ConfluenceActionSupport and implements the PageAware interface with the following fields.

 private Long sourcePageId; private Long destinationPageId; private String spaceKey; private String destinationPageName; private String sourcePageTitle; private String destinationPageTitle; private String diff; private Page sourcePage; private Differ differ; private final PageManager pageManager; 

Where:


We will generate getters and setters, implement all methods except getPage, as suggested by the IDE by default. The fields sourcePageId, spaceKey and destinationPageName will contain the data that came from the form inside dialog2.

 @Override public AbstractPage getPage() { return this.sourcePage; } 

Override the execute method with our own. It is in it that the main logic of the plug-in work is concluded.

 @Override public String execute() throws Exception { if (this.sourcePageId != null && this.spaceKey != null && this.destinationPageName != null) { this.sourcePage = this.pageManager.getPage(this.sourcePageId); Page destinationPage = this.pageManager.getPage(this.spaceKey, this.destinationPageName); if (this.sourcePage != null && destinationPage != null) { this.destinationPageId = destinationPage.getId(); this.sourcePageTitle = this.sourcePage.getTitle(); this.destinationPageTitle = destinationPage.getTitle(); this.diff = this.differ.diff(this.sourcePage, destinationPage); } } return super.execute(); } 

It remains to implement a frontend comparison of 2 pages. It is much easier to pop up.

Create in /templates/diff-page.vm resources. In it, besides the layout, we implement the simplest error checking.

 #if ($action.getDiff()) #diffBody() #else <div class="aui-message aui-message-error"> <p class="title"> <strong>$action.getText("diff.page.error")</strong> </p> <p>$action.getText("diff.page.error.message")</p> </div> #end 

Writing the plugin is finished. Once again we will execute the maven package , select the page and enjoy the result.



The full project code is available on GitHub .

Source: https://habr.com/ru/post/433466/


All Articles