Wiki source code of Write an Editor in a Wiki Page

Last modified by Eleni Cojocariu on 2026/12/04 14:23

Hide last authors
Eleni Cojocariu 5.1 1 The editor is an ##XWiki.EditorClass## object on a page, and the object's properties say what it edits, under which identifier and with which code. The example below is a plain text area for wiki content, in the ##text## category.
2
Michael Hamann 9.1 3 1. Create the page that will hold the editor. Its **title** becomes the editor's name and its **content** the editor's description, read from the translated page every time the editor is described, so both follow the reader's language:(((
Eleni Cojocariu 8.1 4 {{code language="none"}}
Eleni Cojocariu 5.1 5 Title: Simple Text Area
6 Content: A plain text area with no toolbar, for editing wiki syntax by hand.
7 {{/code}}
8 )))
Eleni Cojocariu 12.3 9 1. Add an object of type ##XWiki.EditorClass## to that page with the [[Object editor>>doc:documentation.xs.user.content-edit.edit-object-editor.WebHome]]:(((
Eleni Cojocariu 5.1 10 {{image reference="add-editor-class-object.png" size="extra" alt="The object editor of the Simple Text Area page, with EditorClass typed in the New object field and XWiki EditorClass offered in the drop down list below it"/}}
11 )))
Michael Hamann 9.1 12 1. Fill the object's six properties:(((
Eleni Cojocariu 5.1 13 |=Property|=Value in this example|=What it is
14 |Data Type|##org.xwiki.rendering.syntax.SyntaxContent##|the Java type the editor edits
15 |Role Hint|##simple-textarea##|the editor's identifier
16 |Code|(the next step)|the wiki content that produces the editor
17 |Icon|##pencil##|the icon shown for the editor
18 |Category|##text##|the editing mode the editor belongs to
19 |Scope|Current Wiki|where the editor is registered
20
21 {{image reference="editor-class-properties.png" size="extra" alt="The EditorClass object of the Simple Text Area page, with Data Type set to org.xwiki.rendering.syntax.SyntaxContent, Role Hint to simple-textarea, the Code property holding the editor code, Icon set to pencil, Category to text and Scope to Current Wiki"/}}
22
Michael Hamann 9.1 23 The first two decide whether the editor exists at all: the query that collects the editors defined in wiki pages skips an object whose **Role Hint** is empty, and an object whose **Data Type** is not a type the instance can load fails to register, with the reason in the log. **Scope** is the one thing a Java editor cannot choose: **Current User** registers the editor for the page's author only, **Current Wiki** for the wiki holding the page, **Global** for every wiki of the farm, and an empty value is read as Current Wiki. A scope wider than Current User needs a right of the user who last saved the page: administration right on the wiki for Current Wiki, programming right for Global. Without it the editor isn't registered, again with the reason in the log.
Eleni Cojocariu 5.1 24 )))
Eleni Cojocariu 13.4 25 1. Write the **Code** property. Unlike the template of a Java editor it is page content, so it is written in the page's own syntax and may use macros. It reads ##$edit.data## and ##$edit.parameters## from [[the same ##edit## binding a Java editor's template uses>>doc:documentation.xs.dev.content-edit.write-java-editor.WebHome]]:(((
Eleni Cojocariu 5.1 26 {{code language="velocity"}}
27 {{velocity}}
28 {{html clean="false"}}
29 <div class="box infomessage">Editing $escapetool.xml($edit.data.syntax.toIdString()) content.</div>
30 <textarea id="$!edit.parameters.id" name="$!edit.parameters.name"
31 rows="$!edit.parameters.rows" cols="$!edit.parameters.cols">$escapetool.xml($edit.data.content)</textarea>
32 {{/html}}
33 {{/velocity}}
34 {{/code}}
35
36 {{warning}}
Michael Hamann 9.1 37 The code runs with the rights of the user who last saved the editor page, but in the context of the page being edited, which is how it reaches the data put on the script context. Whoever saves the editor page next changes the rights its code runs with, and unregisters the editor if they lack the right its scope needs, so keep edit right on that page to the users who maintain the editor and consider [[enforcing the required rights>>documentation.xs.admin.rights.required-rights.enforce.WebHome]] on the editor page to avoid accidental breakage.
Eleni Cojocariu 5.1 38 {{/warning}}
39 )))
Michael Hamann 9.1 40 1. Save the page. The editor is registered as soon as the page is saved, and naming it in the query string of an edit URL is enough to see it at work on the data type and category it declared:(((
Eleni Cojocariu 8.1 41 {{code language="none"}}
Eleni Cojocariu 5.1 42 /bin/edit/Sandbox/MeetingNotes?editor=wiki&SyntaxContent.text.editor=simple-textarea
43 {{/code}}
44
45 {{image reference="wiki-page-editor-rendered.png" size="extra" alt="The wiki edit form of the Meeting Notes page rendering the Simple Text Area editor, with the message Editing xwiki 2.1 content above the content text area"/}}
46
Eleni Cojocariu 13.2 47 To hand the editor to a user, a space or the whole wiki without naming it in the URL, see [[Configure the Default Editor>>doc:documentation.xs.dev.content-edit.configure-default-editor.WebHome]].
Eleni Cojocariu 5.1 48 )))
49
50 Two of the editors shipped with XWiki are written this way and are worth reading as examples: ##CKEditor.SyntaxContentEditor## registers ##ckeditor## in the ##wysiwyg## category, and ##XWiki.Realtime.WikiEditor## registers ##realtime-wiki## in the ##text## category, both with the Current Wiki scope. Both keep the editor code in the page content and set the Code property to an include of their own page, and both guard that content so that it renders only while the page is being used as an editor, which keeps it out of the editor's description.

Get Connected