Creating an appendText and appendHTML for the HTTP API

202 views
Skip to first unread message

Demotis Silvermoon

unread,
May 16, 2013, 1:07:58 PM5/16/13
to etherpad...@googlegroups.com
Ok, I need to have the ability to import parts of another page to an active pad using the HTTP API. I know that copy and paste works in the UI, but I need this to be done behind the scenes.

I've started with the appendText since it would be easiest to get working, and everything I need right now is text based.

So I mirrored the existing setText function of API.js and modified the APIHandler.js and got the new API calling correctly from the C# connector. I am able to get it to place the text at the end of the pad, but it kills the other editors (read other users) cursor placement and resets all the Authorship highlights. The users cursor is placed at the head of the pad after the update. It also resets all the formatting, but this is expected as I am using the text of the pad in the append.

I think I need to take the inbound text and create a patch that would be placed in the normal queue to be updated by all the clients, but I'm not sure how to do this. Not asking for someone to do this for me, just looking for a little pointer to help me along in this.

Here is the method that I was using.

exports.appendText = function(padID, text, callback)
{
  //text is required
  if(typeof text != "string")
  {
    callback(new customError("text is no string","apierror"));
    return;
  }

  //get the pad
  getPadSafe(padID, true, function(err, pad)
  {
    if(ERR(err, callback)) return;
    
var curText = pad.text();
var finalText = curText + "\n" + text;
    //set the text
    pad.setText(finalText);
    
    //update the clients on the pad
    padMessageHandler.updatePadClients(pad, callback);
  });
}

As you can see, it's identical to the way that setText works but it gets the current text of the pad, adds a line break and then the inbound text, and sets the pad text to the new value. It then tells the clients to update. I really think that it needs to create a diff of the existing text and the text with the appended text and then place that diff in the timeline to be updated by any active clients.

I also need to do this as HTML. I don't think that it will be difficult for me to do this once I get this working correctly, but if anyone can think of any pitfalls please let me know.

Thank you,
Dusty

Dmitry

unread,
May 17, 2013, 8:58:37 AM5/17/13
to etherpad...@googlegroups.com
The way you do this is wrong since you just override the text and break pad state (you wont' be able to recreate the same state by applying changesets from 0 to the last one).
What you need to do is just change one line from the setText function to this one:

//create the changeset
var changeset = Changeset.makeSplice(oldText, oldText.length, 0, newText);

Demotis Silvermoon

unread,
May 17, 2013, 5:16:58 PM5/17/13
to etherpad...@googlegroups.com
Thank you, Dmitry.

I was able to get it to work with your help.

matteo sule

unread,
Aug 5, 2013, 8:56:30 AM8/5/13
to etherpad...@googlegroups.com
Hi Guys,
I would like to use this function appendText on my etherpad.
I'm planning to use it with the SMS daemon gammu as my user will be able to save the content of SMS on their pad.
They send a SMS, and a script gets the SMS content and add a new line on their pad with the previous content (thanks to the appendText function).

Could you help me to get this working ?
I'm not sure which file I have to change.

Thanks
Matteo

Marcel Klehr

unread,
Aug 10, 2013, 5:48:32 AM8/10/13
to etherpad...@googlegroups.com
Hey matteo,

first you'll have to change https://github.com/ether/etherpad-lite/blob/develop/src/node/handler/APIHandler.js (just add the desired endpoint name to the API version of your choice, i recommend creating a new version).
Then add the above code to https://github.com/ether/etherpad-lite/blob/develop/src/node/db/API.js#L274 as seen in the file.
Reply all
Reply to author
Forward
0 new messages