Tutorial: Using a Custom URL Chooser

Using a Custom URL Chooser

Some editing tasks require the user to link several resources. For example, when inserting an image, the user must provide the URL of that image. The same scenario applies for links between XML document and DITA conrefs.

You can improve the user experience by implementing a custom URL chooser that allows choosing a resource in your CMS. This can be achieved by extending the sync.api.UrlChooser class.

The following code snippet implements a dummy UrlChooser that shows a prompt to the user that asks for the URL.

var DummyUrlChooser = function() {};
DummyUrlChooser.prototype = new sync.api.UrlChooser();
// Only support image choosing
DummyUrlChooser.prototype.supports = function(type) {
    return type == sync.api.UrlChooser.Type.IMAGE;
};
// 
DummyUrlChooser.prototype.chooseUrl = function(context, chosen) {
  if (context.getType() == sync.api.UrlChooser.Type.IMAGE) {
    var result = prompt();
    chosen(result);
  }
  return null;
};

Then, you can register it using the following API call:

workspace.setUrlChooser(new DummyUrlChooser());