xPages - how to append & prepend text to a richtex

2019-08-05 07:46发布

问题:

I'm migrating an app to xpages. I have a doc that's created from an email received by the app. A richtext field on the created document contains the email body. Someone responds to the sender by updating this field and hitting the Send button. Before it actually goes out, I prepend and append a bit of text to the content being sent. I tried lots of ways to do this but am stumped.

I know .getItemValue returns a vector but I thought I'd give it a shot anyway but returns null.

var rtiIssueField = maildoc.createRichTextItem("body");
rtiIssueField.appendText("**IMPORTANT:blah  **");
rtiIssueField.appendText("\n<LWST>");
rtiIssueField.appendText("\n"+stEntryDoc.getItemValue("Issue"));
rtiIssueField.appendText("\n</LWST>")

Any hints? thanks clem

回答1:

Thanks so much for the input. I'm nearly there.. Just a couple of things to clean up. But with your hints I was able to do what I needed. I just thought I'd thank you and post the code that I came up with in case someone else needs it.

    var stream:NotesStream = session.createStream();
    //Grab the contents of the rt field on the web that has just been edited:
    var issueRT:NotesRichTextItem = getComponent("issue1").getValue();
    //Prefix some additional information for the customer.
    stream.writeText("**IMPORTANT: When responding, please do not include the history.  That is, remove this line and everything below it. **");
    //carriage return:
    stream.writeText("<p>");
    //Prefix a tag that will be used to strip off text if they respond WITH email history.
    stream.writeText("&lt;LWST&gt;");
    //carriage return:
    stream.writeText("<p>");
    //Prefix the 
    //Prefix a view scoped variable that contains things like date, responder, etc.
    stream.writeText(viewScope.ResponseHeader);
    //Add the contents of the rt field.
    stream.writeText( issueRT.getText() );
    //carriage return:
    stream.writeText("<p>");
    //Add the end tag.
    stream.writeText("&lt;/LWST&gt;");
    //Create the email body field.
    var emailBody:NotesMIMEEntity = maildoc.createMIMEEntity("body");
    emailBody.setContentFromText(stream,"text/html;charset=UTF-8", 1725);

    stream.close();


回答2:

Speak after me: "There is no RichText on the web, it is a Ghost of Christmas past, there is only MIME" :-) stw

Set the mailbox preferences (in names.nsf) to "Prefers Mime" and the Body field to "Store as MIME". This saves you the headache of constant conversion (with possible format losses) from/to MIME/RichtText. Then you can either intercept the ckedit field on submission or use the doc.getMimeEntity to get hold of the body field. .getMimeEntity in help has the code example you are looking for



回答3:

I have created an XPages version of a 10 old application that adds richtext (entered) to an existing field. This code sounds like it may help you:

var moveAddBody = function(doc:NotesDocument){
var addRt:NotesMIMEEntity = doc.getMIMEEntity("addBody");
var attFiles:NotesRichTextItem = doc.getFirstItem("attachedFiles");
if(addRt == null && attFiles == null) return;
if(attFiles != null) {
//  println("attFiles: " + attFiles.getText() + ", length=" + attFiles.getValueLength());
}
//  if(addRt != null && @Length(addRt.getContentAsText().trim()) <= 28) return;     // The standard tags in an empty field fills 28 chars
// Something to move...
var tmpDoc:NotesDocument = doc.getParentDatabase().createDocument();            // Never saved - just left in the wind...
var mime:NotesMIMEEntity = tmpDoc.createMIMEEntity("myBody");
var stream:NotesStream = session.createStream();
var formatter = java.text.SimpleDateFormat('yyyy-MM-dd HH:mm');
var time = formatter.format(@Now());
var logLine:String = '<font size="2" color="#008080" face="sans-serif"><b>' + time + " - " + @Name("[Abbreviate]",@UserName()) + '</b></font>';
stream.writeText(logLine);
if(addRt != null && @Length(addRt.getContentAsText().trim()) > 28) {
    stream.writeText('<font size="2" face="sans-serif">');      // Enforce "simiilar" font type/size...
    stream.writeText(addRt.getContentAsText());
    stream.writeText('</font>');
}
mime.setContentFromText(stream, "text/html", NotesMIMEEntity.ENC_NONE);
var prevMime = session.isConvertMime();
session.setConvertMime(true);
tmpDoc.closeMIMEEntities(true,"myBody");
var rt:NotesRichTextItem = doc.getFirstItem("Body");
var body = null;
if (rt != null) {
    body = rt.copyItemToDocument(tmpDoc,"Body");
    rt.remove();
}
rt = doc.createRichTextItem("Body");
var rtMime:NotesRichTextItem = tmpDoc.getFirstItem("myBody");
rt.appendRTItem(rtMime);
if(attFiles != null) {
    if(addRt == null) rt.addNewLine(1);
    rt.appendRTItem(attFiles);
    attFiles.remove();
}
if(body != null) {
    rt.addNewLine(2);
    rt.appendRTItem(body);
}
if(addRt != null) {
    addRt.remove();
    addRt.recycle();
}
session.setConvertMime(prevMime);
return true;
}

You will need to take out some of the code above since it also adds date/time with some formating (compliant with the Notes version before someone whispers "css..." ;-) ). It also handles attachemnts.

Hope you can use it to solve your problem?

/John



回答4:

If you want to create mails from your application, take a look at this XPages Snippet from OpenNTF: http://openntf.org/XSnippets.nsf/snippet.xsp?id=emailbean-send-dominodocument-html-emails-cw-embedded-images-attachments-custom-headerfooter The bean offers everything that you need.