Change default text in ShareJS

76 views Asked by At

Situation: I have website, written in PHP & mySQL for documents creating/sharing. Now I need to add some collaboration functions to the website so desided to use shareJS (using default ace editor).

Almost all works fine but I can't set default text in editor cause it takes data from default redis database. Yes, I can try to rewrite data in redis DB or change it by my mySQL DB, but i want to escape it.

In the client part I try to delete all text and set new

// delete text which come from node server
var nodeText = doc.getText();
var nodeTextRows = nodeText.split("\n");
nodeTextRows.forEach(function(text, i, arr) {
    var locIDX = i+1;
    doc.del(locIDX, text.length);
    console.log("Delete: " + locIDX + " " + text + " " + text.length);
});


// insert text from DB into editor
var textRows = text.split("\n");
textRows.forEach(function(text, i, arr) {
    var locIDX = i+1;
    doc.insert(locIDX, text);
    console.log("Insert: " + locIDX + " " + text);
});

But it doesn't work because of this shareJS code

if editorText != otText
    console.error "Text does not match!"
    console.error "editor: #{editorText}"
    console.error "ot: #{otText}"
    # Should probably also replace the editor text with the doc snapshot.
, 0

So my error message is: Text does not match editor: {empty string} ot: {some garbage, made by input and code above}

Mabe I'm doing it in a wrong way.

1

There are 1 answers

0
Svyat On BEST ANSWER

Here is undocumented function that I found in a source code:

doc.attach_ace(editor, true);

second variable says to keep editor content so you can set your own text in editor.

Hope thats would be usefull.