Apache Wicket - Getting the cursor position from TinyMceBehavior

chrishern

We are using the TinyMceBehavior to add rich text behaviour to our text areas:

public class MandatoryContextTextArea extends TextArea<String> {

    private static final long serialVersionUID = -7837811514253179662L;

    public MandatoryContextTextArea(final String id, final IModel<String> model) {
        super(id, model);

        this.setOutputMarkupId(true);
        this.add(new IBehavior[] { new TinyMceBehavior(CustomTinyMCETextEditorSettings.initSettings()) });
    }
}

Is there anyway to get the position of where the user has the cursor currently located within the rich text area?

I have a requirement to have an 'Insert' button which when clicked, adds some text to the text area at the position the user has their cursor positioned. Is there anything within Wicket or TinyMceBehavior which can provide this location?

Thanks in advance.

Martin Cassidy

You won't be able to get the cursor position on the server side very easily, but you can create a simple button which will handle it all on the client side with some JavaScript.

Something like this perhaps could be added to your button:

public class InsertAtCursorPositionBehaviour extends AttributeModifier
{

    public InsertAtCursorPositionBehaviour(final String content, final TextArea<?> textArea)
    {
        super("onclick", Model.of(getInsertJavaScript(content, textArea)));
    }

    private static String getInsertJavaScript(final String content, final TextArea<?> textArea)
    {
        textArea.setOutputMarkupId(true);
        final StringBuilder jsBuilder = new StringBuilder();
        jsBuilder.append("tinyMCE.get('");
        jsBuilder.append(textArea.getMarkupId());
        jsBuilder.append("').execCommand('mceInsertContent', false, '");
        jsBuilder.append(content);
        jsBuilder.append("');");
        return jsBuilder.toString();
    }
}

The string is the content to insert and the TextArea is the tinymce text area. Referring to the text area directly allows the possibility of having more than one per page.

If the content to insert is on the client side, it might be nice to embed the button into tiny mce itself. Take a look these custom options you could use: http://www.tinymce.com/tryit/button.php

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting item position from cursor adapter

From Dev

Getting the cursor position of a GtkEntry

From Dev

Getting cursor position in mailItem htmlbody

From Dev

What is the reason of getting model of an object in apache wicket?

From Dev

Cursor shifted from position

From Dev

Getting value from cursor

From Dev

Getting value from cursor

From Java

Getting and Setting Cursor Position of UITextField and UITextView in Swift

From Dev

Getting Cursor position in Tkinter entry widget

From Dev

Getting Cursor position in Tkinter entry widget

From Dev

Getting values of cursor from indexedDB

From Dev

Get word from EditText on current cursor position

From Dev

How to retrieve user name from CAS in Apache Wicket

From Dev

Wicket ModalWindow position

From Dev

Getting wrong position from view

From Dev

can't getting data from cursor adapter

From Dev

Wicket: Getting Tags Attribute

From Dev

EJB Injection Apache Wicket

From Dev

Populating table with apache wicket

From Dev

Apache Wicket textfield onUpdate

From Dev

Apache Wicket DateTextField clear

From Dev

Jacoco support for apache wicket?

From Dev

Getting an element from a specific position from a ConcurrentLinkedDeque

From Dev

Getting an element from a specific position from a ConcurrentLinkedDeque

From Dev

How to get text from contenteditable div from beginning to the cursor position

From Dev

Getting the string at nth position using Python in Apache Spark

From Dev

Google Documents: get active paragraph from cursor position

From Dev

vim: delete from cursor position to n characters before end of line

From Dev

Google Documents: get active paragraph from cursor position

Related Related

HotTag

Archive